net: add moduleparam.h for users of module_param/MODULE_PARM_DESC
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / wireless / util.c
CommitLineData
8318d78a
JB
1/*
2 * Wireless utility functions
3 *
d3236553 4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
8318d78a 5 */
d3236553 6#include <linux/bitops.h>
e31a16d6 7#include <linux/etherdevice.h>
5a0e3ad6 8#include <linux/slab.h>
0879fa44 9#include <linux/crc32.h>
d3236553 10#include <net/cfg80211.h>
e31a16d6 11#include <net/ip.h>
8318d78a
JB
12#include "core.h"
13
bd815252
JB
14struct ieee80211_rate *
15ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
881d948c 16 u32 basic_rates, int bitrate)
bd815252
JB
17{
18 struct ieee80211_rate *result = &sband->bitrates[0];
19 int i;
20
21 for (i = 0; i < sband->n_bitrates; i++) {
22 if (!(basic_rates & BIT(i)))
23 continue;
24 if (sband->bitrates[i].bitrate > bitrate)
25 continue;
26 result = &sband->bitrates[i];
27 }
28
29 return result;
30}
31EXPORT_SYMBOL(ieee80211_get_response_rate);
32
59eb21a6 33int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
8318d78a 34{
59eb21a6
BR
35 /* see 802.11 17.3.8.3.2 and Annex J
36 * there are overlapping channel numbers in 5GHz and 2GHz bands */
37 if (band == IEEE80211_BAND_5GHZ) {
38 if (chan >= 182 && chan <= 196)
39 return 4000 + chan * 5;
40 else
41 return 5000 + chan * 5;
42 } else { /* IEEE80211_BAND_2GHZ */
43 if (chan == 14)
44 return 2484;
45 else if (chan < 14)
46 return 2407 + chan * 5;
47 else
48 return 0; /* not supported */
49 }
8318d78a
JB
50}
51EXPORT_SYMBOL(ieee80211_channel_to_frequency);
52
53int ieee80211_frequency_to_channel(int freq)
54{
59eb21a6 55 /* see 802.11 17.3.8.3.2 and Annex J */
8318d78a
JB
56 if (freq == 2484)
57 return 14;
59eb21a6 58 else if (freq < 2484)
8318d78a 59 return (freq - 2407) / 5;
59eb21a6
BR
60 else if (freq >= 4910 && freq <= 4980)
61 return (freq - 4000) / 5;
62 else
63 return (freq - 5000) / 5;
8318d78a
JB
64}
65EXPORT_SYMBOL(ieee80211_frequency_to_channel);
66
6c507cd0
JB
67struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
68 int freq)
906c730a
JB
69{
70 enum ieee80211_band band;
71 struct ieee80211_supported_band *sband;
72 int i;
73
74 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
75 sband = wiphy->bands[band];
76
77 if (!sband)
78 continue;
79
80 for (i = 0; i < sband->n_channels; i++) {
81 if (sband->channels[i].center_freq == freq)
82 return &sband->channels[i];
83 }
84 }
85
86 return NULL;
87}
6c507cd0 88EXPORT_SYMBOL(__ieee80211_get_channel);
906c730a 89
8318d78a
JB
90static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
91 enum ieee80211_band band)
92{
93 int i, want;
94
95 switch (band) {
96 case IEEE80211_BAND_5GHZ:
97 want = 3;
98 for (i = 0; i < sband->n_bitrates; i++) {
99 if (sband->bitrates[i].bitrate == 60 ||
100 sband->bitrates[i].bitrate == 120 ||
101 sband->bitrates[i].bitrate == 240) {
102 sband->bitrates[i].flags |=
103 IEEE80211_RATE_MANDATORY_A;
104 want--;
105 }
106 }
107 WARN_ON(want);
108 break;
109 case IEEE80211_BAND_2GHZ:
110 want = 7;
111 for (i = 0; i < sband->n_bitrates; i++) {
112 if (sband->bitrates[i].bitrate == 10) {
113 sband->bitrates[i].flags |=
114 IEEE80211_RATE_MANDATORY_B |
115 IEEE80211_RATE_MANDATORY_G;
116 want--;
117 }
118
119 if (sband->bitrates[i].bitrate == 20 ||
120 sband->bitrates[i].bitrate == 55 ||
121 sband->bitrates[i].bitrate == 110 ||
122 sband->bitrates[i].bitrate == 60 ||
123 sband->bitrates[i].bitrate == 120 ||
124 sband->bitrates[i].bitrate == 240) {
125 sband->bitrates[i].flags |=
126 IEEE80211_RATE_MANDATORY_G;
127 want--;
128 }
129
aac09fbf
JB
130 if (sband->bitrates[i].bitrate != 10 &&
131 sband->bitrates[i].bitrate != 20 &&
132 sband->bitrates[i].bitrate != 55 &&
133 sband->bitrates[i].bitrate != 110)
8318d78a
JB
134 sband->bitrates[i].flags |=
135 IEEE80211_RATE_ERP_G;
136 }
406f2388 137 WARN_ON(want != 0 && want != 3 && want != 6);
8318d78a
JB
138 break;
139 case IEEE80211_NUM_BANDS:
140 WARN_ON(1);
141 break;
142 }
143}
144
145void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
146{
147 enum ieee80211_band band;
148
149 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
150 if (wiphy->bands[band])
151 set_mandatory_flags_band(wiphy->bands[band], band);
152}
08645126 153
38ba3c57
JM
154bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
155{
156 int i;
157 for (i = 0; i < wiphy->n_cipher_suites; i++)
158 if (cipher == wiphy->cipher_suites[i])
159 return true;
160 return false;
161}
162
fffd0934
JB
163int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
164 struct key_params *params, int key_idx,
e31b8213 165 bool pairwise, const u8 *mac_addr)
08645126
JB
166{
167 if (key_idx > 5)
168 return -EINVAL;
169
e31b8213
JB
170 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
171 return -EINVAL;
172
173 if (pairwise && !mac_addr)
174 return -EINVAL;
175
08645126
JB
176 /*
177 * Disallow pairwise keys with non-zero index unless it's WEP
45cbad6a
JO
178 * or a vendor specific cipher (because current deployments use
179 * pairwise WEP keys with non-zero indices and for vendor specific
180 * ciphers this should be validated in the driver or hardware level
181 * - but 802.11i clearly specifies to use zero)
08645126 182 */
e31b8213 183 if (pairwise && key_idx &&
45cbad6a
JO
184 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
185 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
186 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
08645126
JB
187 return -EINVAL;
188
08645126
JB
189 switch (params->cipher) {
190 case WLAN_CIPHER_SUITE_WEP40:
8fc0fee0 191 if (params->key_len != WLAN_KEY_LEN_WEP40)
08645126
JB
192 return -EINVAL;
193 break;
194 case WLAN_CIPHER_SUITE_TKIP:
8fc0fee0 195 if (params->key_len != WLAN_KEY_LEN_TKIP)
08645126
JB
196 return -EINVAL;
197 break;
198 case WLAN_CIPHER_SUITE_CCMP:
8fc0fee0 199 if (params->key_len != WLAN_KEY_LEN_CCMP)
08645126
JB
200 return -EINVAL;
201 break;
202 case WLAN_CIPHER_SUITE_WEP104:
8fc0fee0 203 if (params->key_len != WLAN_KEY_LEN_WEP104)
08645126
JB
204 return -EINVAL;
205 break;
206 case WLAN_CIPHER_SUITE_AES_CMAC:
8fc0fee0 207 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
08645126
JB
208 return -EINVAL;
209 break;
210 default:
7d64b7cc
JB
211 /*
212 * We don't know anything about this algorithm,
213 * allow using it -- but the driver must check
214 * all parameters! We still check below whether
215 * or not the driver supports this algorithm,
216 * of course.
217 */
218 break;
08645126
JB
219 }
220
9f26a952
JM
221 if (params->seq) {
222 switch (params->cipher) {
223 case WLAN_CIPHER_SUITE_WEP40:
224 case WLAN_CIPHER_SUITE_WEP104:
225 /* These ciphers do not use key sequence */
226 return -EINVAL;
227 case WLAN_CIPHER_SUITE_TKIP:
228 case WLAN_CIPHER_SUITE_CCMP:
229 case WLAN_CIPHER_SUITE_AES_CMAC:
230 if (params->seq_len != 6)
231 return -EINVAL;
232 break;
233 }
234 }
235
38ba3c57 236 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
fffd0934
JB
237 return -EINVAL;
238
08645126
JB
239 return 0;
240}
e31a16d6
ZY
241
242/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
243/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
244const unsigned char rfc1042_header[] __aligned(2) =
245 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
246EXPORT_SYMBOL(rfc1042_header);
247
248/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
249const unsigned char bridge_tunnel_header[] __aligned(2) =
250 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
251EXPORT_SYMBOL(bridge_tunnel_header);
252
633adf1a 253unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
e31a16d6
ZY
254{
255 unsigned int hdrlen = 24;
256
257 if (ieee80211_is_data(fc)) {
258 if (ieee80211_has_a4(fc))
259 hdrlen = 30;
d0dd2de0 260 if (ieee80211_is_data_qos(fc)) {
e31a16d6 261 hdrlen += IEEE80211_QOS_CTL_LEN;
d0dd2de0
AT
262 if (ieee80211_has_order(fc))
263 hdrlen += IEEE80211_HT_CTL_LEN;
264 }
e31a16d6
ZY
265 goto out;
266 }
267
268 if (ieee80211_is_ctl(fc)) {
269 /*
270 * ACK and CTS are 10 bytes, all others 16. To see how
271 * to get this condition consider
272 * subtype mask: 0b0000000011110000 (0x00F0)
273 * ACK subtype: 0b0000000011010000 (0x00D0)
274 * CTS subtype: 0b0000000011000000 (0x00C0)
275 * bits that matter: ^^^ (0x00E0)
276 * value of those: 0b0000000011000000 (0x00C0)
277 */
278 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
279 hdrlen = 10;
280 else
281 hdrlen = 16;
282 }
283out:
284 return hdrlen;
285}
286EXPORT_SYMBOL(ieee80211_hdrlen);
287
288unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
289{
290 const struct ieee80211_hdr *hdr =
291 (const struct ieee80211_hdr *)skb->data;
292 unsigned int hdrlen;
293
294 if (unlikely(skb->len < 10))
295 return 0;
296 hdrlen = ieee80211_hdrlen(hdr->frame_control);
297 if (unlikely(hdrlen > skb->len))
298 return 0;
299 return hdrlen;
300}
301EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
302
60fd2b67 303static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
e31a16d6
ZY
304{
305 int ae = meshhdr->flags & MESH_FLAGS_AE;
306 /* 7.1.3.5a.2 */
307 switch (ae) {
308 case 0:
309 return 6;
3c5772a5 310 case MESH_FLAGS_AE_A4:
e31a16d6 311 return 12;
3c5772a5 312 case MESH_FLAGS_AE_A5_A6:
e31a16d6 313 return 18;
3c5772a5 314 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
e31a16d6
ZY
315 return 24;
316 default:
317 return 6;
318 }
319}
320
eaf85ca7 321int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
e31a16d6
ZY
322 enum nl80211_iftype iftype)
323{
324 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
325 u16 hdrlen, ethertype;
326 u8 *payload;
327 u8 dst[ETH_ALEN];
328 u8 src[ETH_ALEN] __aligned(2);
329
330 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
331 return -1;
332
333 hdrlen = ieee80211_hdrlen(hdr->frame_control);
334
335 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
336 * header
337 * IEEE 802.11 address fields:
338 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
339 * 0 0 DA SA BSSID n/a
340 * 0 1 DA BSSID SA n/a
341 * 1 0 BSSID SA DA n/a
342 * 1 1 RA TA DA SA
343 */
344 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
345 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
346
347 switch (hdr->frame_control &
348 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
349 case cpu_to_le16(IEEE80211_FCTL_TODS):
350 if (unlikely(iftype != NL80211_IFTYPE_AP &&
074ac8df
JB
351 iftype != NL80211_IFTYPE_AP_VLAN &&
352 iftype != NL80211_IFTYPE_P2P_GO))
e31a16d6
ZY
353 return -1;
354 break;
355 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
356 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
f14543ee
FF
357 iftype != NL80211_IFTYPE_MESH_POINT &&
358 iftype != NL80211_IFTYPE_AP_VLAN &&
359 iftype != NL80211_IFTYPE_STATION))
e31a16d6
ZY
360 return -1;
361 if (iftype == NL80211_IFTYPE_MESH_POINT) {
362 struct ieee80211s_hdr *meshdr =
363 (struct ieee80211s_hdr *) (skb->data + hdrlen);
e3cf8b3f
ZY
364 /* make sure meshdr->flags is on the linear part */
365 if (!pskb_may_pull(skb, hdrlen + 1))
366 return -1;
e31a16d6 367 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
e3cf8b3f
ZY
368 skb_copy_bits(skb, hdrlen +
369 offsetof(struct ieee80211s_hdr, eaddr1),
370 dst, ETH_ALEN);
371 skb_copy_bits(skb, hdrlen +
372 offsetof(struct ieee80211s_hdr, eaddr2),
373 src, ETH_ALEN);
e31a16d6 374 }
e3cf8b3f 375 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
e31a16d6
ZY
376 }
377 break;
378 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
3c5772a5 379 if ((iftype != NL80211_IFTYPE_STATION &&
074ac8df
JB
380 iftype != NL80211_IFTYPE_P2P_CLIENT &&
381 iftype != NL80211_IFTYPE_MESH_POINT) ||
e31a16d6
ZY
382 (is_multicast_ether_addr(dst) &&
383 !compare_ether_addr(src, addr)))
384 return -1;
3c5772a5
JC
385 if (iftype == NL80211_IFTYPE_MESH_POINT) {
386 struct ieee80211s_hdr *meshdr =
387 (struct ieee80211s_hdr *) (skb->data + hdrlen);
e3cf8b3f
ZY
388 /* make sure meshdr->flags is on the linear part */
389 if (!pskb_may_pull(skb, hdrlen + 1))
390 return -1;
3c5772a5 391 if (meshdr->flags & MESH_FLAGS_AE_A4)
e3cf8b3f
ZY
392 skb_copy_bits(skb, hdrlen +
393 offsetof(struct ieee80211s_hdr, eaddr1),
394 src, ETH_ALEN);
395 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
3c5772a5 396 }
e31a16d6
ZY
397 break;
398 case cpu_to_le16(0):
941c93cd
AN
399 if (iftype != NL80211_IFTYPE_ADHOC &&
400 iftype != NL80211_IFTYPE_STATION)
401 return -1;
e31a16d6
ZY
402 break;
403 }
404
e3cf8b3f 405 if (!pskb_may_pull(skb, hdrlen + 8))
e31a16d6
ZY
406 return -1;
407
408 payload = skb->data + hdrlen;
409 ethertype = (payload[6] << 8) | payload[7];
410
411 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
412 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
413 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
414 /* remove RFC1042 or Bridge-Tunnel encapsulation and
415 * replace EtherType */
416 skb_pull(skb, hdrlen + 6);
417 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
418 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
419 } else {
420 struct ethhdr *ehdr;
421 __be16 len;
422
423 skb_pull(skb, hdrlen);
424 len = htons(skb->len);
425 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
426 memcpy(ehdr->h_dest, dst, ETH_ALEN);
427 memcpy(ehdr->h_source, src, ETH_ALEN);
428 ehdr->h_proto = len;
429 }
430 return 0;
431}
432EXPORT_SYMBOL(ieee80211_data_to_8023);
433
eaf85ca7 434int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
e31a16d6
ZY
435 enum nl80211_iftype iftype, u8 *bssid, bool qos)
436{
437 struct ieee80211_hdr hdr;
438 u16 hdrlen, ethertype;
439 __le16 fc;
440 const u8 *encaps_data;
441 int encaps_len, skip_header_bytes;
442 int nh_pos, h_pos;
443 int head_need;
444
445 if (unlikely(skb->len < ETH_HLEN))
446 return -EINVAL;
447
448 nh_pos = skb_network_header(skb) - skb->data;
449 h_pos = skb_transport_header(skb) - skb->data;
450
451 /* convert Ethernet header to proper 802.11 header (based on
452 * operation mode) */
453 ethertype = (skb->data[12] << 8) | skb->data[13];
454 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
455
456 switch (iftype) {
457 case NL80211_IFTYPE_AP:
458 case NL80211_IFTYPE_AP_VLAN:
074ac8df 459 case NL80211_IFTYPE_P2P_GO:
e31a16d6
ZY
460 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
461 /* DA BSSID SA */
462 memcpy(hdr.addr1, skb->data, ETH_ALEN);
463 memcpy(hdr.addr2, addr, ETH_ALEN);
464 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
465 hdrlen = 24;
466 break;
467 case NL80211_IFTYPE_STATION:
074ac8df 468 case NL80211_IFTYPE_P2P_CLIENT:
e31a16d6
ZY
469 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
470 /* BSSID SA DA */
471 memcpy(hdr.addr1, bssid, ETH_ALEN);
472 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
473 memcpy(hdr.addr3, skb->data, ETH_ALEN);
474 hdrlen = 24;
475 break;
476 case NL80211_IFTYPE_ADHOC:
477 /* DA SA BSSID */
478 memcpy(hdr.addr1, skb->data, ETH_ALEN);
479 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
480 memcpy(hdr.addr3, bssid, ETH_ALEN);
481 hdrlen = 24;
482 break;
483 default:
484 return -EOPNOTSUPP;
485 }
486
487 if (qos) {
488 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
489 hdrlen += 2;
490 }
491
492 hdr.frame_control = fc;
493 hdr.duration_id = 0;
494 hdr.seq_ctrl = 0;
495
496 skip_header_bytes = ETH_HLEN;
497 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
498 encaps_data = bridge_tunnel_header;
499 encaps_len = sizeof(bridge_tunnel_header);
500 skip_header_bytes -= 2;
501 } else if (ethertype > 0x600) {
502 encaps_data = rfc1042_header;
503 encaps_len = sizeof(rfc1042_header);
504 skip_header_bytes -= 2;
505 } else {
506 encaps_data = NULL;
507 encaps_len = 0;
508 }
509
510 skb_pull(skb, skip_header_bytes);
511 nh_pos -= skip_header_bytes;
512 h_pos -= skip_header_bytes;
513
514 head_need = hdrlen + encaps_len - skb_headroom(skb);
515
516 if (head_need > 0 || skb_cloned(skb)) {
517 head_need = max(head_need, 0);
518 if (head_need)
519 skb_orphan(skb);
520
24616152 521 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
e31a16d6 522 return -ENOMEM;
24616152 523
e31a16d6
ZY
524 skb->truesize += head_need;
525 }
526
527 if (encaps_data) {
528 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
529 nh_pos += encaps_len;
530 h_pos += encaps_len;
531 }
532
533 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
534
535 nh_pos += hdrlen;
536 h_pos += hdrlen;
537
538 /* Update skb pointers to various headers since this modified frame
539 * is going to go through Linux networking code that may potentially
540 * need things like pointer to IP header. */
541 skb_set_mac_header(skb, 0);
542 skb_set_network_header(skb, nh_pos);
543 skb_set_transport_header(skb, h_pos);
544
545 return 0;
546}
547EXPORT_SYMBOL(ieee80211_data_from_8023);
548
eaf85ca7
ZY
549
550void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
551 const u8 *addr, enum nl80211_iftype iftype,
8b3becad
YAP
552 const unsigned int extra_headroom,
553 bool has_80211_header)
eaf85ca7
ZY
554{
555 struct sk_buff *frame = NULL;
556 u16 ethertype;
557 u8 *payload;
558 const struct ethhdr *eth;
559 int remaining, err;
560 u8 dst[ETH_ALEN], src[ETH_ALEN];
561
8b3becad
YAP
562 if (has_80211_header) {
563 err = ieee80211_data_to_8023(skb, addr, iftype);
564 if (err)
565 goto out;
eaf85ca7 566
8b3becad
YAP
567 /* skip the wrapping header */
568 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
569 if (!eth)
570 goto out;
571 } else {
572 eth = (struct ethhdr *) skb->data;
573 }
eaf85ca7
ZY
574
575 while (skb != frame) {
576 u8 padding;
577 __be16 len = eth->h_proto;
578 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
579
580 remaining = skb->len;
581 memcpy(dst, eth->h_dest, ETH_ALEN);
582 memcpy(src, eth->h_source, ETH_ALEN);
583
584 padding = (4 - subframe_len) & 0x3;
585 /* the last MSDU has no padding */
586 if (subframe_len > remaining)
587 goto purge;
588
589 skb_pull(skb, sizeof(struct ethhdr));
590 /* reuse skb for the last subframe */
591 if (remaining <= subframe_len + padding)
592 frame = skb;
593 else {
594 unsigned int hlen = ALIGN(extra_headroom, 4);
595 /*
596 * Allocate and reserve two bytes more for payload
597 * alignment since sizeof(struct ethhdr) is 14.
598 */
599 frame = dev_alloc_skb(hlen + subframe_len + 2);
600 if (!frame)
601 goto purge;
602
603 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
604 memcpy(skb_put(frame, ntohs(len)), skb->data,
605 ntohs(len));
606
607 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
608 padding);
609 if (!eth) {
610 dev_kfree_skb(frame);
611 goto purge;
612 }
613 }
614
615 skb_reset_network_header(frame);
616 frame->dev = skb->dev;
617 frame->priority = skb->priority;
618
619 payload = frame->data;
620 ethertype = (payload[6] << 8) | payload[7];
621
622 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
623 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
624 compare_ether_addr(payload,
625 bridge_tunnel_header) == 0)) {
626 /* remove RFC1042 or Bridge-Tunnel
627 * encapsulation and replace EtherType */
628 skb_pull(frame, 6);
629 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
630 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
631 } else {
632 memcpy(skb_push(frame, sizeof(__be16)), &len,
633 sizeof(__be16));
634 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
635 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
636 }
637 __skb_queue_tail(list, frame);
638 }
639
640 return;
641
642 purge:
643 __skb_queue_purge(list);
644 out:
645 dev_kfree_skb(skb);
646}
647EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
648
e31a16d6
ZY
649/* Given a data frame determine the 802.1p/1d tag to use. */
650unsigned int cfg80211_classify8021d(struct sk_buff *skb)
651{
652 unsigned int dscp;
653
654 /* skb->priority values from 256->263 are magic values to
655 * directly indicate a specific 802.1d priority. This is used
656 * to allow 802.1d priority to be passed directly in from VLAN
657 * tags, etc.
658 */
659 if (skb->priority >= 256 && skb->priority <= 263)
660 return skb->priority - 256;
661
662 switch (skb->protocol) {
663 case htons(ETH_P_IP):
664 dscp = ip_hdr(skb)->tos & 0xfc;
665 break;
666 default:
667 return 0;
668 }
669
670 return dscp >> 5;
671}
672EXPORT_SYMBOL(cfg80211_classify8021d);
517357c6
JB
673
674const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
675{
676 u8 *end, *pos;
677
678 pos = bss->information_elements;
679 if (pos == NULL)
680 return NULL;
681 end = pos + bss->len_information_elements;
682
683 while (pos + 1 < end) {
684 if (pos + 2 + pos[1] > end)
685 break;
686 if (pos[0] == ie)
687 return pos;
688 pos += 2 + pos[1];
689 }
690
691 return NULL;
692}
693EXPORT_SYMBOL(ieee80211_bss_get_ie);
fffd0934
JB
694
695void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
696{
697 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
698 struct net_device *dev = wdev->netdev;
699 int i;
700
701 if (!wdev->connect_keys)
702 return;
703
704 for (i = 0; i < 6; i++) {
705 if (!wdev->connect_keys->params[i].cipher)
706 continue;
e31b8213 707 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
1e056665 708 &wdev->connect_keys->params[i])) {
e9c0268f 709 netdev_err(dev, "failed to set key %d\n", i);
1e056665
ZY
710 continue;
711 }
fffd0934 712 if (wdev->connect_keys->def == i)
dbd2fd65
JB
713 if (rdev->ops->set_default_key(wdev->wiphy, dev,
714 i, true, true)) {
e9c0268f 715 netdev_err(dev, "failed to set defkey %d\n", i);
1e056665
ZY
716 continue;
717 }
fffd0934
JB
718 if (wdev->connect_keys->defmgmt == i)
719 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
e9c0268f 720 netdev_err(dev, "failed to set mgtdef %d\n", i);
fffd0934
JB
721 }
722
723 kfree(wdev->connect_keys);
724 wdev->connect_keys = NULL;
725}
3d54d255
JB
726
727static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
728{
729 struct cfg80211_event *ev;
730 unsigned long flags;
731 const u8 *bssid = NULL;
732
733 spin_lock_irqsave(&wdev->event_lock, flags);
734 while (!list_empty(&wdev->event_list)) {
735 ev = list_first_entry(&wdev->event_list,
736 struct cfg80211_event, list);
737 list_del(&ev->list);
738 spin_unlock_irqrestore(&wdev->event_lock, flags);
739
740 wdev_lock(wdev);
741 switch (ev->type) {
742 case EVENT_CONNECT_RESULT:
743 if (!is_zero_ether_addr(ev->cr.bssid))
744 bssid = ev->cr.bssid;
745 __cfg80211_connect_result(
746 wdev->netdev, bssid,
747 ev->cr.req_ie, ev->cr.req_ie_len,
748 ev->cr.resp_ie, ev->cr.resp_ie_len,
749 ev->cr.status,
750 ev->cr.status == WLAN_STATUS_SUCCESS,
751 NULL);
752 break;
753 case EVENT_ROAMED:
ed9d0102 754 __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid,
3d54d255
JB
755 ev->rm.req_ie, ev->rm.req_ie_len,
756 ev->rm.resp_ie, ev->rm.resp_ie_len);
757 break;
758 case EVENT_DISCONNECTED:
759 __cfg80211_disconnected(wdev->netdev,
760 ev->dc.ie, ev->dc.ie_len,
761 ev->dc.reason, true);
762 break;
763 case EVENT_IBSS_JOINED:
764 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
765 break;
766 }
767 wdev_unlock(wdev);
768
769 kfree(ev);
770
771 spin_lock_irqsave(&wdev->event_lock, flags);
772 }
773 spin_unlock_irqrestore(&wdev->event_lock, flags);
774}
775
776void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
777{
778 struct wireless_dev *wdev;
779
780 ASSERT_RTNL();
781 ASSERT_RDEV_LOCK(rdev);
782
783 mutex_lock(&rdev->devlist_mtx);
784
785 list_for_each_entry(wdev, &rdev->netdev_list, list)
786 cfg80211_process_wdev_events(wdev);
787
788 mutex_unlock(&rdev->devlist_mtx);
789}
790
791int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
792 struct net_device *dev, enum nl80211_iftype ntype,
793 u32 *flags, struct vif_params *params)
794{
795 int err;
796 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
797
798 ASSERT_RDEV_LOCK(rdev);
799
800 /* don't support changing VLANs, you just re-create them */
801 if (otype == NL80211_IFTYPE_AP_VLAN)
802 return -EOPNOTSUPP;
803
804 if (!rdev->ops->change_virtual_intf ||
805 !(rdev->wiphy.interface_modes & (1 << ntype)))
806 return -EOPNOTSUPP;
807
ad4bb6f8 808 /* if it's part of a bridge, reject changing type to station/ibss */
f350a0a8 809 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
074ac8df
JB
810 (ntype == NL80211_IFTYPE_ADHOC ||
811 ntype == NL80211_IFTYPE_STATION ||
812 ntype == NL80211_IFTYPE_P2P_CLIENT))
ad4bb6f8
JB
813 return -EBUSY;
814
3d54d255 815 if (ntype != otype) {
7527a782
JB
816 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
817 ntype);
818 if (err)
819 return err;
820
9bc383de 821 dev->ieee80211_ptr->use_4addr = false;
29cbe68c 822 dev->ieee80211_ptr->mesh_id_up_len = 0;
9bc383de 823
3d54d255
JB
824 switch (otype) {
825 case NL80211_IFTYPE_ADHOC:
826 cfg80211_leave_ibss(rdev, dev, false);
827 break;
828 case NL80211_IFTYPE_STATION:
074ac8df 829 case NL80211_IFTYPE_P2P_CLIENT:
3d54d255
JB
830 cfg80211_disconnect(rdev, dev,
831 WLAN_REASON_DEAUTH_LEAVING, true);
832 break;
833 case NL80211_IFTYPE_MESH_POINT:
834 /* mesh should be handled? */
835 break;
836 default:
837 break;
838 }
839
840 cfg80211_process_rdev_events(rdev);
841 }
842
843 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
844 ntype, flags, params);
845
846 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
847
9bc383de
JB
848 if (!err && params && params->use_4addr != -1)
849 dev->ieee80211_ptr->use_4addr = params->use_4addr;
850
ad4bb6f8
JB
851 if (!err) {
852 dev->priv_flags &= ~IFF_DONT_BRIDGE;
853 switch (ntype) {
854 case NL80211_IFTYPE_STATION:
855 if (dev->ieee80211_ptr->use_4addr)
856 break;
857 /* fall through */
074ac8df 858 case NL80211_IFTYPE_P2P_CLIENT:
ad4bb6f8
JB
859 case NL80211_IFTYPE_ADHOC:
860 dev->priv_flags |= IFF_DONT_BRIDGE;
861 break;
074ac8df 862 case NL80211_IFTYPE_P2P_GO:
ad4bb6f8
JB
863 case NL80211_IFTYPE_AP:
864 case NL80211_IFTYPE_AP_VLAN:
865 case NL80211_IFTYPE_WDS:
866 case NL80211_IFTYPE_MESH_POINT:
867 /* bridging OK */
868 break;
869 case NL80211_IFTYPE_MONITOR:
870 /* monitor can't bridge anyway */
871 break;
872 case NL80211_IFTYPE_UNSPECIFIED:
2e161f78 873 case NUM_NL80211_IFTYPES:
ad4bb6f8
JB
874 /* not happening */
875 break;
876 }
877 }
878
3d54d255
JB
879 return err;
880}
254416aa
JL
881
882u16 cfg80211_calculate_bitrate(struct rate_info *rate)
883{
884 int modulation, streams, bitrate;
885
886 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
887 return rate->legacy;
888
889 /* the formula below does only work for MCS values smaller than 32 */
890 if (rate->mcs >= 32)
891 return 0;
892
893 modulation = rate->mcs & 7;
894 streams = (rate->mcs >> 3) + 1;
895
896 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
897 13500000 : 6500000;
898
899 if (modulation < 4)
900 bitrate *= (modulation + 1);
901 else if (modulation == 4)
902 bitrate *= (modulation + 2);
903 else
904 bitrate *= (modulation + 3);
905
906 bitrate *= streams;
907
908 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
909 bitrate = (bitrate / 9) * 10;
910
911 /* do NOT round down here */
912 return (bitrate + 50000) / 100000;
913}
56d1893d
JB
914
915int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
916 u32 beacon_int)
917{
918 struct wireless_dev *wdev;
919 int res = 0;
920
921 if (!beacon_int)
922 return -EINVAL;
923
924 mutex_lock(&rdev->devlist_mtx);
925
926 list_for_each_entry(wdev, &rdev->netdev_list, list) {
927 if (!wdev->beacon_interval)
928 continue;
929 if (wdev->beacon_interval != beacon_int) {
930 res = -EINVAL;
931 break;
932 }
933 }
934
935 mutex_unlock(&rdev->devlist_mtx);
936
937 return res;
938}
7527a782
JB
939
940int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
941 struct wireless_dev *wdev,
942 enum nl80211_iftype iftype)
943{
944 struct wireless_dev *wdev_iter;
945 int num[NUM_NL80211_IFTYPES];
946 int total = 1;
947 int i, j;
948
949 ASSERT_RTNL();
950
951 /* Always allow software iftypes */
952 if (rdev->wiphy.software_iftypes & BIT(iftype))
953 return 0;
954
955 /*
956 * Drivers will gradually all set this flag, until all
957 * have it we only enforce for those that set it.
958 */
959 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
960 return 0;
961
962 memset(num, 0, sizeof(num));
963
964 num[iftype] = 1;
965
966 mutex_lock(&rdev->devlist_mtx);
967 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
968 if (wdev_iter == wdev)
969 continue;
970 if (!netif_running(wdev_iter->netdev))
971 continue;
972
973 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
974 continue;
975
976 num[wdev_iter->iftype]++;
977 total++;
978 }
979 mutex_unlock(&rdev->devlist_mtx);
980
981 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
982 const struct ieee80211_iface_combination *c;
983 struct ieee80211_iface_limit *limits;
984
985 c = &rdev->wiphy.iface_combinations[i];
986
987 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
988 GFP_KERNEL);
989 if (!limits)
990 return -ENOMEM;
991 if (total > c->max_interfaces)
992 goto cont;
993
994 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
995 if (rdev->wiphy.software_iftypes & BIT(iftype))
996 continue;
997 for (j = 0; j < c->n_limits; j++) {
998 if (!(limits[j].types & iftype))
999 continue;
1000 if (limits[j].max < num[iftype])
1001 goto cont;
1002 limits[j].max -= num[iftype];
1003 }
1004 }
1005 /* yay, it fits */
1006 kfree(limits);
1007 return 0;
1008 cont:
1009 kfree(limits);
1010 }
1011
1012 return -EBUSY;
1013}
34850ab2
JB
1014
1015int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1016 const u8 *rates, unsigned int n_rates,
1017 u32 *mask)
1018{
1019 int i, j;
1020
a401d2bb
JB
1021 if (!sband)
1022 return -EINVAL;
1023
34850ab2
JB
1024 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1025 return -EINVAL;
1026
1027 *mask = 0;
1028
1029 for (i = 0; i < n_rates; i++) {
1030 int rate = (rates[i] & 0x7f) * 5;
1031 bool found = false;
1032
1033 for (j = 0; j < sband->n_bitrates; j++) {
1034 if (sband->bitrates[j].bitrate == rate) {
1035 found = true;
1036 *mask |= BIT(j);
1037 break;
1038 }
1039 }
1040 if (!found)
1041 return -EINVAL;
1042 }
1043
1044 /*
1045 * mask must have at least one bit set here since we
1046 * didn't accept a 0-length rates array nor allowed
1047 * entries in the array that didn't exist
1048 */
1049
1050 return 0;
1051}
0879fa44
YAP
1052
1053u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
1054 struct ieee802_11_elems *elems,
1055 u64 filter, u32 crc)
1056{
1057 size_t left = len;
1058 u8 *pos = start;
1059 bool calc_crc = filter != 0;
1060
1061 memset(elems, 0, sizeof(*elems));
1062 elems->ie_start = start;
1063 elems->total_len = len;
1064
1065 while (left >= 2) {
1066 u8 id, elen;
1067
1068 id = *pos++;
1069 elen = *pos++;
1070 left -= 2;
1071
1072 if (elen > left)
1073 break;
1074
1075 if (calc_crc && id < 64 && (filter & (1ULL << id)))
1076 crc = crc32_be(crc, pos - 2, elen + 2);
1077
1078 switch (id) {
1079 case WLAN_EID_SSID:
1080 elems->ssid = pos;
1081 elems->ssid_len = elen;
1082 break;
1083 case WLAN_EID_SUPP_RATES:
1084 elems->supp_rates = pos;
1085 elems->supp_rates_len = elen;
1086 break;
1087 case WLAN_EID_FH_PARAMS:
1088 elems->fh_params = pos;
1089 elems->fh_params_len = elen;
1090 break;
1091 case WLAN_EID_DS_PARAMS:
1092 elems->ds_params = pos;
1093 elems->ds_params_len = elen;
1094 break;
1095 case WLAN_EID_CF_PARAMS:
1096 elems->cf_params = pos;
1097 elems->cf_params_len = elen;
1098 break;
1099 case WLAN_EID_TIM:
1100 if (elen >= sizeof(struct ieee80211_tim_ie)) {
1101 elems->tim = (void *)pos;
1102 elems->tim_len = elen;
1103 }
1104 break;
1105 case WLAN_EID_IBSS_PARAMS:
1106 elems->ibss_params = pos;
1107 elems->ibss_params_len = elen;
1108 break;
1109 case WLAN_EID_CHALLENGE:
1110 elems->challenge = pos;
1111 elems->challenge_len = elen;
1112 break;
1113 case WLAN_EID_VENDOR_SPECIFIC:
1114 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1115 pos[2] == 0xf2) {
1116 /* Microsoft OUI (00:50:F2) */
1117
1118 if (calc_crc)
1119 crc = crc32_be(crc, pos - 2, elen + 2);
1120
1121 if (pos[3] == 1) {
1122 /* OUI Type 1 - WPA IE */
1123 elems->wpa = pos;
1124 elems->wpa_len = elen;
1125 } else if (elen >= 5 && pos[3] == 2) {
1126 /* OUI Type 2 - WMM IE */
1127 if (pos[4] == 0) {
1128 elems->wmm_info = pos;
1129 elems->wmm_info_len = elen;
1130 } else if (pos[4] == 1) {
1131 elems->wmm_param = pos;
1132 elems->wmm_param_len = elen;
1133 }
1134 }
1135 }
1136 break;
1137 case WLAN_EID_RSN:
1138 elems->rsn = pos;
1139 elems->rsn_len = elen;
1140 break;
1141 case WLAN_EID_ERP_INFO:
1142 elems->erp_info = pos;
1143 elems->erp_info_len = elen;
1144 break;
1145 case WLAN_EID_EXT_SUPP_RATES:
1146 elems->ext_supp_rates = pos;
1147 elems->ext_supp_rates_len = elen;
1148 break;
1149 case WLAN_EID_HT_CAPABILITY:
1150 if (elen >= sizeof(struct ieee80211_ht_cap))
1151 elems->ht_cap_elem = (void *)pos;
1152 break;
1153 case WLAN_EID_HT_INFORMATION:
1154 if (elen >= sizeof(struct ieee80211_ht_info))
1155 elems->ht_info_elem = (void *)pos;
1156 break;
1157 case WLAN_EID_MESH_ID:
1158 elems->mesh_id = pos;
1159 elems->mesh_id_len = elen;
1160 break;
1161 case WLAN_EID_MESH_CONFIG:
1162 if (elen >= sizeof(struct ieee80211_meshconf_ie))
1163 elems->mesh_config = (void *)pos;
1164 break;
8db09850
TP
1165 case WLAN_EID_PEER_MGMT:
1166 elems->peering = pos;
1167 elems->peering_len = elen;
0879fa44
YAP
1168 break;
1169 case WLAN_EID_PREQ:
1170 elems->preq = pos;
1171 elems->preq_len = elen;
1172 break;
1173 case WLAN_EID_PREP:
1174 elems->prep = pos;
1175 elems->prep_len = elen;
1176 break;
1177 case WLAN_EID_PERR:
1178 elems->perr = pos;
1179 elems->perr_len = elen;
1180 break;
1181 case WLAN_EID_RANN:
1182 if (elen >= sizeof(struct ieee80211_rann_ie))
1183 elems->rann = (void *)pos;
1184 break;
1185 case WLAN_EID_CHANNEL_SWITCH:
1186 elems->ch_switch_elem = pos;
1187 elems->ch_switch_elem_len = elen;
1188 break;
1189 case WLAN_EID_QUIET:
1190 if (!elems->quiet_elem) {
1191 elems->quiet_elem = pos;
1192 elems->quiet_elem_len = elen;
1193 }
1194 elems->num_of_quiet_elem++;
1195 break;
1196 case WLAN_EID_COUNTRY:
1197 elems->country_elem = pos;
1198 elems->country_elem_len = elen;
1199 break;
1200 case WLAN_EID_PWR_CONSTRAINT:
1201 elems->pwr_constr_elem = pos;
1202 elems->pwr_constr_elem_len = elen;
1203 break;
1204 case WLAN_EID_TIMEOUT_INTERVAL:
1205 elems->timeout_int = pos;
1206 elems->timeout_int_len = elen;
1207 break;
1208 default:
1209 break;
1210 }
1211
1212 left -= elen;
1213 pos += elen;
1214 }
1215
1216 return crc;
1217}
1218EXPORT_SYMBOL(ieee802_11_parse_elems_crc);