2 * Wireless utility functions
4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
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>
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];
21 for (i = 0; i < sband->n_bitrates; i++) {
22 if (!(basic_rates & BIT(i)))
24 if (sband->bitrates[i].bitrate > bitrate)
26 result = &sband->bitrates[i];
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;
41 return 5000 + chan * 5;
42 } else { /* IEEE80211_BAND_2GHZ */
46 return 2407 + chan * 5;
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 */
59 return (freq - 2407) / 5;
60 else if (freq >= 4910 && freq <= 4980)
61 return (freq - 4000) / 5;
63 return (freq - 5000) / 5;
65 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
67 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
70 enum ieee80211_band band;
71 struct ieee80211_supported_band *sband;
74 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
75 sband = wiphy->bands[band];
80 for (i = 0; i < sband->n_channels; i++) {
81 if (sband->channels[i].center_freq == freq)
82 return &sband->channels[i];
88 EXPORT_SYMBOL(__ieee80211_get_channel);
90 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
91 enum ieee80211_band band)
96 case IEEE80211_BAND_5GHZ:
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;
109 case IEEE80211_BAND_2GHZ:
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;
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;
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);
139 case IEEE80211_NUM_BANDS:
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 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
157 for (i = 0; i < wiphy->n_cipher_suites; i++)
158 if (cipher == wiphy->cipher_suites[i])
163 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
164 struct key_params *params, int key_idx,
165 bool pairwise, const u8 *mac_addr)
170 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
173 if (pairwise && !mac_addr)
177 * Disallow pairwise keys with non-zero index unless it's WEP
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)
183 if (pairwise && key_idx &&
184 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
185 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
186 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
189 switch (params->cipher) {
190 case WLAN_CIPHER_SUITE_WEP40:
191 if (params->key_len != WLAN_KEY_LEN_WEP40)
194 case WLAN_CIPHER_SUITE_TKIP:
195 if (params->key_len != WLAN_KEY_LEN_TKIP)
198 case WLAN_CIPHER_SUITE_CCMP:
199 if (params->key_len != WLAN_KEY_LEN_CCMP)
202 case WLAN_CIPHER_SUITE_WEP104:
203 if (params->key_len != WLAN_KEY_LEN_WEP104)
206 case WLAN_CIPHER_SUITE_AES_CMAC:
207 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
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,
222 switch (params->cipher) {
223 case WLAN_CIPHER_SUITE_WEP40:
224 case WLAN_CIPHER_SUITE_WEP104:
225 /* These ciphers do not use key sequence */
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)
236 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
242 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
243 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
244 const unsigned char rfc1042_header[] __aligned(2) =
245 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
246 EXPORT_SYMBOL(rfc1042_header);
248 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
249 const unsigned char bridge_tunnel_header[] __aligned(2) =
250 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
251 EXPORT_SYMBOL(bridge_tunnel_header);
253 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
255 unsigned int hdrlen = 24;
257 if (ieee80211_is_data(fc)) {
258 if (ieee80211_has_a4(fc))
260 if (ieee80211_is_data_qos(fc)) {
261 hdrlen += IEEE80211_QOS_CTL_LEN;
262 if (ieee80211_has_order(fc))
263 hdrlen += IEEE80211_HT_CTL_LEN;
268 if (ieee80211_is_ctl(fc)) {
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)
278 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
286 EXPORT_SYMBOL(ieee80211_hdrlen);
288 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
290 const struct ieee80211_hdr *hdr =
291 (const struct ieee80211_hdr *)skb->data;
294 if (unlikely(skb->len < 10))
296 hdrlen = ieee80211_hdrlen(hdr->frame_control);
297 if (unlikely(hdrlen > skb->len))
301 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
303 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
305 int ae = meshhdr->flags & MESH_FLAGS_AE;
310 case MESH_FLAGS_AE_A4:
312 case MESH_FLAGS_AE_A5_A6:
314 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
321 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
322 enum nl80211_iftype iftype)
324 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
325 u16 hdrlen, ethertype;
328 u8 src[ETH_ALEN] __aligned(2);
330 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
333 hdrlen = ieee80211_hdrlen(hdr->frame_control);
335 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
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
344 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
345 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
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 &&
351 iftype != NL80211_IFTYPE_AP_VLAN &&
352 iftype != NL80211_IFTYPE_P2P_GO))
355 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
356 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
357 iftype != NL80211_IFTYPE_MESH_POINT &&
358 iftype != NL80211_IFTYPE_AP_VLAN &&
359 iftype != NL80211_IFTYPE_STATION))
361 if (iftype == NL80211_IFTYPE_MESH_POINT) {
362 struct ieee80211s_hdr *meshdr =
363 (struct ieee80211s_hdr *) (skb->data + hdrlen);
364 /* make sure meshdr->flags is on the linear part */
365 if (!pskb_may_pull(skb, hdrlen + 1))
367 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
368 skb_copy_bits(skb, hdrlen +
369 offsetof(struct ieee80211s_hdr, eaddr1),
371 skb_copy_bits(skb, hdrlen +
372 offsetof(struct ieee80211s_hdr, eaddr2),
375 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
378 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
379 if ((iftype != NL80211_IFTYPE_STATION &&
380 iftype != NL80211_IFTYPE_P2P_CLIENT &&
381 iftype != NL80211_IFTYPE_MESH_POINT) ||
382 (is_multicast_ether_addr(dst) &&
383 !compare_ether_addr(src, addr)))
385 if (iftype == NL80211_IFTYPE_MESH_POINT) {
386 struct ieee80211s_hdr *meshdr =
387 (struct ieee80211s_hdr *) (skb->data + hdrlen);
388 /* make sure meshdr->flags is on the linear part */
389 if (!pskb_may_pull(skb, hdrlen + 1))
391 if (meshdr->flags & MESH_FLAGS_AE_A4)
392 skb_copy_bits(skb, hdrlen +
393 offsetof(struct ieee80211s_hdr, eaddr1),
395 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
399 if (iftype != NL80211_IFTYPE_ADHOC)
404 if (!pskb_may_pull(skb, hdrlen + 8))
407 payload = skb->data + hdrlen;
408 ethertype = (payload[6] << 8) | payload[7];
410 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
411 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
412 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
413 /* remove RFC1042 or Bridge-Tunnel encapsulation and
414 * replace EtherType */
415 skb_pull(skb, hdrlen + 6);
416 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
417 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
422 skb_pull(skb, hdrlen);
423 len = htons(skb->len);
424 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
425 memcpy(ehdr->h_dest, dst, ETH_ALEN);
426 memcpy(ehdr->h_source, src, ETH_ALEN);
431 EXPORT_SYMBOL(ieee80211_data_to_8023);
433 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
434 enum nl80211_iftype iftype, u8 *bssid, bool qos)
436 struct ieee80211_hdr hdr;
437 u16 hdrlen, ethertype;
439 const u8 *encaps_data;
440 int encaps_len, skip_header_bytes;
444 if (unlikely(skb->len < ETH_HLEN))
447 nh_pos = skb_network_header(skb) - skb->data;
448 h_pos = skb_transport_header(skb) - skb->data;
450 /* convert Ethernet header to proper 802.11 header (based on
452 ethertype = (skb->data[12] << 8) | skb->data[13];
453 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
456 case NL80211_IFTYPE_AP:
457 case NL80211_IFTYPE_AP_VLAN:
458 case NL80211_IFTYPE_P2P_GO:
459 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
461 memcpy(hdr.addr1, skb->data, ETH_ALEN);
462 memcpy(hdr.addr2, addr, ETH_ALEN);
463 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
466 case NL80211_IFTYPE_STATION:
467 case NL80211_IFTYPE_P2P_CLIENT:
468 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
470 memcpy(hdr.addr1, bssid, ETH_ALEN);
471 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
472 memcpy(hdr.addr3, skb->data, ETH_ALEN);
475 case NL80211_IFTYPE_ADHOC:
477 memcpy(hdr.addr1, skb->data, ETH_ALEN);
478 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
479 memcpy(hdr.addr3, bssid, ETH_ALEN);
487 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
491 hdr.frame_control = fc;
495 skip_header_bytes = ETH_HLEN;
496 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
497 encaps_data = bridge_tunnel_header;
498 encaps_len = sizeof(bridge_tunnel_header);
499 skip_header_bytes -= 2;
500 } else if (ethertype > 0x600) {
501 encaps_data = rfc1042_header;
502 encaps_len = sizeof(rfc1042_header);
503 skip_header_bytes -= 2;
509 skb_pull(skb, skip_header_bytes);
510 nh_pos -= skip_header_bytes;
511 h_pos -= skip_header_bytes;
513 head_need = hdrlen + encaps_len - skb_headroom(skb);
515 if (head_need > 0 || skb_cloned(skb)) {
516 head_need = max(head_need, 0);
520 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
523 skb->truesize += head_need;
527 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
528 nh_pos += encaps_len;
532 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
537 /* Update skb pointers to various headers since this modified frame
538 * is going to go through Linux networking code that may potentially
539 * need things like pointer to IP header. */
540 skb_set_mac_header(skb, 0);
541 skb_set_network_header(skb, nh_pos);
542 skb_set_transport_header(skb, h_pos);
546 EXPORT_SYMBOL(ieee80211_data_from_8023);
549 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
550 const u8 *addr, enum nl80211_iftype iftype,
551 const unsigned int extra_headroom,
552 bool has_80211_header)
554 struct sk_buff *frame = NULL;
557 const struct ethhdr *eth;
559 u8 dst[ETH_ALEN], src[ETH_ALEN];
561 if (has_80211_header) {
562 err = ieee80211_data_to_8023(skb, addr, iftype);
566 /* skip the wrapping header */
567 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
571 eth = (struct ethhdr *) skb->data;
574 while (skb != frame) {
576 __be16 len = eth->h_proto;
577 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
579 remaining = skb->len;
580 memcpy(dst, eth->h_dest, ETH_ALEN);
581 memcpy(src, eth->h_source, ETH_ALEN);
583 padding = (4 - subframe_len) & 0x3;
584 /* the last MSDU has no padding */
585 if (subframe_len > remaining)
588 skb_pull(skb, sizeof(struct ethhdr));
589 /* reuse skb for the last subframe */
590 if (remaining <= subframe_len + padding)
593 unsigned int hlen = ALIGN(extra_headroom, 4);
595 * Allocate and reserve two bytes more for payload
596 * alignment since sizeof(struct ethhdr) is 14.
598 frame = dev_alloc_skb(hlen + subframe_len + 2);
602 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
603 memcpy(skb_put(frame, ntohs(len)), skb->data,
606 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
609 dev_kfree_skb(frame);
614 skb_reset_network_header(frame);
615 frame->dev = skb->dev;
616 frame->priority = skb->priority;
618 payload = frame->data;
619 ethertype = (payload[6] << 8) | payload[7];
621 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
622 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
623 compare_ether_addr(payload,
624 bridge_tunnel_header) == 0)) {
625 /* remove RFC1042 or Bridge-Tunnel
626 * encapsulation and replace EtherType */
628 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
629 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
631 memcpy(skb_push(frame, sizeof(__be16)), &len,
633 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
634 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
636 __skb_queue_tail(list, frame);
642 __skb_queue_purge(list);
646 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
648 /* Given a data frame determine the 802.1p/1d tag to use. */
649 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
653 /* skb->priority values from 256->263 are magic values to
654 * directly indicate a specific 802.1d priority. This is used
655 * to allow 802.1d priority to be passed directly in from VLAN
658 if (skb->priority >= 256 && skb->priority <= 263)
659 return skb->priority - 256;
661 switch (skb->protocol) {
662 case htons(ETH_P_IP):
663 dscp = ip_hdr(skb)->tos & 0xfc;
671 EXPORT_SYMBOL(cfg80211_classify8021d);
673 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
677 pos = bss->information_elements;
680 end = pos + bss->len_information_elements;
682 while (pos + 1 < end) {
683 if (pos + 2 + pos[1] > end)
692 EXPORT_SYMBOL(ieee80211_bss_get_ie);
694 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
696 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
697 struct net_device *dev = wdev->netdev;
700 if (!wdev->connect_keys)
703 for (i = 0; i < 6; i++) {
704 if (!wdev->connect_keys->params[i].cipher)
706 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
707 &wdev->connect_keys->params[i])) {
708 netdev_err(dev, "failed to set key %d\n", i);
711 if (wdev->connect_keys->def == i)
712 if (rdev->ops->set_default_key(wdev->wiphy, dev,
714 netdev_err(dev, "failed to set defkey %d\n", i);
717 if (wdev->connect_keys->defmgmt == i)
718 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
719 netdev_err(dev, "failed to set mgtdef %d\n", i);
722 kfree(wdev->connect_keys);
723 wdev->connect_keys = NULL;
726 static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
728 struct cfg80211_event *ev;
730 const u8 *bssid = NULL;
732 spin_lock_irqsave(&wdev->event_lock, flags);
733 while (!list_empty(&wdev->event_list)) {
734 ev = list_first_entry(&wdev->event_list,
735 struct cfg80211_event, list);
737 spin_unlock_irqrestore(&wdev->event_lock, flags);
741 case EVENT_CONNECT_RESULT:
742 if (!is_zero_ether_addr(ev->cr.bssid))
743 bssid = ev->cr.bssid;
744 __cfg80211_connect_result(
746 ev->cr.req_ie, ev->cr.req_ie_len,
747 ev->cr.resp_ie, ev->cr.resp_ie_len,
749 ev->cr.status == WLAN_STATUS_SUCCESS,
753 __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid,
754 ev->rm.req_ie, ev->rm.req_ie_len,
755 ev->rm.resp_ie, ev->rm.resp_ie_len);
757 case EVENT_DISCONNECTED:
758 __cfg80211_disconnected(wdev->netdev,
759 ev->dc.ie, ev->dc.ie_len,
760 ev->dc.reason, true);
762 case EVENT_IBSS_JOINED:
763 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
770 spin_lock_irqsave(&wdev->event_lock, flags);
772 spin_unlock_irqrestore(&wdev->event_lock, flags);
775 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
777 struct wireless_dev *wdev;
780 ASSERT_RDEV_LOCK(rdev);
782 mutex_lock(&rdev->devlist_mtx);
784 list_for_each_entry(wdev, &rdev->netdev_list, list)
785 cfg80211_process_wdev_events(wdev);
787 mutex_unlock(&rdev->devlist_mtx);
790 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
791 struct net_device *dev, enum nl80211_iftype ntype,
792 u32 *flags, struct vif_params *params)
795 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
797 ASSERT_RDEV_LOCK(rdev);
799 /* don't support changing VLANs, you just re-create them */
800 if (otype == NL80211_IFTYPE_AP_VLAN)
803 if (!rdev->ops->change_virtual_intf ||
804 !(rdev->wiphy.interface_modes & (1 << ntype)))
807 /* if it's part of a bridge, reject changing type to station/ibss */
808 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
809 (ntype == NL80211_IFTYPE_ADHOC ||
810 ntype == NL80211_IFTYPE_STATION ||
811 ntype == NL80211_IFTYPE_P2P_CLIENT))
814 if (ntype != otype) {
815 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
820 dev->ieee80211_ptr->use_4addr = false;
821 dev->ieee80211_ptr->mesh_id_up_len = 0;
824 case NL80211_IFTYPE_ADHOC:
825 cfg80211_leave_ibss(rdev, dev, false);
827 case NL80211_IFTYPE_STATION:
828 case NL80211_IFTYPE_P2P_CLIENT:
829 cfg80211_disconnect(rdev, dev,
830 WLAN_REASON_DEAUTH_LEAVING, true);
832 case NL80211_IFTYPE_MESH_POINT:
833 /* mesh should be handled? */
839 cfg80211_process_rdev_events(rdev);
842 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
843 ntype, flags, params);
845 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
847 if (!err && params && params->use_4addr != -1)
848 dev->ieee80211_ptr->use_4addr = params->use_4addr;
851 dev->priv_flags &= ~IFF_DONT_BRIDGE;
853 case NL80211_IFTYPE_STATION:
854 if (dev->ieee80211_ptr->use_4addr)
857 case NL80211_IFTYPE_P2P_CLIENT:
858 case NL80211_IFTYPE_ADHOC:
859 dev->priv_flags |= IFF_DONT_BRIDGE;
861 case NL80211_IFTYPE_P2P_GO:
862 case NL80211_IFTYPE_AP:
863 case NL80211_IFTYPE_AP_VLAN:
864 case NL80211_IFTYPE_WDS:
865 case NL80211_IFTYPE_MESH_POINT:
868 case NL80211_IFTYPE_MONITOR:
869 /* monitor can't bridge anyway */
871 case NL80211_IFTYPE_UNSPECIFIED:
872 case NUM_NL80211_IFTYPES:
881 u16 cfg80211_calculate_bitrate(struct rate_info *rate)
883 int modulation, streams, bitrate;
885 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
888 /* the formula below does only work for MCS values smaller than 32 */
892 modulation = rate->mcs & 7;
893 streams = (rate->mcs >> 3) + 1;
895 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
899 bitrate *= (modulation + 1);
900 else if (modulation == 4)
901 bitrate *= (modulation + 2);
903 bitrate *= (modulation + 3);
907 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
908 bitrate = (bitrate / 9) * 10;
910 /* do NOT round down here */
911 return (bitrate + 50000) / 100000;
914 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
917 struct wireless_dev *wdev;
923 mutex_lock(&rdev->devlist_mtx);
925 list_for_each_entry(wdev, &rdev->netdev_list, list) {
926 if (!wdev->beacon_interval)
928 if (wdev->beacon_interval != beacon_int) {
934 mutex_unlock(&rdev->devlist_mtx);
939 int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
940 struct wireless_dev *wdev,
941 enum nl80211_iftype iftype)
943 struct wireless_dev *wdev_iter;
944 int num[NUM_NL80211_IFTYPES];
950 /* Always allow software iftypes */
951 if (rdev->wiphy.software_iftypes & BIT(iftype))
955 * Drivers will gradually all set this flag, until all
956 * have it we only enforce for those that set it.
958 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
961 memset(num, 0, sizeof(num));
965 mutex_lock(&rdev->devlist_mtx);
966 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
967 if (wdev_iter == wdev)
969 if (!netif_running(wdev_iter->netdev))
972 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
975 num[wdev_iter->iftype]++;
978 mutex_unlock(&rdev->devlist_mtx);
980 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
981 const struct ieee80211_iface_combination *c;
982 struct ieee80211_iface_limit *limits;
984 c = &rdev->wiphy.iface_combinations[i];
986 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
990 if (total > c->max_interfaces)
993 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
994 if (rdev->wiphy.software_iftypes & BIT(iftype))
996 for (j = 0; j < c->n_limits; j++) {
997 if (!(limits[j].types & iftype))
999 if (limits[j].max < num[iftype])
1001 limits[j].max -= num[iftype];
1014 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1015 const u8 *rates, unsigned int n_rates,
1023 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1028 for (i = 0; i < n_rates; i++) {
1029 int rate = (rates[i] & 0x7f) * 5;
1032 for (j = 0; j < sband->n_bitrates; j++) {
1033 if (sband->bitrates[j].bitrate == rate) {
1044 * mask must have at least one bit set here since we
1045 * didn't accept a 0-length rates array nor allowed
1046 * entries in the array that didn't exist
1052 u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
1053 struct ieee802_11_elems *elems,
1054 u64 filter, u32 crc)
1058 bool calc_crc = filter != 0;
1060 memset(elems, 0, sizeof(*elems));
1061 elems->ie_start = start;
1062 elems->total_len = len;
1074 if (calc_crc && id < 64 && (filter & (1ULL << id)))
1075 crc = crc32_be(crc, pos - 2, elen + 2);
1080 elems->ssid_len = elen;
1082 case WLAN_EID_SUPP_RATES:
1083 elems->supp_rates = pos;
1084 elems->supp_rates_len = elen;
1086 case WLAN_EID_FH_PARAMS:
1087 elems->fh_params = pos;
1088 elems->fh_params_len = elen;
1090 case WLAN_EID_DS_PARAMS:
1091 elems->ds_params = pos;
1092 elems->ds_params_len = elen;
1094 case WLAN_EID_CF_PARAMS:
1095 elems->cf_params = pos;
1096 elems->cf_params_len = elen;
1099 if (elen >= sizeof(struct ieee80211_tim_ie)) {
1100 elems->tim = (void *)pos;
1101 elems->tim_len = elen;
1104 case WLAN_EID_IBSS_PARAMS:
1105 elems->ibss_params = pos;
1106 elems->ibss_params_len = elen;
1108 case WLAN_EID_CHALLENGE:
1109 elems->challenge = pos;
1110 elems->challenge_len = elen;
1112 case WLAN_EID_VENDOR_SPECIFIC:
1113 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1115 /* Microsoft OUI (00:50:F2) */
1118 crc = crc32_be(crc, pos - 2, elen + 2);
1121 /* OUI Type 1 - WPA IE */
1123 elems->wpa_len = elen;
1124 } else if (elen >= 5 && pos[3] == 2) {
1125 /* OUI Type 2 - WMM IE */
1127 elems->wmm_info = pos;
1128 elems->wmm_info_len = elen;
1129 } else if (pos[4] == 1) {
1130 elems->wmm_param = pos;
1131 elems->wmm_param_len = elen;
1138 elems->rsn_len = elen;
1140 case WLAN_EID_ERP_INFO:
1141 elems->erp_info = pos;
1142 elems->erp_info_len = elen;
1144 case WLAN_EID_EXT_SUPP_RATES:
1145 elems->ext_supp_rates = pos;
1146 elems->ext_supp_rates_len = elen;
1148 case WLAN_EID_HT_CAPABILITY:
1149 if (elen >= sizeof(struct ieee80211_ht_cap))
1150 elems->ht_cap_elem = (void *)pos;
1152 case WLAN_EID_HT_INFORMATION:
1153 if (elen >= sizeof(struct ieee80211_ht_info))
1154 elems->ht_info_elem = (void *)pos;
1156 case WLAN_EID_MESH_ID:
1157 elems->mesh_id = pos;
1158 elems->mesh_id_len = elen;
1160 case WLAN_EID_MESH_CONFIG:
1161 if (elen >= sizeof(struct ieee80211_meshconf_ie))
1162 elems->mesh_config = (void *)pos;
1164 case WLAN_EID_PEER_MGMT:
1165 elems->peering = pos;
1166 elems->peering_len = elen;
1170 elems->preq_len = elen;
1174 elems->prep_len = elen;
1178 elems->perr_len = elen;
1181 if (elen >= sizeof(struct ieee80211_rann_ie))
1182 elems->rann = (void *)pos;
1184 case WLAN_EID_CHANNEL_SWITCH:
1185 elems->ch_switch_elem = pos;
1186 elems->ch_switch_elem_len = elen;
1188 case WLAN_EID_QUIET:
1189 if (!elems->quiet_elem) {
1190 elems->quiet_elem = pos;
1191 elems->quiet_elem_len = elen;
1193 elems->num_of_quiet_elem++;
1195 case WLAN_EID_COUNTRY:
1196 elems->country_elem = pos;
1197 elems->country_elem_len = elen;
1199 case WLAN_EID_PWR_CONSTRAINT:
1200 elems->pwr_constr_elem = pos;
1201 elems->pwr_constr_elem_len = elen;
1203 case WLAN_EID_TIMEOUT_INTERVAL:
1204 elems->timeout_int = pos;
1205 elems->timeout_int_len = elen;
1217 EXPORT_SYMBOL(ieee802_11_parse_elems_crc);