wireless: Remove unnecessary OOM logging messages
[linux-2.6/cjktty.git] / net / wireless / util.c
blob39dbf4ad7ca17cdca7581355b5a5b0aaa6221b48
1 /*
2 * Wireless utility functions
4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
5 */
6 #include <linux/bitops.h>
7 #include <linux/etherdevice.h>
8 #include <linux/slab.h>
9 #include <linux/crc32.h>
10 #include <net/cfg80211.h>
11 #include <net/ip.h>
12 #include "core.h"
14 struct ieee80211_rate *
15 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
16 u32 basic_rates, int bitrate)
18 struct ieee80211_rate *result = &sband->bitrates[0];
19 int i;
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];
29 return result;
31 EXPORT_SYMBOL(ieee80211_get_response_rate);
33 int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
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 */
51 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
53 int ieee80211_frequency_to_channel(int freq)
55 /* see 802.11 17.3.8.3.2 and Annex J */
56 if (freq == 2484)
57 return 14;
58 else if (freq < 2484)
59 return (freq - 2407) / 5;
60 else if (freq >= 4910 && freq <= 4980)
61 return (freq - 4000) / 5;
62 else
63 return (freq - 5000) / 5;
65 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
67 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
68 int freq)
70 enum ieee80211_band band;
71 struct ieee80211_supported_band *sband;
72 int i;
74 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
75 sband = wiphy->bands[band];
77 if (!sband)
78 continue;
80 for (i = 0; i < sband->n_channels; i++) {
81 if (sband->channels[i].center_freq == freq)
82 return &sband->channels[i];
86 return NULL;
88 EXPORT_SYMBOL(__ieee80211_get_channel);
90 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
91 enum ieee80211_band band)
93 int i, want;
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--;
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--;
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--;
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)
134 sband->bitrates[i].flags |=
135 IEEE80211_RATE_ERP_G;
137 WARN_ON(want != 0 && want != 3 && want != 6);
138 break;
139 case IEEE80211_NUM_BANDS:
140 WARN_ON(1);
141 break;
145 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
147 enum ieee80211_band band;
149 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
150 if (wiphy->bands[band])
151 set_mandatory_flags_band(wiphy->bands[band], band);
154 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
155 struct key_params *params, int key_idx,
156 bool pairwise, const u8 *mac_addr)
158 int i;
160 if (key_idx > 5)
161 return -EINVAL;
163 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
164 return -EINVAL;
166 if (pairwise && !mac_addr)
167 return -EINVAL;
170 * Disallow pairwise keys with non-zero index unless it's WEP
171 * or a vendor specific cipher (because current deployments use
172 * pairwise WEP keys with non-zero indices and for vendor specific
173 * ciphers this should be validated in the driver or hardware level
174 * - but 802.11i clearly specifies to use zero)
176 if (pairwise && key_idx &&
177 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
178 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
179 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
180 return -EINVAL;
182 switch (params->cipher) {
183 case WLAN_CIPHER_SUITE_WEP40:
184 if (params->key_len != WLAN_KEY_LEN_WEP40)
185 return -EINVAL;
186 break;
187 case WLAN_CIPHER_SUITE_TKIP:
188 if (params->key_len != WLAN_KEY_LEN_TKIP)
189 return -EINVAL;
190 break;
191 case WLAN_CIPHER_SUITE_CCMP:
192 if (params->key_len != WLAN_KEY_LEN_CCMP)
193 return -EINVAL;
194 break;
195 case WLAN_CIPHER_SUITE_WEP104:
196 if (params->key_len != WLAN_KEY_LEN_WEP104)
197 return -EINVAL;
198 break;
199 case WLAN_CIPHER_SUITE_AES_CMAC:
200 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
201 return -EINVAL;
202 break;
203 default:
205 * We don't know anything about this algorithm,
206 * allow using it -- but the driver must check
207 * all parameters! We still check below whether
208 * or not the driver supports this algorithm,
209 * of course.
211 break;
214 if (params->seq) {
215 switch (params->cipher) {
216 case WLAN_CIPHER_SUITE_WEP40:
217 case WLAN_CIPHER_SUITE_WEP104:
218 /* These ciphers do not use key sequence */
219 return -EINVAL;
220 case WLAN_CIPHER_SUITE_TKIP:
221 case WLAN_CIPHER_SUITE_CCMP:
222 case WLAN_CIPHER_SUITE_AES_CMAC:
223 if (params->seq_len != 6)
224 return -EINVAL;
225 break;
229 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
230 if (params->cipher == rdev->wiphy.cipher_suites[i])
231 break;
232 if (i == rdev->wiphy.n_cipher_suites)
233 return -EINVAL;
235 return 0;
238 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
239 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
240 const unsigned char rfc1042_header[] __aligned(2) =
241 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
242 EXPORT_SYMBOL(rfc1042_header);
244 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
245 const unsigned char bridge_tunnel_header[] __aligned(2) =
246 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
247 EXPORT_SYMBOL(bridge_tunnel_header);
249 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
251 unsigned int hdrlen = 24;
253 if (ieee80211_is_data(fc)) {
254 if (ieee80211_has_a4(fc))
255 hdrlen = 30;
256 if (ieee80211_is_data_qos(fc)) {
257 hdrlen += IEEE80211_QOS_CTL_LEN;
258 if (ieee80211_has_order(fc))
259 hdrlen += IEEE80211_HT_CTL_LEN;
261 goto out;
264 if (ieee80211_is_ctl(fc)) {
266 * ACK and CTS are 10 bytes, all others 16. To see how
267 * to get this condition consider
268 * subtype mask: 0b0000000011110000 (0x00F0)
269 * ACK subtype: 0b0000000011010000 (0x00D0)
270 * CTS subtype: 0b0000000011000000 (0x00C0)
271 * bits that matter: ^^^ (0x00E0)
272 * value of those: 0b0000000011000000 (0x00C0)
274 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
275 hdrlen = 10;
276 else
277 hdrlen = 16;
279 out:
280 return hdrlen;
282 EXPORT_SYMBOL(ieee80211_hdrlen);
284 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
286 const struct ieee80211_hdr *hdr =
287 (const struct ieee80211_hdr *)skb->data;
288 unsigned int hdrlen;
290 if (unlikely(skb->len < 10))
291 return 0;
292 hdrlen = ieee80211_hdrlen(hdr->frame_control);
293 if (unlikely(hdrlen > skb->len))
294 return 0;
295 return hdrlen;
297 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
299 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
301 int ae = meshhdr->flags & MESH_FLAGS_AE;
302 /* 7.1.3.5a.2 */
303 switch (ae) {
304 case 0:
305 return 6;
306 case MESH_FLAGS_AE_A4:
307 return 12;
308 case MESH_FLAGS_AE_A5_A6:
309 return 18;
310 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
311 return 24;
312 default:
313 return 6;
317 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
318 enum nl80211_iftype iftype)
320 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
321 u16 hdrlen, ethertype;
322 u8 *payload;
323 u8 dst[ETH_ALEN];
324 u8 src[ETH_ALEN] __aligned(2);
326 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
327 return -1;
329 hdrlen = ieee80211_hdrlen(hdr->frame_control);
331 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
332 * header
333 * IEEE 802.11 address fields:
334 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
335 * 0 0 DA SA BSSID n/a
336 * 0 1 DA BSSID SA n/a
337 * 1 0 BSSID SA DA n/a
338 * 1 1 RA TA DA SA
340 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
341 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
343 switch (hdr->frame_control &
344 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
345 case cpu_to_le16(IEEE80211_FCTL_TODS):
346 if (unlikely(iftype != NL80211_IFTYPE_AP &&
347 iftype != NL80211_IFTYPE_AP_VLAN &&
348 iftype != NL80211_IFTYPE_P2P_GO))
349 return -1;
350 break;
351 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
352 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
353 iftype != NL80211_IFTYPE_MESH_POINT &&
354 iftype != NL80211_IFTYPE_AP_VLAN &&
355 iftype != NL80211_IFTYPE_STATION))
356 return -1;
357 if (iftype == NL80211_IFTYPE_MESH_POINT) {
358 struct ieee80211s_hdr *meshdr =
359 (struct ieee80211s_hdr *) (skb->data + hdrlen);
360 /* make sure meshdr->flags is on the linear part */
361 if (!pskb_may_pull(skb, hdrlen + 1))
362 return -1;
363 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
364 skb_copy_bits(skb, hdrlen +
365 offsetof(struct ieee80211s_hdr, eaddr1),
366 dst, ETH_ALEN);
367 skb_copy_bits(skb, hdrlen +
368 offsetof(struct ieee80211s_hdr, eaddr2),
369 src, ETH_ALEN);
371 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
373 break;
374 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
375 if ((iftype != NL80211_IFTYPE_STATION &&
376 iftype != NL80211_IFTYPE_P2P_CLIENT &&
377 iftype != NL80211_IFTYPE_MESH_POINT) ||
378 (is_multicast_ether_addr(dst) &&
379 !compare_ether_addr(src, addr)))
380 return -1;
381 if (iftype == NL80211_IFTYPE_MESH_POINT) {
382 struct ieee80211s_hdr *meshdr =
383 (struct ieee80211s_hdr *) (skb->data + hdrlen);
384 /* make sure meshdr->flags is on the linear part */
385 if (!pskb_may_pull(skb, hdrlen + 1))
386 return -1;
387 if (meshdr->flags & MESH_FLAGS_AE_A4)
388 skb_copy_bits(skb, hdrlen +
389 offsetof(struct ieee80211s_hdr, eaddr1),
390 src, ETH_ALEN);
391 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
393 break;
394 case cpu_to_le16(0):
395 if (iftype != NL80211_IFTYPE_ADHOC)
396 return -1;
397 break;
400 if (!pskb_may_pull(skb, hdrlen + 8))
401 return -1;
403 payload = skb->data + hdrlen;
404 ethertype = (payload[6] << 8) | payload[7];
406 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
407 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
408 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
409 /* remove RFC1042 or Bridge-Tunnel encapsulation and
410 * replace EtherType */
411 skb_pull(skb, hdrlen + 6);
412 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
413 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
414 } else {
415 struct ethhdr *ehdr;
416 __be16 len;
418 skb_pull(skb, hdrlen);
419 len = htons(skb->len);
420 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
421 memcpy(ehdr->h_dest, dst, ETH_ALEN);
422 memcpy(ehdr->h_source, src, ETH_ALEN);
423 ehdr->h_proto = len;
425 return 0;
427 EXPORT_SYMBOL(ieee80211_data_to_8023);
429 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
430 enum nl80211_iftype iftype, u8 *bssid, bool qos)
432 struct ieee80211_hdr hdr;
433 u16 hdrlen, ethertype;
434 __le16 fc;
435 const u8 *encaps_data;
436 int encaps_len, skip_header_bytes;
437 int nh_pos, h_pos;
438 int head_need;
440 if (unlikely(skb->len < ETH_HLEN))
441 return -EINVAL;
443 nh_pos = skb_network_header(skb) - skb->data;
444 h_pos = skb_transport_header(skb) - skb->data;
446 /* convert Ethernet header to proper 802.11 header (based on
447 * operation mode) */
448 ethertype = (skb->data[12] << 8) | skb->data[13];
449 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
451 switch (iftype) {
452 case NL80211_IFTYPE_AP:
453 case NL80211_IFTYPE_AP_VLAN:
454 case NL80211_IFTYPE_P2P_GO:
455 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
456 /* DA BSSID SA */
457 memcpy(hdr.addr1, skb->data, ETH_ALEN);
458 memcpy(hdr.addr2, addr, ETH_ALEN);
459 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
460 hdrlen = 24;
461 break;
462 case NL80211_IFTYPE_STATION:
463 case NL80211_IFTYPE_P2P_CLIENT:
464 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
465 /* BSSID SA DA */
466 memcpy(hdr.addr1, bssid, ETH_ALEN);
467 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
468 memcpy(hdr.addr3, skb->data, ETH_ALEN);
469 hdrlen = 24;
470 break;
471 case NL80211_IFTYPE_ADHOC:
472 /* DA SA BSSID */
473 memcpy(hdr.addr1, skb->data, ETH_ALEN);
474 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
475 memcpy(hdr.addr3, bssid, ETH_ALEN);
476 hdrlen = 24;
477 break;
478 default:
479 return -EOPNOTSUPP;
482 if (qos) {
483 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
484 hdrlen += 2;
487 hdr.frame_control = fc;
488 hdr.duration_id = 0;
489 hdr.seq_ctrl = 0;
491 skip_header_bytes = ETH_HLEN;
492 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
493 encaps_data = bridge_tunnel_header;
494 encaps_len = sizeof(bridge_tunnel_header);
495 skip_header_bytes -= 2;
496 } else if (ethertype > 0x600) {
497 encaps_data = rfc1042_header;
498 encaps_len = sizeof(rfc1042_header);
499 skip_header_bytes -= 2;
500 } else {
501 encaps_data = NULL;
502 encaps_len = 0;
505 skb_pull(skb, skip_header_bytes);
506 nh_pos -= skip_header_bytes;
507 h_pos -= skip_header_bytes;
509 head_need = hdrlen + encaps_len - skb_headroom(skb);
511 if (head_need > 0 || skb_cloned(skb)) {
512 head_need = max(head_need, 0);
513 if (head_need)
514 skb_orphan(skb);
516 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
517 return -ENOMEM;
519 skb->truesize += head_need;
522 if (encaps_data) {
523 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
524 nh_pos += encaps_len;
525 h_pos += encaps_len;
528 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
530 nh_pos += hdrlen;
531 h_pos += hdrlen;
533 /* Update skb pointers to various headers since this modified frame
534 * is going to go through Linux networking code that may potentially
535 * need things like pointer to IP header. */
536 skb_set_mac_header(skb, 0);
537 skb_set_network_header(skb, nh_pos);
538 skb_set_transport_header(skb, h_pos);
540 return 0;
542 EXPORT_SYMBOL(ieee80211_data_from_8023);
545 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
546 const u8 *addr, enum nl80211_iftype iftype,
547 const unsigned int extra_headroom,
548 bool has_80211_header)
550 struct sk_buff *frame = NULL;
551 u16 ethertype;
552 u8 *payload;
553 const struct ethhdr *eth;
554 int remaining, err;
555 u8 dst[ETH_ALEN], src[ETH_ALEN];
557 if (has_80211_header) {
558 err = ieee80211_data_to_8023(skb, addr, iftype);
559 if (err)
560 goto out;
562 /* skip the wrapping header */
563 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
564 if (!eth)
565 goto out;
566 } else {
567 eth = (struct ethhdr *) skb->data;
570 while (skb != frame) {
571 u8 padding;
572 __be16 len = eth->h_proto;
573 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
575 remaining = skb->len;
576 memcpy(dst, eth->h_dest, ETH_ALEN);
577 memcpy(src, eth->h_source, ETH_ALEN);
579 padding = (4 - subframe_len) & 0x3;
580 /* the last MSDU has no padding */
581 if (subframe_len > remaining)
582 goto purge;
584 skb_pull(skb, sizeof(struct ethhdr));
585 /* reuse skb for the last subframe */
586 if (remaining <= subframe_len + padding)
587 frame = skb;
588 else {
589 unsigned int hlen = ALIGN(extra_headroom, 4);
591 * Allocate and reserve two bytes more for payload
592 * alignment since sizeof(struct ethhdr) is 14.
594 frame = dev_alloc_skb(hlen + subframe_len + 2);
595 if (!frame)
596 goto purge;
598 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
599 memcpy(skb_put(frame, ntohs(len)), skb->data,
600 ntohs(len));
602 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
603 padding);
604 if (!eth) {
605 dev_kfree_skb(frame);
606 goto purge;
610 skb_reset_network_header(frame);
611 frame->dev = skb->dev;
612 frame->priority = skb->priority;
614 payload = frame->data;
615 ethertype = (payload[6] << 8) | payload[7];
617 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
618 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
619 compare_ether_addr(payload,
620 bridge_tunnel_header) == 0)) {
621 /* remove RFC1042 or Bridge-Tunnel
622 * encapsulation and replace EtherType */
623 skb_pull(frame, 6);
624 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
625 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
626 } else {
627 memcpy(skb_push(frame, sizeof(__be16)), &len,
628 sizeof(__be16));
629 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
630 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
632 __skb_queue_tail(list, frame);
635 return;
637 purge:
638 __skb_queue_purge(list);
639 out:
640 dev_kfree_skb(skb);
642 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
644 /* Given a data frame determine the 802.1p/1d tag to use. */
645 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
647 unsigned int dscp;
649 /* skb->priority values from 256->263 are magic values to
650 * directly indicate a specific 802.1d priority. This is used
651 * to allow 802.1d priority to be passed directly in from VLAN
652 * tags, etc.
654 if (skb->priority >= 256 && skb->priority <= 263)
655 return skb->priority - 256;
657 switch (skb->protocol) {
658 case htons(ETH_P_IP):
659 dscp = ip_hdr(skb)->tos & 0xfc;
660 break;
661 default:
662 return 0;
665 return dscp >> 5;
667 EXPORT_SYMBOL(cfg80211_classify8021d);
669 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
671 u8 *end, *pos;
673 pos = bss->information_elements;
674 if (pos == NULL)
675 return NULL;
676 end = pos + bss->len_information_elements;
678 while (pos + 1 < end) {
679 if (pos + 2 + pos[1] > end)
680 break;
681 if (pos[0] == ie)
682 return pos;
683 pos += 2 + pos[1];
686 return NULL;
688 EXPORT_SYMBOL(ieee80211_bss_get_ie);
690 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
692 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
693 struct net_device *dev = wdev->netdev;
694 int i;
696 if (!wdev->connect_keys)
697 return;
699 for (i = 0; i < 6; i++) {
700 if (!wdev->connect_keys->params[i].cipher)
701 continue;
702 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
703 &wdev->connect_keys->params[i])) {
704 netdev_err(dev, "failed to set key %d\n", i);
705 continue;
707 if (wdev->connect_keys->def == i)
708 if (rdev->ops->set_default_key(wdev->wiphy, dev,
709 i, true, true)) {
710 netdev_err(dev, "failed to set defkey %d\n", i);
711 continue;
713 if (wdev->connect_keys->defmgmt == i)
714 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
715 netdev_err(dev, "failed to set mgtdef %d\n", i);
718 kfree(wdev->connect_keys);
719 wdev->connect_keys = NULL;
722 static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
724 struct cfg80211_event *ev;
725 unsigned long flags;
726 const u8 *bssid = NULL;
728 spin_lock_irqsave(&wdev->event_lock, flags);
729 while (!list_empty(&wdev->event_list)) {
730 ev = list_first_entry(&wdev->event_list,
731 struct cfg80211_event, list);
732 list_del(&ev->list);
733 spin_unlock_irqrestore(&wdev->event_lock, flags);
735 wdev_lock(wdev);
736 switch (ev->type) {
737 case EVENT_CONNECT_RESULT:
738 if (!is_zero_ether_addr(ev->cr.bssid))
739 bssid = ev->cr.bssid;
740 __cfg80211_connect_result(
741 wdev->netdev, bssid,
742 ev->cr.req_ie, ev->cr.req_ie_len,
743 ev->cr.resp_ie, ev->cr.resp_ie_len,
744 ev->cr.status,
745 ev->cr.status == WLAN_STATUS_SUCCESS,
746 NULL);
747 break;
748 case EVENT_ROAMED:
749 __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid,
750 ev->rm.req_ie, ev->rm.req_ie_len,
751 ev->rm.resp_ie, ev->rm.resp_ie_len);
752 break;
753 case EVENT_DISCONNECTED:
754 __cfg80211_disconnected(wdev->netdev,
755 ev->dc.ie, ev->dc.ie_len,
756 ev->dc.reason, true);
757 break;
758 case EVENT_IBSS_JOINED:
759 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
760 break;
762 wdev_unlock(wdev);
764 kfree(ev);
766 spin_lock_irqsave(&wdev->event_lock, flags);
768 spin_unlock_irqrestore(&wdev->event_lock, flags);
771 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
773 struct wireless_dev *wdev;
775 ASSERT_RTNL();
776 ASSERT_RDEV_LOCK(rdev);
778 mutex_lock(&rdev->devlist_mtx);
780 list_for_each_entry(wdev, &rdev->netdev_list, list)
781 cfg80211_process_wdev_events(wdev);
783 mutex_unlock(&rdev->devlist_mtx);
786 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
787 struct net_device *dev, enum nl80211_iftype ntype,
788 u32 *flags, struct vif_params *params)
790 int err;
791 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
793 ASSERT_RDEV_LOCK(rdev);
795 /* don't support changing VLANs, you just re-create them */
796 if (otype == NL80211_IFTYPE_AP_VLAN)
797 return -EOPNOTSUPP;
799 if (!rdev->ops->change_virtual_intf ||
800 !(rdev->wiphy.interface_modes & (1 << ntype)))
801 return -EOPNOTSUPP;
803 /* if it's part of a bridge, reject changing type to station/ibss */
804 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
805 (ntype == NL80211_IFTYPE_ADHOC ||
806 ntype == NL80211_IFTYPE_STATION ||
807 ntype == NL80211_IFTYPE_P2P_CLIENT))
808 return -EBUSY;
810 if (ntype != otype) {
811 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
812 ntype);
813 if (err)
814 return err;
816 dev->ieee80211_ptr->use_4addr = false;
817 dev->ieee80211_ptr->mesh_id_up_len = 0;
819 switch (otype) {
820 case NL80211_IFTYPE_ADHOC:
821 cfg80211_leave_ibss(rdev, dev, false);
822 break;
823 case NL80211_IFTYPE_STATION:
824 case NL80211_IFTYPE_P2P_CLIENT:
825 cfg80211_disconnect(rdev, dev,
826 WLAN_REASON_DEAUTH_LEAVING, true);
827 break;
828 case NL80211_IFTYPE_MESH_POINT:
829 /* mesh should be handled? */
830 break;
831 default:
832 break;
835 cfg80211_process_rdev_events(rdev);
838 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
839 ntype, flags, params);
841 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
843 if (!err && params && params->use_4addr != -1)
844 dev->ieee80211_ptr->use_4addr = params->use_4addr;
846 if (!err) {
847 dev->priv_flags &= ~IFF_DONT_BRIDGE;
848 switch (ntype) {
849 case NL80211_IFTYPE_STATION:
850 if (dev->ieee80211_ptr->use_4addr)
851 break;
852 /* fall through */
853 case NL80211_IFTYPE_P2P_CLIENT:
854 case NL80211_IFTYPE_ADHOC:
855 dev->priv_flags |= IFF_DONT_BRIDGE;
856 break;
857 case NL80211_IFTYPE_P2P_GO:
858 case NL80211_IFTYPE_AP:
859 case NL80211_IFTYPE_AP_VLAN:
860 case NL80211_IFTYPE_WDS:
861 case NL80211_IFTYPE_MESH_POINT:
862 /* bridging OK */
863 break;
864 case NL80211_IFTYPE_MONITOR:
865 /* monitor can't bridge anyway */
866 break;
867 case NL80211_IFTYPE_UNSPECIFIED:
868 case NUM_NL80211_IFTYPES:
869 /* not happening */
870 break;
874 return err;
877 u16 cfg80211_calculate_bitrate(struct rate_info *rate)
879 int modulation, streams, bitrate;
881 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
882 return rate->legacy;
884 /* the formula below does only work for MCS values smaller than 32 */
885 if (rate->mcs >= 32)
886 return 0;
888 modulation = rate->mcs & 7;
889 streams = (rate->mcs >> 3) + 1;
891 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
892 13500000 : 6500000;
894 if (modulation < 4)
895 bitrate *= (modulation + 1);
896 else if (modulation == 4)
897 bitrate *= (modulation + 2);
898 else
899 bitrate *= (modulation + 3);
901 bitrate *= streams;
903 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
904 bitrate = (bitrate / 9) * 10;
906 /* do NOT round down here */
907 return (bitrate + 50000) / 100000;
910 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
911 u32 beacon_int)
913 struct wireless_dev *wdev;
914 int res = 0;
916 if (!beacon_int)
917 return -EINVAL;
919 mutex_lock(&rdev->devlist_mtx);
921 list_for_each_entry(wdev, &rdev->netdev_list, list) {
922 if (!wdev->beacon_interval)
923 continue;
924 if (wdev->beacon_interval != beacon_int) {
925 res = -EINVAL;
926 break;
930 mutex_unlock(&rdev->devlist_mtx);
932 return res;
935 int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
936 struct wireless_dev *wdev,
937 enum nl80211_iftype iftype)
939 struct wireless_dev *wdev_iter;
940 int num[NUM_NL80211_IFTYPES];
941 int total = 1;
942 int i, j;
944 ASSERT_RTNL();
946 /* Always allow software iftypes */
947 if (rdev->wiphy.software_iftypes & BIT(iftype))
948 return 0;
951 * Drivers will gradually all set this flag, until all
952 * have it we only enforce for those that set it.
954 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
955 return 0;
957 memset(num, 0, sizeof(num));
959 num[iftype] = 1;
961 mutex_lock(&rdev->devlist_mtx);
962 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
963 if (wdev_iter == wdev)
964 continue;
965 if (!netif_running(wdev_iter->netdev))
966 continue;
968 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
969 continue;
971 num[wdev_iter->iftype]++;
972 total++;
974 mutex_unlock(&rdev->devlist_mtx);
976 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
977 const struct ieee80211_iface_combination *c;
978 struct ieee80211_iface_limit *limits;
980 c = &rdev->wiphy.iface_combinations[i];
982 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
983 GFP_KERNEL);
984 if (!limits)
985 return -ENOMEM;
986 if (total > c->max_interfaces)
987 goto cont;
989 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
990 if (rdev->wiphy.software_iftypes & BIT(iftype))
991 continue;
992 for (j = 0; j < c->n_limits; j++) {
993 if (!(limits[j].types & iftype))
994 continue;
995 if (limits[j].max < num[iftype])
996 goto cont;
997 limits[j].max -= num[iftype];
1000 /* yay, it fits */
1001 kfree(limits);
1002 return 0;
1003 cont:
1004 kfree(limits);
1007 return -EBUSY;
1010 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1011 const u8 *rates, unsigned int n_rates,
1012 u32 *mask)
1014 int i, j;
1016 if (!sband)
1017 return -EINVAL;
1019 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1020 return -EINVAL;
1022 *mask = 0;
1024 for (i = 0; i < n_rates; i++) {
1025 int rate = (rates[i] & 0x7f) * 5;
1026 bool found = false;
1028 for (j = 0; j < sband->n_bitrates; j++) {
1029 if (sband->bitrates[j].bitrate == rate) {
1030 found = true;
1031 *mask |= BIT(j);
1032 break;
1035 if (!found)
1036 return -EINVAL;
1040 * mask must have at least one bit set here since we
1041 * didn't accept a 0-length rates array nor allowed
1042 * entries in the array that didn't exist
1045 return 0;
1048 u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
1049 struct ieee802_11_elems *elems,
1050 u64 filter, u32 crc)
1052 size_t left = len;
1053 u8 *pos = start;
1054 bool calc_crc = filter != 0;
1056 memset(elems, 0, sizeof(*elems));
1057 elems->ie_start = start;
1058 elems->total_len = len;
1060 while (left >= 2) {
1061 u8 id, elen;
1063 id = *pos++;
1064 elen = *pos++;
1065 left -= 2;
1067 if (elen > left)
1068 break;
1070 if (calc_crc && id < 64 && (filter & (1ULL << id)))
1071 crc = crc32_be(crc, pos - 2, elen + 2);
1073 switch (id) {
1074 case WLAN_EID_SSID:
1075 elems->ssid = pos;
1076 elems->ssid_len = elen;
1077 break;
1078 case WLAN_EID_SUPP_RATES:
1079 elems->supp_rates = pos;
1080 elems->supp_rates_len = elen;
1081 break;
1082 case WLAN_EID_FH_PARAMS:
1083 elems->fh_params = pos;
1084 elems->fh_params_len = elen;
1085 break;
1086 case WLAN_EID_DS_PARAMS:
1087 elems->ds_params = pos;
1088 elems->ds_params_len = elen;
1089 break;
1090 case WLAN_EID_CF_PARAMS:
1091 elems->cf_params = pos;
1092 elems->cf_params_len = elen;
1093 break;
1094 case WLAN_EID_TIM:
1095 if (elen >= sizeof(struct ieee80211_tim_ie)) {
1096 elems->tim = (void *)pos;
1097 elems->tim_len = elen;
1099 break;
1100 case WLAN_EID_IBSS_PARAMS:
1101 elems->ibss_params = pos;
1102 elems->ibss_params_len = elen;
1103 break;
1104 case WLAN_EID_CHALLENGE:
1105 elems->challenge = pos;
1106 elems->challenge_len = elen;
1107 break;
1108 case WLAN_EID_VENDOR_SPECIFIC:
1109 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1110 pos[2] == 0xf2) {
1111 /* Microsoft OUI (00:50:F2) */
1113 if (calc_crc)
1114 crc = crc32_be(crc, pos - 2, elen + 2);
1116 if (pos[3] == 1) {
1117 /* OUI Type 1 - WPA IE */
1118 elems->wpa = pos;
1119 elems->wpa_len = elen;
1120 } else if (elen >= 5 && pos[3] == 2) {
1121 /* OUI Type 2 - WMM IE */
1122 if (pos[4] == 0) {
1123 elems->wmm_info = pos;
1124 elems->wmm_info_len = elen;
1125 } else if (pos[4] == 1) {
1126 elems->wmm_param = pos;
1127 elems->wmm_param_len = elen;
1131 break;
1132 case WLAN_EID_RSN:
1133 elems->rsn = pos;
1134 elems->rsn_len = elen;
1135 break;
1136 case WLAN_EID_ERP_INFO:
1137 elems->erp_info = pos;
1138 elems->erp_info_len = elen;
1139 break;
1140 case WLAN_EID_EXT_SUPP_RATES:
1141 elems->ext_supp_rates = pos;
1142 elems->ext_supp_rates_len = elen;
1143 break;
1144 case WLAN_EID_HT_CAPABILITY:
1145 if (elen >= sizeof(struct ieee80211_ht_cap))
1146 elems->ht_cap_elem = (void *)pos;
1147 break;
1148 case WLAN_EID_HT_INFORMATION:
1149 if (elen >= sizeof(struct ieee80211_ht_info))
1150 elems->ht_info_elem = (void *)pos;
1151 break;
1152 case WLAN_EID_MESH_ID:
1153 elems->mesh_id = pos;
1154 elems->mesh_id_len = elen;
1155 break;
1156 case WLAN_EID_MESH_CONFIG:
1157 if (elen >= sizeof(struct ieee80211_meshconf_ie))
1158 elems->mesh_config = (void *)pos;
1159 break;
1160 case WLAN_EID_PEER_MGMT:
1161 elems->peering = pos;
1162 elems->peering_len = elen;
1163 break;
1164 case WLAN_EID_PREQ:
1165 elems->preq = pos;
1166 elems->preq_len = elen;
1167 break;
1168 case WLAN_EID_PREP:
1169 elems->prep = pos;
1170 elems->prep_len = elen;
1171 break;
1172 case WLAN_EID_PERR:
1173 elems->perr = pos;
1174 elems->perr_len = elen;
1175 break;
1176 case WLAN_EID_RANN:
1177 if (elen >= sizeof(struct ieee80211_rann_ie))
1178 elems->rann = (void *)pos;
1179 break;
1180 case WLAN_EID_CHANNEL_SWITCH:
1181 elems->ch_switch_elem = pos;
1182 elems->ch_switch_elem_len = elen;
1183 break;
1184 case WLAN_EID_QUIET:
1185 if (!elems->quiet_elem) {
1186 elems->quiet_elem = pos;
1187 elems->quiet_elem_len = elen;
1189 elems->num_of_quiet_elem++;
1190 break;
1191 case WLAN_EID_COUNTRY:
1192 elems->country_elem = pos;
1193 elems->country_elem_len = elen;
1194 break;
1195 case WLAN_EID_PWR_CONSTRAINT:
1196 elems->pwr_constr_elem = pos;
1197 elems->pwr_constr_elem_len = elen;
1198 break;
1199 case WLAN_EID_TIMEOUT_INTERVAL:
1200 elems->timeout_int = pos;
1201 elems->timeout_int_len = elen;
1202 break;
1203 default:
1204 break;
1207 left -= elen;
1208 pos += elen;
1211 return crc;
1213 EXPORT_SYMBOL(ieee802_11_parse_elems_crc);