cfg80211: fix channel configuration in IBSS join
[linux-block.git] / net / wireless / util.c
CommitLineData
8318d78a
JB
1/*
2 * Wireless utility functions
3 *
d3236553 4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
8318d78a 5 */
bc3b2d7f 6#include <linux/export.h>
d3236553 7#include <linux/bitops.h>
e31a16d6 8#include <linux/etherdevice.h>
5a0e3ad6 9#include <linux/slab.h>
d3236553 10#include <net/cfg80211.h>
e31a16d6 11#include <net/ip.h>
b156579b 12#include <net/dsfield.h>
c6ca5e28 13#include <linux/if_vlan.h>
8318d78a 14#include "core.h"
e35e4d28
HG
15#include "rdev-ops.h"
16
8318d78a 17
bd815252
JB
18struct ieee80211_rate *
19ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
881d948c 20 u32 basic_rates, int bitrate)
bd815252
JB
21{
22 struct ieee80211_rate *result = &sband->bitrates[0];
23 int i;
24
25 for (i = 0; i < sband->n_bitrates; i++) {
26 if (!(basic_rates & BIT(i)))
27 continue;
28 if (sband->bitrates[i].bitrate > bitrate)
29 continue;
30 result = &sband->bitrates[i];
31 }
32
33 return result;
34}
35EXPORT_SYMBOL(ieee80211_get_response_rate);
36
74608aca
SW
37u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
38 enum nl80211_bss_scan_width scan_width)
b422c6cd
AN
39{
40 struct ieee80211_rate *bitrates;
41 u32 mandatory_rates = 0;
42 enum ieee80211_rate_flags mandatory_flag;
43 int i;
44
45 if (WARN_ON(!sband))
46 return 1;
47
74608aca
SW
48 if (sband->band == IEEE80211_BAND_2GHZ) {
49 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
50 scan_width == NL80211_BSS_CHAN_WIDTH_10)
51 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
52 else
53 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
54 } else {
b422c6cd 55 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
74608aca 56 }
b422c6cd
AN
57
58 bitrates = sband->bitrates;
59 for (i = 0; i < sband->n_bitrates; i++)
60 if (bitrates[i].flags & mandatory_flag)
61 mandatory_rates |= BIT(i);
62 return mandatory_rates;
63}
64EXPORT_SYMBOL(ieee80211_mandatory_rates);
65
59eb21a6 66int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
8318d78a 67{
59eb21a6
BR
68 /* see 802.11 17.3.8.3.2 and Annex J
69 * there are overlapping channel numbers in 5GHz and 2GHz bands */
3a0c52a6
VK
70 if (chan <= 0)
71 return 0; /* not supported */
72 switch (band) {
73 case IEEE80211_BAND_2GHZ:
59eb21a6
BR
74 if (chan == 14)
75 return 2484;
76 else if (chan < 14)
77 return 2407 + chan * 5;
3a0c52a6
VK
78 break;
79 case IEEE80211_BAND_5GHZ:
80 if (chan >= 182 && chan <= 196)
81 return 4000 + chan * 5;
59eb21a6 82 else
3a0c52a6
VK
83 return 5000 + chan * 5;
84 break;
85 case IEEE80211_BAND_60GHZ:
86 if (chan < 5)
87 return 56160 + chan * 2160;
88 break;
89 default:
90 ;
59eb21a6 91 }
3a0c52a6 92 return 0; /* not supported */
8318d78a
JB
93}
94EXPORT_SYMBOL(ieee80211_channel_to_frequency);
95
96int ieee80211_frequency_to_channel(int freq)
97{
59eb21a6 98 /* see 802.11 17.3.8.3.2 and Annex J */
8318d78a
JB
99 if (freq == 2484)
100 return 14;
59eb21a6 101 else if (freq < 2484)
8318d78a 102 return (freq - 2407) / 5;
59eb21a6
BR
103 else if (freq >= 4910 && freq <= 4980)
104 return (freq - 4000) / 5;
3a0c52a6 105 else if (freq <= 45000) /* DMG band lower limit */
59eb21a6 106 return (freq - 5000) / 5;
3a0c52a6
VK
107 else if (freq >= 58320 && freq <= 64800)
108 return (freq - 56160) / 2160;
109 else
110 return 0;
8318d78a
JB
111}
112EXPORT_SYMBOL(ieee80211_frequency_to_channel);
113
6c507cd0
JB
114struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
115 int freq)
906c730a
JB
116{
117 enum ieee80211_band band;
118 struct ieee80211_supported_band *sband;
119 int i;
120
121 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
122 sband = wiphy->bands[band];
123
124 if (!sband)
125 continue;
126
127 for (i = 0; i < sband->n_channels; i++) {
128 if (sband->channels[i].center_freq == freq)
129 return &sband->channels[i];
130 }
131 }
132
133 return NULL;
134}
6c507cd0 135EXPORT_SYMBOL(__ieee80211_get_channel);
906c730a 136
8318d78a
JB
137static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
138 enum ieee80211_band band)
139{
140 int i, want;
141
142 switch (band) {
143 case IEEE80211_BAND_5GHZ:
144 want = 3;
145 for (i = 0; i < sband->n_bitrates; i++) {
146 if (sband->bitrates[i].bitrate == 60 ||
147 sband->bitrates[i].bitrate == 120 ||
148 sband->bitrates[i].bitrate == 240) {
149 sband->bitrates[i].flags |=
150 IEEE80211_RATE_MANDATORY_A;
151 want--;
152 }
153 }
154 WARN_ON(want);
155 break;
156 case IEEE80211_BAND_2GHZ:
157 want = 7;
158 for (i = 0; i < sband->n_bitrates; i++) {
159 if (sband->bitrates[i].bitrate == 10) {
160 sband->bitrates[i].flags |=
161 IEEE80211_RATE_MANDATORY_B |
162 IEEE80211_RATE_MANDATORY_G;
163 want--;
164 }
165
166 if (sband->bitrates[i].bitrate == 20 ||
167 sband->bitrates[i].bitrate == 55 ||
168 sband->bitrates[i].bitrate == 110 ||
169 sband->bitrates[i].bitrate == 60 ||
170 sband->bitrates[i].bitrate == 120 ||
171 sband->bitrates[i].bitrate == 240) {
172 sband->bitrates[i].flags |=
173 IEEE80211_RATE_MANDATORY_G;
174 want--;
175 }
176
aac09fbf
JB
177 if (sband->bitrates[i].bitrate != 10 &&
178 sband->bitrates[i].bitrate != 20 &&
179 sband->bitrates[i].bitrate != 55 &&
180 sband->bitrates[i].bitrate != 110)
8318d78a
JB
181 sband->bitrates[i].flags |=
182 IEEE80211_RATE_ERP_G;
183 }
406f2388 184 WARN_ON(want != 0 && want != 3 && want != 6);
8318d78a 185 break;
3a0c52a6
VK
186 case IEEE80211_BAND_60GHZ:
187 /* check for mandatory HT MCS 1..4 */
188 WARN_ON(!sband->ht_cap.ht_supported);
189 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
190 break;
8318d78a
JB
191 case IEEE80211_NUM_BANDS:
192 WARN_ON(1);
193 break;
194 }
195}
196
197void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
198{
199 enum ieee80211_band band;
200
201 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
202 if (wiphy->bands[band])
203 set_mandatory_flags_band(wiphy->bands[band], band);
204}
08645126 205
38ba3c57
JM
206bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
207{
208 int i;
209 for (i = 0; i < wiphy->n_cipher_suites; i++)
210 if (cipher == wiphy->cipher_suites[i])
211 return true;
212 return false;
213}
214
fffd0934
JB
215int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
216 struct key_params *params, int key_idx,
e31b8213 217 bool pairwise, const u8 *mac_addr)
08645126
JB
218{
219 if (key_idx > 5)
220 return -EINVAL;
221
e31b8213
JB
222 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
223 return -EINVAL;
224
225 if (pairwise && !mac_addr)
226 return -EINVAL;
227
08645126
JB
228 /*
229 * Disallow pairwise keys with non-zero index unless it's WEP
45cbad6a
JO
230 * or a vendor specific cipher (because current deployments use
231 * pairwise WEP keys with non-zero indices and for vendor specific
232 * ciphers this should be validated in the driver or hardware level
233 * - but 802.11i clearly specifies to use zero)
08645126 234 */
e31b8213 235 if (pairwise && key_idx &&
45cbad6a
JO
236 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
237 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
238 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
08645126
JB
239 return -EINVAL;
240
08645126
JB
241 switch (params->cipher) {
242 case WLAN_CIPHER_SUITE_WEP40:
8fc0fee0 243 if (params->key_len != WLAN_KEY_LEN_WEP40)
08645126
JB
244 return -EINVAL;
245 break;
246 case WLAN_CIPHER_SUITE_TKIP:
8fc0fee0 247 if (params->key_len != WLAN_KEY_LEN_TKIP)
08645126
JB
248 return -EINVAL;
249 break;
250 case WLAN_CIPHER_SUITE_CCMP:
8fc0fee0 251 if (params->key_len != WLAN_KEY_LEN_CCMP)
08645126
JB
252 return -EINVAL;
253 break;
254 case WLAN_CIPHER_SUITE_WEP104:
8fc0fee0 255 if (params->key_len != WLAN_KEY_LEN_WEP104)
08645126
JB
256 return -EINVAL;
257 break;
258 case WLAN_CIPHER_SUITE_AES_CMAC:
8fc0fee0 259 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
08645126
JB
260 return -EINVAL;
261 break;
262 default:
7d64b7cc
JB
263 /*
264 * We don't know anything about this algorithm,
265 * allow using it -- but the driver must check
266 * all parameters! We still check below whether
267 * or not the driver supports this algorithm,
268 * of course.
269 */
270 break;
08645126
JB
271 }
272
9f26a952
JM
273 if (params->seq) {
274 switch (params->cipher) {
275 case WLAN_CIPHER_SUITE_WEP40:
276 case WLAN_CIPHER_SUITE_WEP104:
277 /* These ciphers do not use key sequence */
278 return -EINVAL;
279 case WLAN_CIPHER_SUITE_TKIP:
280 case WLAN_CIPHER_SUITE_CCMP:
281 case WLAN_CIPHER_SUITE_AES_CMAC:
282 if (params->seq_len != 6)
283 return -EINVAL;
284 break;
285 }
286 }
287
38ba3c57 288 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
fffd0934
JB
289 return -EINVAL;
290
08645126
JB
291 return 0;
292}
e31a16d6 293
633adf1a 294unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
e31a16d6
ZY
295{
296 unsigned int hdrlen = 24;
297
298 if (ieee80211_is_data(fc)) {
299 if (ieee80211_has_a4(fc))
300 hdrlen = 30;
d0dd2de0 301 if (ieee80211_is_data_qos(fc)) {
e31a16d6 302 hdrlen += IEEE80211_QOS_CTL_LEN;
d0dd2de0
AT
303 if (ieee80211_has_order(fc))
304 hdrlen += IEEE80211_HT_CTL_LEN;
305 }
e31a16d6
ZY
306 goto out;
307 }
308
309 if (ieee80211_is_ctl(fc)) {
310 /*
311 * ACK and CTS are 10 bytes, all others 16. To see how
312 * to get this condition consider
313 * subtype mask: 0b0000000011110000 (0x00F0)
314 * ACK subtype: 0b0000000011010000 (0x00D0)
315 * CTS subtype: 0b0000000011000000 (0x00C0)
316 * bits that matter: ^^^ (0x00E0)
317 * value of those: 0b0000000011000000 (0x00C0)
318 */
319 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
320 hdrlen = 10;
321 else
322 hdrlen = 16;
323 }
324out:
325 return hdrlen;
326}
327EXPORT_SYMBOL(ieee80211_hdrlen);
328
329unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
330{
331 const struct ieee80211_hdr *hdr =
332 (const struct ieee80211_hdr *)skb->data;
333 unsigned int hdrlen;
334
335 if (unlikely(skb->len < 10))
336 return 0;
337 hdrlen = ieee80211_hdrlen(hdr->frame_control);
338 if (unlikely(hdrlen > skb->len))
339 return 0;
340 return hdrlen;
341}
342EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
343
9b395bc3 344unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
e31a16d6
ZY
345{
346 int ae = meshhdr->flags & MESH_FLAGS_AE;
7dd111e8 347 /* 802.11-2012, 8.2.4.7.3 */
e31a16d6 348 switch (ae) {
7dd111e8 349 default:
e31a16d6
ZY
350 case 0:
351 return 6;
3c5772a5 352 case MESH_FLAGS_AE_A4:
e31a16d6 353 return 12;
3c5772a5 354 case MESH_FLAGS_AE_A5_A6:
e31a16d6 355 return 18;
e31a16d6
ZY
356 }
357}
9b395bc3 358EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
e31a16d6 359
eaf85ca7 360int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
e31a16d6
ZY
361 enum nl80211_iftype iftype)
362{
363 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
364 u16 hdrlen, ethertype;
365 u8 *payload;
366 u8 dst[ETH_ALEN];
367 u8 src[ETH_ALEN] __aligned(2);
368
369 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
370 return -1;
371
372 hdrlen = ieee80211_hdrlen(hdr->frame_control);
373
374 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
375 * header
376 * IEEE 802.11 address fields:
377 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
378 * 0 0 DA SA BSSID n/a
379 * 0 1 DA BSSID SA n/a
380 * 1 0 BSSID SA DA n/a
381 * 1 1 RA TA DA SA
382 */
383 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
384 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
385
386 switch (hdr->frame_control &
387 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
388 case cpu_to_le16(IEEE80211_FCTL_TODS):
389 if (unlikely(iftype != NL80211_IFTYPE_AP &&
074ac8df
JB
390 iftype != NL80211_IFTYPE_AP_VLAN &&
391 iftype != NL80211_IFTYPE_P2P_GO))
e31a16d6
ZY
392 return -1;
393 break;
394 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
395 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
f14543ee
FF
396 iftype != NL80211_IFTYPE_MESH_POINT &&
397 iftype != NL80211_IFTYPE_AP_VLAN &&
398 iftype != NL80211_IFTYPE_STATION))
e31a16d6
ZY
399 return -1;
400 if (iftype == NL80211_IFTYPE_MESH_POINT) {
401 struct ieee80211s_hdr *meshdr =
402 (struct ieee80211s_hdr *) (skb->data + hdrlen);
e3cf8b3f
ZY
403 /* make sure meshdr->flags is on the linear part */
404 if (!pskb_may_pull(skb, hdrlen + 1))
405 return -1;
7dd111e8
JB
406 if (meshdr->flags & MESH_FLAGS_AE_A4)
407 return -1;
e31a16d6 408 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
e3cf8b3f
ZY
409 skb_copy_bits(skb, hdrlen +
410 offsetof(struct ieee80211s_hdr, eaddr1),
411 dst, ETH_ALEN);
412 skb_copy_bits(skb, hdrlen +
413 offsetof(struct ieee80211s_hdr, eaddr2),
414 src, ETH_ALEN);
e31a16d6 415 }
e3cf8b3f 416 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
e31a16d6
ZY
417 }
418 break;
419 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
3c5772a5 420 if ((iftype != NL80211_IFTYPE_STATION &&
074ac8df
JB
421 iftype != NL80211_IFTYPE_P2P_CLIENT &&
422 iftype != NL80211_IFTYPE_MESH_POINT) ||
e31a16d6 423 (is_multicast_ether_addr(dst) &&
4c764729 424 ether_addr_equal(src, addr)))
e31a16d6 425 return -1;
3c5772a5
JC
426 if (iftype == NL80211_IFTYPE_MESH_POINT) {
427 struct ieee80211s_hdr *meshdr =
428 (struct ieee80211s_hdr *) (skb->data + hdrlen);
e3cf8b3f
ZY
429 /* make sure meshdr->flags is on the linear part */
430 if (!pskb_may_pull(skb, hdrlen + 1))
431 return -1;
7dd111e8
JB
432 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
433 return -1;
3c5772a5 434 if (meshdr->flags & MESH_FLAGS_AE_A4)
e3cf8b3f
ZY
435 skb_copy_bits(skb, hdrlen +
436 offsetof(struct ieee80211s_hdr, eaddr1),
437 src, ETH_ALEN);
438 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
3c5772a5 439 }
e31a16d6
ZY
440 break;
441 case cpu_to_le16(0):
941c93cd
AN
442 if (iftype != NL80211_IFTYPE_ADHOC &&
443 iftype != NL80211_IFTYPE_STATION)
444 return -1;
e31a16d6
ZY
445 break;
446 }
447
e3cf8b3f 448 if (!pskb_may_pull(skb, hdrlen + 8))
e31a16d6
ZY
449 return -1;
450
451 payload = skb->data + hdrlen;
452 ethertype = (payload[6] << 8) | payload[7];
453
4c764729 454 if (likely((ether_addr_equal(payload, rfc1042_header) &&
e31a16d6 455 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
4c764729 456 ether_addr_equal(payload, bridge_tunnel_header))) {
e31a16d6
ZY
457 /* remove RFC1042 or Bridge-Tunnel encapsulation and
458 * replace EtherType */
459 skb_pull(skb, hdrlen + 6);
460 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
461 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
462 } else {
463 struct ethhdr *ehdr;
464 __be16 len;
465
466 skb_pull(skb, hdrlen);
467 len = htons(skb->len);
468 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
469 memcpy(ehdr->h_dest, dst, ETH_ALEN);
470 memcpy(ehdr->h_source, src, ETH_ALEN);
471 ehdr->h_proto = len;
472 }
473 return 0;
474}
475EXPORT_SYMBOL(ieee80211_data_to_8023);
476
eaf85ca7 477int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
e31a16d6
ZY
478 enum nl80211_iftype iftype, u8 *bssid, bool qos)
479{
480 struct ieee80211_hdr hdr;
481 u16 hdrlen, ethertype;
482 __le16 fc;
483 const u8 *encaps_data;
484 int encaps_len, skip_header_bytes;
485 int nh_pos, h_pos;
486 int head_need;
487
488 if (unlikely(skb->len < ETH_HLEN))
489 return -EINVAL;
490
491 nh_pos = skb_network_header(skb) - skb->data;
492 h_pos = skb_transport_header(skb) - skb->data;
493
494 /* convert Ethernet header to proper 802.11 header (based on
495 * operation mode) */
496 ethertype = (skb->data[12] << 8) | skb->data[13];
497 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
498
499 switch (iftype) {
500 case NL80211_IFTYPE_AP:
501 case NL80211_IFTYPE_AP_VLAN:
074ac8df 502 case NL80211_IFTYPE_P2P_GO:
e31a16d6
ZY
503 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
504 /* DA BSSID SA */
505 memcpy(hdr.addr1, skb->data, ETH_ALEN);
506 memcpy(hdr.addr2, addr, ETH_ALEN);
507 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
508 hdrlen = 24;
509 break;
510 case NL80211_IFTYPE_STATION:
074ac8df 511 case NL80211_IFTYPE_P2P_CLIENT:
e31a16d6
ZY
512 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
513 /* BSSID SA DA */
514 memcpy(hdr.addr1, bssid, ETH_ALEN);
515 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
516 memcpy(hdr.addr3, skb->data, ETH_ALEN);
517 hdrlen = 24;
518 break;
519 case NL80211_IFTYPE_ADHOC:
520 /* DA SA BSSID */
521 memcpy(hdr.addr1, skb->data, ETH_ALEN);
522 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
523 memcpy(hdr.addr3, bssid, ETH_ALEN);
524 hdrlen = 24;
525 break;
526 default:
527 return -EOPNOTSUPP;
528 }
529
530 if (qos) {
531 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
532 hdrlen += 2;
533 }
534
535 hdr.frame_control = fc;
536 hdr.duration_id = 0;
537 hdr.seq_ctrl = 0;
538
539 skip_header_bytes = ETH_HLEN;
540 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
541 encaps_data = bridge_tunnel_header;
542 encaps_len = sizeof(bridge_tunnel_header);
543 skip_header_bytes -= 2;
e5c5d22e 544 } else if (ethertype >= ETH_P_802_3_MIN) {
e31a16d6
ZY
545 encaps_data = rfc1042_header;
546 encaps_len = sizeof(rfc1042_header);
547 skip_header_bytes -= 2;
548 } else {
549 encaps_data = NULL;
550 encaps_len = 0;
551 }
552
553 skb_pull(skb, skip_header_bytes);
554 nh_pos -= skip_header_bytes;
555 h_pos -= skip_header_bytes;
556
557 head_need = hdrlen + encaps_len - skb_headroom(skb);
558
559 if (head_need > 0 || skb_cloned(skb)) {
560 head_need = max(head_need, 0);
561 if (head_need)
562 skb_orphan(skb);
563
24616152 564 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
e31a16d6 565 return -ENOMEM;
24616152 566
e31a16d6
ZY
567 skb->truesize += head_need;
568 }
569
570 if (encaps_data) {
571 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
572 nh_pos += encaps_len;
573 h_pos += encaps_len;
574 }
575
576 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
577
578 nh_pos += hdrlen;
579 h_pos += hdrlen;
580
581 /* Update skb pointers to various headers since this modified frame
582 * is going to go through Linux networking code that may potentially
583 * need things like pointer to IP header. */
584 skb_set_mac_header(skb, 0);
585 skb_set_network_header(skb, nh_pos);
586 skb_set_transport_header(skb, h_pos);
587
588 return 0;
589}
590EXPORT_SYMBOL(ieee80211_data_from_8023);
591
eaf85ca7
ZY
592
593void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
594 const u8 *addr, enum nl80211_iftype iftype,
8b3becad
YAP
595 const unsigned int extra_headroom,
596 bool has_80211_header)
eaf85ca7
ZY
597{
598 struct sk_buff *frame = NULL;
599 u16 ethertype;
600 u8 *payload;
601 const struct ethhdr *eth;
602 int remaining, err;
603 u8 dst[ETH_ALEN], src[ETH_ALEN];
604
8b3becad
YAP
605 if (has_80211_header) {
606 err = ieee80211_data_to_8023(skb, addr, iftype);
607 if (err)
608 goto out;
eaf85ca7 609
8b3becad
YAP
610 /* skip the wrapping header */
611 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
612 if (!eth)
613 goto out;
614 } else {
615 eth = (struct ethhdr *) skb->data;
616 }
eaf85ca7
ZY
617
618 while (skb != frame) {
619 u8 padding;
620 __be16 len = eth->h_proto;
621 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
622
623 remaining = skb->len;
624 memcpy(dst, eth->h_dest, ETH_ALEN);
625 memcpy(src, eth->h_source, ETH_ALEN);
626
627 padding = (4 - subframe_len) & 0x3;
628 /* the last MSDU has no padding */
629 if (subframe_len > remaining)
630 goto purge;
631
632 skb_pull(skb, sizeof(struct ethhdr));
633 /* reuse skb for the last subframe */
634 if (remaining <= subframe_len + padding)
635 frame = skb;
636 else {
637 unsigned int hlen = ALIGN(extra_headroom, 4);
638 /*
639 * Allocate and reserve two bytes more for payload
640 * alignment since sizeof(struct ethhdr) is 14.
641 */
642 frame = dev_alloc_skb(hlen + subframe_len + 2);
643 if (!frame)
644 goto purge;
645
646 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
647 memcpy(skb_put(frame, ntohs(len)), skb->data,
648 ntohs(len));
649
650 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
651 padding);
652 if (!eth) {
653 dev_kfree_skb(frame);
654 goto purge;
655 }
656 }
657
658 skb_reset_network_header(frame);
659 frame->dev = skb->dev;
660 frame->priority = skb->priority;
661
662 payload = frame->data;
663 ethertype = (payload[6] << 8) | payload[7];
664
ac422d3c 665 if (likely((ether_addr_equal(payload, rfc1042_header) &&
eaf85ca7 666 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
ac422d3c 667 ether_addr_equal(payload, bridge_tunnel_header))) {
eaf85ca7
ZY
668 /* remove RFC1042 or Bridge-Tunnel
669 * encapsulation and replace EtherType */
670 skb_pull(frame, 6);
671 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
672 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
673 } else {
674 memcpy(skb_push(frame, sizeof(__be16)), &len,
675 sizeof(__be16));
676 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
677 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
678 }
679 __skb_queue_tail(list, frame);
680 }
681
682 return;
683
684 purge:
685 __skb_queue_purge(list);
686 out:
687 dev_kfree_skb(skb);
688}
689EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
690
e31a16d6 691/* Given a data frame determine the 802.1p/1d tag to use. */
fa9ffc74
KP
692unsigned int cfg80211_classify8021d(struct sk_buff *skb,
693 struct cfg80211_qos_map *qos_map)
e31a16d6
ZY
694{
695 unsigned int dscp;
c6ca5e28 696 unsigned char vlan_priority;
e31a16d6
ZY
697
698 /* skb->priority values from 256->263 are magic values to
699 * directly indicate a specific 802.1d priority. This is used
700 * to allow 802.1d priority to be passed directly in from VLAN
701 * tags, etc.
702 */
703 if (skb->priority >= 256 && skb->priority <= 263)
704 return skb->priority - 256;
705
c6ca5e28
V
706 if (vlan_tx_tag_present(skb)) {
707 vlan_priority = (vlan_tx_tag_get(skb) & VLAN_PRIO_MASK)
708 >> VLAN_PRIO_SHIFT;
709 if (vlan_priority > 0)
710 return vlan_priority;
711 }
712
e31a16d6
ZY
713 switch (skb->protocol) {
714 case htons(ETH_P_IP):
b156579b
DT
715 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
716 break;
717 case htons(ETH_P_IPV6):
718 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
e31a16d6
ZY
719 break;
720 default:
721 return 0;
722 }
723
fa9ffc74
KP
724 if (qos_map) {
725 unsigned int i, tmp_dscp = dscp >> 2;
726
727 for (i = 0; i < qos_map->num_des; i++) {
728 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
729 return qos_map->dscp_exception[i].up;
730 }
731
732 for (i = 0; i < 8; i++) {
733 if (tmp_dscp >= qos_map->up[i].low &&
734 tmp_dscp <= qos_map->up[i].high)
735 return i;
736 }
737 }
738
e31a16d6
ZY
739 return dscp >> 5;
740}
741EXPORT_SYMBOL(cfg80211_classify8021d);
517357c6
JB
742
743const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
744{
9caf0364
JB
745 const struct cfg80211_bss_ies *ies;
746
747 ies = rcu_dereference(bss->ies);
748 if (!ies)
517357c6 749 return NULL;
9caf0364
JB
750
751 return cfg80211_find_ie(ie, ies->data, ies->len);
517357c6
JB
752}
753EXPORT_SYMBOL(ieee80211_bss_get_ie);
fffd0934
JB
754
755void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
756{
757 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
758 struct net_device *dev = wdev->netdev;
759 int i;
760
761 if (!wdev->connect_keys)
762 return;
763
764 for (i = 0; i < 6; i++) {
765 if (!wdev->connect_keys->params[i].cipher)
766 continue;
e35e4d28
HG
767 if (rdev_add_key(rdev, dev, i, false, NULL,
768 &wdev->connect_keys->params[i])) {
e9c0268f 769 netdev_err(dev, "failed to set key %d\n", i);
1e056665
ZY
770 continue;
771 }
fffd0934 772 if (wdev->connect_keys->def == i)
e35e4d28 773 if (rdev_set_default_key(rdev, dev, i, true, true)) {
e9c0268f 774 netdev_err(dev, "failed to set defkey %d\n", i);
1e056665
ZY
775 continue;
776 }
fffd0934 777 if (wdev->connect_keys->defmgmt == i)
e35e4d28 778 if (rdev_set_default_mgmt_key(rdev, dev, i))
e9c0268f 779 netdev_err(dev, "failed to set mgtdef %d\n", i);
fffd0934
JB
780 }
781
782 kfree(wdev->connect_keys);
783 wdev->connect_keys = NULL;
784}
3d54d255 785
1f6fc43e 786void cfg80211_process_wdev_events(struct wireless_dev *wdev)
3d54d255
JB
787{
788 struct cfg80211_event *ev;
789 unsigned long flags;
790 const u8 *bssid = NULL;
791
792 spin_lock_irqsave(&wdev->event_lock, flags);
793 while (!list_empty(&wdev->event_list)) {
794 ev = list_first_entry(&wdev->event_list,
795 struct cfg80211_event, list);
796 list_del(&ev->list);
797 spin_unlock_irqrestore(&wdev->event_lock, flags);
798
799 wdev_lock(wdev);
800 switch (ev->type) {
801 case EVENT_CONNECT_RESULT:
802 if (!is_zero_ether_addr(ev->cr.bssid))
803 bssid = ev->cr.bssid;
804 __cfg80211_connect_result(
805 wdev->netdev, bssid,
806 ev->cr.req_ie, ev->cr.req_ie_len,
807 ev->cr.resp_ie, ev->cr.resp_ie_len,
808 ev->cr.status,
809 ev->cr.status == WLAN_STATUS_SUCCESS,
810 NULL);
811 break;
812 case EVENT_ROAMED:
adbde344
VT
813 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
814 ev->rm.req_ie_len, ev->rm.resp_ie,
815 ev->rm.resp_ie_len);
3d54d255
JB
816 break;
817 case EVENT_DISCONNECTED:
818 __cfg80211_disconnected(wdev->netdev,
819 ev->dc.ie, ev->dc.ie_len,
820 ev->dc.reason, true);
821 break;
822 case EVENT_IBSS_JOINED:
fe94f3a4
AQ
823 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
824 ev->ij.channel);
3d54d255
JB
825 break;
826 }
827 wdev_unlock(wdev);
828
829 kfree(ev);
830
831 spin_lock_irqsave(&wdev->event_lock, flags);
832 }
833 spin_unlock_irqrestore(&wdev->event_lock, flags);
834}
835
836void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
837{
838 struct wireless_dev *wdev;
839
840 ASSERT_RTNL();
841 ASSERT_RDEV_LOCK(rdev);
842
89a54e48 843 list_for_each_entry(wdev, &rdev->wdev_list, list)
3d54d255 844 cfg80211_process_wdev_events(wdev);
3d54d255
JB
845}
846
847int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
848 struct net_device *dev, enum nl80211_iftype ntype,
849 u32 *flags, struct vif_params *params)
850{
851 int err;
852 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
853
854 ASSERT_RDEV_LOCK(rdev);
855
856 /* don't support changing VLANs, you just re-create them */
857 if (otype == NL80211_IFTYPE_AP_VLAN)
858 return -EOPNOTSUPP;
859
98104fde
JB
860 /* cannot change into P2P device type */
861 if (ntype == NL80211_IFTYPE_P2P_DEVICE)
862 return -EOPNOTSUPP;
863
3d54d255
JB
864 if (!rdev->ops->change_virtual_intf ||
865 !(rdev->wiphy.interface_modes & (1 << ntype)))
866 return -EOPNOTSUPP;
867
ad4bb6f8 868 /* if it's part of a bridge, reject changing type to station/ibss */
f350a0a8 869 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
074ac8df
JB
870 (ntype == NL80211_IFTYPE_ADHOC ||
871 ntype == NL80211_IFTYPE_STATION ||
872 ntype == NL80211_IFTYPE_P2P_CLIENT))
ad4bb6f8
JB
873 return -EBUSY;
874
f8cdddb8 875 if (ntype != otype && netif_running(dev)) {
7527a782
JB
876 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
877 ntype);
878 if (err)
879 return err;
880
9bc383de 881 dev->ieee80211_ptr->use_4addr = false;
29cbe68c 882 dev->ieee80211_ptr->mesh_id_up_len = 0;
194ff52d 883 wdev_lock(dev->ieee80211_ptr);
fa9ffc74 884 rdev_set_qos_map(rdev, dev, NULL);
194ff52d 885 wdev_unlock(dev->ieee80211_ptr);
9bc383de 886
3d54d255 887 switch (otype) {
ac800140
MK
888 case NL80211_IFTYPE_AP:
889 cfg80211_stop_ap(rdev, dev);
890 break;
3d54d255
JB
891 case NL80211_IFTYPE_ADHOC:
892 cfg80211_leave_ibss(rdev, dev, false);
893 break;
894 case NL80211_IFTYPE_STATION:
074ac8df 895 case NL80211_IFTYPE_P2P_CLIENT:
83739b03 896 wdev_lock(dev->ieee80211_ptr);
3d54d255
JB
897 cfg80211_disconnect(rdev, dev,
898 WLAN_REASON_DEAUTH_LEAVING, true);
83739b03 899 wdev_unlock(dev->ieee80211_ptr);
3d54d255
JB
900 break;
901 case NL80211_IFTYPE_MESH_POINT:
902 /* mesh should be handled? */
903 break;
904 default:
905 break;
906 }
907
908 cfg80211_process_rdev_events(rdev);
909 }
910
e35e4d28 911 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
3d54d255
JB
912
913 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
914
9bc383de
JB
915 if (!err && params && params->use_4addr != -1)
916 dev->ieee80211_ptr->use_4addr = params->use_4addr;
917
ad4bb6f8
JB
918 if (!err) {
919 dev->priv_flags &= ~IFF_DONT_BRIDGE;
920 switch (ntype) {
921 case NL80211_IFTYPE_STATION:
922 if (dev->ieee80211_ptr->use_4addr)
923 break;
924 /* fall through */
074ac8df 925 case NL80211_IFTYPE_P2P_CLIENT:
ad4bb6f8
JB
926 case NL80211_IFTYPE_ADHOC:
927 dev->priv_flags |= IFF_DONT_BRIDGE;
928 break;
074ac8df 929 case NL80211_IFTYPE_P2P_GO:
ad4bb6f8
JB
930 case NL80211_IFTYPE_AP:
931 case NL80211_IFTYPE_AP_VLAN:
932 case NL80211_IFTYPE_WDS:
933 case NL80211_IFTYPE_MESH_POINT:
934 /* bridging OK */
935 break;
936 case NL80211_IFTYPE_MONITOR:
937 /* monitor can't bridge anyway */
938 break;
939 case NL80211_IFTYPE_UNSPECIFIED:
2e161f78 940 case NUM_NL80211_IFTYPES:
ad4bb6f8
JB
941 /* not happening */
942 break;
98104fde
JB
943 case NL80211_IFTYPE_P2P_DEVICE:
944 WARN_ON(1);
945 break;
ad4bb6f8
JB
946 }
947 }
948
dbbae26a
MK
949 if (!err && ntype != otype && netif_running(dev)) {
950 cfg80211_update_iface_num(rdev, ntype, 1);
951 cfg80211_update_iface_num(rdev, otype, -1);
952 }
953
3d54d255
JB
954 return err;
955}
254416aa 956
95ddc1fc
VK
957static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
958{
959 static const u32 __mcs2bitrate[] = {
960 /* control PHY */
961 [0] = 275,
962 /* SC PHY */
963 [1] = 3850,
964 [2] = 7700,
965 [3] = 9625,
966 [4] = 11550,
967 [5] = 12512, /* 1251.25 mbps */
968 [6] = 15400,
969 [7] = 19250,
970 [8] = 23100,
971 [9] = 25025,
972 [10] = 30800,
973 [11] = 38500,
974 [12] = 46200,
975 /* OFDM PHY */
976 [13] = 6930,
977 [14] = 8662, /* 866.25 mbps */
978 [15] = 13860,
979 [16] = 17325,
980 [17] = 20790,
981 [18] = 27720,
982 [19] = 34650,
983 [20] = 41580,
984 [21] = 45045,
985 [22] = 51975,
986 [23] = 62370,
987 [24] = 67568, /* 6756.75 mbps */
988 /* LP-SC PHY */
989 [25] = 6260,
990 [26] = 8340,
991 [27] = 11120,
992 [28] = 12510,
993 [29] = 16680,
994 [30] = 22240,
995 [31] = 25030,
996 };
997
998 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
999 return 0;
1000
1001 return __mcs2bitrate[rate->mcs];
1002}
1003
db9c64cf
JB
1004static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1005{
1006 static const u32 base[4][10] = {
1007 { 6500000,
1008 13000000,
1009 19500000,
1010 26000000,
1011 39000000,
1012 52000000,
1013 58500000,
1014 65000000,
1015 78000000,
1016 0,
1017 },
1018 { 13500000,
1019 27000000,
1020 40500000,
1021 54000000,
1022 81000000,
1023 108000000,
1024 121500000,
1025 135000000,
1026 162000000,
1027 180000000,
1028 },
1029 { 29300000,
1030 58500000,
1031 87800000,
1032 117000000,
1033 175500000,
1034 234000000,
1035 263300000,
1036 292500000,
1037 351000000,
1038 390000000,
1039 },
1040 { 58500000,
1041 117000000,
1042 175500000,
1043 234000000,
1044 351000000,
1045 468000000,
1046 526500000,
1047 585000000,
1048 702000000,
1049 780000000,
1050 },
1051 };
1052 u32 bitrate;
1053 int idx;
1054
1055 if (WARN_ON_ONCE(rate->mcs > 9))
1056 return 0;
1057
1058 idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
1059 RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
1060 rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
1061 rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
1062
1063 bitrate = base[idx][rate->mcs];
1064 bitrate *= rate->nss;
1065
1066 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1067 bitrate = (bitrate / 9) * 10;
1068
1069 /* do NOT round down here */
1070 return (bitrate + 50000) / 100000;
1071}
1072
8eb41c8d 1073u32 cfg80211_calculate_bitrate(struct rate_info *rate)
254416aa
JL
1074{
1075 int modulation, streams, bitrate;
1076
db9c64cf
JB
1077 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1078 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
254416aa 1079 return rate->legacy;
95ddc1fc
VK
1080 if (rate->flags & RATE_INFO_FLAGS_60G)
1081 return cfg80211_calculate_bitrate_60g(rate);
db9c64cf
JB
1082 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1083 return cfg80211_calculate_bitrate_vht(rate);
254416aa
JL
1084
1085 /* the formula below does only work for MCS values smaller than 32 */
2615f375 1086 if (WARN_ON_ONCE(rate->mcs >= 32))
254416aa
JL
1087 return 0;
1088
1089 modulation = rate->mcs & 7;
1090 streams = (rate->mcs >> 3) + 1;
1091
1092 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1093 13500000 : 6500000;
1094
1095 if (modulation < 4)
1096 bitrate *= (modulation + 1);
1097 else if (modulation == 4)
1098 bitrate *= (modulation + 2);
1099 else
1100 bitrate *= (modulation + 3);
1101
1102 bitrate *= streams;
1103
1104 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1105 bitrate = (bitrate / 9) * 10;
1106
1107 /* do NOT round down here */
1108 return (bitrate + 50000) / 100000;
1109}
8097e149 1110EXPORT_SYMBOL(cfg80211_calculate_bitrate);
56d1893d 1111
c216e641
AS
1112int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1113 enum ieee80211_p2p_attr_id attr,
1114 u8 *buf, unsigned int bufsize)
0ee45355
JB
1115{
1116 u8 *out = buf;
1117 u16 attr_remaining = 0;
1118 bool desired_attr = false;
1119 u16 desired_len = 0;
1120
1121 while (len > 0) {
1122 unsigned int iedatalen;
1123 unsigned int copy;
1124 const u8 *iedata;
1125
1126 if (len < 2)
1127 return -EILSEQ;
1128 iedatalen = ies[1];
1129 if (iedatalen + 2 > len)
1130 return -EILSEQ;
1131
1132 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1133 goto cont;
1134
1135 if (iedatalen < 4)
1136 goto cont;
1137
1138 iedata = ies + 2;
1139
1140 /* check WFA OUI, P2P subtype */
1141 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1142 iedata[2] != 0x9a || iedata[3] != 0x09)
1143 goto cont;
1144
1145 iedatalen -= 4;
1146 iedata += 4;
1147
1148 /* check attribute continuation into this IE */
1149 copy = min_t(unsigned int, attr_remaining, iedatalen);
1150 if (copy && desired_attr) {
1151 desired_len += copy;
1152 if (out) {
1153 memcpy(out, iedata, min(bufsize, copy));
1154 out += min(bufsize, copy);
1155 bufsize -= min(bufsize, copy);
1156 }
1157
1158
1159 if (copy == attr_remaining)
1160 return desired_len;
1161 }
1162
1163 attr_remaining -= copy;
1164 if (attr_remaining)
1165 goto cont;
1166
1167 iedatalen -= copy;
1168 iedata += copy;
1169
1170 while (iedatalen > 0) {
1171 u16 attr_len;
1172
1173 /* P2P attribute ID & size must fit */
1174 if (iedatalen < 3)
1175 return -EILSEQ;
1176 desired_attr = iedata[0] == attr;
1177 attr_len = get_unaligned_le16(iedata + 1);
1178 iedatalen -= 3;
1179 iedata += 3;
1180
1181 copy = min_t(unsigned int, attr_len, iedatalen);
1182
1183 if (desired_attr) {
1184 desired_len += copy;
1185 if (out) {
1186 memcpy(out, iedata, min(bufsize, copy));
1187 out += min(bufsize, copy);
1188 bufsize -= min(bufsize, copy);
1189 }
1190
1191 if (copy == attr_len)
1192 return desired_len;
1193 }
1194
1195 iedata += copy;
1196 iedatalen -= copy;
1197 attr_remaining = attr_len - copy;
1198 }
1199
1200 cont:
1201 len -= ies[1] + 2;
1202 ies += ies[1] + 2;
1203 }
1204
1205 if (attr_remaining && desired_attr)
1206 return -EILSEQ;
1207
1208 return -ENOENT;
1209}
1210EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1211
1ce3e82b
JB
1212bool ieee80211_operating_class_to_band(u8 operating_class,
1213 enum ieee80211_band *band)
1214{
1215 switch (operating_class) {
1216 case 112:
1217 case 115 ... 127:
1218 *band = IEEE80211_BAND_5GHZ;
1219 return true;
1220 case 81:
1221 case 82:
1222 case 83:
1223 case 84:
1224 *band = IEEE80211_BAND_2GHZ;
1225 return true;
55300a13
VK
1226 case 180:
1227 *band = IEEE80211_BAND_60GHZ;
1228 return true;
1ce3e82b
JB
1229 }
1230
1231 return false;
1232}
1233EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1234
56d1893d
JB
1235int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1236 u32 beacon_int)
1237{
1238 struct wireless_dev *wdev;
1239 int res = 0;
1240
1241 if (!beacon_int)
1242 return -EINVAL;
1243
89a54e48 1244 list_for_each_entry(wdev, &rdev->wdev_list, list) {
56d1893d
JB
1245 if (!wdev->beacon_interval)
1246 continue;
1247 if (wdev->beacon_interval != beacon_int) {
1248 res = -EINVAL;
1249 break;
1250 }
1251 }
1252
56d1893d
JB
1253 return res;
1254}
7527a782 1255
d4e50c59
MK
1256int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1257 struct wireless_dev *wdev,
1258 enum nl80211_iftype iftype,
1259 struct ieee80211_channel *chan,
11c4a075
SW
1260 enum cfg80211_chan_mode chanmode,
1261 u8 radar_detect)
7527a782
JB
1262{
1263 struct wireless_dev *wdev_iter;
463454b5 1264 u32 used_iftypes = BIT(iftype);
7527a782 1265 int num[NUM_NL80211_IFTYPES];
d4e50c59
MK
1266 struct ieee80211_channel
1267 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1268 struct ieee80211_channel *ch;
1269 enum cfg80211_chan_mode chmode;
1270 int num_different_channels = 0;
7527a782 1271 int total = 1;
5336fa88 1272 bool radar_required = false;
7527a782
JB
1273 int i, j;
1274
1275 ASSERT_RTNL();
1276
11c4a075
SW
1277 if (WARN_ON(hweight32(radar_detect) > 1))
1278 return -EINVAL;
1279
1280 switch (iftype) {
1281 case NL80211_IFTYPE_ADHOC:
1282 case NL80211_IFTYPE_AP:
1283 case NL80211_IFTYPE_AP_VLAN:
1284 case NL80211_IFTYPE_MESH_POINT:
1285 case NL80211_IFTYPE_P2P_GO:
1286 case NL80211_IFTYPE_WDS:
5336fa88
SW
1287 /* if the interface could potentially choose a DFS channel,
1288 * then mark DFS as required.
1289 */
1290 if (!chan) {
1291 if (chanmode != CHAN_MODE_UNDEFINED && radar_detect)
1292 radar_required = true;
1293 break;
1294 }
1295 radar_required = !!(chan->flags & IEEE80211_CHAN_RADAR);
11c4a075
SW
1296 break;
1297 case NL80211_IFTYPE_P2P_CLIENT:
1298 case NL80211_IFTYPE_STATION:
bba87ffe 1299 case NL80211_IFTYPE_P2P_DEVICE:
11c4a075 1300 case NL80211_IFTYPE_MONITOR:
11c4a075 1301 break;
11c4a075
SW
1302 case NUM_NL80211_IFTYPES:
1303 case NL80211_IFTYPE_UNSPECIFIED:
1304 default:
1305 return -EINVAL;
1306 }
1307
1308 if (radar_required && !radar_detect)
1309 return -EINVAL;
1310
7527a782 1311 /* Always allow software iftypes */
11c4a075
SW
1312 if (rdev->wiphy.software_iftypes & BIT(iftype)) {
1313 if (radar_detect)
1314 return -EINVAL;
7527a782 1315 return 0;
11c4a075 1316 }
7527a782 1317
7527a782 1318 memset(num, 0, sizeof(num));
d4e50c59 1319 memset(used_channels, 0, sizeof(used_channels));
7527a782
JB
1320
1321 num[iftype] = 1;
1322
d4e50c59
MK
1323 switch (chanmode) {
1324 case CHAN_MODE_UNDEFINED:
1325 break;
1326 case CHAN_MODE_SHARED:
1327 WARN_ON(!chan);
1328 used_channels[0] = chan;
1329 num_different_channels++;
1330 break;
1331 case CHAN_MODE_EXCLUSIVE:
1332 num_different_channels++;
1333 break;
1334 }
1335
89a54e48 1336 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
7527a782
JB
1337 if (wdev_iter == wdev)
1338 continue;
6e3ab554 1339 if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
98104fde
JB
1340 if (!wdev_iter->p2p_started)
1341 continue;
6e3ab554
JB
1342 } else if (wdev_iter->netdev) {
1343 if (!netif_running(wdev_iter->netdev))
1344 continue;
98104fde
JB
1345 } else {
1346 WARN_ON(1);
1347 }
7527a782
JB
1348
1349 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1350 continue;
1351
8e95ea49
JB
1352 /*
1353 * We may be holding the "wdev" mutex, but now need to lock
1354 * wdev_iter. This is OK because once we get here wdev_iter
1355 * is not wdev (tested above), but we need to use the nested
1356 * locking for lockdep.
1357 */
1358 mutex_lock_nested(&wdev_iter->mtx, 1);
1359 __acquire(wdev_iter->mtx);
1360 cfg80211_get_chan_state(wdev_iter, &ch, &chmode);
1361 wdev_unlock(wdev_iter);
d4e50c59
MK
1362
1363 switch (chmode) {
1364 case CHAN_MODE_UNDEFINED:
1365 break;
1366 case CHAN_MODE_SHARED:
1367 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1368 if (!used_channels[i] || used_channels[i] == ch)
1369 break;
1370
e4e32459 1371 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
d4e50c59 1372 return -EBUSY;
d4e50c59
MK
1373
1374 if (used_channels[i] == NULL) {
1375 used_channels[i] = ch;
1376 num_different_channels++;
1377 }
1378 break;
1379 case CHAN_MODE_EXCLUSIVE:
1380 num_different_channels++;
1381 break;
1382 }
1383
7527a782
JB
1384 num[wdev_iter->iftype]++;
1385 total++;
463454b5 1386 used_iftypes |= BIT(wdev_iter->iftype);
7527a782 1387 }
7527a782 1388
11c4a075 1389 if (total == 1 && !radar_detect)
8e8b41f9
JB
1390 return 0;
1391
7527a782
JB
1392 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1393 const struct ieee80211_iface_combination *c;
1394 struct ieee80211_iface_limit *limits;
463454b5 1395 u32 all_iftypes = 0;
7527a782
JB
1396
1397 c = &rdev->wiphy.iface_combinations[i];
1398
d4e50c59
MK
1399 if (total > c->max_interfaces)
1400 continue;
1401 if (num_different_channels > c->num_different_channels)
1402 continue;
1403
7527a782
JB
1404 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1405 GFP_KERNEL);
1406 if (!limits)
1407 return -ENOMEM;
7527a782
JB
1408
1409 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1410 if (rdev->wiphy.software_iftypes & BIT(iftype))
1411 continue;
1412 for (j = 0; j < c->n_limits; j++) {
463454b5 1413 all_iftypes |= limits[j].types;
e55a4046 1414 if (!(limits[j].types & BIT(iftype)))
7527a782
JB
1415 continue;
1416 if (limits[j].max < num[iftype])
1417 goto cont;
1418 limits[j].max -= num[iftype];
1419 }
1420 }
463454b5 1421
11c4a075
SW
1422 if (radar_detect && !(c->radar_detect_widths & radar_detect))
1423 goto cont;
1424
463454b5
JB
1425 /*
1426 * Finally check that all iftypes that we're currently
1427 * using are actually part of this combination. If they
1428 * aren't then we can't use this combination and have
1429 * to continue to the next.
1430 */
1431 if ((all_iftypes & used_iftypes) != used_iftypes)
1432 goto cont;
1433
1434 /*
1435 * This combination covered all interface types and
1436 * supported the requested numbers, so we're good.
1437 */
7527a782
JB
1438 kfree(limits);
1439 return 0;
1440 cont:
1441 kfree(limits);
1442 }
1443
1444 return -EBUSY;
1445}
34850ab2
JB
1446
1447int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1448 const u8 *rates, unsigned int n_rates,
1449 u32 *mask)
1450{
1451 int i, j;
1452
a401d2bb
JB
1453 if (!sband)
1454 return -EINVAL;
1455
34850ab2
JB
1456 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1457 return -EINVAL;
1458
1459 *mask = 0;
1460
1461 for (i = 0; i < n_rates; i++) {
1462 int rate = (rates[i] & 0x7f) * 5;
1463 bool found = false;
1464
1465 for (j = 0; j < sband->n_bitrates; j++) {
1466 if (sband->bitrates[j].bitrate == rate) {
1467 found = true;
1468 *mask |= BIT(j);
1469 break;
1470 }
1471 }
1472 if (!found)
1473 return -EINVAL;
1474 }
1475
1476 /*
1477 * mask must have at least one bit set here since we
1478 * didn't accept a 0-length rates array nor allowed
1479 * entries in the array that didn't exist
1480 */
1481
1482 return 0;
1483}
11a2a357 1484
bdfbec2d
IP
1485unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1486{
1487 enum ieee80211_band band;
1488 unsigned int n_channels = 0;
1489
1490 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1491 if (wiphy->bands[band])
1492 n_channels += wiphy->bands[band]->n_channels;
1493
1494 return n_channels;
1495}
1496EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1497
11a2a357
JB
1498/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1499/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1500const unsigned char rfc1042_header[] __aligned(2) =
1501 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1502EXPORT_SYMBOL(rfc1042_header);
1503
1504/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1505const unsigned char bridge_tunnel_header[] __aligned(2) =
1506 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1507EXPORT_SYMBOL(bridge_tunnel_header);