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 <net/cfg80211.h>
12 #include <net/dsfield.h>
13 #include <linux/if_vlan.h>
14 #include <linux/mpls.h>
19 struct ieee80211_rate *
20 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
21 u32 basic_rates, int bitrate)
23 struct ieee80211_rate *result = &sband->bitrates[0];
26 for (i = 0; i < sband->n_bitrates; i++) {
27 if (!(basic_rates & BIT(i)))
29 if (sband->bitrates[i].bitrate > bitrate)
31 result = &sband->bitrates[i];
36 EXPORT_SYMBOL(ieee80211_get_response_rate);
38 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
39 enum nl80211_bss_scan_width scan_width)
41 struct ieee80211_rate *bitrates;
42 u32 mandatory_rates = 0;
43 enum ieee80211_rate_flags mandatory_flag;
49 if (sband->band == IEEE80211_BAND_2GHZ) {
50 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
51 scan_width == NL80211_BSS_CHAN_WIDTH_10)
52 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
54 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
56 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
59 bitrates = sband->bitrates;
60 for (i = 0; i < sband->n_bitrates; i++)
61 if (bitrates[i].flags & mandatory_flag)
62 mandatory_rates |= BIT(i);
63 return mandatory_rates;
65 EXPORT_SYMBOL(ieee80211_mandatory_rates);
67 int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
69 /* see 802.11 17.3.8.3.2 and Annex J
70 * there are overlapping channel numbers in 5GHz and 2GHz bands */
72 return 0; /* not supported */
74 case IEEE80211_BAND_2GHZ:
78 return 2407 + chan * 5;
80 case IEEE80211_BAND_5GHZ:
81 if (chan >= 182 && chan <= 196)
82 return 4000 + chan * 5;
84 return 5000 + chan * 5;
86 case IEEE80211_BAND_60GHZ:
88 return 56160 + chan * 2160;
93 return 0; /* not supported */
95 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
97 int ieee80211_frequency_to_channel(int freq)
99 /* see 802.11 17.3.8.3.2 and Annex J */
102 else if (freq < 2484)
103 return (freq - 2407) / 5;
104 else if (freq >= 4910 && freq <= 4980)
105 return (freq - 4000) / 5;
106 else if (freq <= 45000) /* DMG band lower limit */
107 return (freq - 5000) / 5;
108 else if (freq >= 58320 && freq <= 64800)
109 return (freq - 56160) / 2160;
113 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
115 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
118 enum ieee80211_band band;
119 struct ieee80211_supported_band *sband;
122 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
123 sband = wiphy->bands[band];
128 for (i = 0; i < sband->n_channels; i++) {
129 if (sband->channels[i].center_freq == freq)
130 return &sband->channels[i];
136 EXPORT_SYMBOL(__ieee80211_get_channel);
138 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
139 enum ieee80211_band band)
144 case IEEE80211_BAND_5GHZ:
146 for (i = 0; i < sband->n_bitrates; i++) {
147 if (sband->bitrates[i].bitrate == 60 ||
148 sband->bitrates[i].bitrate == 120 ||
149 sband->bitrates[i].bitrate == 240) {
150 sband->bitrates[i].flags |=
151 IEEE80211_RATE_MANDATORY_A;
157 case IEEE80211_BAND_2GHZ:
159 for (i = 0; i < sband->n_bitrates; i++) {
160 if (sband->bitrates[i].bitrate == 10) {
161 sband->bitrates[i].flags |=
162 IEEE80211_RATE_MANDATORY_B |
163 IEEE80211_RATE_MANDATORY_G;
167 if (sband->bitrates[i].bitrate == 20 ||
168 sband->bitrates[i].bitrate == 55 ||
169 sband->bitrates[i].bitrate == 110 ||
170 sband->bitrates[i].bitrate == 60 ||
171 sband->bitrates[i].bitrate == 120 ||
172 sband->bitrates[i].bitrate == 240) {
173 sband->bitrates[i].flags |=
174 IEEE80211_RATE_MANDATORY_G;
178 if (sband->bitrates[i].bitrate != 10 &&
179 sband->bitrates[i].bitrate != 20 &&
180 sband->bitrates[i].bitrate != 55 &&
181 sband->bitrates[i].bitrate != 110)
182 sband->bitrates[i].flags |=
183 IEEE80211_RATE_ERP_G;
185 WARN_ON(want != 0 && want != 3 && want != 6);
187 case IEEE80211_BAND_60GHZ:
188 /* check for mandatory HT MCS 1..4 */
189 WARN_ON(!sband->ht_cap.ht_supported);
190 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
192 case IEEE80211_NUM_BANDS:
198 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
200 enum ieee80211_band band;
202 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
203 if (wiphy->bands[band])
204 set_mandatory_flags_band(wiphy->bands[band], band);
207 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
210 for (i = 0; i < wiphy->n_cipher_suites; i++)
211 if (cipher == wiphy->cipher_suites[i])
216 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
217 struct key_params *params, int key_idx,
218 bool pairwise, const u8 *mac_addr)
223 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
226 if (pairwise && !mac_addr)
230 * Disallow pairwise keys with non-zero index unless it's WEP
231 * or a vendor specific cipher (because current deployments use
232 * pairwise WEP keys with non-zero indices and for vendor specific
233 * ciphers this should be validated in the driver or hardware level
234 * - but 802.11i clearly specifies to use zero)
236 if (pairwise && key_idx &&
237 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
238 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
239 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
242 switch (params->cipher) {
243 case WLAN_CIPHER_SUITE_WEP40:
244 if (params->key_len != WLAN_KEY_LEN_WEP40)
247 case WLAN_CIPHER_SUITE_TKIP:
248 if (params->key_len != WLAN_KEY_LEN_TKIP)
251 case WLAN_CIPHER_SUITE_CCMP:
252 if (params->key_len != WLAN_KEY_LEN_CCMP)
255 case WLAN_CIPHER_SUITE_WEP104:
256 if (params->key_len != WLAN_KEY_LEN_WEP104)
259 case WLAN_CIPHER_SUITE_AES_CMAC:
260 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
265 * We don't know anything about this algorithm,
266 * allow using it -- but the driver must check
267 * all parameters! We still check below whether
268 * or not the driver supports this algorithm,
275 switch (params->cipher) {
276 case WLAN_CIPHER_SUITE_WEP40:
277 case WLAN_CIPHER_SUITE_WEP104:
278 /* These ciphers do not use key sequence */
280 case WLAN_CIPHER_SUITE_TKIP:
281 case WLAN_CIPHER_SUITE_CCMP:
282 case WLAN_CIPHER_SUITE_AES_CMAC:
283 if (params->seq_len != 6)
289 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
295 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
297 unsigned int hdrlen = 24;
299 if (ieee80211_is_data(fc)) {
300 if (ieee80211_has_a4(fc))
302 if (ieee80211_is_data_qos(fc)) {
303 hdrlen += IEEE80211_QOS_CTL_LEN;
304 if (ieee80211_has_order(fc))
305 hdrlen += IEEE80211_HT_CTL_LEN;
310 if (ieee80211_is_ctl(fc)) {
312 * ACK and CTS are 10 bytes, all others 16. To see how
313 * to get this condition consider
314 * subtype mask: 0b0000000011110000 (0x00F0)
315 * ACK subtype: 0b0000000011010000 (0x00D0)
316 * CTS subtype: 0b0000000011000000 (0x00C0)
317 * bits that matter: ^^^ (0x00E0)
318 * value of those: 0b0000000011000000 (0x00C0)
320 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
328 EXPORT_SYMBOL(ieee80211_hdrlen);
330 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
332 const struct ieee80211_hdr *hdr =
333 (const struct ieee80211_hdr *)skb->data;
336 if (unlikely(skb->len < 10))
338 hdrlen = ieee80211_hdrlen(hdr->frame_control);
339 if (unlikely(hdrlen > skb->len))
343 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
345 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
347 int ae = meshhdr->flags & MESH_FLAGS_AE;
348 /* 802.11-2012, 8.2.4.7.3 */
353 case MESH_FLAGS_AE_A4:
355 case MESH_FLAGS_AE_A5_A6:
359 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
361 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
362 enum nl80211_iftype iftype)
364 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
365 u16 hdrlen, ethertype;
368 u8 src[ETH_ALEN] __aligned(2);
370 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
373 hdrlen = ieee80211_hdrlen(hdr->frame_control);
375 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
377 * IEEE 802.11 address fields:
378 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
379 * 0 0 DA SA BSSID n/a
380 * 0 1 DA BSSID SA n/a
381 * 1 0 BSSID SA DA n/a
384 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
385 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
387 switch (hdr->frame_control &
388 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
389 case cpu_to_le16(IEEE80211_FCTL_TODS):
390 if (unlikely(iftype != NL80211_IFTYPE_AP &&
391 iftype != NL80211_IFTYPE_AP_VLAN &&
392 iftype != NL80211_IFTYPE_P2P_GO))
395 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
396 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
397 iftype != NL80211_IFTYPE_MESH_POINT &&
398 iftype != NL80211_IFTYPE_AP_VLAN &&
399 iftype != NL80211_IFTYPE_STATION))
401 if (iftype == NL80211_IFTYPE_MESH_POINT) {
402 struct ieee80211s_hdr *meshdr =
403 (struct ieee80211s_hdr *) (skb->data + hdrlen);
404 /* make sure meshdr->flags is on the linear part */
405 if (!pskb_may_pull(skb, hdrlen + 1))
407 if (meshdr->flags & MESH_FLAGS_AE_A4)
409 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
410 skb_copy_bits(skb, hdrlen +
411 offsetof(struct ieee80211s_hdr, eaddr1),
413 skb_copy_bits(skb, hdrlen +
414 offsetof(struct ieee80211s_hdr, eaddr2),
417 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
420 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
421 if ((iftype != NL80211_IFTYPE_STATION &&
422 iftype != NL80211_IFTYPE_P2P_CLIENT &&
423 iftype != NL80211_IFTYPE_MESH_POINT) ||
424 (is_multicast_ether_addr(dst) &&
425 ether_addr_equal(src, addr)))
427 if (iftype == NL80211_IFTYPE_MESH_POINT) {
428 struct ieee80211s_hdr *meshdr =
429 (struct ieee80211s_hdr *) (skb->data + hdrlen);
430 /* make sure meshdr->flags is on the linear part */
431 if (!pskb_may_pull(skb, hdrlen + 1))
433 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
435 if (meshdr->flags & MESH_FLAGS_AE_A4)
436 skb_copy_bits(skb, hdrlen +
437 offsetof(struct ieee80211s_hdr, eaddr1),
439 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
443 if (iftype != NL80211_IFTYPE_ADHOC &&
444 iftype != NL80211_IFTYPE_STATION)
449 if (!pskb_may_pull(skb, hdrlen + 8))
452 payload = skb->data + hdrlen;
453 ethertype = (payload[6] << 8) | payload[7];
455 if (likely((ether_addr_equal(payload, rfc1042_header) &&
456 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
457 ether_addr_equal(payload, bridge_tunnel_header))) {
458 /* remove RFC1042 or Bridge-Tunnel encapsulation and
459 * replace EtherType */
460 skb_pull(skb, hdrlen + 6);
461 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
462 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
467 skb_pull(skb, hdrlen);
468 len = htons(skb->len);
469 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
470 memcpy(ehdr->h_dest, dst, ETH_ALEN);
471 memcpy(ehdr->h_source, src, ETH_ALEN);
476 EXPORT_SYMBOL(ieee80211_data_to_8023);
478 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
479 enum nl80211_iftype iftype, u8 *bssid, bool qos)
481 struct ieee80211_hdr hdr;
482 u16 hdrlen, ethertype;
484 const u8 *encaps_data;
485 int encaps_len, skip_header_bytes;
489 if (unlikely(skb->len < ETH_HLEN))
492 nh_pos = skb_network_header(skb) - skb->data;
493 h_pos = skb_transport_header(skb) - skb->data;
495 /* convert Ethernet header to proper 802.11 header (based on
497 ethertype = (skb->data[12] << 8) | skb->data[13];
498 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
501 case NL80211_IFTYPE_AP:
502 case NL80211_IFTYPE_AP_VLAN:
503 case NL80211_IFTYPE_P2P_GO:
504 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
506 memcpy(hdr.addr1, skb->data, ETH_ALEN);
507 memcpy(hdr.addr2, addr, ETH_ALEN);
508 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
511 case NL80211_IFTYPE_STATION:
512 case NL80211_IFTYPE_P2P_CLIENT:
513 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
515 memcpy(hdr.addr1, bssid, ETH_ALEN);
516 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
517 memcpy(hdr.addr3, skb->data, ETH_ALEN);
520 case NL80211_IFTYPE_ADHOC:
522 memcpy(hdr.addr1, skb->data, ETH_ALEN);
523 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
524 memcpy(hdr.addr3, bssid, ETH_ALEN);
532 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
536 hdr.frame_control = fc;
540 skip_header_bytes = ETH_HLEN;
541 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
542 encaps_data = bridge_tunnel_header;
543 encaps_len = sizeof(bridge_tunnel_header);
544 skip_header_bytes -= 2;
545 } else if (ethertype >= ETH_P_802_3_MIN) {
546 encaps_data = rfc1042_header;
547 encaps_len = sizeof(rfc1042_header);
548 skip_header_bytes -= 2;
554 skb_pull(skb, skip_header_bytes);
555 nh_pos -= skip_header_bytes;
556 h_pos -= skip_header_bytes;
558 head_need = hdrlen + encaps_len - skb_headroom(skb);
560 if (head_need > 0 || skb_cloned(skb)) {
561 head_need = max(head_need, 0);
565 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
568 skb->truesize += head_need;
572 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
573 nh_pos += encaps_len;
577 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
582 /* Update skb pointers to various headers since this modified frame
583 * is going to go through Linux networking code that may potentially
584 * need things like pointer to IP header. */
585 skb_set_mac_header(skb, 0);
586 skb_set_network_header(skb, nh_pos);
587 skb_set_transport_header(skb, h_pos);
591 EXPORT_SYMBOL(ieee80211_data_from_8023);
594 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
595 const u8 *addr, enum nl80211_iftype iftype,
596 const unsigned int extra_headroom,
597 bool has_80211_header)
599 struct sk_buff *frame = NULL;
602 const struct ethhdr *eth;
604 u8 dst[ETH_ALEN], src[ETH_ALEN];
606 if (has_80211_header) {
607 err = ieee80211_data_to_8023(skb, addr, iftype);
611 /* skip the wrapping header */
612 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
616 eth = (struct ethhdr *) skb->data;
619 while (skb != frame) {
621 __be16 len = eth->h_proto;
622 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
624 remaining = skb->len;
625 memcpy(dst, eth->h_dest, ETH_ALEN);
626 memcpy(src, eth->h_source, ETH_ALEN);
628 padding = (4 - subframe_len) & 0x3;
629 /* the last MSDU has no padding */
630 if (subframe_len > remaining)
633 skb_pull(skb, sizeof(struct ethhdr));
634 /* reuse skb for the last subframe */
635 if (remaining <= subframe_len + padding)
638 unsigned int hlen = ALIGN(extra_headroom, 4);
640 * Allocate and reserve two bytes more for payload
641 * alignment since sizeof(struct ethhdr) is 14.
643 frame = dev_alloc_skb(hlen + subframe_len + 2);
647 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
648 memcpy(skb_put(frame, ntohs(len)), skb->data,
651 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
654 dev_kfree_skb(frame);
659 skb_reset_network_header(frame);
660 frame->dev = skb->dev;
661 frame->priority = skb->priority;
663 payload = frame->data;
664 ethertype = (payload[6] << 8) | payload[7];
666 if (likely((ether_addr_equal(payload, rfc1042_header) &&
667 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
668 ether_addr_equal(payload, bridge_tunnel_header))) {
669 /* remove RFC1042 or Bridge-Tunnel
670 * encapsulation and replace EtherType */
672 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
673 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
675 memcpy(skb_push(frame, sizeof(__be16)), &len,
677 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
678 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
680 __skb_queue_tail(list, frame);
686 __skb_queue_purge(list);
690 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
692 /* Given a data frame determine the 802.1p/1d tag to use. */
693 unsigned int cfg80211_classify8021d(struct sk_buff *skb,
694 struct cfg80211_qos_map *qos_map)
697 unsigned char vlan_priority;
699 /* skb->priority values from 256->263 are magic values to
700 * directly indicate a specific 802.1d priority. This is used
701 * to allow 802.1d priority to be passed directly in from VLAN
704 if (skb->priority >= 256 && skb->priority <= 263)
705 return skb->priority - 256;
707 if (vlan_tx_tag_present(skb)) {
708 vlan_priority = (vlan_tx_tag_get(skb) & VLAN_PRIO_MASK)
710 if (vlan_priority > 0)
711 return vlan_priority;
714 switch (skb->protocol) {
715 case htons(ETH_P_IP):
716 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
718 case htons(ETH_P_IPV6):
719 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
721 case htons(ETH_P_MPLS_UC):
722 case htons(ETH_P_MPLS_MC): {
723 struct mpls_label mpls_tmp, *mpls;
725 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
726 sizeof(*mpls), &mpls_tmp);
730 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
733 case htons(ETH_P_80221):
734 /* 802.21 is always network control traffic */
741 unsigned int i, tmp_dscp = dscp >> 2;
743 for (i = 0; i < qos_map->num_des; i++) {
744 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
745 return qos_map->dscp_exception[i].up;
748 for (i = 0; i < 8; i++) {
749 if (tmp_dscp >= qos_map->up[i].low &&
750 tmp_dscp <= qos_map->up[i].high)
757 EXPORT_SYMBOL(cfg80211_classify8021d);
759 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
761 const struct cfg80211_bss_ies *ies;
763 ies = rcu_dereference(bss->ies);
767 return cfg80211_find_ie(ie, ies->data, ies->len);
769 EXPORT_SYMBOL(ieee80211_bss_get_ie);
771 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
773 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
774 struct net_device *dev = wdev->netdev;
777 if (!wdev->connect_keys)
780 for (i = 0; i < 6; i++) {
781 if (!wdev->connect_keys->params[i].cipher)
783 if (rdev_add_key(rdev, dev, i, false, NULL,
784 &wdev->connect_keys->params[i])) {
785 netdev_err(dev, "failed to set key %d\n", i);
788 if (wdev->connect_keys->def == i)
789 if (rdev_set_default_key(rdev, dev, i, true, true)) {
790 netdev_err(dev, "failed to set defkey %d\n", i);
793 if (wdev->connect_keys->defmgmt == i)
794 if (rdev_set_default_mgmt_key(rdev, dev, i))
795 netdev_err(dev, "failed to set mgtdef %d\n", i);
798 kfree(wdev->connect_keys);
799 wdev->connect_keys = NULL;
802 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
804 struct cfg80211_event *ev;
806 const u8 *bssid = NULL;
808 spin_lock_irqsave(&wdev->event_lock, flags);
809 while (!list_empty(&wdev->event_list)) {
810 ev = list_first_entry(&wdev->event_list,
811 struct cfg80211_event, list);
813 spin_unlock_irqrestore(&wdev->event_lock, flags);
817 case EVENT_CONNECT_RESULT:
818 if (!is_zero_ether_addr(ev->cr.bssid))
819 bssid = ev->cr.bssid;
820 __cfg80211_connect_result(
822 ev->cr.req_ie, ev->cr.req_ie_len,
823 ev->cr.resp_ie, ev->cr.resp_ie_len,
825 ev->cr.status == WLAN_STATUS_SUCCESS,
829 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
830 ev->rm.req_ie_len, ev->rm.resp_ie,
833 case EVENT_DISCONNECTED:
834 __cfg80211_disconnected(wdev->netdev,
835 ev->dc.ie, ev->dc.ie_len,
836 ev->dc.reason, true);
838 case EVENT_IBSS_JOINED:
839 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
847 spin_lock_irqsave(&wdev->event_lock, flags);
849 spin_unlock_irqrestore(&wdev->event_lock, flags);
852 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
854 struct wireless_dev *wdev;
858 list_for_each_entry(wdev, &rdev->wdev_list, list)
859 cfg80211_process_wdev_events(wdev);
862 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
863 struct net_device *dev, enum nl80211_iftype ntype,
864 u32 *flags, struct vif_params *params)
867 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
871 /* don't support changing VLANs, you just re-create them */
872 if (otype == NL80211_IFTYPE_AP_VLAN)
875 /* cannot change into P2P device type */
876 if (ntype == NL80211_IFTYPE_P2P_DEVICE)
879 if (!rdev->ops->change_virtual_intf ||
880 !(rdev->wiphy.interface_modes & (1 << ntype)))
883 /* if it's part of a bridge, reject changing type to station/ibss */
884 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
885 (ntype == NL80211_IFTYPE_ADHOC ||
886 ntype == NL80211_IFTYPE_STATION ||
887 ntype == NL80211_IFTYPE_P2P_CLIENT))
890 if (ntype != otype && netif_running(dev)) {
891 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
896 dev->ieee80211_ptr->use_4addr = false;
897 dev->ieee80211_ptr->mesh_id_up_len = 0;
898 wdev_lock(dev->ieee80211_ptr);
899 rdev_set_qos_map(rdev, dev, NULL);
900 wdev_unlock(dev->ieee80211_ptr);
903 case NL80211_IFTYPE_AP:
904 cfg80211_stop_ap(rdev, dev, true);
906 case NL80211_IFTYPE_ADHOC:
907 cfg80211_leave_ibss(rdev, dev, false);
909 case NL80211_IFTYPE_STATION:
910 case NL80211_IFTYPE_P2P_CLIENT:
911 wdev_lock(dev->ieee80211_ptr);
912 cfg80211_disconnect(rdev, dev,
913 WLAN_REASON_DEAUTH_LEAVING, true);
914 wdev_unlock(dev->ieee80211_ptr);
916 case NL80211_IFTYPE_MESH_POINT:
917 /* mesh should be handled? */
923 cfg80211_process_rdev_events(rdev);
926 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
928 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
930 if (!err && params && params->use_4addr != -1)
931 dev->ieee80211_ptr->use_4addr = params->use_4addr;
934 dev->priv_flags &= ~IFF_DONT_BRIDGE;
936 case NL80211_IFTYPE_STATION:
937 if (dev->ieee80211_ptr->use_4addr)
940 case NL80211_IFTYPE_P2P_CLIENT:
941 case NL80211_IFTYPE_ADHOC:
942 dev->priv_flags |= IFF_DONT_BRIDGE;
944 case NL80211_IFTYPE_P2P_GO:
945 case NL80211_IFTYPE_AP:
946 case NL80211_IFTYPE_AP_VLAN:
947 case NL80211_IFTYPE_WDS:
948 case NL80211_IFTYPE_MESH_POINT:
951 case NL80211_IFTYPE_MONITOR:
952 /* monitor can't bridge anyway */
954 case NL80211_IFTYPE_UNSPECIFIED:
955 case NUM_NL80211_IFTYPES:
958 case NL80211_IFTYPE_P2P_DEVICE:
964 if (!err && ntype != otype && netif_running(dev)) {
965 cfg80211_update_iface_num(rdev, ntype, 1);
966 cfg80211_update_iface_num(rdev, otype, -1);
972 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
974 static const u32 __mcs2bitrate[] = {
982 [5] = 12512, /* 1251.25 mbps */
992 [14] = 8662, /* 866.25 mbps */
1002 [24] = 67568, /* 6756.75 mbps */
1013 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1016 return __mcs2bitrate[rate->mcs];
1019 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1021 static const u32 base[4][10] = {
1070 if (WARN_ON_ONCE(rate->mcs > 9))
1073 idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
1074 RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
1075 rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
1076 rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
1078 bitrate = base[idx][rate->mcs];
1079 bitrate *= rate->nss;
1081 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1082 bitrate = (bitrate / 9) * 10;
1084 /* do NOT round down here */
1085 return (bitrate + 50000) / 100000;
1088 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1090 int modulation, streams, bitrate;
1092 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1093 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
1094 return rate->legacy;
1095 if (rate->flags & RATE_INFO_FLAGS_60G)
1096 return cfg80211_calculate_bitrate_60g(rate);
1097 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1098 return cfg80211_calculate_bitrate_vht(rate);
1100 /* the formula below does only work for MCS values smaller than 32 */
1101 if (WARN_ON_ONCE(rate->mcs >= 32))
1104 modulation = rate->mcs & 7;
1105 streams = (rate->mcs >> 3) + 1;
1107 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1111 bitrate *= (modulation + 1);
1112 else if (modulation == 4)
1113 bitrate *= (modulation + 2);
1115 bitrate *= (modulation + 3);
1119 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1120 bitrate = (bitrate / 9) * 10;
1122 /* do NOT round down here */
1123 return (bitrate + 50000) / 100000;
1125 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1127 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1128 enum ieee80211_p2p_attr_id attr,
1129 u8 *buf, unsigned int bufsize)
1132 u16 attr_remaining = 0;
1133 bool desired_attr = false;
1134 u16 desired_len = 0;
1137 unsigned int iedatalen;
1144 if (iedatalen + 2 > len)
1147 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1155 /* check WFA OUI, P2P subtype */
1156 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1157 iedata[2] != 0x9a || iedata[3] != 0x09)
1163 /* check attribute continuation into this IE */
1164 copy = min_t(unsigned int, attr_remaining, iedatalen);
1165 if (copy && desired_attr) {
1166 desired_len += copy;
1168 memcpy(out, iedata, min(bufsize, copy));
1169 out += min(bufsize, copy);
1170 bufsize -= min(bufsize, copy);
1174 if (copy == attr_remaining)
1178 attr_remaining -= copy;
1185 while (iedatalen > 0) {
1188 /* P2P attribute ID & size must fit */
1191 desired_attr = iedata[0] == attr;
1192 attr_len = get_unaligned_le16(iedata + 1);
1196 copy = min_t(unsigned int, attr_len, iedatalen);
1199 desired_len += copy;
1201 memcpy(out, iedata, min(bufsize, copy));
1202 out += min(bufsize, copy);
1203 bufsize -= min(bufsize, copy);
1206 if (copy == attr_len)
1212 attr_remaining = attr_len - copy;
1220 if (attr_remaining && desired_attr)
1225 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1227 bool ieee80211_operating_class_to_band(u8 operating_class,
1228 enum ieee80211_band *band)
1230 switch (operating_class) {
1233 *band = IEEE80211_BAND_5GHZ;
1239 *band = IEEE80211_BAND_2GHZ;
1242 *band = IEEE80211_BAND_60GHZ;
1248 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1250 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1253 struct wireless_dev *wdev;
1259 list_for_each_entry(wdev, &rdev->wdev_list, list) {
1260 if (!wdev->beacon_interval)
1262 if (wdev->beacon_interval != beacon_int) {
1271 int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1272 struct wireless_dev *wdev,
1273 enum nl80211_iftype iftype,
1274 struct ieee80211_channel *chan,
1275 enum cfg80211_chan_mode chanmode,
1278 struct wireless_dev *wdev_iter;
1279 u32 used_iftypes = BIT(iftype);
1280 int num[NUM_NL80211_IFTYPES];
1281 struct ieee80211_channel
1282 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1283 struct ieee80211_channel *ch;
1284 enum cfg80211_chan_mode chmode;
1285 int num_different_channels = 0;
1291 if (WARN_ON(hweight32(radar_detect) > 1))
1294 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
1297 /* Always allow software iftypes */
1298 if (rdev->wiphy.software_iftypes & BIT(iftype)) {
1304 memset(num, 0, sizeof(num));
1305 memset(used_channels, 0, sizeof(used_channels));
1310 case CHAN_MODE_UNDEFINED:
1312 case CHAN_MODE_SHARED:
1314 used_channels[0] = chan;
1315 num_different_channels++;
1317 case CHAN_MODE_EXCLUSIVE:
1318 num_different_channels++;
1322 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
1323 if (wdev_iter == wdev)
1325 if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
1326 if (!wdev_iter->p2p_started)
1328 } else if (wdev_iter->netdev) {
1329 if (!netif_running(wdev_iter->netdev))
1335 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1339 * We may be holding the "wdev" mutex, but now need to lock
1340 * wdev_iter. This is OK because once we get here wdev_iter
1341 * is not wdev (tested above), but we need to use the nested
1342 * locking for lockdep.
1344 mutex_lock_nested(&wdev_iter->mtx, 1);
1345 __acquire(wdev_iter->mtx);
1346 cfg80211_get_chan_state(wdev_iter, &ch, &chmode, &radar_detect);
1347 wdev_unlock(wdev_iter);
1350 case CHAN_MODE_UNDEFINED:
1352 case CHAN_MODE_SHARED:
1353 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1354 if (!used_channels[i] || used_channels[i] == ch)
1357 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
1360 if (used_channels[i] == NULL) {
1361 used_channels[i] = ch;
1362 num_different_channels++;
1365 case CHAN_MODE_EXCLUSIVE:
1366 num_different_channels++;
1370 num[wdev_iter->iftype]++;
1372 used_iftypes |= BIT(wdev_iter->iftype);
1375 if (total == 1 && !radar_detect)
1378 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1379 const struct ieee80211_iface_combination *c;
1380 struct ieee80211_iface_limit *limits;
1381 u32 all_iftypes = 0;
1383 c = &rdev->wiphy.iface_combinations[i];
1385 if (total > c->max_interfaces)
1387 if (num_different_channels > c->num_different_channels)
1390 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1395 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1396 if (rdev->wiphy.software_iftypes & BIT(iftype))
1398 for (j = 0; j < c->n_limits; j++) {
1399 all_iftypes |= limits[j].types;
1400 if (!(limits[j].types & BIT(iftype)))
1402 if (limits[j].max < num[iftype])
1404 limits[j].max -= num[iftype];
1408 if (radar_detect && !(c->radar_detect_widths & radar_detect))
1412 * Finally check that all iftypes that we're currently
1413 * using are actually part of this combination. If they
1414 * aren't then we can't use this combination and have
1415 * to continue to the next.
1417 if ((all_iftypes & used_iftypes) != used_iftypes)
1421 * This combination covered all interface types and
1422 * supported the requested numbers, so we're good.
1433 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1434 const u8 *rates, unsigned int n_rates,
1442 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1447 for (i = 0; i < n_rates; i++) {
1448 int rate = (rates[i] & 0x7f) * 5;
1451 for (j = 0; j < sband->n_bitrates; j++) {
1452 if (sband->bitrates[j].bitrate == rate) {
1463 * mask must have at least one bit set here since we
1464 * didn't accept a 0-length rates array nor allowed
1465 * entries in the array that didn't exist
1471 unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1473 enum ieee80211_band band;
1474 unsigned int n_channels = 0;
1476 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1477 if (wiphy->bands[band])
1478 n_channels += wiphy->bands[band]->n_channels;
1482 EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1484 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1485 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1486 const unsigned char rfc1042_header[] __aligned(2) =
1487 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1488 EXPORT_SYMBOL(rfc1042_header);
1490 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1491 const unsigned char bridge_tunnel_header[] __aligned(2) =
1492 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1493 EXPORT_SYMBOL(bridge_tunnel_header);