2 * Wireless utility functions
4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
6 #include <linux/bitops.h>
7 #include <linux/etherdevice.h>
8 #include <linux/slab.h>
9 #include <net/cfg80211.h>
13 struct ieee80211_rate
*
14 ieee80211_get_response_rate(struct ieee80211_supported_band
*sband
,
15 u32 basic_rates
, int bitrate
)
17 struct ieee80211_rate
*result
= &sband
->bitrates
[0];
20 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
21 if (!(basic_rates
& BIT(i
)))
23 if (sband
->bitrates
[i
].bitrate
> bitrate
)
25 result
= &sband
->bitrates
[i
];
30 EXPORT_SYMBOL(ieee80211_get_response_rate
);
32 int ieee80211_channel_to_frequency(int chan
, enum ieee80211_band band
)
34 /* see 802.11 17.3.8.3.2 and Annex J
35 * there are overlapping channel numbers in 5GHz and 2GHz bands */
36 if (band
== IEEE80211_BAND_5GHZ
) {
37 if (chan
>= 182 && chan
<= 196)
38 return 4000 + chan
* 5;
40 return 5000 + chan
* 5;
41 } else { /* IEEE80211_BAND_2GHZ */
45 return 2407 + chan
* 5;
47 return 0; /* not supported */
50 EXPORT_SYMBOL(ieee80211_channel_to_frequency
);
52 int ieee80211_frequency_to_channel(int freq
)
54 /* see 802.11 17.3.8.3.2 and Annex J */
58 return (freq
- 2407) / 5;
59 else if (freq
>= 4910 && freq
<= 4980)
60 return (freq
- 4000) / 5;
62 return (freq
- 5000) / 5;
64 EXPORT_SYMBOL(ieee80211_frequency_to_channel
);
66 struct ieee80211_channel
*__ieee80211_get_channel(struct wiphy
*wiphy
,
69 enum ieee80211_band band
;
70 struct ieee80211_supported_band
*sband
;
73 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
74 sband
= wiphy
->bands
[band
];
79 for (i
= 0; i
< sband
->n_channels
; i
++) {
80 if (sband
->channels
[i
].center_freq
== freq
)
81 return &sband
->channels
[i
];
87 EXPORT_SYMBOL(__ieee80211_get_channel
);
89 static void set_mandatory_flags_band(struct ieee80211_supported_band
*sband
,
90 enum ieee80211_band band
)
95 case IEEE80211_BAND_5GHZ
:
97 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
98 if (sband
->bitrates
[i
].bitrate
== 60 ||
99 sband
->bitrates
[i
].bitrate
== 120 ||
100 sband
->bitrates
[i
].bitrate
== 240) {
101 sband
->bitrates
[i
].flags
|=
102 IEEE80211_RATE_MANDATORY_A
;
108 case IEEE80211_BAND_2GHZ
:
110 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
111 if (sband
->bitrates
[i
].bitrate
== 10) {
112 sband
->bitrates
[i
].flags
|=
113 IEEE80211_RATE_MANDATORY_B
|
114 IEEE80211_RATE_MANDATORY_G
;
118 if (sband
->bitrates
[i
].bitrate
== 20 ||
119 sband
->bitrates
[i
].bitrate
== 55 ||
120 sband
->bitrates
[i
].bitrate
== 110 ||
121 sband
->bitrates
[i
].bitrate
== 60 ||
122 sband
->bitrates
[i
].bitrate
== 120 ||
123 sband
->bitrates
[i
].bitrate
== 240) {
124 sband
->bitrates
[i
].flags
|=
125 IEEE80211_RATE_MANDATORY_G
;
129 if (sband
->bitrates
[i
].bitrate
!= 10 &&
130 sband
->bitrates
[i
].bitrate
!= 20 &&
131 sband
->bitrates
[i
].bitrate
!= 55 &&
132 sband
->bitrates
[i
].bitrate
!= 110)
133 sband
->bitrates
[i
].flags
|=
134 IEEE80211_RATE_ERP_G
;
136 WARN_ON(want
!= 0 && want
!= 3 && want
!= 6);
138 case IEEE80211_NUM_BANDS
:
144 void ieee80211_set_bitrate_flags(struct wiphy
*wiphy
)
146 enum ieee80211_band band
;
148 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++)
149 if (wiphy
->bands
[band
])
150 set_mandatory_flags_band(wiphy
->bands
[band
], band
);
153 int cfg80211_validate_key_settings(struct cfg80211_registered_device
*rdev
,
154 struct key_params
*params
, int key_idx
,
155 bool pairwise
, const u8
*mac_addr
)
162 if (!pairwise
&& mac_addr
&& !(rdev
->wiphy
.flags
& WIPHY_FLAG_IBSS_RSN
))
165 if (pairwise
&& !mac_addr
)
169 * Disallow pairwise keys with non-zero index unless it's WEP
170 * or a vendor specific cipher (because current deployments use
171 * pairwise WEP keys with non-zero indices and for vendor specific
172 * ciphers this should be validated in the driver or hardware level
173 * - but 802.11i clearly specifies to use zero)
175 if (pairwise
&& key_idx
&&
176 ((params
->cipher
== WLAN_CIPHER_SUITE_TKIP
) ||
177 (params
->cipher
== WLAN_CIPHER_SUITE_CCMP
) ||
178 (params
->cipher
== WLAN_CIPHER_SUITE_AES_CMAC
)))
181 switch (params
->cipher
) {
182 case WLAN_CIPHER_SUITE_WEP40
:
183 if (params
->key_len
!= WLAN_KEY_LEN_WEP40
)
186 case WLAN_CIPHER_SUITE_TKIP
:
187 if (params
->key_len
!= WLAN_KEY_LEN_TKIP
)
190 case WLAN_CIPHER_SUITE_CCMP
:
191 if (params
->key_len
!= WLAN_KEY_LEN_CCMP
)
194 case WLAN_CIPHER_SUITE_WEP104
:
195 if (params
->key_len
!= WLAN_KEY_LEN_WEP104
)
198 case WLAN_CIPHER_SUITE_AES_CMAC
:
199 if (params
->key_len
!= WLAN_KEY_LEN_AES_CMAC
)
204 * We don't know anything about this algorithm,
205 * allow using it -- but the driver must check
206 * all parameters! We still check below whether
207 * or not the driver supports this algorithm,
214 switch (params
->cipher
) {
215 case WLAN_CIPHER_SUITE_WEP40
:
216 case WLAN_CIPHER_SUITE_WEP104
:
217 /* These ciphers do not use key sequence */
219 case WLAN_CIPHER_SUITE_TKIP
:
220 case WLAN_CIPHER_SUITE_CCMP
:
221 case WLAN_CIPHER_SUITE_AES_CMAC
:
222 if (params
->seq_len
!= 6)
228 for (i
= 0; i
< rdev
->wiphy
.n_cipher_suites
; i
++)
229 if (params
->cipher
== rdev
->wiphy
.cipher_suites
[i
])
231 if (i
== rdev
->wiphy
.n_cipher_suites
)
237 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
238 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
239 const unsigned char rfc1042_header
[] __aligned(2) =
240 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
241 EXPORT_SYMBOL(rfc1042_header
);
243 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
244 const unsigned char bridge_tunnel_header
[] __aligned(2) =
245 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
246 EXPORT_SYMBOL(bridge_tunnel_header
);
248 unsigned int __attribute_const__
ieee80211_hdrlen(__le16 fc
)
250 unsigned int hdrlen
= 24;
252 if (ieee80211_is_data(fc
)) {
253 if (ieee80211_has_a4(fc
))
255 if (ieee80211_is_data_qos(fc
)) {
256 hdrlen
+= IEEE80211_QOS_CTL_LEN
;
257 if (ieee80211_has_order(fc
))
258 hdrlen
+= IEEE80211_HT_CTL_LEN
;
263 if (ieee80211_is_ctl(fc
)) {
265 * ACK and CTS are 10 bytes, all others 16. To see how
266 * to get this condition consider
267 * subtype mask: 0b0000000011110000 (0x00F0)
268 * ACK subtype: 0b0000000011010000 (0x00D0)
269 * CTS subtype: 0b0000000011000000 (0x00C0)
270 * bits that matter: ^^^ (0x00E0)
271 * value of those: 0b0000000011000000 (0x00C0)
273 if ((fc
& cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
281 EXPORT_SYMBOL(ieee80211_hdrlen
);
283 unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff
*skb
)
285 const struct ieee80211_hdr
*hdr
=
286 (const struct ieee80211_hdr
*)skb
->data
;
289 if (unlikely(skb
->len
< 10))
291 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
292 if (unlikely(hdrlen
> skb
->len
))
296 EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb
);
298 static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr
*meshhdr
)
300 int ae
= meshhdr
->flags
& MESH_FLAGS_AE
;
305 case MESH_FLAGS_AE_A4
:
307 case MESH_FLAGS_AE_A5_A6
:
309 case (MESH_FLAGS_AE_A4
| MESH_FLAGS_AE_A5_A6
):
316 int ieee80211_data_to_8023(struct sk_buff
*skb
, const u8
*addr
,
317 enum nl80211_iftype iftype
)
319 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
320 u16 hdrlen
, ethertype
;
323 u8 src
[ETH_ALEN
] __aligned(2);
325 if (unlikely(!ieee80211_is_data_present(hdr
->frame_control
)))
328 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
330 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
332 * IEEE 802.11 address fields:
333 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
334 * 0 0 DA SA BSSID n/a
335 * 0 1 DA BSSID SA n/a
336 * 1 0 BSSID SA DA n/a
339 memcpy(dst
, ieee80211_get_DA(hdr
), ETH_ALEN
);
340 memcpy(src
, ieee80211_get_SA(hdr
), ETH_ALEN
);
342 switch (hdr
->frame_control
&
343 cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) {
344 case cpu_to_le16(IEEE80211_FCTL_TODS
):
345 if (unlikely(iftype
!= NL80211_IFTYPE_AP
&&
346 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
347 iftype
!= NL80211_IFTYPE_P2P_GO
))
350 case cpu_to_le16(IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
):
351 if (unlikely(iftype
!= NL80211_IFTYPE_WDS
&&
352 iftype
!= NL80211_IFTYPE_MESH_POINT
&&
353 iftype
!= NL80211_IFTYPE_AP_VLAN
&&
354 iftype
!= NL80211_IFTYPE_STATION
))
356 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
357 struct ieee80211s_hdr
*meshdr
=
358 (struct ieee80211s_hdr
*) (skb
->data
+ hdrlen
);
359 /* make sure meshdr->flags is on the linear part */
360 if (!pskb_may_pull(skb
, hdrlen
+ 1))
362 if (meshdr
->flags
& MESH_FLAGS_AE_A5_A6
) {
363 skb_copy_bits(skb
, hdrlen
+
364 offsetof(struct ieee80211s_hdr
, eaddr1
),
366 skb_copy_bits(skb
, hdrlen
+
367 offsetof(struct ieee80211s_hdr
, eaddr2
),
370 hdrlen
+= ieee80211_get_mesh_hdrlen(meshdr
);
373 case cpu_to_le16(IEEE80211_FCTL_FROMDS
):
374 if ((iftype
!= NL80211_IFTYPE_STATION
&&
375 iftype
!= NL80211_IFTYPE_P2P_CLIENT
&&
376 iftype
!= NL80211_IFTYPE_MESH_POINT
) ||
377 (is_multicast_ether_addr(dst
) &&
378 !compare_ether_addr(src
, addr
)))
380 if (iftype
== NL80211_IFTYPE_MESH_POINT
) {
381 struct ieee80211s_hdr
*meshdr
=
382 (struct ieee80211s_hdr
*) (skb
->data
+ hdrlen
);
383 /* make sure meshdr->flags is on the linear part */
384 if (!pskb_may_pull(skb
, hdrlen
+ 1))
386 if (meshdr
->flags
& MESH_FLAGS_AE_A4
)
387 skb_copy_bits(skb
, hdrlen
+
388 offsetof(struct ieee80211s_hdr
, eaddr1
),
390 hdrlen
+= ieee80211_get_mesh_hdrlen(meshdr
);
394 if (iftype
!= NL80211_IFTYPE_ADHOC
)
399 if (!pskb_may_pull(skb
, hdrlen
+ 8))
402 payload
= skb
->data
+ hdrlen
;
403 ethertype
= (payload
[6] << 8) | payload
[7];
405 if (likely((compare_ether_addr(payload
, rfc1042_header
) == 0 &&
406 ethertype
!= ETH_P_AARP
&& ethertype
!= ETH_P_IPX
) ||
407 compare_ether_addr(payload
, bridge_tunnel_header
) == 0)) {
408 /* remove RFC1042 or Bridge-Tunnel encapsulation and
409 * replace EtherType */
410 skb_pull(skb
, hdrlen
+ 6);
411 memcpy(skb_push(skb
, ETH_ALEN
), src
, ETH_ALEN
);
412 memcpy(skb_push(skb
, ETH_ALEN
), dst
, ETH_ALEN
);
417 skb_pull(skb
, hdrlen
);
418 len
= htons(skb
->len
);
419 ehdr
= (struct ethhdr
*) skb_push(skb
, sizeof(struct ethhdr
));
420 memcpy(ehdr
->h_dest
, dst
, ETH_ALEN
);
421 memcpy(ehdr
->h_source
, src
, ETH_ALEN
);
426 EXPORT_SYMBOL(ieee80211_data_to_8023
);
428 int ieee80211_data_from_8023(struct sk_buff
*skb
, const u8
*addr
,
429 enum nl80211_iftype iftype
, u8
*bssid
, bool qos
)
431 struct ieee80211_hdr hdr
;
432 u16 hdrlen
, ethertype
;
434 const u8
*encaps_data
;
435 int encaps_len
, skip_header_bytes
;
439 if (unlikely(skb
->len
< ETH_HLEN
))
442 nh_pos
= skb_network_header(skb
) - skb
->data
;
443 h_pos
= skb_transport_header(skb
) - skb
->data
;
445 /* convert Ethernet header to proper 802.11 header (based on
447 ethertype
= (skb
->data
[12] << 8) | skb
->data
[13];
448 fc
= cpu_to_le16(IEEE80211_FTYPE_DATA
| IEEE80211_STYPE_DATA
);
451 case NL80211_IFTYPE_AP
:
452 case NL80211_IFTYPE_AP_VLAN
:
453 case NL80211_IFTYPE_P2P_GO
:
454 fc
|= cpu_to_le16(IEEE80211_FCTL_FROMDS
);
456 memcpy(hdr
.addr1
, skb
->data
, ETH_ALEN
);
457 memcpy(hdr
.addr2
, addr
, ETH_ALEN
);
458 memcpy(hdr
.addr3
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
461 case NL80211_IFTYPE_STATION
:
462 case NL80211_IFTYPE_P2P_CLIENT
:
463 fc
|= cpu_to_le16(IEEE80211_FCTL_TODS
);
465 memcpy(hdr
.addr1
, bssid
, ETH_ALEN
);
466 memcpy(hdr
.addr2
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
467 memcpy(hdr
.addr3
, skb
->data
, ETH_ALEN
);
470 case NL80211_IFTYPE_ADHOC
:
472 memcpy(hdr
.addr1
, skb
->data
, ETH_ALEN
);
473 memcpy(hdr
.addr2
, skb
->data
+ ETH_ALEN
, ETH_ALEN
);
474 memcpy(hdr
.addr3
, bssid
, ETH_ALEN
);
482 fc
|= cpu_to_le16(IEEE80211_STYPE_QOS_DATA
);
486 hdr
.frame_control
= fc
;
490 skip_header_bytes
= ETH_HLEN
;
491 if (ethertype
== ETH_P_AARP
|| ethertype
== ETH_P_IPX
) {
492 encaps_data
= bridge_tunnel_header
;
493 encaps_len
= sizeof(bridge_tunnel_header
);
494 skip_header_bytes
-= 2;
495 } else if (ethertype
> 0x600) {
496 encaps_data
= rfc1042_header
;
497 encaps_len
= sizeof(rfc1042_header
);
498 skip_header_bytes
-= 2;
504 skb_pull(skb
, skip_header_bytes
);
505 nh_pos
-= skip_header_bytes
;
506 h_pos
-= skip_header_bytes
;
508 head_need
= hdrlen
+ encaps_len
- skb_headroom(skb
);
510 if (head_need
> 0 || skb_cloned(skb
)) {
511 head_need
= max(head_need
, 0);
515 if (pskb_expand_head(skb
, head_need
, 0, GFP_ATOMIC
)) {
516 pr_err("failed to reallocate Tx buffer\n");
519 skb
->truesize
+= head_need
;
523 memcpy(skb_push(skb
, encaps_len
), encaps_data
, encaps_len
);
524 nh_pos
+= encaps_len
;
528 memcpy(skb_push(skb
, hdrlen
), &hdr
, hdrlen
);
533 /* Update skb pointers to various headers since this modified frame
534 * is going to go through Linux networking code that may potentially
535 * need things like pointer to IP header. */
536 skb_set_mac_header(skb
, 0);
537 skb_set_network_header(skb
, nh_pos
);
538 skb_set_transport_header(skb
, h_pos
);
542 EXPORT_SYMBOL(ieee80211_data_from_8023
);
545 void ieee80211_amsdu_to_8023s(struct sk_buff
*skb
, struct sk_buff_head
*list
,
546 const u8
*addr
, enum nl80211_iftype iftype
,
547 const unsigned int extra_headroom
,
548 bool has_80211_header
)
550 struct sk_buff
*frame
= NULL
;
553 const struct ethhdr
*eth
;
555 u8 dst
[ETH_ALEN
], src
[ETH_ALEN
];
557 if (has_80211_header
) {
558 err
= ieee80211_data_to_8023(skb
, addr
, iftype
);
562 /* skip the wrapping header */
563 eth
= (struct ethhdr
*) skb_pull(skb
, sizeof(struct ethhdr
));
567 eth
= (struct ethhdr
*) skb
->data
;
570 while (skb
!= frame
) {
572 __be16 len
= eth
->h_proto
;
573 unsigned int subframe_len
= sizeof(struct ethhdr
) + ntohs(len
);
575 remaining
= skb
->len
;
576 memcpy(dst
, eth
->h_dest
, ETH_ALEN
);
577 memcpy(src
, eth
->h_source
, ETH_ALEN
);
579 padding
= (4 - subframe_len
) & 0x3;
580 /* the last MSDU has no padding */
581 if (subframe_len
> remaining
)
584 skb_pull(skb
, sizeof(struct ethhdr
));
585 /* reuse skb for the last subframe */
586 if (remaining
<= subframe_len
+ padding
)
589 unsigned int hlen
= ALIGN(extra_headroom
, 4);
591 * Allocate and reserve two bytes more for payload
592 * alignment since sizeof(struct ethhdr) is 14.
594 frame
= dev_alloc_skb(hlen
+ subframe_len
+ 2);
598 skb_reserve(frame
, hlen
+ sizeof(struct ethhdr
) + 2);
599 memcpy(skb_put(frame
, ntohs(len
)), skb
->data
,
602 eth
= (struct ethhdr
*)skb_pull(skb
, ntohs(len
) +
605 dev_kfree_skb(frame
);
610 skb_reset_network_header(frame
);
611 frame
->dev
= skb
->dev
;
612 frame
->priority
= skb
->priority
;
614 payload
= frame
->data
;
615 ethertype
= (payload
[6] << 8) | payload
[7];
617 if (likely((compare_ether_addr(payload
, rfc1042_header
) == 0 &&
618 ethertype
!= ETH_P_AARP
&& ethertype
!= ETH_P_IPX
) ||
619 compare_ether_addr(payload
,
620 bridge_tunnel_header
) == 0)) {
621 /* remove RFC1042 or Bridge-Tunnel
622 * encapsulation and replace EtherType */
624 memcpy(skb_push(frame
, ETH_ALEN
), src
, ETH_ALEN
);
625 memcpy(skb_push(frame
, ETH_ALEN
), dst
, ETH_ALEN
);
627 memcpy(skb_push(frame
, sizeof(__be16
)), &len
,
629 memcpy(skb_push(frame
, ETH_ALEN
), src
, ETH_ALEN
);
630 memcpy(skb_push(frame
, ETH_ALEN
), dst
, ETH_ALEN
);
632 __skb_queue_tail(list
, frame
);
638 __skb_queue_purge(list
);
642 EXPORT_SYMBOL(ieee80211_amsdu_to_8023s
);
644 /* Given a data frame determine the 802.1p/1d tag to use. */
645 unsigned int cfg80211_classify8021d(struct sk_buff
*skb
)
649 /* skb->priority values from 256->263 are magic values to
650 * directly indicate a specific 802.1d priority. This is used
651 * to allow 802.1d priority to be passed directly in from VLAN
654 if (skb
->priority
>= 256 && skb
->priority
<= 263)
655 return skb
->priority
- 256;
657 switch (skb
->protocol
) {
658 case htons(ETH_P_IP
):
659 dscp
= ip_hdr(skb
)->tos
& 0xfc;
667 EXPORT_SYMBOL(cfg80211_classify8021d
);
669 const u8
*ieee80211_bss_get_ie(struct cfg80211_bss
*bss
, u8 ie
)
673 pos
= bss
->information_elements
;
676 end
= pos
+ bss
->len_information_elements
;
678 while (pos
+ 1 < end
) {
679 if (pos
+ 2 + pos
[1] > end
)
688 EXPORT_SYMBOL(ieee80211_bss_get_ie
);
690 void cfg80211_upload_connect_keys(struct wireless_dev
*wdev
)
692 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
693 struct net_device
*dev
= wdev
->netdev
;
696 if (!wdev
->connect_keys
)
699 for (i
= 0; i
< 6; i
++) {
700 if (!wdev
->connect_keys
->params
[i
].cipher
)
702 if (rdev
->ops
->add_key(wdev
->wiphy
, dev
, i
, false, NULL
,
703 &wdev
->connect_keys
->params
[i
])) {
704 netdev_err(dev
, "failed to set key %d\n", i
);
707 if (wdev
->connect_keys
->def
== i
)
708 if (rdev
->ops
->set_default_key(wdev
->wiphy
, dev
,
710 netdev_err(dev
, "failed to set defkey %d\n", i
);
713 if (wdev
->connect_keys
->defmgmt
== i
)
714 if (rdev
->ops
->set_default_mgmt_key(wdev
->wiphy
, dev
, i
))
715 netdev_err(dev
, "failed to set mgtdef %d\n", i
);
718 kfree(wdev
->connect_keys
);
719 wdev
->connect_keys
= NULL
;
722 static void cfg80211_process_wdev_events(struct wireless_dev
*wdev
)
724 struct cfg80211_event
*ev
;
726 const u8
*bssid
= NULL
;
728 spin_lock_irqsave(&wdev
->event_lock
, flags
);
729 while (!list_empty(&wdev
->event_list
)) {
730 ev
= list_first_entry(&wdev
->event_list
,
731 struct cfg80211_event
, list
);
733 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
737 case EVENT_CONNECT_RESULT
:
738 if (!is_zero_ether_addr(ev
->cr
.bssid
))
739 bssid
= ev
->cr
.bssid
;
740 __cfg80211_connect_result(
742 ev
->cr
.req_ie
, ev
->cr
.req_ie_len
,
743 ev
->cr
.resp_ie
, ev
->cr
.resp_ie_len
,
745 ev
->cr
.status
== WLAN_STATUS_SUCCESS
,
749 __cfg80211_roamed(wdev
, ev
->rm
.channel
, ev
->rm
.bssid
,
750 ev
->rm
.req_ie
, ev
->rm
.req_ie_len
,
751 ev
->rm
.resp_ie
, ev
->rm
.resp_ie_len
);
753 case EVENT_DISCONNECTED
:
754 __cfg80211_disconnected(wdev
->netdev
,
755 ev
->dc
.ie
, ev
->dc
.ie_len
,
756 ev
->dc
.reason
, true);
758 case EVENT_IBSS_JOINED
:
759 __cfg80211_ibss_joined(wdev
->netdev
, ev
->ij
.bssid
);
766 spin_lock_irqsave(&wdev
->event_lock
, flags
);
768 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
771 void cfg80211_process_rdev_events(struct cfg80211_registered_device
*rdev
)
773 struct wireless_dev
*wdev
;
776 ASSERT_RDEV_LOCK(rdev
);
778 mutex_lock(&rdev
->devlist_mtx
);
780 list_for_each_entry(wdev
, &rdev
->netdev_list
, list
)
781 cfg80211_process_wdev_events(wdev
);
783 mutex_unlock(&rdev
->devlist_mtx
);
786 int cfg80211_change_iface(struct cfg80211_registered_device
*rdev
,
787 struct net_device
*dev
, enum nl80211_iftype ntype
,
788 u32
*flags
, struct vif_params
*params
)
791 enum nl80211_iftype otype
= dev
->ieee80211_ptr
->iftype
;
793 ASSERT_RDEV_LOCK(rdev
);
795 /* don't support changing VLANs, you just re-create them */
796 if (otype
== NL80211_IFTYPE_AP_VLAN
)
799 if (!rdev
->ops
->change_virtual_intf
||
800 !(rdev
->wiphy
.interface_modes
& (1 << ntype
)))
803 /* if it's part of a bridge, reject changing type to station/ibss */
804 if ((dev
->priv_flags
& IFF_BRIDGE_PORT
) &&
805 (ntype
== NL80211_IFTYPE_ADHOC
||
806 ntype
== NL80211_IFTYPE_STATION
||
807 ntype
== NL80211_IFTYPE_P2P_CLIENT
))
810 if (ntype
!= otype
) {
811 err
= cfg80211_can_change_interface(rdev
, dev
->ieee80211_ptr
,
816 dev
->ieee80211_ptr
->use_4addr
= false;
817 dev
->ieee80211_ptr
->mesh_id_up_len
= 0;
820 case NL80211_IFTYPE_ADHOC
:
821 cfg80211_leave_ibss(rdev
, dev
, false);
823 case NL80211_IFTYPE_STATION
:
824 case NL80211_IFTYPE_P2P_CLIENT
:
825 cfg80211_disconnect(rdev
, dev
,
826 WLAN_REASON_DEAUTH_LEAVING
, true);
828 case NL80211_IFTYPE_MESH_POINT
:
829 /* mesh should be handled? */
835 cfg80211_process_rdev_events(rdev
);
838 err
= rdev
->ops
->change_virtual_intf(&rdev
->wiphy
, dev
,
839 ntype
, flags
, params
);
841 WARN_ON(!err
&& dev
->ieee80211_ptr
->iftype
!= ntype
);
843 if (!err
&& params
&& params
->use_4addr
!= -1)
844 dev
->ieee80211_ptr
->use_4addr
= params
->use_4addr
;
847 dev
->priv_flags
&= ~IFF_DONT_BRIDGE
;
849 case NL80211_IFTYPE_STATION
:
850 if (dev
->ieee80211_ptr
->use_4addr
)
853 case NL80211_IFTYPE_P2P_CLIENT
:
854 case NL80211_IFTYPE_ADHOC
:
855 dev
->priv_flags
|= IFF_DONT_BRIDGE
;
857 case NL80211_IFTYPE_P2P_GO
:
858 case NL80211_IFTYPE_AP
:
859 case NL80211_IFTYPE_AP_VLAN
:
860 case NL80211_IFTYPE_WDS
:
861 case NL80211_IFTYPE_MESH_POINT
:
864 case NL80211_IFTYPE_MONITOR
:
865 /* monitor can't bridge anyway */
867 case NL80211_IFTYPE_UNSPECIFIED
:
868 case NUM_NL80211_IFTYPES
:
877 u16
cfg80211_calculate_bitrate(struct rate_info
*rate
)
879 int modulation
, streams
, bitrate
;
881 if (!(rate
->flags
& RATE_INFO_FLAGS_MCS
))
884 /* the formula below does only work for MCS values smaller than 32 */
888 modulation
= rate
->mcs
& 7;
889 streams
= (rate
->mcs
>> 3) + 1;
891 bitrate
= (rate
->flags
& RATE_INFO_FLAGS_40_MHZ_WIDTH
) ?
895 bitrate
*= (modulation
+ 1);
896 else if (modulation
== 4)
897 bitrate
*= (modulation
+ 2);
899 bitrate
*= (modulation
+ 3);
903 if (rate
->flags
& RATE_INFO_FLAGS_SHORT_GI
)
904 bitrate
= (bitrate
/ 9) * 10;
906 /* do NOT round down here */
907 return (bitrate
+ 50000) / 100000;
910 int cfg80211_validate_beacon_int(struct cfg80211_registered_device
*rdev
,
913 struct wireless_dev
*wdev
;
919 mutex_lock(&rdev
->devlist_mtx
);
921 list_for_each_entry(wdev
, &rdev
->netdev_list
, list
) {
922 if (!wdev
->beacon_interval
)
924 if (wdev
->beacon_interval
!= beacon_int
) {
930 mutex_unlock(&rdev
->devlist_mtx
);
935 int cfg80211_can_change_interface(struct cfg80211_registered_device
*rdev
,
936 struct wireless_dev
*wdev
,
937 enum nl80211_iftype iftype
)
939 struct wireless_dev
*wdev_iter
;
940 int num
[NUM_NL80211_IFTYPES
];
946 /* Always allow software iftypes */
947 if (rdev
->wiphy
.software_iftypes
& BIT(iftype
))
951 * Drivers will gradually all set this flag, until all
952 * have it we only enforce for those that set it.
954 if (!(rdev
->wiphy
.flags
& WIPHY_FLAG_ENFORCE_COMBINATIONS
))
957 memset(num
, 0, sizeof(num
));
961 mutex_lock(&rdev
->devlist_mtx
);
962 list_for_each_entry(wdev_iter
, &rdev
->netdev_list
, list
) {
963 if (wdev_iter
== wdev
)
965 if (!netif_running(wdev_iter
->netdev
))
968 if (rdev
->wiphy
.software_iftypes
& BIT(wdev_iter
->iftype
))
971 num
[wdev_iter
->iftype
]++;
974 mutex_unlock(&rdev
->devlist_mtx
);
976 for (i
= 0; i
< rdev
->wiphy
.n_iface_combinations
; i
++) {
977 const struct ieee80211_iface_combination
*c
;
978 struct ieee80211_iface_limit
*limits
;
980 c
= &rdev
->wiphy
.iface_combinations
[i
];
982 limits
= kmemdup(c
->limits
, sizeof(limits
[0]) * c
->n_limits
,
986 if (total
> c
->max_interfaces
)
989 for (iftype
= 0; iftype
< NUM_NL80211_IFTYPES
; iftype
++) {
990 if (rdev
->wiphy
.software_iftypes
& BIT(iftype
))
992 for (j
= 0; j
< c
->n_limits
; j
++) {
993 if (!(limits
[j
].types
& iftype
))
995 if (limits
[j
].max
< num
[iftype
])
997 limits
[j
].max
-= num
[iftype
];
1010 int ieee80211_get_ratemask(struct ieee80211_supported_band
*sband
,
1011 const u8
*rates
, unsigned int n_rates
,
1019 if (n_rates
== 0 || n_rates
> NL80211_MAX_SUPP_RATES
)
1024 for (i
= 0; i
< n_rates
; i
++) {
1025 int rate
= (rates
[i
] & 0x7f) * 5;
1028 for (j
= 0; j
< sband
->n_bitrates
; j
++) {
1029 if (sband
->bitrates
[j
].bitrate
== rate
) {
1040 * mask must have at least one bit set here since we
1041 * didn't accept a 0-length rates array nor allowed
1042 * entries in the array that didn't exist