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>
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 */
39 return 0; /* not supported */
41 case IEEE80211_BAND_2GHZ:
45 return 2407 + chan * 5;
47 case IEEE80211_BAND_5GHZ:
48 if (chan >= 182 && chan <= 196)
49 return 4000 + chan * 5;
51 return 5000 + chan * 5;
53 case IEEE80211_BAND_60GHZ:
55 return 56160 + chan * 2160;
60 return 0; /* not supported */
62 EXPORT_SYMBOL(ieee80211_channel_to_frequency);
64 int ieee80211_frequency_to_channel(int freq)
66 /* see 802.11 17.3.8.3.2 and Annex J */
70 return (freq - 2407) / 5;
71 else if (freq >= 4910 && freq <= 4980)
72 return (freq - 4000) / 5;
73 else if (freq <= 45000) /* DMG band lower limit */
74 return (freq - 5000) / 5;
75 else if (freq >= 58320 && freq <= 64800)
76 return (freq - 56160) / 2160;
80 EXPORT_SYMBOL(ieee80211_frequency_to_channel);
82 struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
85 enum ieee80211_band band;
86 struct ieee80211_supported_band *sband;
89 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
90 sband = wiphy->bands[band];
95 for (i = 0; i < sband->n_channels; i++) {
96 if (sband->channels[i].center_freq == freq)
97 return &sband->channels[i];
103 EXPORT_SYMBOL(__ieee80211_get_channel);
105 static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
106 enum ieee80211_band band)
111 case IEEE80211_BAND_5GHZ:
113 for (i = 0; i < sband->n_bitrates; i++) {
114 if (sband->bitrates[i].bitrate == 60 ||
115 sband->bitrates[i].bitrate == 120 ||
116 sband->bitrates[i].bitrate == 240) {
117 sband->bitrates[i].flags |=
118 IEEE80211_RATE_MANDATORY_A;
124 case IEEE80211_BAND_2GHZ:
126 for (i = 0; i < sband->n_bitrates; i++) {
127 if (sband->bitrates[i].bitrate == 10) {
128 sband->bitrates[i].flags |=
129 IEEE80211_RATE_MANDATORY_B |
130 IEEE80211_RATE_MANDATORY_G;
134 if (sband->bitrates[i].bitrate == 20 ||
135 sband->bitrates[i].bitrate == 55 ||
136 sband->bitrates[i].bitrate == 110 ||
137 sband->bitrates[i].bitrate == 60 ||
138 sband->bitrates[i].bitrate == 120 ||
139 sband->bitrates[i].bitrate == 240) {
140 sband->bitrates[i].flags |=
141 IEEE80211_RATE_MANDATORY_G;
145 if (sband->bitrates[i].bitrate != 10 &&
146 sband->bitrates[i].bitrate != 20 &&
147 sband->bitrates[i].bitrate != 55 &&
148 sband->bitrates[i].bitrate != 110)
149 sband->bitrates[i].flags |=
150 IEEE80211_RATE_ERP_G;
152 WARN_ON(want != 0 && want != 3 && want != 6);
154 case IEEE80211_BAND_60GHZ:
155 /* check for mandatory HT MCS 1..4 */
156 WARN_ON(!sband->ht_cap.ht_supported);
157 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
159 case IEEE80211_NUM_BANDS:
165 void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
167 enum ieee80211_band band;
169 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
170 if (wiphy->bands[band])
171 set_mandatory_flags_band(wiphy->bands[band], band);
174 bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
177 for (i = 0; i < wiphy->n_cipher_suites; i++)
178 if (cipher == wiphy->cipher_suites[i])
183 int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
184 struct key_params *params, int key_idx,
185 bool pairwise, const u8 *mac_addr)
190 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
193 if (pairwise && !mac_addr)
197 * Disallow pairwise keys with non-zero index unless it's WEP
198 * or a vendor specific cipher (because current deployments use
199 * pairwise WEP keys with non-zero indices and for vendor specific
200 * ciphers this should be validated in the driver or hardware level
201 * - but 802.11i clearly specifies to use zero)
203 if (pairwise && key_idx &&
204 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
205 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
206 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
209 switch (params->cipher) {
210 case WLAN_CIPHER_SUITE_WEP40:
211 if (params->key_len != WLAN_KEY_LEN_WEP40)
214 case WLAN_CIPHER_SUITE_TKIP:
215 if (params->key_len != WLAN_KEY_LEN_TKIP)
218 case WLAN_CIPHER_SUITE_CCMP:
219 if (params->key_len != WLAN_KEY_LEN_CCMP)
222 case WLAN_CIPHER_SUITE_WEP104:
223 if (params->key_len != WLAN_KEY_LEN_WEP104)
226 case WLAN_CIPHER_SUITE_AES_CMAC:
227 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
232 * We don't know anything about this algorithm,
233 * allow using it -- but the driver must check
234 * all parameters! We still check below whether
235 * or not the driver supports this algorithm,
242 switch (params->cipher) {
243 case WLAN_CIPHER_SUITE_WEP40:
244 case WLAN_CIPHER_SUITE_WEP104:
245 /* These ciphers do not use key sequence */
247 case WLAN_CIPHER_SUITE_TKIP:
248 case WLAN_CIPHER_SUITE_CCMP:
249 case WLAN_CIPHER_SUITE_AES_CMAC:
250 if (params->seq_len != 6)
256 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
262 unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
264 unsigned int hdrlen = 24;
266 if (ieee80211_is_data(fc)) {
267 if (ieee80211_has_a4(fc))
269 if (ieee80211_is_data_qos(fc)) {
270 hdrlen += IEEE80211_QOS_CTL_LEN;
271 if (ieee80211_has_order(fc))
272 hdrlen += IEEE80211_HT_CTL_LEN;
277 if (ieee80211_is_ctl(fc)) {
279 * ACK and CTS are 10 bytes, all others 16. To see how
280 * to get this condition consider
281 * subtype mask: 0b0000000011110000 (0x00F0)
282 * ACK subtype: 0b0000000011010000 (0x00D0)
283 * CTS subtype: 0b0000000011000000 (0x00C0)
284 * bits that matter: ^^^ (0x00E0)
285 * value of those: 0b0000000011000000 (0x00C0)
287 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
295 EXPORT_SYMBOL(ieee80211_hdrlen);
297 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
299 const struct ieee80211_hdr *hdr =
300 (const struct ieee80211_hdr *)skb->data;
303 if (unlikely(skb->len < 10))
305 hdrlen = ieee80211_hdrlen(hdr->frame_control);
306 if (unlikely(hdrlen > skb->len))
310 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
312 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
314 int ae = meshhdr->flags & MESH_FLAGS_AE;
315 /* 802.11-2012, 8.2.4.7.3 */
320 case MESH_FLAGS_AE_A4:
322 case MESH_FLAGS_AE_A5_A6:
327 int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
328 enum nl80211_iftype iftype)
330 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
331 u16 hdrlen, ethertype;
334 u8 src[ETH_ALEN] __aligned(2);
336 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
339 hdrlen = ieee80211_hdrlen(hdr->frame_control);
341 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
343 * IEEE 802.11 address fields:
344 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
345 * 0 0 DA SA BSSID n/a
346 * 0 1 DA BSSID SA n/a
347 * 1 0 BSSID SA DA n/a
350 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
351 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
353 switch (hdr->frame_control &
354 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
355 case cpu_to_le16(IEEE80211_FCTL_TODS):
356 if (unlikely(iftype != NL80211_IFTYPE_AP &&
357 iftype != NL80211_IFTYPE_AP_VLAN &&
358 iftype != NL80211_IFTYPE_P2P_GO))
361 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
362 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
363 iftype != NL80211_IFTYPE_MESH_POINT &&
364 iftype != NL80211_IFTYPE_AP_VLAN &&
365 iftype != NL80211_IFTYPE_STATION))
367 if (iftype == NL80211_IFTYPE_MESH_POINT) {
368 struct ieee80211s_hdr *meshdr =
369 (struct ieee80211s_hdr *) (skb->data + hdrlen);
370 /* make sure meshdr->flags is on the linear part */
371 if (!pskb_may_pull(skb, hdrlen + 1))
373 if (meshdr->flags & MESH_FLAGS_AE_A4)
375 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
376 skb_copy_bits(skb, hdrlen +
377 offsetof(struct ieee80211s_hdr, eaddr1),
379 skb_copy_bits(skb, hdrlen +
380 offsetof(struct ieee80211s_hdr, eaddr2),
383 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
386 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
387 if ((iftype != NL80211_IFTYPE_STATION &&
388 iftype != NL80211_IFTYPE_P2P_CLIENT &&
389 iftype != NL80211_IFTYPE_MESH_POINT) ||
390 (is_multicast_ether_addr(dst) &&
391 ether_addr_equal(src, addr)))
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_A5_A6)
401 if (meshdr->flags & MESH_FLAGS_AE_A4)
402 skb_copy_bits(skb, hdrlen +
403 offsetof(struct ieee80211s_hdr, eaddr1),
405 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
409 if (iftype != NL80211_IFTYPE_ADHOC &&
410 iftype != NL80211_IFTYPE_STATION)
415 if (!pskb_may_pull(skb, hdrlen + 8))
418 payload = skb->data + hdrlen;
419 ethertype = (payload[6] << 8) | payload[7];
421 if (likely((ether_addr_equal(payload, rfc1042_header) &&
422 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
423 ether_addr_equal(payload, bridge_tunnel_header))) {
424 /* remove RFC1042 or Bridge-Tunnel encapsulation and
425 * replace EtherType */
426 skb_pull(skb, hdrlen + 6);
427 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
428 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
433 skb_pull(skb, hdrlen);
434 len = htons(skb->len);
435 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
436 memcpy(ehdr->h_dest, dst, ETH_ALEN);
437 memcpy(ehdr->h_source, src, ETH_ALEN);
442 EXPORT_SYMBOL(ieee80211_data_to_8023);
444 int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
445 enum nl80211_iftype iftype, u8 *bssid, bool qos)
447 struct ieee80211_hdr hdr;
448 u16 hdrlen, ethertype;
450 const u8 *encaps_data;
451 int encaps_len, skip_header_bytes;
455 if (unlikely(skb->len < ETH_HLEN))
458 nh_pos = skb_network_header(skb) - skb->data;
459 h_pos = skb_transport_header(skb) - skb->data;
461 /* convert Ethernet header to proper 802.11 header (based on
463 ethertype = (skb->data[12] << 8) | skb->data[13];
464 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
467 case NL80211_IFTYPE_AP:
468 case NL80211_IFTYPE_AP_VLAN:
469 case NL80211_IFTYPE_P2P_GO:
470 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
472 memcpy(hdr.addr1, skb->data, ETH_ALEN);
473 memcpy(hdr.addr2, addr, ETH_ALEN);
474 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
477 case NL80211_IFTYPE_STATION:
478 case NL80211_IFTYPE_P2P_CLIENT:
479 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
481 memcpy(hdr.addr1, bssid, ETH_ALEN);
482 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
483 memcpy(hdr.addr3, skb->data, ETH_ALEN);
486 case NL80211_IFTYPE_ADHOC:
488 memcpy(hdr.addr1, skb->data, ETH_ALEN);
489 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
490 memcpy(hdr.addr3, bssid, ETH_ALEN);
498 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
502 hdr.frame_control = fc;
506 skip_header_bytes = ETH_HLEN;
507 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
508 encaps_data = bridge_tunnel_header;
509 encaps_len = sizeof(bridge_tunnel_header);
510 skip_header_bytes -= 2;
511 } else if (ethertype > 0x600) {
512 encaps_data = rfc1042_header;
513 encaps_len = sizeof(rfc1042_header);
514 skip_header_bytes -= 2;
520 skb_pull(skb, skip_header_bytes);
521 nh_pos -= skip_header_bytes;
522 h_pos -= skip_header_bytes;
524 head_need = hdrlen + encaps_len - skb_headroom(skb);
526 if (head_need > 0 || skb_cloned(skb)) {
527 head_need = max(head_need, 0);
531 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
534 skb->truesize += head_need;
538 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
539 nh_pos += encaps_len;
543 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
548 /* Update skb pointers to various headers since this modified frame
549 * is going to go through Linux networking code that may potentially
550 * need things like pointer to IP header. */
551 skb_set_mac_header(skb, 0);
552 skb_set_network_header(skb, nh_pos);
553 skb_set_transport_header(skb, h_pos);
557 EXPORT_SYMBOL(ieee80211_data_from_8023);
560 void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
561 const u8 *addr, enum nl80211_iftype iftype,
562 const unsigned int extra_headroom,
563 bool has_80211_header)
565 struct sk_buff *frame = NULL;
568 const struct ethhdr *eth;
570 u8 dst[ETH_ALEN], src[ETH_ALEN];
572 if (has_80211_header) {
573 err = ieee80211_data_to_8023(skb, addr, iftype);
577 /* skip the wrapping header */
578 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
582 eth = (struct ethhdr *) skb->data;
585 while (skb != frame) {
587 __be16 len = eth->h_proto;
588 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
590 remaining = skb->len;
591 memcpy(dst, eth->h_dest, ETH_ALEN);
592 memcpy(src, eth->h_source, ETH_ALEN);
594 padding = (4 - subframe_len) & 0x3;
595 /* the last MSDU has no padding */
596 if (subframe_len > remaining)
599 skb_pull(skb, sizeof(struct ethhdr));
600 /* reuse skb for the last subframe */
601 if (remaining <= subframe_len + padding)
604 unsigned int hlen = ALIGN(extra_headroom, 4);
606 * Allocate and reserve two bytes more for payload
607 * alignment since sizeof(struct ethhdr) is 14.
609 frame = dev_alloc_skb(hlen + subframe_len + 2);
613 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
614 memcpy(skb_put(frame, ntohs(len)), skb->data,
617 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
620 dev_kfree_skb(frame);
625 skb_reset_network_header(frame);
626 frame->dev = skb->dev;
627 frame->priority = skb->priority;
629 payload = frame->data;
630 ethertype = (payload[6] << 8) | payload[7];
632 if (likely((ether_addr_equal(payload, rfc1042_header) &&
633 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
634 ether_addr_equal(payload, bridge_tunnel_header))) {
635 /* remove RFC1042 or Bridge-Tunnel
636 * encapsulation and replace EtherType */
638 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
639 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
641 memcpy(skb_push(frame, sizeof(__be16)), &len,
643 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
644 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
646 __skb_queue_tail(list, frame);
652 __skb_queue_purge(list);
656 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
658 /* Given a data frame determine the 802.1p/1d tag to use. */
659 unsigned int cfg80211_classify8021d(struct sk_buff *skb)
663 /* skb->priority values from 256->263 are magic values to
664 * directly indicate a specific 802.1d priority. This is used
665 * to allow 802.1d priority to be passed directly in from VLAN
668 if (skb->priority >= 256 && skb->priority <= 263)
669 return skb->priority - 256;
671 switch (skb->protocol) {
672 case htons(ETH_P_IP):
673 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
675 case htons(ETH_P_IPV6):
676 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
684 EXPORT_SYMBOL(cfg80211_classify8021d);
686 const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
688 if (bss->information_elements == NULL)
690 return cfg80211_find_ie(ie, bss->information_elements,
691 bss->len_information_elements);
693 EXPORT_SYMBOL(ieee80211_bss_get_ie);
695 void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
697 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
698 struct net_device *dev = wdev->netdev;
701 if (!wdev->connect_keys)
704 for (i = 0; i < 6; i++) {
705 if (!wdev->connect_keys->params[i].cipher)
707 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
708 &wdev->connect_keys->params[i])) {
709 netdev_err(dev, "failed to set key %d\n", i);
712 if (wdev->connect_keys->def == i)
713 if (rdev->ops->set_default_key(wdev->wiphy, dev,
715 netdev_err(dev, "failed to set defkey %d\n", i);
718 if (wdev->connect_keys->defmgmt == i)
719 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
720 netdev_err(dev, "failed to set mgtdef %d\n", i);
723 kfree(wdev->connect_keys);
724 wdev->connect_keys = NULL;
727 void cfg80211_process_wdev_events(struct wireless_dev *wdev)
729 struct cfg80211_event *ev;
731 const u8 *bssid = NULL;
733 spin_lock_irqsave(&wdev->event_lock, flags);
734 while (!list_empty(&wdev->event_list)) {
735 ev = list_first_entry(&wdev->event_list,
736 struct cfg80211_event, list);
738 spin_unlock_irqrestore(&wdev->event_lock, flags);
742 case EVENT_CONNECT_RESULT:
743 if (!is_zero_ether_addr(ev->cr.bssid))
744 bssid = ev->cr.bssid;
745 __cfg80211_connect_result(
747 ev->cr.req_ie, ev->cr.req_ie_len,
748 ev->cr.resp_ie, ev->cr.resp_ie_len,
750 ev->cr.status == WLAN_STATUS_SUCCESS,
754 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
755 ev->rm.req_ie_len, ev->rm.resp_ie,
758 case EVENT_DISCONNECTED:
759 __cfg80211_disconnected(wdev->netdev,
760 ev->dc.ie, ev->dc.ie_len,
761 ev->dc.reason, true);
763 case EVENT_IBSS_JOINED:
764 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
771 spin_lock_irqsave(&wdev->event_lock, flags);
773 spin_unlock_irqrestore(&wdev->event_lock, flags);
776 void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
778 struct wireless_dev *wdev;
781 ASSERT_RDEV_LOCK(rdev);
783 mutex_lock(&rdev->devlist_mtx);
785 list_for_each_entry(wdev, &rdev->wdev_list, list)
786 cfg80211_process_wdev_events(wdev);
788 mutex_unlock(&rdev->devlist_mtx);
791 int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
792 struct net_device *dev, enum nl80211_iftype ntype,
793 u32 *flags, struct vif_params *params)
796 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
798 ASSERT_RDEV_LOCK(rdev);
800 /* don't support changing VLANs, you just re-create them */
801 if (otype == NL80211_IFTYPE_AP_VLAN)
804 /* cannot change into P2P device type */
805 if (ntype == NL80211_IFTYPE_P2P_DEVICE)
808 if (!rdev->ops->change_virtual_intf ||
809 !(rdev->wiphy.interface_modes & (1 << ntype)))
812 /* if it's part of a bridge, reject changing type to station/ibss */
813 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
814 (ntype == NL80211_IFTYPE_ADHOC ||
815 ntype == NL80211_IFTYPE_STATION ||
816 ntype == NL80211_IFTYPE_P2P_CLIENT))
819 if (ntype != otype && netif_running(dev)) {
820 mutex_lock(&rdev->devlist_mtx);
821 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
823 mutex_unlock(&rdev->devlist_mtx);
827 dev->ieee80211_ptr->use_4addr = false;
828 dev->ieee80211_ptr->mesh_id_up_len = 0;
831 case NL80211_IFTYPE_AP:
832 cfg80211_stop_ap(rdev, dev);
834 case NL80211_IFTYPE_ADHOC:
835 cfg80211_leave_ibss(rdev, dev, false);
837 case NL80211_IFTYPE_STATION:
838 case NL80211_IFTYPE_P2P_CLIENT:
839 cfg80211_disconnect(rdev, dev,
840 WLAN_REASON_DEAUTH_LEAVING, true);
842 case NL80211_IFTYPE_MESH_POINT:
843 /* mesh should be handled? */
849 cfg80211_process_rdev_events(rdev);
852 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
853 ntype, flags, params);
855 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
857 if (!err && params && params->use_4addr != -1)
858 dev->ieee80211_ptr->use_4addr = params->use_4addr;
861 dev->priv_flags &= ~IFF_DONT_BRIDGE;
863 case NL80211_IFTYPE_STATION:
864 if (dev->ieee80211_ptr->use_4addr)
867 case NL80211_IFTYPE_P2P_CLIENT:
868 case NL80211_IFTYPE_ADHOC:
869 dev->priv_flags |= IFF_DONT_BRIDGE;
871 case NL80211_IFTYPE_P2P_GO:
872 case NL80211_IFTYPE_AP:
873 case NL80211_IFTYPE_AP_VLAN:
874 case NL80211_IFTYPE_WDS:
875 case NL80211_IFTYPE_MESH_POINT:
878 case NL80211_IFTYPE_MONITOR:
879 /* monitor can't bridge anyway */
881 case NL80211_IFTYPE_UNSPECIFIED:
882 case NUM_NL80211_IFTYPES:
885 case NL80211_IFTYPE_P2P_DEVICE:
891 if (!err && ntype != otype && netif_running(dev)) {
892 cfg80211_update_iface_num(rdev, ntype, 1);
893 cfg80211_update_iface_num(rdev, otype, -1);
899 static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
901 static const u32 __mcs2bitrate[] = {
909 [5] = 12512, /* 1251.25 mbps */
919 [14] = 8662, /* 866.25 mbps */
929 [24] = 67568, /* 6756.75 mbps */
940 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
943 return __mcs2bitrate[rate->mcs];
946 u32 cfg80211_calculate_bitrate(struct rate_info *rate)
948 int modulation, streams, bitrate;
950 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
952 if (rate->flags & RATE_INFO_FLAGS_60G)
953 return cfg80211_calculate_bitrate_60g(rate);
955 /* the formula below does only work for MCS values smaller than 32 */
956 if (WARN_ON_ONCE(rate->mcs >= 32))
959 modulation = rate->mcs & 7;
960 streams = (rate->mcs >> 3) + 1;
962 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
966 bitrate *= (modulation + 1);
967 else if (modulation == 4)
968 bitrate *= (modulation + 2);
970 bitrate *= (modulation + 3);
974 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
975 bitrate = (bitrate / 9) * 10;
977 /* do NOT round down here */
978 return (bitrate + 50000) / 100000;
980 EXPORT_SYMBOL(cfg80211_calculate_bitrate);
982 int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
985 struct wireless_dev *wdev;
991 mutex_lock(&rdev->devlist_mtx);
993 list_for_each_entry(wdev, &rdev->wdev_list, list) {
994 if (!wdev->beacon_interval)
996 if (wdev->beacon_interval != beacon_int) {
1002 mutex_unlock(&rdev->devlist_mtx);
1007 int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1008 struct wireless_dev *wdev,
1009 enum nl80211_iftype iftype,
1010 struct ieee80211_channel *chan,
1011 enum cfg80211_chan_mode chanmode)
1013 struct wireless_dev *wdev_iter;
1014 u32 used_iftypes = BIT(iftype);
1015 int num[NUM_NL80211_IFTYPES];
1016 struct ieee80211_channel
1017 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1018 struct ieee80211_channel *ch;
1019 enum cfg80211_chan_mode chmode;
1020 int num_different_channels = 0;
1025 lockdep_assert_held(&rdev->devlist_mtx);
1027 /* Always allow software iftypes */
1028 if (rdev->wiphy.software_iftypes & BIT(iftype))
1031 memset(num, 0, sizeof(num));
1032 memset(used_channels, 0, sizeof(used_channels));
1037 case CHAN_MODE_UNDEFINED:
1039 case CHAN_MODE_SHARED:
1041 used_channels[0] = chan;
1042 num_different_channels++;
1044 case CHAN_MODE_EXCLUSIVE:
1045 num_different_channels++;
1049 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
1050 if (wdev_iter == wdev)
1052 if (wdev_iter->netdev) {
1053 if (!netif_running(wdev_iter->netdev))
1055 } else if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
1056 if (!wdev_iter->p2p_started)
1062 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1066 * We may be holding the "wdev" mutex, but now need to lock
1067 * wdev_iter. This is OK because once we get here wdev_iter
1068 * is not wdev (tested above), but we need to use the nested
1069 * locking for lockdep.
1071 mutex_lock_nested(&wdev_iter->mtx, 1);
1072 __acquire(wdev_iter->mtx);
1073 cfg80211_get_chan_state(wdev_iter, &ch, &chmode);
1074 wdev_unlock(wdev_iter);
1077 case CHAN_MODE_UNDEFINED:
1079 case CHAN_MODE_SHARED:
1080 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1081 if (!used_channels[i] || used_channels[i] == ch)
1084 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
1087 if (used_channels[i] == NULL) {
1088 used_channels[i] = ch;
1089 num_different_channels++;
1092 case CHAN_MODE_EXCLUSIVE:
1093 num_different_channels++;
1097 num[wdev_iter->iftype]++;
1099 used_iftypes |= BIT(wdev_iter->iftype);
1105 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1106 const struct ieee80211_iface_combination *c;
1107 struct ieee80211_iface_limit *limits;
1108 u32 all_iftypes = 0;
1110 c = &rdev->wiphy.iface_combinations[i];
1112 if (total > c->max_interfaces)
1114 if (num_different_channels > c->num_different_channels)
1117 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1122 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1123 if (rdev->wiphy.software_iftypes & BIT(iftype))
1125 for (j = 0; j < c->n_limits; j++) {
1126 all_iftypes |= limits[j].types;
1127 if (!(limits[j].types & BIT(iftype)))
1129 if (limits[j].max < num[iftype])
1131 limits[j].max -= num[iftype];
1136 * Finally check that all iftypes that we're currently
1137 * using are actually part of this combination. If they
1138 * aren't then we can't use this combination and have
1139 * to continue to the next.
1141 if ((all_iftypes & used_iftypes) != used_iftypes)
1145 * This combination covered all interface types and
1146 * supported the requested numbers, so we're good.
1157 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1158 const u8 *rates, unsigned int n_rates,
1166 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1171 for (i = 0; i < n_rates; i++) {
1172 int rate = (rates[i] & 0x7f) * 5;
1175 for (j = 0; j < sband->n_bitrates; j++) {
1176 if (sband->bitrates[j].bitrate == rate) {
1187 * mask must have at least one bit set here since we
1188 * didn't accept a 0-length rates array nor allowed
1189 * entries in the array that didn't exist
1195 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1196 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1197 const unsigned char rfc1042_header[] __aligned(2) =
1198 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1199 EXPORT_SYMBOL(rfc1042_header);
1201 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1202 const unsigned char bridge_tunnel_header[] __aligned(2) =
1203 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1204 EXPORT_SYMBOL(bridge_tunnel_header);