blob: 4dde429441d2a45f748b0d0c8c67022e17fe5c36 [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>
Yogesh Ashok Powar0879fa42011-08-09 18:02:26 -070010#include <linux/crc32.h>
Johannes Bergd3236552009-04-20 14:31:42 +020011#include <net/cfg80211.h>
Zhu Yie31a16d2009-05-21 21:47:03 +080012#include <net/ip.h>
Johannes Berg8318d782008-01-24 19:38:38 +010013#include "core.h"
14
Johannes Bergbd815252008-10-29 20:00:45 +010015struct ieee80211_rate *
16ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010017 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010018{
19 struct ieee80211_rate *result = &sband->bitrates[0];
20 int i;
21
22 for (i = 0; i < sband->n_bitrates; i++) {
23 if (!(basic_rates & BIT(i)))
24 continue;
25 if (sband->bitrates[i].bitrate > bitrate)
26 continue;
27 result = &sband->bitrates[i];
28 }
29
30 return result;
31}
32EXPORT_SYMBOL(ieee80211_get_response_rate);
33
Bruno Randolf59eb21a2011-01-17 13:37:28 +090034int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010035{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090036 /* see 802.11 17.3.8.3.2 and Annex J
37 * there are overlapping channel numbers in 5GHz and 2GHz bands */
38 if (band == IEEE80211_BAND_5GHZ) {
39 if (chan >= 182 && chan <= 196)
40 return 4000 + chan * 5;
41 else
42 return 5000 + chan * 5;
43 } else { /* IEEE80211_BAND_2GHZ */
44 if (chan == 14)
45 return 2484;
46 else if (chan < 14)
47 return 2407 + chan * 5;
48 else
49 return 0; /* not supported */
50 }
Johannes Berg8318d782008-01-24 19:38:38 +010051}
52EXPORT_SYMBOL(ieee80211_channel_to_frequency);
53
54int ieee80211_frequency_to_channel(int freq)
55{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090056 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +010057 if (freq == 2484)
58 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090059 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +010060 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090061 else if (freq >= 4910 && freq <= 4980)
62 return (freq - 4000) / 5;
63 else
64 return (freq - 5000) / 5;
Johannes Berg8318d782008-01-24 19:38:38 +010065}
66EXPORT_SYMBOL(ieee80211_frequency_to_channel);
67
Johannes Berg6c507cd2008-03-26 14:14:55 +010068struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
69 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +010070{
71 enum ieee80211_band band;
72 struct ieee80211_supported_band *sband;
73 int i;
74
75 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
76 sband = wiphy->bands[band];
77
78 if (!sband)
79 continue;
80
81 for (i = 0; i < sband->n_channels; i++) {
82 if (sband->channels[i].center_freq == freq)
83 return &sband->channels[i];
84 }
85 }
86
87 return NULL;
88}
Johannes Berg6c507cd2008-03-26 14:14:55 +010089EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +010090
Johannes Berg8318d782008-01-24 19:38:38 +010091static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
92 enum ieee80211_band band)
93{
94 int i, want;
95
96 switch (band) {
97 case IEEE80211_BAND_5GHZ:
98 want = 3;
99 for (i = 0; i < sband->n_bitrates; i++) {
100 if (sband->bitrates[i].bitrate == 60 ||
101 sband->bitrates[i].bitrate == 120 ||
102 sband->bitrates[i].bitrate == 240) {
103 sband->bitrates[i].flags |=
104 IEEE80211_RATE_MANDATORY_A;
105 want--;
106 }
107 }
108 WARN_ON(want);
109 break;
110 case IEEE80211_BAND_2GHZ:
111 want = 7;
112 for (i = 0; i < sband->n_bitrates; i++) {
113 if (sband->bitrates[i].bitrate == 10) {
114 sband->bitrates[i].flags |=
115 IEEE80211_RATE_MANDATORY_B |
116 IEEE80211_RATE_MANDATORY_G;
117 want--;
118 }
119
120 if (sband->bitrates[i].bitrate == 20 ||
121 sband->bitrates[i].bitrate == 55 ||
122 sband->bitrates[i].bitrate == 110 ||
123 sband->bitrates[i].bitrate == 60 ||
124 sband->bitrates[i].bitrate == 120 ||
125 sband->bitrates[i].bitrate == 240) {
126 sband->bitrates[i].flags |=
127 IEEE80211_RATE_MANDATORY_G;
128 want--;
129 }
130
Johannes Bergaac09fb2008-01-30 17:36:10 +0100131 if (sband->bitrates[i].bitrate != 10 &&
132 sband->bitrates[i].bitrate != 20 &&
133 sband->bitrates[i].bitrate != 55 &&
134 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100135 sband->bitrates[i].flags |=
136 IEEE80211_RATE_ERP_G;
137 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100138 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100139 break;
140 case IEEE80211_NUM_BANDS:
141 WARN_ON(1);
142 break;
143 }
144}
145
146void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
147{
148 enum ieee80211_band band;
149
150 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
151 if (wiphy->bands[band])
152 set_mandatory_flags_band(wiphy->bands[band], band);
153}
Johannes Berg08645122009-05-11 13:54:58 +0200154
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300155bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
156{
157 int i;
158 for (i = 0; i < wiphy->n_cipher_suites; i++)
159 if (cipher == wiphy->cipher_suites[i])
160 return true;
161 return false;
162}
163
Johannes Bergfffd0932009-07-08 14:22:54 +0200164int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
165 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200166 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200167{
168 if (key_idx > 5)
169 return -EINVAL;
170
Johannes Berge31b8212010-10-05 19:39:30 +0200171 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
172 return -EINVAL;
173
174 if (pairwise && !mac_addr)
175 return -EINVAL;
176
Johannes Berg08645122009-05-11 13:54:58 +0200177 /*
178 * Disallow pairwise keys with non-zero index unless it's WEP
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200179 * or a vendor specific cipher (because current deployments use
180 * pairwise WEP keys with non-zero indices and for vendor specific
181 * ciphers this should be validated in the driver or hardware level
182 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200183 */
Johannes Berge31b8212010-10-05 19:39:30 +0200184 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200185 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
186 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
187 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200188 return -EINVAL;
189
Johannes Berg08645122009-05-11 13:54:58 +0200190 switch (params->cipher) {
191 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200192 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200193 return -EINVAL;
194 break;
195 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200196 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200197 return -EINVAL;
198 break;
199 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200200 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200201 return -EINVAL;
202 break;
203 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200204 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200205 return -EINVAL;
206 break;
207 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200208 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200209 return -EINVAL;
210 break;
211 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300212 /*
213 * We don't know anything about this algorithm,
214 * allow using it -- but the driver must check
215 * all parameters! We still check below whether
216 * or not the driver supports this algorithm,
217 * of course.
218 */
219 break;
Johannes Berg08645122009-05-11 13:54:58 +0200220 }
221
Jouni Malinen9f26a952009-05-15 12:38:32 +0300222 if (params->seq) {
223 switch (params->cipher) {
224 case WLAN_CIPHER_SUITE_WEP40:
225 case WLAN_CIPHER_SUITE_WEP104:
226 /* These ciphers do not use key sequence */
227 return -EINVAL;
228 case WLAN_CIPHER_SUITE_TKIP:
229 case WLAN_CIPHER_SUITE_CCMP:
230 case WLAN_CIPHER_SUITE_AES_CMAC:
231 if (params->seq_len != 6)
232 return -EINVAL;
233 break;
234 }
235 }
236
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300237 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200238 return -EINVAL;
239
Johannes Berg08645122009-05-11 13:54:58 +0200240 return 0;
241}
Zhu Yie31a16d2009-05-21 21:47:03 +0800242
243/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
244/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
245const unsigned char rfc1042_header[] __aligned(2) =
246 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
247EXPORT_SYMBOL(rfc1042_header);
248
249/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
250const unsigned char bridge_tunnel_header[] __aligned(2) =
251 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
252EXPORT_SYMBOL(bridge_tunnel_header);
253
Johannes Berg633adf12010-08-12 14:49:58 +0200254unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800255{
256 unsigned int hdrlen = 24;
257
258 if (ieee80211_is_data(fc)) {
259 if (ieee80211_has_a4(fc))
260 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200261 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800262 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200263 if (ieee80211_has_order(fc))
264 hdrlen += IEEE80211_HT_CTL_LEN;
265 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800266 goto out;
267 }
268
269 if (ieee80211_is_ctl(fc)) {
270 /*
271 * ACK and CTS are 10 bytes, all others 16. To see how
272 * to get this condition consider
273 * subtype mask: 0b0000000011110000 (0x00F0)
274 * ACK subtype: 0b0000000011010000 (0x00D0)
275 * CTS subtype: 0b0000000011000000 (0x00C0)
276 * bits that matter: ^^^ (0x00E0)
277 * value of those: 0b0000000011000000 (0x00C0)
278 */
279 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
280 hdrlen = 10;
281 else
282 hdrlen = 16;
283 }
284out:
285 return hdrlen;
286}
287EXPORT_SYMBOL(ieee80211_hdrlen);
288
289unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
290{
291 const struct ieee80211_hdr *hdr =
292 (const struct ieee80211_hdr *)skb->data;
293 unsigned int hdrlen;
294
295 if (unlikely(skb->len < 10))
296 return 0;
297 hdrlen = ieee80211_hdrlen(hdr->frame_control);
298 if (unlikely(hdrlen > skb->len))
299 return 0;
300 return hdrlen;
301}
302EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
303
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400304static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800305{
306 int ae = meshhdr->flags & MESH_FLAGS_AE;
307 /* 7.1.3.5a.2 */
308 switch (ae) {
309 case 0:
310 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700311 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800312 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700313 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800314 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700315 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800316 return 24;
317 default:
318 return 6;
319 }
320}
321
Zhu Yieaf85ca2009-12-01 10:18:37 +0800322int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800323 enum nl80211_iftype iftype)
324{
325 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
326 u16 hdrlen, ethertype;
327 u8 *payload;
328 u8 dst[ETH_ALEN];
329 u8 src[ETH_ALEN] __aligned(2);
330
331 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
332 return -1;
333
334 hdrlen = ieee80211_hdrlen(hdr->frame_control);
335
336 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
337 * header
338 * IEEE 802.11 address fields:
339 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
340 * 0 0 DA SA BSSID n/a
341 * 0 1 DA BSSID SA n/a
342 * 1 0 BSSID SA DA n/a
343 * 1 1 RA TA DA SA
344 */
345 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
346 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
347
348 switch (hdr->frame_control &
349 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
350 case cpu_to_le16(IEEE80211_FCTL_TODS):
351 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200352 iftype != NL80211_IFTYPE_AP_VLAN &&
353 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800354 return -1;
355 break;
356 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
357 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543e2009-11-10 20:10:05 +0100358 iftype != NL80211_IFTYPE_MESH_POINT &&
359 iftype != NL80211_IFTYPE_AP_VLAN &&
360 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800361 return -1;
362 if (iftype == NL80211_IFTYPE_MESH_POINT) {
363 struct ieee80211s_hdr *meshdr =
364 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800365 /* make sure meshdr->flags is on the linear part */
366 if (!pskb_may_pull(skb, hdrlen + 1))
367 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800368 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800369 skb_copy_bits(skb, hdrlen +
370 offsetof(struct ieee80211s_hdr, eaddr1),
371 dst, ETH_ALEN);
372 skb_copy_bits(skb, hdrlen +
373 offsetof(struct ieee80211s_hdr, eaddr2),
374 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800375 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800376 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800377 }
378 break;
379 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700380 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200381 iftype != NL80211_IFTYPE_P2P_CLIENT &&
382 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800383 (is_multicast_ether_addr(dst) &&
384 !compare_ether_addr(src, addr)))
385 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700386 if (iftype == NL80211_IFTYPE_MESH_POINT) {
387 struct ieee80211s_hdr *meshdr =
388 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800389 /* make sure meshdr->flags is on the linear part */
390 if (!pskb_may_pull(skb, hdrlen + 1))
391 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700392 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800393 skb_copy_bits(skb, hdrlen +
394 offsetof(struct ieee80211s_hdr, eaddr1),
395 src, ETH_ALEN);
396 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700397 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800398 break;
399 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300400 if (iftype != NL80211_IFTYPE_ADHOC &&
401 iftype != NL80211_IFTYPE_STATION)
402 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800403 break;
404 }
405
Zhu Yie3cf8b32010-03-29 17:35:07 +0800406 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800407 return -1;
408
409 payload = skb->data + hdrlen;
410 ethertype = (payload[6] << 8) | payload[7];
411
412 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
413 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
414 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
415 /* remove RFC1042 or Bridge-Tunnel encapsulation and
416 * replace EtherType */
417 skb_pull(skb, hdrlen + 6);
418 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
419 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
420 } else {
421 struct ethhdr *ehdr;
422 __be16 len;
423
424 skb_pull(skb, hdrlen);
425 len = htons(skb->len);
426 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
427 memcpy(ehdr->h_dest, dst, ETH_ALEN);
428 memcpy(ehdr->h_source, src, ETH_ALEN);
429 ehdr->h_proto = len;
430 }
431 return 0;
432}
433EXPORT_SYMBOL(ieee80211_data_to_8023);
434
Zhu Yieaf85ca2009-12-01 10:18:37 +0800435int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800436 enum nl80211_iftype iftype, u8 *bssid, bool qos)
437{
438 struct ieee80211_hdr hdr;
439 u16 hdrlen, ethertype;
440 __le16 fc;
441 const u8 *encaps_data;
442 int encaps_len, skip_header_bytes;
443 int nh_pos, h_pos;
444 int head_need;
445
446 if (unlikely(skb->len < ETH_HLEN))
447 return -EINVAL;
448
449 nh_pos = skb_network_header(skb) - skb->data;
450 h_pos = skb_transport_header(skb) - skb->data;
451
452 /* convert Ethernet header to proper 802.11 header (based on
453 * operation mode) */
454 ethertype = (skb->data[12] << 8) | skb->data[13];
455 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
456
457 switch (iftype) {
458 case NL80211_IFTYPE_AP:
459 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200460 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800461 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
462 /* DA BSSID SA */
463 memcpy(hdr.addr1, skb->data, ETH_ALEN);
464 memcpy(hdr.addr2, addr, ETH_ALEN);
465 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
466 hdrlen = 24;
467 break;
468 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200469 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800470 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
471 /* BSSID SA DA */
472 memcpy(hdr.addr1, bssid, ETH_ALEN);
473 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
474 memcpy(hdr.addr3, skb->data, ETH_ALEN);
475 hdrlen = 24;
476 break;
477 case NL80211_IFTYPE_ADHOC:
478 /* DA SA BSSID */
479 memcpy(hdr.addr1, skb->data, ETH_ALEN);
480 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
481 memcpy(hdr.addr3, bssid, ETH_ALEN);
482 hdrlen = 24;
483 break;
484 default:
485 return -EOPNOTSUPP;
486 }
487
488 if (qos) {
489 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
490 hdrlen += 2;
491 }
492
493 hdr.frame_control = fc;
494 hdr.duration_id = 0;
495 hdr.seq_ctrl = 0;
496
497 skip_header_bytes = ETH_HLEN;
498 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
499 encaps_data = bridge_tunnel_header;
500 encaps_len = sizeof(bridge_tunnel_header);
501 skip_header_bytes -= 2;
502 } else if (ethertype > 0x600) {
503 encaps_data = rfc1042_header;
504 encaps_len = sizeof(rfc1042_header);
505 skip_header_bytes -= 2;
506 } else {
507 encaps_data = NULL;
508 encaps_len = 0;
509 }
510
511 skb_pull(skb, skip_header_bytes);
512 nh_pos -= skip_header_bytes;
513 h_pos -= skip_header_bytes;
514
515 head_need = hdrlen + encaps_len - skb_headroom(skb);
516
517 if (head_need > 0 || skb_cloned(skb)) {
518 head_need = max(head_need, 0);
519 if (head_need)
520 skb_orphan(skb);
521
Joe Perches24616152011-08-29 14:17:41 -0700522 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
Zhu Yie31a16d2009-05-21 21:47:03 +0800523 return -ENOMEM;
Joe Perches24616152011-08-29 14:17:41 -0700524
Zhu Yie31a16d2009-05-21 21:47:03 +0800525 skb->truesize += head_need;
526 }
527
528 if (encaps_data) {
529 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
530 nh_pos += encaps_len;
531 h_pos += encaps_len;
532 }
533
534 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
535
536 nh_pos += hdrlen;
537 h_pos += hdrlen;
538
539 /* Update skb pointers to various headers since this modified frame
540 * is going to go through Linux networking code that may potentially
541 * need things like pointer to IP header. */
542 skb_set_mac_header(skb, 0);
543 skb_set_network_header(skb, nh_pos);
544 skb_set_transport_header(skb, h_pos);
545
546 return 0;
547}
548EXPORT_SYMBOL(ieee80211_data_from_8023);
549
Zhu Yieaf85ca2009-12-01 10:18:37 +0800550
551void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
552 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700553 const unsigned int extra_headroom,
554 bool has_80211_header)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800555{
556 struct sk_buff *frame = NULL;
557 u16 ethertype;
558 u8 *payload;
559 const struct ethhdr *eth;
560 int remaining, err;
561 u8 dst[ETH_ALEN], src[ETH_ALEN];
562
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700563 if (has_80211_header) {
564 err = ieee80211_data_to_8023(skb, addr, iftype);
565 if (err)
566 goto out;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800567
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700568 /* skip the wrapping header */
569 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
570 if (!eth)
571 goto out;
572 } else {
573 eth = (struct ethhdr *) skb->data;
574 }
Zhu Yieaf85ca2009-12-01 10:18:37 +0800575
576 while (skb != frame) {
577 u8 padding;
578 __be16 len = eth->h_proto;
579 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
580
581 remaining = skb->len;
582 memcpy(dst, eth->h_dest, ETH_ALEN);
583 memcpy(src, eth->h_source, ETH_ALEN);
584
585 padding = (4 - subframe_len) & 0x3;
586 /* the last MSDU has no padding */
587 if (subframe_len > remaining)
588 goto purge;
589
590 skb_pull(skb, sizeof(struct ethhdr));
591 /* reuse skb for the last subframe */
592 if (remaining <= subframe_len + padding)
593 frame = skb;
594 else {
595 unsigned int hlen = ALIGN(extra_headroom, 4);
596 /*
597 * Allocate and reserve two bytes more for payload
598 * alignment since sizeof(struct ethhdr) is 14.
599 */
600 frame = dev_alloc_skb(hlen + subframe_len + 2);
601 if (!frame)
602 goto purge;
603
604 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
605 memcpy(skb_put(frame, ntohs(len)), skb->data,
606 ntohs(len));
607
608 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
609 padding);
610 if (!eth) {
611 dev_kfree_skb(frame);
612 goto purge;
613 }
614 }
615
616 skb_reset_network_header(frame);
617 frame->dev = skb->dev;
618 frame->priority = skb->priority;
619
620 payload = frame->data;
621 ethertype = (payload[6] << 8) | payload[7];
622
623 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
624 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
625 compare_ether_addr(payload,
626 bridge_tunnel_header) == 0)) {
627 /* remove RFC1042 or Bridge-Tunnel
628 * encapsulation and replace EtherType */
629 skb_pull(frame, 6);
630 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
631 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
632 } else {
633 memcpy(skb_push(frame, sizeof(__be16)), &len,
634 sizeof(__be16));
635 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
636 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
637 }
638 __skb_queue_tail(list, frame);
639 }
640
641 return;
642
643 purge:
644 __skb_queue_purge(list);
645 out:
646 dev_kfree_skb(skb);
647}
648EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
649
Zhu Yie31a16d2009-05-21 21:47:03 +0800650/* Given a data frame determine the 802.1p/1d tag to use. */
651unsigned int cfg80211_classify8021d(struct sk_buff *skb)
652{
653 unsigned int dscp;
654
655 /* skb->priority values from 256->263 are magic values to
656 * directly indicate a specific 802.1d priority. This is used
657 * to allow 802.1d priority to be passed directly in from VLAN
658 * tags, etc.
659 */
660 if (skb->priority >= 256 && skb->priority <= 263)
661 return skb->priority - 256;
662
663 switch (skb->protocol) {
664 case htons(ETH_P_IP):
665 dscp = ip_hdr(skb)->tos & 0xfc;
666 break;
667 default:
668 return 0;
669 }
670
671 return dscp >> 5;
672}
673EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200674
675const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
676{
677 u8 *end, *pos;
678
679 pos = bss->information_elements;
680 if (pos == NULL)
681 return NULL;
682 end = pos + bss->len_information_elements;
683
684 while (pos + 1 < end) {
685 if (pos + 2 + pos[1] > end)
686 break;
687 if (pos[0] == ie)
688 return pos;
689 pos += 2 + pos[1];
690 }
691
692 return NULL;
693}
694EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200695
696void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
697{
698 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
699 struct net_device *dev = wdev->netdev;
700 int i;
701
702 if (!wdev->connect_keys)
703 return;
704
705 for (i = 0; i < 6; i++) {
706 if (!wdev->connect_keys->params[i].cipher)
707 continue;
Johannes Berge31b8212010-10-05 19:39:30 +0200708 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800709 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800710 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800711 continue;
712 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200713 if (wdev->connect_keys->def == i)
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100714 if (rdev->ops->set_default_key(wdev->wiphy, dev,
715 i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800716 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800717 continue;
718 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200719 if (wdev->connect_keys->defmgmt == i)
720 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800721 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200722 }
723
724 kfree(wdev->connect_keys);
725 wdev->connect_keys = NULL;
726}
Johannes Berg3d54d252009-08-21 14:51:05 +0200727
728static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
729{
730 struct cfg80211_event *ev;
731 unsigned long flags;
732 const u8 *bssid = NULL;
733
734 spin_lock_irqsave(&wdev->event_lock, flags);
735 while (!list_empty(&wdev->event_list)) {
736 ev = list_first_entry(&wdev->event_list,
737 struct cfg80211_event, list);
738 list_del(&ev->list);
739 spin_unlock_irqrestore(&wdev->event_lock, flags);
740
741 wdev_lock(wdev);
742 switch (ev->type) {
743 case EVENT_CONNECT_RESULT:
744 if (!is_zero_ether_addr(ev->cr.bssid))
745 bssid = ev->cr.bssid;
746 __cfg80211_connect_result(
747 wdev->netdev, bssid,
748 ev->cr.req_ie, ev->cr.req_ie_len,
749 ev->cr.resp_ie, ev->cr.resp_ie_len,
750 ev->cr.status,
751 ev->cr.status == WLAN_STATUS_SUCCESS,
752 NULL);
753 break;
754 case EVENT_ROAMED:
Jouni Malinened9d0102011-05-16 19:40:15 +0300755 __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid,
Johannes Berg3d54d252009-08-21 14:51:05 +0200756 ev->rm.req_ie, ev->rm.req_ie_len,
757 ev->rm.resp_ie, ev->rm.resp_ie_len);
758 break;
759 case EVENT_DISCONNECTED:
760 __cfg80211_disconnected(wdev->netdev,
761 ev->dc.ie, ev->dc.ie_len,
762 ev->dc.reason, true);
763 break;
764 case EVENT_IBSS_JOINED:
765 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
766 break;
767 }
768 wdev_unlock(wdev);
769
770 kfree(ev);
771
772 spin_lock_irqsave(&wdev->event_lock, flags);
773 }
774 spin_unlock_irqrestore(&wdev->event_lock, flags);
775}
776
777void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
778{
779 struct wireless_dev *wdev;
780
781 ASSERT_RTNL();
782 ASSERT_RDEV_LOCK(rdev);
783
784 mutex_lock(&rdev->devlist_mtx);
785
786 list_for_each_entry(wdev, &rdev->netdev_list, list)
787 cfg80211_process_wdev_events(wdev);
788
789 mutex_unlock(&rdev->devlist_mtx);
790}
791
792int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
793 struct net_device *dev, enum nl80211_iftype ntype,
794 u32 *flags, struct vif_params *params)
795{
796 int err;
797 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
798
799 ASSERT_RDEV_LOCK(rdev);
800
801 /* don't support changing VLANs, you just re-create them */
802 if (otype == NL80211_IFTYPE_AP_VLAN)
803 return -EOPNOTSUPP;
804
805 if (!rdev->ops->change_virtual_intf ||
806 !(rdev->wiphy.interface_modes & (1 << ntype)))
807 return -EOPNOTSUPP;
808
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100809 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a2010-06-15 06:50:45 +0000810 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200811 (ntype == NL80211_IFTYPE_ADHOC ||
812 ntype == NL80211_IFTYPE_STATION ||
813 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100814 return -EBUSY;
815
Johannes Berg3d54d252009-08-21 14:51:05 +0200816 if (ntype != otype) {
Johannes Berg7527a782011-05-13 10:58:57 +0200817 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
818 ntype);
819 if (err)
820 return err;
821
Johannes Berg9bc383d2009-11-19 11:55:19 +0100822 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100823 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100824
Johannes Berg3d54d252009-08-21 14:51:05 +0200825 switch (otype) {
826 case NL80211_IFTYPE_ADHOC:
827 cfg80211_leave_ibss(rdev, dev, false);
828 break;
829 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200830 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200831 cfg80211_disconnect(rdev, dev,
832 WLAN_REASON_DEAUTH_LEAVING, true);
833 break;
834 case NL80211_IFTYPE_MESH_POINT:
835 /* mesh should be handled? */
836 break;
837 default:
838 break;
839 }
840
841 cfg80211_process_rdev_events(rdev);
842 }
843
844 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
845 ntype, flags, params);
846
847 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
848
Johannes Berg9bc383d2009-11-19 11:55:19 +0100849 if (!err && params && params->use_4addr != -1)
850 dev->ieee80211_ptr->use_4addr = params->use_4addr;
851
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100852 if (!err) {
853 dev->priv_flags &= ~IFF_DONT_BRIDGE;
854 switch (ntype) {
855 case NL80211_IFTYPE_STATION:
856 if (dev->ieee80211_ptr->use_4addr)
857 break;
858 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200859 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100860 case NL80211_IFTYPE_ADHOC:
861 dev->priv_flags |= IFF_DONT_BRIDGE;
862 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200863 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100864 case NL80211_IFTYPE_AP:
865 case NL80211_IFTYPE_AP_VLAN:
866 case NL80211_IFTYPE_WDS:
867 case NL80211_IFTYPE_MESH_POINT:
868 /* bridging OK */
869 break;
870 case NL80211_IFTYPE_MONITOR:
871 /* monitor can't bridge anyway */
872 break;
873 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200874 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100875 /* not happening */
876 break;
877 }
878 }
879
Johannes Berg3d54d252009-08-21 14:51:05 +0200880 return err;
881}
John W. Linville254416a2009-12-09 16:43:52 -0500882
883u16 cfg80211_calculate_bitrate(struct rate_info *rate)
884{
885 int modulation, streams, bitrate;
886
887 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
888 return rate->legacy;
889
890 /* the formula below does only work for MCS values smaller than 32 */
891 if (rate->mcs >= 32)
892 return 0;
893
894 modulation = rate->mcs & 7;
895 streams = (rate->mcs >> 3) + 1;
896
897 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
898 13500000 : 6500000;
899
900 if (modulation < 4)
901 bitrate *= (modulation + 1);
902 else if (modulation == 4)
903 bitrate *= (modulation + 2);
904 else
905 bitrate *= (modulation + 3);
906
907 bitrate *= streams;
908
909 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
910 bitrate = (bitrate / 9) * 10;
911
912 /* do NOT round down here */
913 return (bitrate + 50000) / 100000;
914}
Johannes Berg56d18932011-05-09 18:41:15 +0200915
916int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
917 u32 beacon_int)
918{
919 struct wireless_dev *wdev;
920 int res = 0;
921
922 if (!beacon_int)
923 return -EINVAL;
924
925 mutex_lock(&rdev->devlist_mtx);
926
927 list_for_each_entry(wdev, &rdev->netdev_list, list) {
928 if (!wdev->beacon_interval)
929 continue;
930 if (wdev->beacon_interval != beacon_int) {
931 res = -EINVAL;
932 break;
933 }
934 }
935
936 mutex_unlock(&rdev->devlist_mtx);
937
938 return res;
939}
Johannes Berg7527a782011-05-13 10:58:57 +0200940
941int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
942 struct wireless_dev *wdev,
943 enum nl80211_iftype iftype)
944{
945 struct wireless_dev *wdev_iter;
946 int num[NUM_NL80211_IFTYPES];
947 int total = 1;
948 int i, j;
949
950 ASSERT_RTNL();
951
952 /* Always allow software iftypes */
953 if (rdev->wiphy.software_iftypes & BIT(iftype))
954 return 0;
955
956 /*
957 * Drivers will gradually all set this flag, until all
958 * have it we only enforce for those that set it.
959 */
960 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
961 return 0;
962
963 memset(num, 0, sizeof(num));
964
965 num[iftype] = 1;
966
967 mutex_lock(&rdev->devlist_mtx);
968 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
969 if (wdev_iter == wdev)
970 continue;
971 if (!netif_running(wdev_iter->netdev))
972 continue;
973
974 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
975 continue;
976
977 num[wdev_iter->iftype]++;
978 total++;
979 }
980 mutex_unlock(&rdev->devlist_mtx);
981
982 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
983 const struct ieee80211_iface_combination *c;
984 struct ieee80211_iface_limit *limits;
985
986 c = &rdev->wiphy.iface_combinations[i];
987
988 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
989 GFP_KERNEL);
990 if (!limits)
991 return -ENOMEM;
992 if (total > c->max_interfaces)
993 goto cont;
994
995 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
996 if (rdev->wiphy.software_iftypes & BIT(iftype))
997 continue;
998 for (j = 0; j < c->n_limits; j++) {
999 if (!(limits[j].types & iftype))
1000 continue;
1001 if (limits[j].max < num[iftype])
1002 goto cont;
1003 limits[j].max -= num[iftype];
1004 }
1005 }
1006 /* yay, it fits */
1007 kfree(limits);
1008 return 0;
1009 cont:
1010 kfree(limits);
1011 }
1012
1013 return -EBUSY;
1014}
Johannes Berg34850ab2011-07-18 18:08:35 +02001015
1016int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1017 const u8 *rates, unsigned int n_rates,
1018 u32 *mask)
1019{
1020 int i, j;
1021
Johannes Berga401d2b2011-07-20 00:52:16 +02001022 if (!sband)
1023 return -EINVAL;
1024
Johannes Berg34850ab2011-07-18 18:08:35 +02001025 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1026 return -EINVAL;
1027
1028 *mask = 0;
1029
1030 for (i = 0; i < n_rates; i++) {
1031 int rate = (rates[i] & 0x7f) * 5;
1032 bool found = false;
1033
1034 for (j = 0; j < sband->n_bitrates; j++) {
1035 if (sband->bitrates[j].bitrate == rate) {
1036 found = true;
1037 *mask |= BIT(j);
1038 break;
1039 }
1040 }
1041 if (!found)
1042 return -EINVAL;
1043 }
1044
1045 /*
1046 * mask must have at least one bit set here since we
1047 * didn't accept a 0-length rates array nor allowed
1048 * entries in the array that didn't exist
1049 */
1050
1051 return 0;
1052}
Yogesh Ashok Powar0879fa42011-08-09 18:02:26 -07001053
1054u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
1055 struct ieee802_11_elems *elems,
1056 u64 filter, u32 crc)
1057{
1058 size_t left = len;
1059 u8 *pos = start;
1060 bool calc_crc = filter != 0;
1061
1062 memset(elems, 0, sizeof(*elems));
1063 elems->ie_start = start;
1064 elems->total_len = len;
1065
1066 while (left >= 2) {
1067 u8 id, elen;
1068
1069 id = *pos++;
1070 elen = *pos++;
1071 left -= 2;
1072
1073 if (elen > left)
1074 break;
1075
1076 if (calc_crc && id < 64 && (filter & (1ULL << id)))
1077 crc = crc32_be(crc, pos - 2, elen + 2);
1078
1079 switch (id) {
1080 case WLAN_EID_SSID:
1081 elems->ssid = pos;
1082 elems->ssid_len = elen;
1083 break;
1084 case WLAN_EID_SUPP_RATES:
1085 elems->supp_rates = pos;
1086 elems->supp_rates_len = elen;
1087 break;
1088 case WLAN_EID_FH_PARAMS:
1089 elems->fh_params = pos;
1090 elems->fh_params_len = elen;
1091 break;
1092 case WLAN_EID_DS_PARAMS:
1093 elems->ds_params = pos;
1094 elems->ds_params_len = elen;
1095 break;
1096 case WLAN_EID_CF_PARAMS:
1097 elems->cf_params = pos;
1098 elems->cf_params_len = elen;
1099 break;
1100 case WLAN_EID_TIM:
1101 if (elen >= sizeof(struct ieee80211_tim_ie)) {
1102 elems->tim = (void *)pos;
1103 elems->tim_len = elen;
1104 }
1105 break;
1106 case WLAN_EID_IBSS_PARAMS:
1107 elems->ibss_params = pos;
1108 elems->ibss_params_len = elen;
1109 break;
1110 case WLAN_EID_CHALLENGE:
1111 elems->challenge = pos;
1112 elems->challenge_len = elen;
1113 break;
1114 case WLAN_EID_VENDOR_SPECIFIC:
1115 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1116 pos[2] == 0xf2) {
1117 /* Microsoft OUI (00:50:F2) */
1118
1119 if (calc_crc)
1120 crc = crc32_be(crc, pos - 2, elen + 2);
1121
1122 if (pos[3] == 1) {
1123 /* OUI Type 1 - WPA IE */
1124 elems->wpa = pos;
1125 elems->wpa_len = elen;
1126 } else if (elen >= 5 && pos[3] == 2) {
1127 /* OUI Type 2 - WMM IE */
1128 if (pos[4] == 0) {
1129 elems->wmm_info = pos;
1130 elems->wmm_info_len = elen;
1131 } else if (pos[4] == 1) {
1132 elems->wmm_param = pos;
1133 elems->wmm_param_len = elen;
1134 }
1135 }
1136 }
1137 break;
1138 case WLAN_EID_RSN:
1139 elems->rsn = pos;
1140 elems->rsn_len = elen;
1141 break;
1142 case WLAN_EID_ERP_INFO:
1143 elems->erp_info = pos;
1144 elems->erp_info_len = elen;
1145 break;
1146 case WLAN_EID_EXT_SUPP_RATES:
1147 elems->ext_supp_rates = pos;
1148 elems->ext_supp_rates_len = elen;
1149 break;
1150 case WLAN_EID_HT_CAPABILITY:
1151 if (elen >= sizeof(struct ieee80211_ht_cap))
1152 elems->ht_cap_elem = (void *)pos;
1153 break;
1154 case WLAN_EID_HT_INFORMATION:
1155 if (elen >= sizeof(struct ieee80211_ht_info))
1156 elems->ht_info_elem = (void *)pos;
1157 break;
1158 case WLAN_EID_MESH_ID:
1159 elems->mesh_id = pos;
1160 elems->mesh_id_len = elen;
1161 break;
1162 case WLAN_EID_MESH_CONFIG:
1163 if (elen >= sizeof(struct ieee80211_meshconf_ie))
1164 elems->mesh_config = (void *)pos;
1165 break;
Thomas Pedersen8db09852011-08-12 20:01:00 -07001166 case WLAN_EID_PEER_MGMT:
1167 elems->peering = pos;
1168 elems->peering_len = elen;
Yogesh Ashok Powar0879fa42011-08-09 18:02:26 -07001169 break;
1170 case WLAN_EID_PREQ:
1171 elems->preq = pos;
1172 elems->preq_len = elen;
1173 break;
1174 case WLAN_EID_PREP:
1175 elems->prep = pos;
1176 elems->prep_len = elen;
1177 break;
1178 case WLAN_EID_PERR:
1179 elems->perr = pos;
1180 elems->perr_len = elen;
1181 break;
1182 case WLAN_EID_RANN:
1183 if (elen >= sizeof(struct ieee80211_rann_ie))
1184 elems->rann = (void *)pos;
1185 break;
1186 case WLAN_EID_CHANNEL_SWITCH:
1187 elems->ch_switch_elem = pos;
1188 elems->ch_switch_elem_len = elen;
1189 break;
1190 case WLAN_EID_QUIET:
1191 if (!elems->quiet_elem) {
1192 elems->quiet_elem = pos;
1193 elems->quiet_elem_len = elen;
1194 }
1195 elems->num_of_quiet_elem++;
1196 break;
1197 case WLAN_EID_COUNTRY:
1198 elems->country_elem = pos;
1199 elems->country_elem_len = elen;
1200 break;
1201 case WLAN_EID_PWR_CONSTRAINT:
1202 elems->pwr_constr_elem = pos;
1203 elems->pwr_constr_elem_len = elen;
1204 break;
1205 case WLAN_EID_TIMEOUT_INTERVAL:
1206 elems->timeout_int = pos;
1207 elems->timeout_int_len = elen;
1208 break;
1209 default:
1210 break;
1211 }
1212
1213 left -= elen;
1214 pos += elen;
1215 }
1216
1217 return crc;
1218}
1219EXPORT_SYMBOL(ieee802_11_parse_elems_crc);