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>
17 struct ieee80211_rate *
18 ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
19 u32 basic_rates, int bitrate)
21 struct ieee80211_rate *result = &sband->bitrates[0];
24 for (i = 0; i < sband->n_bitrates; i++) {
25 if (!(basic_rates & BIT(i)))
27 if (sband->bitrates[i].bitrate > bitrate)
29 result = &sband->bitrates[i];
34 EXPORT_SYMBOL(ieee80211_get_response_rate);
36 u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
37 enum nl80211_bss_scan_width scan_width)
39 struct ieee80211_rate *bitrates;
40 u32 mandatory_rates = 0;
41 enum ieee80211_rate_flags mandatory_flag;
47 if (sband->band == IEEE80211_BAND_2GHZ) {
48 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
49 scan_width == NL80211_BSS_CHAN_WIDTH_10)
50 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
52 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
54 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
57 bitrates = sband->bitrates;
58 for (i = 0; i < sband->n_bitrates; i++)
59 if (bitrates[i].flags & mandatory_flag)
60 mandatory_rates |= BIT(i);
61 return mandatory_rates;
63 EXPORT_SYMBOL(ieee80211_mandatory_rates);
65 int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
67 /* see 802.11 17.3.8.3.2 and Annex J
68 * there are overlapping channel numbers in 5GHz and 2GHz bands */
70 return 0; /* not supported */
72 case IEEE80211_BAND_2GHZ:
76 return 2407 + chan * 5;
78 case IEEE80211_BAND_5GHZ:
79 if (chan >= 182 && chan <= 196)
80 return 4000 + chan * 5;
82 return 5000 + chan * 5;
84 case IEEE80211_BAND_60GHZ:
86 return 56160 + chan * 2160;
91 return 0; /* not supported */
93 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
95 int ieee80211_frequency_to_channel(int freq)
97 /* see 802.11 17.3.8.3.2 and Annex J */
100 else if (freq < 2484)
101 return (freq - 2407) / 5;
102 else if (freq >= 4910 && freq <= 4980)
103 return (freq - 4000) / 5;
104 else if (freq <= 45000) /* DMG band lower limit */
105 return (freq - 5000) / 5;
106 else if (freq >= 58320 && freq <= 64800)
107 return (freq - 56160) / 2160;
111 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
113 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
116 enum ieee80211_band band;
117 struct ieee80211_supported_band *sband;
120 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
121 sband = wiphy->bands[band];
126 for (i = 0; i < sband->n_channels; i++) {
127 if (sband->channels[i].center_freq == freq)
128 return &sband->channels[i];
134 EXPORT_SYMBOL(__ieee80211_get_channel);
136 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
137 enum ieee80211_band band)
142 case IEEE80211_BAND_5GHZ:
144 for (i = 0; i < sband->n_bitrates; i++) {
145 if (sband->bitrates[i].bitrate == 60 ||
146 sband->bitrates[i].bitrate == 120 ||
147 sband->bitrates[i].bitrate == 240) {
148 sband->bitrates[i].flags |=
149 IEEE80211_RATE_MANDATORY_A;
155 case IEEE80211_BAND_2GHZ:
157 for (i = 0; i < sband->n_bitrates; i++) {
158 if (sband->bitrates[i].bitrate == 10) {
159 sband->bitrates[i].flags |=
160 IEEE80211_RATE_MANDATORY_B |
161 IEEE80211_RATE_MANDATORY_G;
165 if (sband->bitrates[i].bitrate == 20 ||
166 sband->bitrates[i].bitrate == 55 ||
167 sband->bitrates[i].bitrate == 110 ||
168 sband->bitrates[i].bitrate == 60 ||
169 sband->bitrates[i].bitrate == 120 ||
170 sband->bitrates[i].bitrate == 240) {
171 sband->bitrates[i].flags |=
172 IEEE80211_RATE_MANDATORY_G;
176 if (sband->bitrates[i].bitrate != 10 &&
177 sband->bitrates[i].bitrate != 20 &&
178 sband->bitrates[i].bitrate != 55 &&
179 sband->bitrates[i].bitrate != 110)
180 sband->bitrates[i].flags |=
181 IEEE80211_RATE_ERP_G;
183 WARN_ON(want != 0 && want != 3 && want != 6);
185 case IEEE80211_BAND_60GHZ:
186 /* check for mandatory HT MCS 1..4 */
187 WARN_ON(!sband->ht_cap.ht_supported);
188 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
190 case IEEE80211_NUM_BANDS:
196 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
198 enum ieee80211_band band;
200 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
201 if (wiphy->bands[band])
202 set_mandatory_flags_band(wiphy->bands[band], band);
205 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
208 for (i = 0; i < wiphy->n_cipher_suites; i++)
209 if (cipher == wiphy->cipher_suites[i])
214 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
215 struct key_params *params, int key_idx,
216 bool pairwise, const u8 *mac_addr)
221 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
224 if (pairwise && !mac_addr)
228 * Disallow pairwise keys with non-zero index unless it's WEP
229 * or a vendor specific cipher (because current deployments use
230 * pairwise WEP keys with non-zero indices and for vendor specific
231 * ciphers this should be validated in the driver or hardware level
232 * - but 802.11i clearly specifies to use zero)
234 if (pairwise && key_idx &&
235 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
236 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
237 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
240 switch (params->cipher) {
241 case WLAN_CIPHER_SUITE_WEP40:
242 if (params->key_len != WLAN_KEY_LEN_WEP40)
245 case WLAN_CIPHER_SUITE_TKIP:
246 if (params->key_len != WLAN_KEY_LEN_TKIP)
249 case WLAN_CIPHER_SUITE_CCMP:
250 if (params->key_len != WLAN_KEY_LEN_CCMP)
253 case WLAN_CIPHER_SUITE_WEP104:
254 if (params->key_len != WLAN_KEY_LEN_WEP104)
257 case WLAN_CIPHER_SUITE_AES_CMAC:
258 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
263 * We don't know anything about this algorithm,
264 * allow using it -- but the driver must check
265 * all parameters! We still check below whether
266 * or not the driver supports this algorithm,
273 switch (params->cipher) {
274 case WLAN_CIPHER_SUITE_WEP40:
275 case WLAN_CIPHER_SUITE_WEP104:
276 /* These ciphers do not use key sequence */
278 case WLAN_CIPHER_SUITE_TKIP:
279 case WLAN_CIPHER_SUITE_CCMP:
280 case WLAN_CIPHER_SUITE_AES_CMAC:
281 if (params->seq_len != 6)
287 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
293 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
295 unsigned int hdrlen = 24;
297 if (ieee80211_is_data(fc)) {
298 if (ieee80211_has_a4(fc))
300 if (ieee80211_is_data_qos(fc)) {
301 hdrlen += IEEE80211_QOS_CTL_LEN;
302 if (ieee80211_has_order(fc))
303 hdrlen += IEEE80211_HT_CTL_LEN;
308 if (ieee80211_is_ctl(fc)) {
310 * ACK and CTS are 10 bytes, all others 16. To see how
311 * to get this condition consider
312 * subtype mask: 0b0000000011110000 (0x00F0)
313 * ACK subtype: 0b0000000011010000 (0x00D0)
314 * CTS subtype: 0b0000000011000000 (0x00C0)
315 * bits that matter: ^^^ (0x00E0)
316 * value of those: 0b0000000011000000 (0x00C0)
318 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
326 EXPORT_SYMBOL(ieee80211_hdrlen);
328 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
330 const struct ieee80211_hdr *hdr =
331 (const struct ieee80211_hdr *)skb->data;
334 if (unlikely(skb->len < 10))
336 hdrlen = ieee80211_hdrlen(hdr->frame_control);
337 if (unlikely(hdrlen > skb->len))
341 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
343 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
345 int ae = meshhdr->flags & MESH_FLAGS_AE;
346 /* 802.11-2012, 8.2.4.7.3 */
351 case MESH_FLAGS_AE_A4:
353 case MESH_FLAGS_AE_A5_A6:
357 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
359 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
360 enum nl80211_iftype iftype)
362 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
363 u16 hdrlen, ethertype;
366 u8 src[ETH_ALEN] __aligned(2);
368 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
371 hdrlen = ieee80211_hdrlen(hdr->frame_control);
373 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
375 * IEEE 802.11 address fields:
376 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
377 * 0 0 DA SA BSSID n/a
378 * 0 1 DA BSSID SA n/a
379 * 1 0 BSSID SA DA n/a
382 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
383 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
385 switch (hdr->frame_control &
386 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
387 case cpu_to_le16(IEEE80211_FCTL_TODS):
388 if (unlikely(iftype != NL80211_IFTYPE_AP &&
389 iftype != NL80211_IFTYPE_AP_VLAN &&
390 iftype != NL80211_IFTYPE_P2P_GO))
393 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
394 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
395 iftype != NL80211_IFTYPE_MESH_POINT &&
396 iftype != NL80211_IFTYPE_AP_VLAN &&
397 iftype != NL80211_IFTYPE_STATION))
399 if (iftype == NL80211_IFTYPE_MESH_POINT) {
400 struct ieee80211s_hdr *meshdr =
401 (struct ieee80211s_hdr *) (skb->data + hdrlen);
402 /* make sure meshdr->flags is on the linear part */
403 if (!pskb_may_pull(skb, hdrlen + 1))
405 if (meshdr->flags & MESH_FLAGS_AE_A4)
407 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
408 skb_copy_bits(skb, hdrlen +
409 offsetof(struct ieee80211s_hdr, eaddr1),
411 skb_copy_bits(skb, hdrlen +
412 offsetof(struct ieee80211s_hdr, eaddr2),
415 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
418 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
419 if ((iftype != NL80211_IFTYPE_STATION &&
420 iftype != NL80211_IFTYPE_P2P_CLIENT &&
421 iftype != NL80211_IFTYPE_MESH_POINT) ||
422 (is_multicast_ether_addr(dst) &&
423 ether_addr_equal(src, addr)))
425 if (iftype == NL80211_IFTYPE_MESH_POINT) {
426 struct ieee80211s_hdr *meshdr =
427 (struct ieee80211s_hdr *) (skb->data + hdrlen);
428 /* make sure meshdr->flags is on the linear part */
429 if (!pskb_may_pull(skb, hdrlen + 1))
431 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
433 if (meshdr->flags & MESH_FLAGS_AE_A4)
434 skb_copy_bits(skb, hdrlen +
435 offsetof(struct ieee80211s_hdr, eaddr1),
437 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
441 if (iftype != NL80211_IFTYPE_ADHOC &&
442 iftype != NL80211_IFTYPE_STATION)
447 if (!pskb_may_pull(skb, hdrlen + 8))
450 payload = skb->data + hdrlen;
451 ethertype = (payload[6] << 8) | payload[7];
453 if (likely((ether_addr_equal(payload, rfc1042_header) &&
454 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
455 ether_addr_equal(payload, bridge_tunnel_header))) {
456 /* remove RFC1042 or Bridge-Tunnel encapsulation and
457 * replace EtherType */
458 skb_pull(skb, hdrlen + 6);
459 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
460 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
465 skb_pull(skb, hdrlen);
466 len = htons(skb->len);
467 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
468 memcpy(ehdr->h_dest, dst, ETH_ALEN);
469 memcpy(ehdr->h_source, src, ETH_ALEN);
474 EXPORT_SYMBOL(ieee80211_data_to_8023);
476 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
477 enum nl80211_iftype iftype, u8 *bssid, bool qos)
479 struct ieee80211_hdr hdr;
480 u16 hdrlen, ethertype;
482 const u8 *encaps_data;
483 int encaps_len, skip_header_bytes;
487 if (unlikely(skb->len < ETH_HLEN))
490 nh_pos = skb_network_header(skb) - skb->data;
491 h_pos = skb_transport_header(skb) - skb->data;
493 /* convert Ethernet header to proper 802.11 header (based on
495 ethertype = (skb->data[12] << 8) | skb->data[13];
496 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
499 case NL80211_IFTYPE_AP:
500 case NL80211_IFTYPE_AP_VLAN:
501 case NL80211_IFTYPE_P2P_GO:
502 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
504 memcpy(hdr.addr1, skb->data, ETH_ALEN);
505 memcpy(hdr.addr2, addr, ETH_ALEN);
506 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
509 case NL80211_IFTYPE_STATION:
510 case NL80211_IFTYPE_P2P_CLIENT:
511 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
513 memcpy(hdr.addr1, bssid, ETH_ALEN);
514 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
515 memcpy(hdr.addr3, skb->data, ETH_ALEN);
518 case NL80211_IFTYPE_ADHOC:
520 memcpy(hdr.addr1, skb->data, ETH_ALEN);
521 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
522 memcpy(hdr.addr3, bssid, ETH_ALEN);
530 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
534 hdr.frame_control = fc;
538 skip_header_bytes = ETH_HLEN;
539 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
540 encaps_data = bridge_tunnel_header;
541 encaps_len = sizeof(bridge_tunnel_header);
542 skip_header_bytes -= 2;
543 } else if (ethertype >= ETH_P_802_3_MIN) {
544 encaps_data = rfc1042_header;
545 encaps_len = sizeof(rfc1042_header);
546 skip_header_bytes -= 2;
552 skb_pull(skb, skip_header_bytes);
553 nh_pos -= skip_header_bytes;
554 h_pos -= skip_header_bytes;
556 head_need = hdrlen + encaps_len - skb_headroom(skb);
558 if (head_need > 0 || skb_cloned(skb)) {
559 head_need = max(head_need, 0);
563 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
566 skb->truesize += head_need;
570 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
571 nh_pos += encaps_len;
575 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
580 /* Update skb pointers to various headers since this modified frame
581 * is going to go through Linux networking code that may potentially
582 * need things like pointer to IP header. */
583 skb_set_mac_header(skb, 0);
584 skb_set_network_header(skb, nh_pos);
585 skb_set_transport_header(skb, h_pos);
589 EXPORT_SYMBOL(ieee80211_data_from_8023);
592 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
593 const u8 *addr, enum nl80211_iftype iftype,
594 const unsigned int extra_headroom,
595 bool has_80211_header)
597 struct sk_buff *frame = NULL;
600 const struct ethhdr *eth;
602 u8 dst[ETH_ALEN], src[ETH_ALEN];
604 if (has_80211_header) {
605 err = ieee80211_data_to_8023(skb, addr, iftype);
609 /* skip the wrapping header */
610 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
614 eth = (struct ethhdr *) skb->data;
617 while (skb != frame) {
619 __be16 len = eth->h_proto;
620 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
622 remaining = skb->len;
623 memcpy(dst, eth->h_dest, ETH_ALEN);
624 memcpy(src, eth->h_source, ETH_ALEN);
626 padding = (4 - subframe_len) & 0x3;
627 /* the last MSDU has no padding */
628 if (subframe_len > remaining)
631 skb_pull(skb, sizeof(struct ethhdr));
632 /* reuse skb for the last subframe */
633 if (remaining <= subframe_len + padding)
636 unsigned int hlen = ALIGN(extra_headroom, 4);
638 * Allocate and reserve two bytes more for payload
639 * alignment since sizeof(struct ethhdr) is 14.
641 frame = dev_alloc_skb(hlen + subframe_len + 2);
645 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
646 memcpy(skb_put(frame, ntohs(len)), skb->data,
649 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
652 dev_kfree_skb(frame);
657 skb_reset_network_header(frame);
658 frame->dev = skb->dev;
659 frame->priority = skb->priority;
661 payload = frame->data;
662 ethertype = (payload[6] << 8) | payload[7];
664 if (likely((ether_addr_equal(payload, rfc1042_header) &&
665 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
666 ether_addr_equal(payload, bridge_tunnel_header))) {
667 /* remove RFC1042 or Bridge-Tunnel
668 * encapsulation and replace EtherType */
670 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
671 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
673 memcpy(skb_push(frame, sizeof(__be16)), &len,
675 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
676 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
678 __skb_queue_tail(list, frame);
684 __skb_queue_purge(list);
688 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
690 /* Given a data frame determine the 802.1p/1d tag to use. */
691 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
695 /* skb->priority values from 256->263 are magic values to
696 * directly indicate a specific 802.1d priority. This is used
697 * to allow 802.1d priority to be passed directly in from VLAN
700 if (skb->priority >= 256 && skb->priority <= 263)
701 return skb->priority - 256;
703 switch (skb->protocol) {
704 case htons(ETH_P_IP):
705 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
707 case htons(ETH_P_IPV6):
708 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
716 EXPORT_SYMBOL(cfg80211_classify8021d);
718 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
720 const struct cfg80211_bss_ies *ies;
722 ies = rcu_dereference(bss->ies);
726 return cfg80211_find_ie(ie, ies->data, ies->len);
728 EXPORT_SYMBOL(ieee80211_bss_get_ie);
730 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
732 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
733 struct net_device *dev = wdev->netdev;
736 if (!wdev->connect_keys)
739 for (i = 0; i < 6; i++) {
740 if (!wdev->connect_keys->params[i].cipher)
742 if (rdev_add_key(rdev, dev, i, false, NULL,
743 &wdev->connect_keys->params[i])) {
744 netdev_err(dev, "failed to set key %d\n", i);
747 if (wdev->connect_keys->def == i)
748 if (rdev_set_default_key(rdev, dev, i, true, true)) {
749 netdev_err(dev, "failed to set defkey %d\n", i);
752 if (wdev->connect_keys->defmgmt == i)
753 if (rdev_set_default_mgmt_key(rdev, dev, i))
754 netdev_err(dev, "failed to set mgtdef %d\n", i);
757 kfree(wdev->connect_keys);
758 wdev->connect_keys = NULL;
761 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
763 struct cfg80211_event *ev;
765 const u8 *bssid = NULL;
767 spin_lock_irqsave(&wdev->event_lock, flags);
768 while (!list_empty(&wdev->event_list)) {
769 ev = list_first_entry(&wdev->event_list,
770 struct cfg80211_event, list);
772 spin_unlock_irqrestore(&wdev->event_lock, flags);
776 case EVENT_CONNECT_RESULT:
777 if (!is_zero_ether_addr(ev->cr.bssid))
778 bssid = ev->cr.bssid;
779 __cfg80211_connect_result(
781 ev->cr.req_ie, ev->cr.req_ie_len,
782 ev->cr.resp_ie, ev->cr.resp_ie_len,
784 ev->cr.status == WLAN_STATUS_SUCCESS,
788 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
789 ev->rm.req_ie_len, ev->rm.resp_ie,
792 case EVENT_DISCONNECTED:
793 __cfg80211_disconnected(wdev->netdev,
794 ev->dc.ie, ev->dc.ie_len,
795 ev->dc.reason, true);
797 case EVENT_IBSS_JOINED:
798 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
805 spin_lock_irqsave(&wdev->event_lock, flags);
807 spin_unlock_irqrestore(&wdev->event_lock, flags);
810 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
812 struct wireless_dev *wdev;
815 ASSERT_RDEV_LOCK(rdev);
817 list_for_each_entry(wdev, &rdev->wdev_list, list)
818 cfg80211_process_wdev_events(wdev);
821 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
822 struct net_device *dev, enum nl80211_iftype ntype,
823 u32 *flags, struct vif_params *params)
826 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
828 ASSERT_RDEV_LOCK(rdev);
830 /* don't support changing VLANs, you just re-create them */
831 if (otype == NL80211_IFTYPE_AP_VLAN)
834 /* cannot change into P2P device type */
835 if (ntype == NL80211_IFTYPE_P2P_DEVICE)
838 if (!rdev->ops->change_virtual_intf ||
839 !(rdev->wiphy.interface_modes & (1 << ntype)))
842 /* if it's part of a bridge, reject changing type to station/ibss */
843 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
844 (ntype == NL80211_IFTYPE_ADHOC ||
845 ntype == NL80211_IFTYPE_STATION ||
846 ntype == NL80211_IFTYPE_P2P_CLIENT))
849 if (ntype != otype && netif_running(dev)) {
850 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
855 dev->ieee80211_ptr->use_4addr = false;
856 dev->ieee80211_ptr->mesh_id_up_len = 0;
859 case NL80211_IFTYPE_AP:
860 cfg80211_stop_ap(rdev, dev);
862 case NL80211_IFTYPE_ADHOC:
863 cfg80211_leave_ibss(rdev, dev, false);
865 case NL80211_IFTYPE_STATION:
866 case NL80211_IFTYPE_P2P_CLIENT:
867 wdev_lock(dev->ieee80211_ptr);
868 cfg80211_disconnect(rdev, dev,
869 WLAN_REASON_DEAUTH_LEAVING, true);
870 wdev_unlock(dev->ieee80211_ptr);
872 case NL80211_IFTYPE_MESH_POINT:
873 /* mesh should be handled? */
879 cfg80211_process_rdev_events(rdev);
882 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
884 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
886 if (!err && params && params->use_4addr != -1)
887 dev->ieee80211_ptr->use_4addr = params->use_4addr;
890 dev->priv_flags &= ~IFF_DONT_BRIDGE;
892 case NL80211_IFTYPE_STATION:
893 if (dev->ieee80211_ptr->use_4addr)
896 case NL80211_IFTYPE_P2P_CLIENT:
897 case NL80211_IFTYPE_ADHOC:
898 dev->priv_flags |= IFF_DONT_BRIDGE;
900 case NL80211_IFTYPE_P2P_GO:
901 case NL80211_IFTYPE_AP:
902 case NL80211_IFTYPE_AP_VLAN:
903 case NL80211_IFTYPE_WDS:
904 case NL80211_IFTYPE_MESH_POINT:
907 case NL80211_IFTYPE_MONITOR:
908 /* monitor can't bridge anyway */
910 case NL80211_IFTYPE_UNSPECIFIED:
911 case NUM_NL80211_IFTYPES:
914 case NL80211_IFTYPE_P2P_DEVICE:
920 if (!err && ntype != otype && netif_running(dev)) {
921 cfg80211_update_iface_num(rdev, ntype, 1);
922 cfg80211_update_iface_num(rdev, otype, -1);
928 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
930 static const u32 __mcs2bitrate[] = {
938 [5] = 12512, /* 1251.25 mbps */
948 [14] = 8662, /* 866.25 mbps */
958 [24] = 67568, /* 6756.75 mbps */
969 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
972 return __mcs2bitrate[rate->mcs];
975 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
977 static const u32 base[4][10] = {
1026 if (WARN_ON_ONCE(rate->mcs > 9))
1029 idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
1030 RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
1031 rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
1032 rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
1034 bitrate = base[idx][rate->mcs];
1035 bitrate *= rate->nss;
1037 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1038 bitrate = (bitrate / 9) * 10;
1040 /* do NOT round down here */
1041 return (bitrate + 50000) / 100000;
1044 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1046 int modulation, streams, bitrate;
1048 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1049 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
1050 return rate->legacy;
1051 if (rate->flags & RATE_INFO_FLAGS_60G)
1052 return cfg80211_calculate_bitrate_60g(rate);
1053 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1054 return cfg80211_calculate_bitrate_vht(rate);
1056 /* the formula below does only work for MCS values smaller than 32 */
1057 if (WARN_ON_ONCE(rate->mcs >= 32))
1060 modulation = rate->mcs & 7;
1061 streams = (rate->mcs >> 3) + 1;
1063 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1067 bitrate *= (modulation + 1);
1068 else if (modulation == 4)
1069 bitrate *= (modulation + 2);
1071 bitrate *= (modulation + 3);
1075 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1076 bitrate = (bitrate / 9) * 10;
1078 /* do NOT round down here */
1079 return (bitrate + 50000) / 100000;
1081 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1083 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1084 enum ieee80211_p2p_attr_id attr,
1085 u8 *buf, unsigned int bufsize)
1088 u16 attr_remaining = 0;
1089 bool desired_attr = false;
1090 u16 desired_len = 0;
1093 unsigned int iedatalen;
1100 if (iedatalen + 2 > len)
1103 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1111 /* check WFA OUI, P2P subtype */
1112 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1113 iedata[2] != 0x9a || iedata[3] != 0x09)
1119 /* check attribute continuation into this IE */
1120 copy = min_t(unsigned int, attr_remaining, iedatalen);
1121 if (copy && desired_attr) {
1122 desired_len += copy;
1124 memcpy(out, iedata, min(bufsize, copy));
1125 out += min(bufsize, copy);
1126 bufsize -= min(bufsize, copy);
1130 if (copy == attr_remaining)
1134 attr_remaining -= copy;
1141 while (iedatalen > 0) {
1144 /* P2P attribute ID & size must fit */
1147 desired_attr = iedata[0] == attr;
1148 attr_len = get_unaligned_le16(iedata + 1);
1152 copy = min_t(unsigned int, attr_len, iedatalen);
1155 desired_len += copy;
1157 memcpy(out, iedata, min(bufsize, copy));
1158 out += min(bufsize, copy);
1159 bufsize -= min(bufsize, copy);
1162 if (copy == attr_len)
1168 attr_remaining = attr_len - copy;
1176 if (attr_remaining && desired_attr)
1181 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1183 bool ieee80211_operating_class_to_band(u8 operating_class,
1184 enum ieee80211_band *band)
1186 switch (operating_class) {
1189 *band = IEEE80211_BAND_5GHZ;
1195 *band = IEEE80211_BAND_2GHZ;
1198 *band = IEEE80211_BAND_60GHZ;
1204 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1206 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1209 struct wireless_dev *wdev;
1215 list_for_each_entry(wdev, &rdev->wdev_list, list) {
1216 if (!wdev->beacon_interval)
1218 if (wdev->beacon_interval != beacon_int) {
1227 int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1228 struct wireless_dev *wdev,
1229 enum nl80211_iftype iftype,
1230 struct ieee80211_channel *chan,
1231 enum cfg80211_chan_mode chanmode,
1234 struct wireless_dev *wdev_iter;
1235 u32 used_iftypes = BIT(iftype);
1236 int num[NUM_NL80211_IFTYPES];
1237 struct ieee80211_channel
1238 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1239 struct ieee80211_channel *ch;
1240 enum cfg80211_chan_mode chmode;
1241 int num_different_channels = 0;
1243 bool radar_required;
1248 if (WARN_ON(hweight32(radar_detect) > 1))
1252 case NL80211_IFTYPE_ADHOC:
1253 case NL80211_IFTYPE_AP:
1254 case NL80211_IFTYPE_AP_VLAN:
1255 case NL80211_IFTYPE_MESH_POINT:
1256 case NL80211_IFTYPE_P2P_GO:
1257 case NL80211_IFTYPE_WDS:
1258 radar_required = !!(chan &&
1259 (chan->flags & IEEE80211_CHAN_RADAR));
1261 case NL80211_IFTYPE_P2P_CLIENT:
1262 case NL80211_IFTYPE_STATION:
1263 case NL80211_IFTYPE_P2P_DEVICE:
1264 case NL80211_IFTYPE_MONITOR:
1265 radar_required = false;
1267 case NUM_NL80211_IFTYPES:
1268 case NL80211_IFTYPE_UNSPECIFIED:
1273 if (radar_required && !radar_detect)
1276 /* Always allow software iftypes */
1277 if (rdev->wiphy.software_iftypes & BIT(iftype)) {
1283 memset(num, 0, sizeof(num));
1284 memset(used_channels, 0, sizeof(used_channels));
1289 case CHAN_MODE_UNDEFINED:
1291 case CHAN_MODE_SHARED:
1293 used_channels[0] = chan;
1294 num_different_channels++;
1296 case CHAN_MODE_EXCLUSIVE:
1297 num_different_channels++;
1301 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
1302 if (wdev_iter == wdev)
1304 if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
1305 if (!wdev_iter->p2p_started)
1307 } else if (wdev_iter->netdev) {
1308 if (!netif_running(wdev_iter->netdev))
1314 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1318 * We may be holding the "wdev" mutex, but now need to lock
1319 * wdev_iter. This is OK because once we get here wdev_iter
1320 * is not wdev (tested above), but we need to use the nested
1321 * locking for lockdep.
1323 mutex_lock_nested(&wdev_iter->mtx, 1);
1324 __acquire(wdev_iter->mtx);
1325 cfg80211_get_chan_state(wdev_iter, &ch, &chmode);
1326 wdev_unlock(wdev_iter);
1329 case CHAN_MODE_UNDEFINED:
1331 case CHAN_MODE_SHARED:
1332 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1333 if (!used_channels[i] || used_channels[i] == ch)
1336 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
1339 if (used_channels[i] == NULL) {
1340 used_channels[i] = ch;
1341 num_different_channels++;
1344 case CHAN_MODE_EXCLUSIVE:
1345 num_different_channels++;
1349 num[wdev_iter->iftype]++;
1351 used_iftypes |= BIT(wdev_iter->iftype);
1354 if (total == 1 && !radar_detect)
1357 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1358 const struct ieee80211_iface_combination *c;
1359 struct ieee80211_iface_limit *limits;
1360 u32 all_iftypes = 0;
1362 c = &rdev->wiphy.iface_combinations[i];
1364 if (total > c->max_interfaces)
1366 if (num_different_channels > c->num_different_channels)
1369 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1374 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1375 if (rdev->wiphy.software_iftypes & BIT(iftype))
1377 for (j = 0; j < c->n_limits; j++) {
1378 all_iftypes |= limits[j].types;
1379 if (!(limits[j].types & BIT(iftype)))
1381 if (limits[j].max < num[iftype])
1383 limits[j].max -= num[iftype];
1387 if (radar_detect && !(c->radar_detect_widths & radar_detect))
1391 * Finally check that all iftypes that we're currently
1392 * using are actually part of this combination. If they
1393 * aren't then we can't use this combination and have
1394 * to continue to the next.
1396 if ((all_iftypes & used_iftypes) != used_iftypes)
1400 * This combination covered all interface types and
1401 * supported the requested numbers, so we're good.
1412 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1413 const u8 *rates, unsigned int n_rates,
1421 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1426 for (i = 0; i < n_rates; i++) {
1427 int rate = (rates[i] & 0x7f) * 5;
1430 for (j = 0; j < sband->n_bitrates; j++) {
1431 if (sband->bitrates[j].bitrate == rate) {
1442 * mask must have at least one bit set here since we
1443 * didn't accept a 0-length rates array nor allowed
1444 * entries in the array that didn't exist
1450 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1451 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1452 const unsigned char rfc1042_header[] __aligned(2) =
1453 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1454 EXPORT_SYMBOL(rfc1042_header);
1456 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1457 const unsigned char bridge_tunnel_header[] __aligned(2) =
1458 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1459 EXPORT_SYMBOL(bridge_tunnel_header);