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)
38 struct ieee80211_rate *bitrates;
39 u32 mandatory_rates = 0;
40 enum ieee80211_rate_flags mandatory_flag;
46 if (sband->band == IEEE80211_BAND_2GHZ)
47 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
49 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
51 bitrates = sband->bitrates;
52 for (i = 0; i < sband->n_bitrates; i++)
53 if (bitrates[i].flags & mandatory_flag)
54 mandatory_rates |= BIT(i);
55 return mandatory_rates;
57 EXPORT_SYMBOL(ieee80211_mandatory_rates);
59 int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
61 /* see 802.11 17.3.8.3.2 and Annex J
62 * there are overlapping channel numbers in 5GHz and 2GHz bands */
64 return 0; /* not supported */
66 case IEEE80211_BAND_2GHZ:
70 return 2407 + chan * 5;
72 case IEEE80211_BAND_5GHZ:
73 if (chan >= 182 && chan <= 196)
74 return 4000 + chan * 5;
76 return 5000 + chan * 5;
78 case IEEE80211_BAND_60GHZ:
80 return 56160 + chan * 2160;
85 return 0; /* not supported */
87 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
89 int ieee80211_frequency_to_channel(int freq)
91 /* see 802.11 17.3.8.3.2 and Annex J */
95 return (freq - 2407) / 5;
96 else if (freq >= 4910 && freq <= 4980)
97 return (freq - 4000) / 5;
98 else if (freq <= 45000) /* DMG band lower limit */
99 return (freq - 5000) / 5;
100 else if (freq >= 58320 && freq <= 64800)
101 return (freq - 56160) / 2160;
105 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
107 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
110 enum ieee80211_band band;
111 struct ieee80211_supported_band *sband;
114 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
115 sband = wiphy->bands[band];
120 for (i = 0; i < sband->n_channels; i++) {
121 if (sband->channels[i].center_freq == freq)
122 return &sband->channels[i];
128 EXPORT_SYMBOL(__ieee80211_get_channel);
130 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
131 enum ieee80211_band band)
136 case IEEE80211_BAND_5GHZ:
138 for (i = 0; i < sband->n_bitrates; i++) {
139 if (sband->bitrates[i].bitrate == 60 ||
140 sband->bitrates[i].bitrate == 120 ||
141 sband->bitrates[i].bitrate == 240) {
142 sband->bitrates[i].flags |=
143 IEEE80211_RATE_MANDATORY_A;
149 case IEEE80211_BAND_2GHZ:
151 for (i = 0; i < sband->n_bitrates; i++) {
152 if (sband->bitrates[i].bitrate == 10) {
153 sband->bitrates[i].flags |=
154 IEEE80211_RATE_MANDATORY_B |
155 IEEE80211_RATE_MANDATORY_G;
159 if (sband->bitrates[i].bitrate == 20 ||
160 sband->bitrates[i].bitrate == 55 ||
161 sband->bitrates[i].bitrate == 110 ||
162 sband->bitrates[i].bitrate == 60 ||
163 sband->bitrates[i].bitrate == 120 ||
164 sband->bitrates[i].bitrate == 240) {
165 sband->bitrates[i].flags |=
166 IEEE80211_RATE_MANDATORY_G;
170 if (sband->bitrates[i].bitrate != 10 &&
171 sband->bitrates[i].bitrate != 20 &&
172 sband->bitrates[i].bitrate != 55 &&
173 sband->bitrates[i].bitrate != 110)
174 sband->bitrates[i].flags |=
175 IEEE80211_RATE_ERP_G;
177 WARN_ON(want != 0 && want != 3 && want != 6);
179 case IEEE80211_BAND_60GHZ:
180 /* check for mandatory HT MCS 1..4 */
181 WARN_ON(!sband->ht_cap.ht_supported);
182 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
184 case IEEE80211_NUM_BANDS:
190 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
192 enum ieee80211_band band;
194 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
195 if (wiphy->bands[band])
196 set_mandatory_flags_band(wiphy->bands[band], band);
199 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
202 for (i = 0; i < wiphy->n_cipher_suites; i++)
203 if (cipher == wiphy->cipher_suites[i])
208 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
209 struct key_params *params, int key_idx,
210 bool pairwise, const u8 *mac_addr)
215 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
218 if (pairwise && !mac_addr)
222 * Disallow pairwise keys with non-zero index unless it's WEP
223 * or a vendor specific cipher (because current deployments use
224 * pairwise WEP keys with non-zero indices and for vendor specific
225 * ciphers this should be validated in the driver or hardware level
226 * - but 802.11i clearly specifies to use zero)
228 if (pairwise && key_idx &&
229 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
230 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
231 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
234 switch (params->cipher) {
235 case WLAN_CIPHER_SUITE_WEP40:
236 if (params->key_len != WLAN_KEY_LEN_WEP40)
239 case WLAN_CIPHER_SUITE_TKIP:
240 if (params->key_len != WLAN_KEY_LEN_TKIP)
243 case WLAN_CIPHER_SUITE_CCMP:
244 if (params->key_len != WLAN_KEY_LEN_CCMP)
247 case WLAN_CIPHER_SUITE_WEP104:
248 if (params->key_len != WLAN_KEY_LEN_WEP104)
251 case WLAN_CIPHER_SUITE_AES_CMAC:
252 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
257 * We don't know anything about this algorithm,
258 * allow using it -- but the driver must check
259 * all parameters! We still check below whether
260 * or not the driver supports this algorithm,
267 switch (params->cipher) {
268 case WLAN_CIPHER_SUITE_WEP40:
269 case WLAN_CIPHER_SUITE_WEP104:
270 /* These ciphers do not use key sequence */
272 case WLAN_CIPHER_SUITE_TKIP:
273 case WLAN_CIPHER_SUITE_CCMP:
274 case WLAN_CIPHER_SUITE_AES_CMAC:
275 if (params->seq_len != 6)
281 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
287 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
289 unsigned int hdrlen = 24;
291 if (ieee80211_is_data(fc)) {
292 if (ieee80211_has_a4(fc))
294 if (ieee80211_is_data_qos(fc)) {
295 hdrlen += IEEE80211_QOS_CTL_LEN;
296 if (ieee80211_has_order(fc))
297 hdrlen += IEEE80211_HT_CTL_LEN;
302 if (ieee80211_is_ctl(fc)) {
304 * ACK and CTS are 10 bytes, all others 16. To see how
305 * to get this condition consider
306 * subtype mask: 0b0000000011110000 (0x00F0)
307 * ACK subtype: 0b0000000011010000 (0x00D0)
308 * CTS subtype: 0b0000000011000000 (0x00C0)
309 * bits that matter: ^^^ (0x00E0)
310 * value of those: 0b0000000011000000 (0x00C0)
312 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
320 EXPORT_SYMBOL(ieee80211_hdrlen);
322 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
324 const struct ieee80211_hdr *hdr =
325 (const struct ieee80211_hdr *)skb->data;
328 if (unlikely(skb->len < 10))
330 hdrlen = ieee80211_hdrlen(hdr->frame_control);
331 if (unlikely(hdrlen > skb->len))
335 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
337 unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
339 int ae = meshhdr->flags & MESH_FLAGS_AE;
340 /* 802.11-2012, 8.2.4.7.3 */
345 case MESH_FLAGS_AE_A4:
347 case MESH_FLAGS_AE_A5_A6:
351 EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
353 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
354 enum nl80211_iftype iftype)
356 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
357 u16 hdrlen, ethertype;
360 u8 src[ETH_ALEN] __aligned(2);
362 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
365 hdrlen = ieee80211_hdrlen(hdr->frame_control);
367 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
369 * IEEE 802.11 address fields:
370 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
371 * 0 0 DA SA BSSID n/a
372 * 0 1 DA BSSID SA n/a
373 * 1 0 BSSID SA DA n/a
376 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
377 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
379 switch (hdr->frame_control &
380 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
381 case cpu_to_le16(IEEE80211_FCTL_TODS):
382 if (unlikely(iftype != NL80211_IFTYPE_AP &&
383 iftype != NL80211_IFTYPE_AP_VLAN &&
384 iftype != NL80211_IFTYPE_P2P_GO))
387 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
388 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
389 iftype != NL80211_IFTYPE_MESH_POINT &&
390 iftype != NL80211_IFTYPE_AP_VLAN &&
391 iftype != NL80211_IFTYPE_STATION))
393 if (iftype == NL80211_IFTYPE_MESH_POINT) {
394 struct ieee80211s_hdr *meshdr =
395 (struct ieee80211s_hdr *) (skb->data + hdrlen);
396 /* make sure meshdr->flags is on the linear part */
397 if (!pskb_may_pull(skb, hdrlen + 1))
399 if (meshdr->flags & MESH_FLAGS_AE_A4)
401 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
402 skb_copy_bits(skb, hdrlen +
403 offsetof(struct ieee80211s_hdr, eaddr1),
405 skb_copy_bits(skb, hdrlen +
406 offsetof(struct ieee80211s_hdr, eaddr2),
409 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
412 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
413 if ((iftype != NL80211_IFTYPE_STATION &&
414 iftype != NL80211_IFTYPE_P2P_CLIENT &&
415 iftype != NL80211_IFTYPE_MESH_POINT) ||
416 (is_multicast_ether_addr(dst) &&
417 ether_addr_equal(src, addr)))
419 if (iftype == NL80211_IFTYPE_MESH_POINT) {
420 struct ieee80211s_hdr *meshdr =
421 (struct ieee80211s_hdr *) (skb->data + hdrlen);
422 /* make sure meshdr->flags is on the linear part */
423 if (!pskb_may_pull(skb, hdrlen + 1))
425 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
427 if (meshdr->flags & MESH_FLAGS_AE_A4)
428 skb_copy_bits(skb, hdrlen +
429 offsetof(struct ieee80211s_hdr, eaddr1),
431 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
435 if (iftype != NL80211_IFTYPE_ADHOC &&
436 iftype != NL80211_IFTYPE_STATION)
441 if (!pskb_may_pull(skb, hdrlen + 8))
444 payload = skb->data + hdrlen;
445 ethertype = (payload[6] << 8) | payload[7];
447 if (likely((ether_addr_equal(payload, rfc1042_header) &&
448 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
449 ether_addr_equal(payload, bridge_tunnel_header))) {
450 /* remove RFC1042 or Bridge-Tunnel encapsulation and
451 * replace EtherType */
452 skb_pull(skb, hdrlen + 6);
453 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
454 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
459 skb_pull(skb, hdrlen);
460 len = htons(skb->len);
461 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
462 memcpy(ehdr->h_dest, dst, ETH_ALEN);
463 memcpy(ehdr->h_source, src, ETH_ALEN);
468 EXPORT_SYMBOL(ieee80211_data_to_8023);
470 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
471 enum nl80211_iftype iftype, u8 *bssid, bool qos)
473 struct ieee80211_hdr hdr;
474 u16 hdrlen, ethertype;
476 const u8 *encaps_data;
477 int encaps_len, skip_header_bytes;
481 if (unlikely(skb->len < ETH_HLEN))
484 nh_pos = skb_network_header(skb) - skb->data;
485 h_pos = skb_transport_header(skb) - skb->data;
487 /* convert Ethernet header to proper 802.11 header (based on
489 ethertype = (skb->data[12] << 8) | skb->data[13];
490 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
493 case NL80211_IFTYPE_AP:
494 case NL80211_IFTYPE_AP_VLAN:
495 case NL80211_IFTYPE_P2P_GO:
496 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
498 memcpy(hdr.addr1, skb->data, ETH_ALEN);
499 memcpy(hdr.addr2, addr, ETH_ALEN);
500 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
503 case NL80211_IFTYPE_STATION:
504 case NL80211_IFTYPE_P2P_CLIENT:
505 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
507 memcpy(hdr.addr1, bssid, ETH_ALEN);
508 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
509 memcpy(hdr.addr3, skb->data, ETH_ALEN);
512 case NL80211_IFTYPE_ADHOC:
514 memcpy(hdr.addr1, skb->data, ETH_ALEN);
515 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
516 memcpy(hdr.addr3, bssid, ETH_ALEN);
524 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
528 hdr.frame_control = fc;
532 skip_header_bytes = ETH_HLEN;
533 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
534 encaps_data = bridge_tunnel_header;
535 encaps_len = sizeof(bridge_tunnel_header);
536 skip_header_bytes -= 2;
537 } else if (ethertype >= ETH_P_802_3_MIN) {
538 encaps_data = rfc1042_header;
539 encaps_len = sizeof(rfc1042_header);
540 skip_header_bytes -= 2;
546 skb_pull(skb, skip_header_bytes);
547 nh_pos -= skip_header_bytes;
548 h_pos -= skip_header_bytes;
550 head_need = hdrlen + encaps_len - skb_headroom(skb);
552 if (head_need > 0 || skb_cloned(skb)) {
553 head_need = max(head_need, 0);
557 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
560 skb->truesize += head_need;
564 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
565 nh_pos += encaps_len;
569 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
574 /* Update skb pointers to various headers since this modified frame
575 * is going to go through Linux networking code that may potentially
576 * need things like pointer to IP header. */
577 skb_set_mac_header(skb, 0);
578 skb_set_network_header(skb, nh_pos);
579 skb_set_transport_header(skb, h_pos);
583 EXPORT_SYMBOL(ieee80211_data_from_8023);
586 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
587 const u8 *addr, enum nl80211_iftype iftype,
588 const unsigned int extra_headroom,
589 bool has_80211_header)
591 struct sk_buff *frame = NULL;
594 const struct ethhdr *eth;
596 u8 dst[ETH_ALEN], src[ETH_ALEN];
598 if (has_80211_header) {
599 err = ieee80211_data_to_8023(skb, addr, iftype);
603 /* skip the wrapping header */
604 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
608 eth = (struct ethhdr *) skb->data;
611 while (skb != frame) {
613 __be16 len = eth->h_proto;
614 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
616 remaining = skb->len;
617 memcpy(dst, eth->h_dest, ETH_ALEN);
618 memcpy(src, eth->h_source, ETH_ALEN);
620 padding = (4 - subframe_len) & 0x3;
621 /* the last MSDU has no padding */
622 if (subframe_len > remaining)
625 skb_pull(skb, sizeof(struct ethhdr));
626 /* reuse skb for the last subframe */
627 if (remaining <= subframe_len + padding)
630 unsigned int hlen = ALIGN(extra_headroom, 4);
632 * Allocate and reserve two bytes more for payload
633 * alignment since sizeof(struct ethhdr) is 14.
635 frame = dev_alloc_skb(hlen + subframe_len + 2);
639 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
640 memcpy(skb_put(frame, ntohs(len)), skb->data,
643 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
646 dev_kfree_skb(frame);
651 skb_reset_network_header(frame);
652 frame->dev = skb->dev;
653 frame->priority = skb->priority;
655 payload = frame->data;
656 ethertype = (payload[6] << 8) | payload[7];
658 if (likely((ether_addr_equal(payload, rfc1042_header) &&
659 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
660 ether_addr_equal(payload, bridge_tunnel_header))) {
661 /* remove RFC1042 or Bridge-Tunnel
662 * encapsulation and replace EtherType */
664 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
665 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
667 memcpy(skb_push(frame, sizeof(__be16)), &len,
669 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
670 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
672 __skb_queue_tail(list, frame);
678 __skb_queue_purge(list);
682 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
684 /* Given a data frame determine the 802.1p/1d tag to use. */
685 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
689 /* skb->priority values from 256->263 are magic values to
690 * directly indicate a specific 802.1d priority. This is used
691 * to allow 802.1d priority to be passed directly in from VLAN
694 if (skb->priority >= 256 && skb->priority <= 263)
695 return skb->priority - 256;
697 switch (skb->protocol) {
698 case htons(ETH_P_IP):
699 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
701 case htons(ETH_P_IPV6):
702 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
710 EXPORT_SYMBOL(cfg80211_classify8021d);
712 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
714 const struct cfg80211_bss_ies *ies;
716 ies = rcu_dereference(bss->ies);
720 return cfg80211_find_ie(ie, ies->data, ies->len);
722 EXPORT_SYMBOL(ieee80211_bss_get_ie);
724 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
726 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
727 struct net_device *dev = wdev->netdev;
730 if (!wdev->connect_keys)
733 for (i = 0; i < 6; i++) {
734 if (!wdev->connect_keys->params[i].cipher)
736 if (rdev_add_key(rdev, dev, i, false, NULL,
737 &wdev->connect_keys->params[i])) {
738 netdev_err(dev, "failed to set key %d\n", i);
741 if (wdev->connect_keys->def == i)
742 if (rdev_set_default_key(rdev, dev, i, true, true)) {
743 netdev_err(dev, "failed to set defkey %d\n", i);
746 if (wdev->connect_keys->defmgmt == i)
747 if (rdev_set_default_mgmt_key(rdev, dev, i))
748 netdev_err(dev, "failed to set mgtdef %d\n", i);
751 kfree(wdev->connect_keys);
752 wdev->connect_keys = NULL;
755 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
757 struct cfg80211_event *ev;
759 const u8 *bssid = NULL;
761 spin_lock_irqsave(&wdev->event_lock, flags);
762 while (!list_empty(&wdev->event_list)) {
763 ev = list_first_entry(&wdev->event_list,
764 struct cfg80211_event, list);
766 spin_unlock_irqrestore(&wdev->event_lock, flags);
770 case EVENT_CONNECT_RESULT:
771 if (!is_zero_ether_addr(ev->cr.bssid))
772 bssid = ev->cr.bssid;
773 __cfg80211_connect_result(
775 ev->cr.req_ie, ev->cr.req_ie_len,
776 ev->cr.resp_ie, ev->cr.resp_ie_len,
778 ev->cr.status == WLAN_STATUS_SUCCESS,
782 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
783 ev->rm.req_ie_len, ev->rm.resp_ie,
786 case EVENT_DISCONNECTED:
787 __cfg80211_disconnected(wdev->netdev,
788 ev->dc.ie, ev->dc.ie_len,
789 ev->dc.reason, true);
791 case EVENT_IBSS_JOINED:
792 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
799 spin_lock_irqsave(&wdev->event_lock, flags);
801 spin_unlock_irqrestore(&wdev->event_lock, flags);
804 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
806 struct wireless_dev *wdev;
809 ASSERT_RDEV_LOCK(rdev);
811 list_for_each_entry(wdev, &rdev->wdev_list, list)
812 cfg80211_process_wdev_events(wdev);
815 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
816 struct net_device *dev, enum nl80211_iftype ntype,
817 u32 *flags, struct vif_params *params)
820 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
822 ASSERT_RDEV_LOCK(rdev);
824 /* don't support changing VLANs, you just re-create them */
825 if (otype == NL80211_IFTYPE_AP_VLAN)
828 /* cannot change into P2P device type */
829 if (ntype == NL80211_IFTYPE_P2P_DEVICE)
832 if (!rdev->ops->change_virtual_intf ||
833 !(rdev->wiphy.interface_modes & (1 << ntype)))
836 /* if it's part of a bridge, reject changing type to station/ibss */
837 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
838 (ntype == NL80211_IFTYPE_ADHOC ||
839 ntype == NL80211_IFTYPE_STATION ||
840 ntype == NL80211_IFTYPE_P2P_CLIENT))
843 if (ntype != otype && netif_running(dev)) {
844 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
849 dev->ieee80211_ptr->use_4addr = false;
850 dev->ieee80211_ptr->mesh_id_up_len = 0;
853 case NL80211_IFTYPE_AP:
854 cfg80211_stop_ap(rdev, dev);
856 case NL80211_IFTYPE_ADHOC:
857 cfg80211_leave_ibss(rdev, dev, false);
859 case NL80211_IFTYPE_STATION:
860 case NL80211_IFTYPE_P2P_CLIENT:
861 wdev_lock(dev->ieee80211_ptr);
862 cfg80211_disconnect(rdev, dev,
863 WLAN_REASON_DEAUTH_LEAVING, true);
864 wdev_unlock(dev->ieee80211_ptr);
866 case NL80211_IFTYPE_MESH_POINT:
867 /* mesh should be handled? */
873 cfg80211_process_rdev_events(rdev);
876 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
878 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
880 if (!err && params && params->use_4addr != -1)
881 dev->ieee80211_ptr->use_4addr = params->use_4addr;
884 dev->priv_flags &= ~IFF_DONT_BRIDGE;
886 case NL80211_IFTYPE_STATION:
887 if (dev->ieee80211_ptr->use_4addr)
890 case NL80211_IFTYPE_P2P_CLIENT:
891 case NL80211_IFTYPE_ADHOC:
892 dev->priv_flags |= IFF_DONT_BRIDGE;
894 case NL80211_IFTYPE_P2P_GO:
895 case NL80211_IFTYPE_AP:
896 case NL80211_IFTYPE_AP_VLAN:
897 case NL80211_IFTYPE_WDS:
898 case NL80211_IFTYPE_MESH_POINT:
901 case NL80211_IFTYPE_MONITOR:
902 /* monitor can't bridge anyway */
904 case NL80211_IFTYPE_UNSPECIFIED:
905 case NUM_NL80211_IFTYPES:
908 case NL80211_IFTYPE_P2P_DEVICE:
914 if (!err && ntype != otype && netif_running(dev)) {
915 cfg80211_update_iface_num(rdev, ntype, 1);
916 cfg80211_update_iface_num(rdev, otype, -1);
922 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
924 static const u32 __mcs2bitrate[] = {
932 [5] = 12512, /* 1251.25 mbps */
942 [14] = 8662, /* 866.25 mbps */
952 [24] = 67568, /* 6756.75 mbps */
963 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
966 return __mcs2bitrate[rate->mcs];
969 static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
971 static const u32 base[4][10] = {
1020 if (WARN_ON_ONCE(rate->mcs > 9))
1023 idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
1024 RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
1025 rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
1026 rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
1028 bitrate = base[idx][rate->mcs];
1029 bitrate *= rate->nss;
1031 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1032 bitrate = (bitrate / 9) * 10;
1034 /* do NOT round down here */
1035 return (bitrate + 50000) / 100000;
1038 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1040 int modulation, streams, bitrate;
1042 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1043 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
1044 return rate->legacy;
1045 if (rate->flags & RATE_INFO_FLAGS_60G)
1046 return cfg80211_calculate_bitrate_60g(rate);
1047 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1048 return cfg80211_calculate_bitrate_vht(rate);
1050 /* the formula below does only work for MCS values smaller than 32 */
1051 if (WARN_ON_ONCE(rate->mcs >= 32))
1054 modulation = rate->mcs & 7;
1055 streams = (rate->mcs >> 3) + 1;
1057 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1061 bitrate *= (modulation + 1);
1062 else if (modulation == 4)
1063 bitrate *= (modulation + 2);
1065 bitrate *= (modulation + 3);
1069 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1070 bitrate = (bitrate / 9) * 10;
1072 /* do NOT round down here */
1073 return (bitrate + 50000) / 100000;
1075 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1077 int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1078 enum ieee80211_p2p_attr_id attr,
1079 u8 *buf, unsigned int bufsize)
1082 u16 attr_remaining = 0;
1083 bool desired_attr = false;
1084 u16 desired_len = 0;
1087 unsigned int iedatalen;
1094 if (iedatalen + 2 > len)
1097 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1105 /* check WFA OUI, P2P subtype */
1106 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1107 iedata[2] != 0x9a || iedata[3] != 0x09)
1113 /* check attribute continuation into this IE */
1114 copy = min_t(unsigned int, attr_remaining, iedatalen);
1115 if (copy && desired_attr) {
1116 desired_len += copy;
1118 memcpy(out, iedata, min(bufsize, copy));
1119 out += min(bufsize, copy);
1120 bufsize -= min(bufsize, copy);
1124 if (copy == attr_remaining)
1128 attr_remaining -= copy;
1135 while (iedatalen > 0) {
1138 /* P2P attribute ID & size must fit */
1141 desired_attr = iedata[0] == attr;
1142 attr_len = get_unaligned_le16(iedata + 1);
1146 copy = min_t(unsigned int, attr_len, iedatalen);
1149 desired_len += copy;
1151 memcpy(out, iedata, min(bufsize, copy));
1152 out += min(bufsize, copy);
1153 bufsize -= min(bufsize, copy);
1156 if (copy == attr_len)
1162 attr_remaining = attr_len - copy;
1170 if (attr_remaining && desired_attr)
1175 EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1177 bool ieee80211_operating_class_to_band(u8 operating_class,
1178 enum ieee80211_band *band)
1180 switch (operating_class) {
1183 *band = IEEE80211_BAND_5GHZ;
1189 *band = IEEE80211_BAND_2GHZ;
1192 *band = IEEE80211_BAND_60GHZ;
1198 EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1200 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1203 struct wireless_dev *wdev;
1209 list_for_each_entry(wdev, &rdev->wdev_list, list) {
1210 if (!wdev->beacon_interval)
1212 if (wdev->beacon_interval != beacon_int) {
1221 int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1222 struct wireless_dev *wdev,
1223 enum nl80211_iftype iftype,
1224 struct ieee80211_channel *chan,
1225 enum cfg80211_chan_mode chanmode,
1228 struct wireless_dev *wdev_iter;
1229 u32 used_iftypes = BIT(iftype);
1230 int num[NUM_NL80211_IFTYPES];
1231 struct ieee80211_channel
1232 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1233 struct ieee80211_channel *ch;
1234 enum cfg80211_chan_mode chmode;
1235 int num_different_channels = 0;
1237 bool radar_required;
1242 if (WARN_ON(hweight32(radar_detect) > 1))
1246 case NL80211_IFTYPE_ADHOC:
1247 case NL80211_IFTYPE_AP:
1248 case NL80211_IFTYPE_AP_VLAN:
1249 case NL80211_IFTYPE_MESH_POINT:
1250 case NL80211_IFTYPE_P2P_GO:
1251 case NL80211_IFTYPE_WDS:
1252 radar_required = !!(chan &&
1253 (chan->flags & IEEE80211_CHAN_RADAR));
1255 case NL80211_IFTYPE_P2P_CLIENT:
1256 case NL80211_IFTYPE_STATION:
1257 case NL80211_IFTYPE_P2P_DEVICE:
1258 case NL80211_IFTYPE_MONITOR:
1259 radar_required = false;
1261 case NUM_NL80211_IFTYPES:
1262 case NL80211_IFTYPE_UNSPECIFIED:
1267 if (radar_required && !radar_detect)
1270 /* Always allow software iftypes */
1271 if (rdev->wiphy.software_iftypes & BIT(iftype)) {
1277 memset(num, 0, sizeof(num));
1278 memset(used_channels, 0, sizeof(used_channels));
1283 case CHAN_MODE_UNDEFINED:
1285 case CHAN_MODE_SHARED:
1287 used_channels[0] = chan;
1288 num_different_channels++;
1290 case CHAN_MODE_EXCLUSIVE:
1291 num_different_channels++;
1295 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
1296 if (wdev_iter == wdev)
1298 if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
1299 if (!wdev_iter->p2p_started)
1301 } else if (wdev_iter->netdev) {
1302 if (!netif_running(wdev_iter->netdev))
1308 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1312 * We may be holding the "wdev" mutex, but now need to lock
1313 * wdev_iter. This is OK because once we get here wdev_iter
1314 * is not wdev (tested above), but we need to use the nested
1315 * locking for lockdep.
1317 mutex_lock_nested(&wdev_iter->mtx, 1);
1318 __acquire(wdev_iter->mtx);
1319 cfg80211_get_chan_state(wdev_iter, &ch, &chmode);
1320 wdev_unlock(wdev_iter);
1323 case CHAN_MODE_UNDEFINED:
1325 case CHAN_MODE_SHARED:
1326 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1327 if (!used_channels[i] || used_channels[i] == ch)
1330 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
1333 if (used_channels[i] == NULL) {
1334 used_channels[i] = ch;
1335 num_different_channels++;
1338 case CHAN_MODE_EXCLUSIVE:
1339 num_different_channels++;
1343 num[wdev_iter->iftype]++;
1345 used_iftypes |= BIT(wdev_iter->iftype);
1348 if (total == 1 && !radar_detect)
1351 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1352 const struct ieee80211_iface_combination *c;
1353 struct ieee80211_iface_limit *limits;
1354 u32 all_iftypes = 0;
1356 c = &rdev->wiphy.iface_combinations[i];
1358 if (total > c->max_interfaces)
1360 if (num_different_channels > c->num_different_channels)
1363 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1368 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1369 if (rdev->wiphy.software_iftypes & BIT(iftype))
1371 for (j = 0; j < c->n_limits; j++) {
1372 all_iftypes |= limits[j].types;
1373 if (!(limits[j].types & BIT(iftype)))
1375 if (limits[j].max < num[iftype])
1377 limits[j].max -= num[iftype];
1381 if (radar_detect && !(c->radar_detect_widths & radar_detect))
1385 * Finally check that all iftypes that we're currently
1386 * using are actually part of this combination. If they
1387 * aren't then we can't use this combination and have
1388 * to continue to the next.
1390 if ((all_iftypes & used_iftypes) != used_iftypes)
1394 * This combination covered all interface types and
1395 * supported the requested numbers, so we're good.
1406 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1407 const u8 *rates, unsigned int n_rates,
1415 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1420 for (i = 0; i < n_rates; i++) {
1421 int rate = (rates[i] & 0x7f) * 5;
1424 for (j = 0; j < sband->n_bitrates; j++) {
1425 if (sband->bitrates[j].bitrate == rate) {
1436 * mask must have at least one bit set here since we
1437 * didn't accept a 0-length rates array nor allowed
1438 * entries in the array that didn't exist
1444 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1445 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1446 const unsigned char rfc1042_header[] __aligned(2) =
1447 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1448 EXPORT_SYMBOL(rfc1042_header);
1450 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1451 const unsigned char bridge_tunnel_header[] __aligned(2) =
1452 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1453 EXPORT_SYMBOL(bridge_tunnel_header);