2 * Wireless utility functions
4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
6 #include <linux/export.h>
7 #include <linux/bitops.h>
8 #include <linux/etherdevice.h>
9 #include <linux/slab.h>
10 #include <linux/crc32.h>
11 #include <net/cfg80211.h>
15 struct ieee80211_rate *
16 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
17 u32 basic_rates, int bitrate)
19 struct ieee80211_rate *result = &sband->bitrates[0];
22 for (i = 0; i < sband->n_bitrates; i++) {
23 if (!(basic_rates & BIT(i)))
25 if (sband->bitrates[i].bitrate > bitrate)
27 result = &sband->bitrates[i];
32 EXPORT_SYMBOL(ieee80211_get_response_rate);
34 int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
36 /* 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;
42 return 5000 + chan * 5;
43 } else { /* IEEE80211_BAND_2GHZ */
47 return 2407 + chan * 5;
49 return 0; /* not supported */
52 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
54 int ieee80211_frequency_to_channel(int freq)
56 /* see 802.11 17.3.8.3.2 and Annex J */
60 return (freq - 2407) / 5;
61 else if (freq >= 4910 && freq <= 4980)
62 return (freq - 4000) / 5;
64 return (freq - 5000) / 5;
66 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
68 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
71 enum ieee80211_band band;
72 struct ieee80211_supported_band *sband;
75 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
76 sband = wiphy->bands[band];
81 for (i = 0; i < sband->n_channels; i++) {
82 if (sband->channels[i].center_freq == freq)
83 return &sband->channels[i];
89 EXPORT_SYMBOL(__ieee80211_get_channel);
91 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
92 enum ieee80211_band band)
97 case IEEE80211_BAND_5GHZ:
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;
110 case IEEE80211_BAND_2GHZ:
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;
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;
131 if (sband->bitrates[i].bitrate != 10 &&
132 sband->bitrates[i].bitrate != 20 &&
133 sband->bitrates[i].bitrate != 55 &&
134 sband->bitrates[i].bitrate != 110)
135 sband->bitrates[i].flags |=
136 IEEE80211_RATE_ERP_G;
138 WARN_ON(want != 0 && want != 3 && want != 6);
140 case IEEE80211_NUM_BANDS:
146 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
148 enum ieee80211_band band;
150 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
151 if (wiphy->bands[band])
152 set_mandatory_flags_band(wiphy->bands[band], band);
155 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
158 for (i = 0; i < wiphy->n_cipher_suites; i++)
159 if (cipher == wiphy->cipher_suites[i])
164 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
165 struct key_params *params, int key_idx,
166 bool pairwise, const u8 *mac_addr)
171 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
174 if (pairwise && !mac_addr)
178 * Disallow pairwise keys with non-zero index unless it's WEP
179 * 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)
184 if (pairwise && key_idx &&
185 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
186 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
187 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
190 switch (params->cipher) {
191 case WLAN_CIPHER_SUITE_WEP40:
192 if (params->key_len != WLAN_KEY_LEN_WEP40)
195 case WLAN_CIPHER_SUITE_TKIP:
196 if (params->key_len != WLAN_KEY_LEN_TKIP)
199 case WLAN_CIPHER_SUITE_CCMP:
200 if (params->key_len != WLAN_KEY_LEN_CCMP)
203 case WLAN_CIPHER_SUITE_WEP104:
204 if (params->key_len != WLAN_KEY_LEN_WEP104)
207 case WLAN_CIPHER_SUITE_AES_CMAC:
208 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
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,
223 switch (params->cipher) {
224 case WLAN_CIPHER_SUITE_WEP40:
225 case WLAN_CIPHER_SUITE_WEP104:
226 /* These ciphers do not use key sequence */
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)
237 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
243 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
244 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
245 const unsigned char rfc1042_header[] __aligned(2) =
246 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
247 EXPORT_SYMBOL(rfc1042_header);
249 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
250 const unsigned char bridge_tunnel_header[] __aligned(2) =
251 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
252 EXPORT_SYMBOL(bridge_tunnel_header);
254 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
256 unsigned int hdrlen = 24;
258 if (ieee80211_is_data(fc)) {
259 if (ieee80211_has_a4(fc))
261 if (ieee80211_is_data_qos(fc)) {
262 hdrlen += IEEE80211_QOS_CTL_LEN;
263 if (ieee80211_has_order(fc))
264 hdrlen += IEEE80211_HT_CTL_LEN;
269 if (ieee80211_is_ctl(fc)) {
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)
279 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
287 EXPORT_SYMBOL(ieee80211_hdrlen);
289 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
291 const struct ieee80211_hdr *hdr =
292 (const struct ieee80211_hdr *)skb->data;
295 if (unlikely(skb->len < 10))
297 hdrlen = ieee80211_hdrlen(hdr->frame_control);
298 if (unlikely(hdrlen > skb->len))
302 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
304 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
306 int ae = meshhdr->flags & MESH_FLAGS_AE;
307 /* 802.11-2012, 8.2.4.7.3 */
312 case MESH_FLAGS_AE_A4:
314 case MESH_FLAGS_AE_A5_A6:
318 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
320 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
321 enum nl80211_iftype iftype)
323 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
324 u16 hdrlen, ethertype;
327 u8 src[ETH_ALEN] __aligned(2);
329 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
332 hdrlen = ieee80211_hdrlen(hdr->frame_control);
334 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
336 * IEEE 802.11 address fields:
337 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
338 * 0 0 DA SA BSSID n/a
339 * 0 1 DA BSSID SA n/a
340 * 1 0 BSSID SA DA n/a
343 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
344 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
346 switch (hdr->frame_control &
347 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
348 case cpu_to_le16(IEEE80211_FCTL_TODS):
349 if (unlikely(iftype != NL80211_IFTYPE_AP &&
350 iftype != NL80211_IFTYPE_AP_VLAN &&
351 iftype != NL80211_IFTYPE_P2P_GO))
354 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
355 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
356 iftype != NL80211_IFTYPE_MESH_POINT &&
357 iftype != NL80211_IFTYPE_AP_VLAN &&
358 iftype != NL80211_IFTYPE_STATION))
360 if (iftype == NL80211_IFTYPE_MESH_POINT) {
361 struct ieee80211s_hdr *meshdr =
362 (struct ieee80211s_hdr *) (skb->data + hdrlen);
363 /* make sure meshdr->flags is on the linear part */
364 if (!pskb_may_pull(skb, hdrlen + 1))
366 if (meshdr->flags & MESH_FLAGS_AE_A4)
368 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
369 skb_copy_bits(skb, hdrlen +
370 offsetof(struct ieee80211s_hdr, eaddr1),
372 skb_copy_bits(skb, hdrlen +
373 offsetof(struct ieee80211s_hdr, eaddr2),
376 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
379 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
380 if ((iftype != NL80211_IFTYPE_STATION &&
381 iftype != NL80211_IFTYPE_P2P_CLIENT &&
382 iftype != NL80211_IFTYPE_MESH_POINT) ||
383 (is_multicast_ether_addr(dst) &&
384 !compare_ether_addr(src, addr)))
386 if (iftype == NL80211_IFTYPE_MESH_POINT) {
387 struct ieee80211s_hdr *meshdr =
388 (struct ieee80211s_hdr *) (skb->data + hdrlen);
389 /* make sure meshdr->flags is on the linear part */
390 if (!pskb_may_pull(skb, hdrlen + 1))
392 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
394 if (meshdr->flags & MESH_FLAGS_AE_A4)
395 skb_copy_bits(skb, hdrlen +
396 offsetof(struct ieee80211s_hdr, eaddr1),
398 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
402 if (iftype != NL80211_IFTYPE_ADHOC &&
403 iftype != NL80211_IFTYPE_STATION)
408 if (!pskb_may_pull(skb, hdrlen + 8))
411 payload = skb->data + hdrlen;
412 ethertype = (payload[6] << 8) | payload[7];
414 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
415 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
416 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
417 /* remove RFC1042 or Bridge-Tunnel encapsulation and
418 * replace EtherType */
419 skb_pull(skb, hdrlen + 6);
420 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
421 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
426 skb_pull(skb, hdrlen);
427 len = htons(skb->len);
428 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
429 memcpy(ehdr->h_dest, dst, ETH_ALEN);
430 memcpy(ehdr->h_source, src, ETH_ALEN);
435 EXPORT_SYMBOL(ieee80211_data_to_8023);
437 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
438 enum nl80211_iftype iftype, u8 *bssid, bool qos)
440 struct ieee80211_hdr hdr;
441 u16 hdrlen, ethertype;
443 const u8 *encaps_data;
444 int encaps_len, skip_header_bytes;
448 if (unlikely(skb->len < ETH_HLEN))
451 nh_pos = skb_network_header(skb) - skb->data;
452 h_pos = skb_transport_header(skb) - skb->data;
454 /* convert Ethernet header to proper 802.11 header (based on
456 ethertype = (skb->data[12] << 8) | skb->data[13];
457 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
460 case NL80211_IFTYPE_AP:
461 case NL80211_IFTYPE_AP_VLAN:
462 case NL80211_IFTYPE_P2P_GO:
463 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
465 memcpy(hdr.addr1, skb->data, ETH_ALEN);
466 memcpy(hdr.addr2, addr, ETH_ALEN);
467 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
470 case NL80211_IFTYPE_STATION:
471 case NL80211_IFTYPE_P2P_CLIENT:
472 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
474 memcpy(hdr.addr1, bssid, ETH_ALEN);
475 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
476 memcpy(hdr.addr3, skb->data, ETH_ALEN);
479 case NL80211_IFTYPE_ADHOC:
481 memcpy(hdr.addr1, skb->data, ETH_ALEN);
482 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
483 memcpy(hdr.addr3, bssid, ETH_ALEN);
491 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
495 hdr.frame_control = fc;
499 skip_header_bytes = ETH_HLEN;
500 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
501 encaps_data = bridge_tunnel_header;
502 encaps_len = sizeof(bridge_tunnel_header);
503 skip_header_bytes -= 2;
504 } else if (ethertype > 0x600) {
505 encaps_data = rfc1042_header;
506 encaps_len = sizeof(rfc1042_header);
507 skip_header_bytes -= 2;
513 skb_pull(skb, skip_header_bytes);
514 nh_pos -= skip_header_bytes;
515 h_pos -= skip_header_bytes;
517 head_need = hdrlen + encaps_len - skb_headroom(skb);
519 if (head_need > 0 || skb_cloned(skb)) {
520 head_need = max(head_need, 0);
524 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
527 skb->truesize += head_need;
531 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
532 nh_pos += encaps_len;
536 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
541 /* Update skb pointers to various headers since this modified frame
542 * is going to go through Linux networking code that may potentially
543 * need things like pointer to IP header. */
544 skb_set_mac_header(skb, 0);
545 skb_set_network_header(skb, nh_pos);
546 skb_set_transport_header(skb, h_pos);
550 EXPORT_SYMBOL(ieee80211_data_from_8023);
553 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
554 const u8 *addr, enum nl80211_iftype iftype,
555 const unsigned int extra_headroom,
556 bool has_80211_header)
558 struct sk_buff *frame = NULL;
561 const struct ethhdr *eth;
563 u8 dst[ETH_ALEN], src[ETH_ALEN];
565 if (has_80211_header) {
566 err = ieee80211_data_to_8023(skb, addr, iftype);
570 /* skip the wrapping header */
571 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
575 eth = (struct ethhdr *) skb->data;
578 while (skb != frame) {
580 __be16 len = eth->h_proto;
581 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
583 remaining = skb->len;
584 memcpy(dst, eth->h_dest, ETH_ALEN);
585 memcpy(src, eth->h_source, ETH_ALEN);
587 padding = (4 - subframe_len) & 0x3;
588 /* the last MSDU has no padding */
589 if (subframe_len > remaining)
592 skb_pull(skb, sizeof(struct ethhdr));
593 /* reuse skb for the last subframe */
594 if (remaining <= subframe_len + padding)
597 unsigned int hlen = ALIGN(extra_headroom, 4);
599 * Allocate and reserve two bytes more for payload
600 * alignment since sizeof(struct ethhdr) is 14.
602 frame = dev_alloc_skb(hlen + subframe_len + 2);
606 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
607 memcpy(skb_put(frame, ntohs(len)), skb->data,
610 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
613 dev_kfree_skb(frame);
618 skb_reset_network_header(frame);
619 frame->dev = skb->dev;
620 frame->priority = skb->priority;
622 payload = frame->data;
623 ethertype = (payload[6] << 8) | payload[7];
625 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
626 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
627 compare_ether_addr(payload,
628 bridge_tunnel_header) == 0)) {
629 /* remove RFC1042 or Bridge-Tunnel
630 * encapsulation and replace EtherType */
632 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
633 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
635 memcpy(skb_push(frame, sizeof(__be16)), &len,
637 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
638 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
640 __skb_queue_tail(list, frame);
646 __skb_queue_purge(list);
650 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
652 /* Given a data frame determine the 802.1p/1d tag to use. */
653 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
657 /* skb->priority values from 256->263 are magic values to
658 * directly indicate a specific 802.1d priority. This is used
659 * to allow 802.1d priority to be passed directly in from VLAN
662 if (skb->priority >= 256 && skb->priority <= 263)
663 return skb->priority - 256;
665 switch (skb->protocol) {
666 case htons(ETH_P_IP):
667 dscp = ip_hdr(skb)->tos & 0xfc;
675 EXPORT_SYMBOL(cfg80211_classify8021d);
677 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
681 pos = bss->information_elements;
684 end = pos + bss->len_information_elements;
686 while (pos + 1 < end) {
687 if (pos + 2 + pos[1] > end)
696 EXPORT_SYMBOL(ieee80211_bss_get_ie);
698 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
700 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
701 struct net_device *dev = wdev->netdev;
704 if (!wdev->connect_keys)
707 for (i = 0; i < 6; i++) {
708 if (!wdev->connect_keys->params[i].cipher)
710 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
711 &wdev->connect_keys->params[i])) {
712 netdev_err(dev, "failed to set key %d\n", i);
715 if (wdev->connect_keys->def == i)
716 if (rdev->ops->set_default_key(wdev->wiphy, dev,
718 netdev_err(dev, "failed to set defkey %d\n", i);
721 if (wdev->connect_keys->defmgmt == i)
722 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
723 netdev_err(dev, "failed to set mgtdef %d\n", i);
726 kfree(wdev->connect_keys);
727 wdev->connect_keys = NULL;
730 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
732 struct cfg80211_event *ev;
734 const u8 *bssid = NULL;
736 spin_lock_irqsave(&wdev->event_lock, flags);
737 while (!list_empty(&wdev->event_list)) {
738 ev = list_first_entry(&wdev->event_list,
739 struct cfg80211_event, list);
741 spin_unlock_irqrestore(&wdev->event_lock, flags);
745 case EVENT_CONNECT_RESULT:
746 if (!is_zero_ether_addr(ev->cr.bssid))
747 bssid = ev->cr.bssid;
748 __cfg80211_connect_result(
750 ev->cr.req_ie, ev->cr.req_ie_len,
751 ev->cr.resp_ie, ev->cr.resp_ie_len,
753 ev->cr.status == WLAN_STATUS_SUCCESS,
757 __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid,
758 ev->rm.req_ie, ev->rm.req_ie_len,
759 ev->rm.resp_ie, ev->rm.resp_ie_len);
761 case EVENT_DISCONNECTED:
762 __cfg80211_disconnected(wdev->netdev,
763 ev->dc.ie, ev->dc.ie_len,
764 ev->dc.reason, true);
766 case EVENT_IBSS_JOINED:
767 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
774 spin_lock_irqsave(&wdev->event_lock, flags);
776 spin_unlock_irqrestore(&wdev->event_lock, flags);
779 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
781 struct wireless_dev *wdev;
784 ASSERT_RDEV_LOCK(rdev);
786 mutex_lock(&rdev->devlist_mtx);
788 list_for_each_entry(wdev, &rdev->netdev_list, list)
789 cfg80211_process_wdev_events(wdev);
791 mutex_unlock(&rdev->devlist_mtx);
794 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
795 struct net_device *dev, enum nl80211_iftype ntype,
796 u32 *flags, struct vif_params *params)
799 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
801 ASSERT_RDEV_LOCK(rdev);
803 /* don't support changing VLANs, you just re-create them */
804 if (otype == NL80211_IFTYPE_AP_VLAN)
807 if (!rdev->ops->change_virtual_intf ||
808 !(rdev->wiphy.interface_modes & (1 << ntype)))
811 /* if it's part of a bridge, reject changing type to station/ibss */
812 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
813 (ntype == NL80211_IFTYPE_ADHOC ||
814 ntype == NL80211_IFTYPE_STATION ||
815 ntype == NL80211_IFTYPE_P2P_CLIENT))
818 if (ntype != otype && netif_running(dev)) {
819 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
824 dev->ieee80211_ptr->use_4addr = false;
825 dev->ieee80211_ptr->mesh_id_up_len = 0;
828 case NL80211_IFTYPE_ADHOC:
829 cfg80211_leave_ibss(rdev, dev, false);
831 case NL80211_IFTYPE_STATION:
832 case NL80211_IFTYPE_P2P_CLIENT:
833 cfg80211_disconnect(rdev, dev,
834 WLAN_REASON_DEAUTH_LEAVING, true);
836 case NL80211_IFTYPE_MESH_POINT:
837 /* mesh should be handled? */
843 cfg80211_process_rdev_events(rdev);
846 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
847 ntype, flags, params);
849 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
851 if (!err && params && params->use_4addr != -1)
852 dev->ieee80211_ptr->use_4addr = params->use_4addr;
855 dev->priv_flags &= ~IFF_DONT_BRIDGE;
857 case NL80211_IFTYPE_STATION:
858 if (dev->ieee80211_ptr->use_4addr)
861 case NL80211_IFTYPE_P2P_CLIENT:
862 case NL80211_IFTYPE_ADHOC:
863 dev->priv_flags |= IFF_DONT_BRIDGE;
865 case NL80211_IFTYPE_P2P_GO:
866 case NL80211_IFTYPE_AP:
867 case NL80211_IFTYPE_AP_VLAN:
868 case NL80211_IFTYPE_WDS:
869 case NL80211_IFTYPE_MESH_POINT:
872 case NL80211_IFTYPE_MONITOR:
873 /* monitor can't bridge anyway */
875 case NL80211_IFTYPE_UNSPECIFIED:
876 case NUM_NL80211_IFTYPES:
885 u16 cfg80211_calculate_bitrate(struct rate_info *rate)
887 int modulation, streams, bitrate;
889 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
892 /* the formula below does only work for MCS values smaller than 32 */
896 modulation = rate->mcs & 7;
897 streams = (rate->mcs >> 3) + 1;
899 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
903 bitrate *= (modulation + 1);
904 else if (modulation == 4)
905 bitrate *= (modulation + 2);
907 bitrate *= (modulation + 3);
911 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
912 bitrate = (bitrate / 9) * 10;
914 /* do NOT round down here */
915 return (bitrate + 50000) / 100000;
918 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
921 struct wireless_dev *wdev;
927 mutex_lock(&rdev->devlist_mtx);
929 list_for_each_entry(wdev, &rdev->netdev_list, list) {
930 if (!wdev->beacon_interval)
932 if (wdev->beacon_interval != beacon_int) {
938 mutex_unlock(&rdev->devlist_mtx);
943 int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
944 struct wireless_dev *wdev,
945 enum nl80211_iftype iftype)
947 struct wireless_dev *wdev_iter;
948 u32 used_iftypes = BIT(iftype);
949 int num[NUM_NL80211_IFTYPES];
955 /* Always allow software iftypes */
956 if (rdev->wiphy.software_iftypes & BIT(iftype))
960 * Drivers will gradually all set this flag, until all
961 * have it we only enforce for those that set it.
963 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
966 memset(num, 0, sizeof(num));
970 mutex_lock(&rdev->devlist_mtx);
971 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
972 if (wdev_iter == wdev)
974 if (!netif_running(wdev_iter->netdev))
977 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
980 num[wdev_iter->iftype]++;
982 used_iftypes |= BIT(wdev_iter->iftype);
984 mutex_unlock(&rdev->devlist_mtx);
989 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
990 const struct ieee80211_iface_combination *c;
991 struct ieee80211_iface_limit *limits;
994 c = &rdev->wiphy.iface_combinations[i];
996 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1000 if (total > c->max_interfaces)
1003 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1004 if (rdev->wiphy.software_iftypes & BIT(iftype))
1006 for (j = 0; j < c->n_limits; j++) {
1007 all_iftypes |= limits[j].types;
1008 if (!(limits[j].types & BIT(iftype)))
1010 if (limits[j].max < num[iftype])
1012 limits[j].max -= num[iftype];
1017 * Finally check that all iftypes that we're currently
1018 * using are actually part of this combination. If they
1019 * aren't then we can't use this combination and have
1020 * to continue to the next.
1022 if ((all_iftypes & used_iftypes) != used_iftypes)
1026 * This combination covered all interface types and
1027 * supported the requested numbers, so we're good.
1038 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1039 const u8 *rates, unsigned int n_rates,
1047 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1052 for (i = 0; i < n_rates; i++) {
1053 int rate = (rates[i] & 0x7f) * 5;
1056 for (j = 0; j < sband->n_bitrates; j++) {
1057 if (sband->bitrates[j].bitrate == rate) {
1068 * mask must have at least one bit set here since we
1069 * didn't accept a 0-length rates array nor allowed
1070 * entries in the array that didn't exist
1076 u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
1077 struct ieee802_11_elems *elems,
1078 u64 filter, u32 crc)
1082 bool calc_crc = filter != 0;
1084 memset(elems, 0, sizeof(*elems));
1085 elems->ie_start = start;
1086 elems->total_len = len;
1098 if (calc_crc && id < 64 && (filter & (1ULL << id)))
1099 crc = crc32_be(crc, pos - 2, elen + 2);
1104 elems->ssid_len = elen;
1106 case WLAN_EID_SUPP_RATES:
1107 elems->supp_rates = pos;
1108 elems->supp_rates_len = elen;
1110 case WLAN_EID_FH_PARAMS:
1111 elems->fh_params = pos;
1112 elems->fh_params_len = elen;
1114 case WLAN_EID_DS_PARAMS:
1115 elems->ds_params = pos;
1116 elems->ds_params_len = elen;
1118 case WLAN_EID_CF_PARAMS:
1119 elems->cf_params = pos;
1120 elems->cf_params_len = elen;
1123 if (elen >= sizeof(struct ieee80211_tim_ie)) {
1124 elems->tim = (void *)pos;
1125 elems->tim_len = elen;
1128 case WLAN_EID_IBSS_PARAMS:
1129 elems->ibss_params = pos;
1130 elems->ibss_params_len = elen;
1132 case WLAN_EID_CHALLENGE:
1133 elems->challenge = pos;
1134 elems->challenge_len = elen;
1136 case WLAN_EID_VENDOR_SPECIFIC:
1137 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
1139 /* Microsoft OUI (00:50:F2) */
1142 crc = crc32_be(crc, pos - 2, elen + 2);
1145 /* OUI Type 1 - WPA IE */
1147 elems->wpa_len = elen;
1148 } else if (elen >= 5 && pos[3] == 2) {
1149 /* OUI Type 2 - WMM IE */
1151 elems->wmm_info = pos;
1152 elems->wmm_info_len = elen;
1153 } else if (pos[4] == 1) {
1154 elems->wmm_param = pos;
1155 elems->wmm_param_len = elen;
1162 elems->rsn_len = elen;
1164 case WLAN_EID_ERP_INFO:
1165 elems->erp_info = pos;
1166 elems->erp_info_len = elen;
1168 case WLAN_EID_EXT_SUPP_RATES:
1169 elems->ext_supp_rates = pos;
1170 elems->ext_supp_rates_len = elen;
1172 case WLAN_EID_HT_CAPABILITY:
1173 if (elen >= sizeof(struct ieee80211_ht_cap))
1174 elems->ht_cap_elem = (void *)pos;
1176 case WLAN_EID_HT_INFORMATION:
1177 if (elen >= sizeof(struct ieee80211_ht_info))
1178 elems->ht_info_elem = (void *)pos;
1180 case WLAN_EID_MESH_ID:
1181 elems->mesh_id = pos;
1182 elems->mesh_id_len = elen;
1184 case WLAN_EID_MESH_CONFIG:
1185 if (elen >= sizeof(struct ieee80211_meshconf_ie))
1186 elems->mesh_config = (void *)pos;
1188 case WLAN_EID_PEER_MGMT:
1189 elems->peering = pos;
1190 elems->peering_len = elen;
1194 elems->preq_len = elen;
1198 elems->prep_len = elen;
1202 elems->perr_len = elen;
1205 if (elen >= sizeof(struct ieee80211_rann_ie))
1206 elems->rann = (void *)pos;
1208 case WLAN_EID_CHANNEL_SWITCH:
1209 elems->ch_switch_elem = pos;
1210 elems->ch_switch_elem_len = elen;
1212 case WLAN_EID_QUIET:
1213 if (!elems->quiet_elem) {
1214 elems->quiet_elem = pos;
1215 elems->quiet_elem_len = elen;
1217 elems->num_of_quiet_elem++;
1219 case WLAN_EID_COUNTRY:
1220 elems->country_elem = pos;
1221 elems->country_elem_len = elen;
1223 case WLAN_EID_PWR_CONSTRAINT:
1224 elems->pwr_constr_elem = pos;
1225 elems->pwr_constr_elem_len = elen;
1227 case WLAN_EID_TIMEOUT_INTERVAL:
1228 elems->timeout_int = pos;
1229 elems->timeout_int_len = elen;
1241 EXPORT_SYMBOL(ieee802_11_parse_elems_crc);