Loading...
1/*
2 * Wireless utility functions
3 *
4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 */
7#include <linux/export.h>
8#include <linux/bitops.h>
9#include <linux/etherdevice.h>
10#include <linux/slab.h>
11#include <net/cfg80211.h>
12#include <net/ip.h>
13#include <net/dsfield.h>
14#include <linux/if_vlan.h>
15#include <linux/mpls.h>
16#include <linux/gcd.h>
17#include "core.h"
18#include "rdev-ops.h"
19
20
21struct ieee80211_rate *
22ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
23 u32 basic_rates, int bitrate)
24{
25 struct ieee80211_rate *result = &sband->bitrates[0];
26 int i;
27
28 for (i = 0; i < sband->n_bitrates; i++) {
29 if (!(basic_rates & BIT(i)))
30 continue;
31 if (sband->bitrates[i].bitrate > bitrate)
32 continue;
33 result = &sband->bitrates[i];
34 }
35
36 return result;
37}
38EXPORT_SYMBOL(ieee80211_get_response_rate);
39
40u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
41 enum nl80211_bss_scan_width scan_width)
42{
43 struct ieee80211_rate *bitrates;
44 u32 mandatory_rates = 0;
45 enum ieee80211_rate_flags mandatory_flag;
46 int i;
47
48 if (WARN_ON(!sband))
49 return 1;
50
51 if (sband->band == NL80211_BAND_2GHZ) {
52 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
53 scan_width == NL80211_BSS_CHAN_WIDTH_10)
54 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
55 else
56 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
57 } else {
58 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
59 }
60
61 bitrates = sband->bitrates;
62 for (i = 0; i < sband->n_bitrates; i++)
63 if (bitrates[i].flags & mandatory_flag)
64 mandatory_rates |= BIT(i);
65 return mandatory_rates;
66}
67EXPORT_SYMBOL(ieee80211_mandatory_rates);
68
69int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
70{
71 /* see 802.11 17.3.8.3.2 and Annex J
72 * there are overlapping channel numbers in 5GHz and 2GHz bands */
73 if (chan <= 0)
74 return 0; /* not supported */
75 switch (band) {
76 case NL80211_BAND_2GHZ:
77 if (chan == 14)
78 return 2484;
79 else if (chan < 14)
80 return 2407 + chan * 5;
81 break;
82 case NL80211_BAND_5GHZ:
83 if (chan >= 182 && chan <= 196)
84 return 4000 + chan * 5;
85 else
86 return 5000 + chan * 5;
87 break;
88 case NL80211_BAND_60GHZ:
89 if (chan < 5)
90 return 56160 + chan * 2160;
91 break;
92 default:
93 ;
94 }
95 return 0; /* not supported */
96}
97EXPORT_SYMBOL(ieee80211_channel_to_frequency);
98
99int ieee80211_frequency_to_channel(int freq)
100{
101 /* see 802.11 17.3.8.3.2 and Annex J */
102 if (freq == 2484)
103 return 14;
104 else if (freq < 2484)
105 return (freq - 2407) / 5;
106 else if (freq >= 4910 && freq <= 4980)
107 return (freq - 4000) / 5;
108 else if (freq <= 45000) /* DMG band lower limit */
109 return (freq - 5000) / 5;
110 else if (freq >= 58320 && freq <= 64800)
111 return (freq - 56160) / 2160;
112 else
113 return 0;
114}
115EXPORT_SYMBOL(ieee80211_frequency_to_channel);
116
117struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
118 int freq)
119{
120 enum nl80211_band band;
121 struct ieee80211_supported_band *sband;
122 int i;
123
124 for (band = 0; band < NUM_NL80211_BANDS; band++) {
125 sband = wiphy->bands[band];
126
127 if (!sband)
128 continue;
129
130 for (i = 0; i < sband->n_channels; i++) {
131 if (sband->channels[i].center_freq == freq)
132 return &sband->channels[i];
133 }
134 }
135
136 return NULL;
137}
138EXPORT_SYMBOL(__ieee80211_get_channel);
139
140static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
141 enum nl80211_band band)
142{
143 int i, want;
144
145 switch (band) {
146 case NL80211_BAND_5GHZ:
147 want = 3;
148 for (i = 0; i < sband->n_bitrates; i++) {
149 if (sband->bitrates[i].bitrate == 60 ||
150 sband->bitrates[i].bitrate == 120 ||
151 sband->bitrates[i].bitrate == 240) {
152 sband->bitrates[i].flags |=
153 IEEE80211_RATE_MANDATORY_A;
154 want--;
155 }
156 }
157 WARN_ON(want);
158 break;
159 case NL80211_BAND_2GHZ:
160 want = 7;
161 for (i = 0; i < sband->n_bitrates; i++) {
162 if (sband->bitrates[i].bitrate == 10) {
163 sband->bitrates[i].flags |=
164 IEEE80211_RATE_MANDATORY_B |
165 IEEE80211_RATE_MANDATORY_G;
166 want--;
167 }
168
169 if (sband->bitrates[i].bitrate == 20 ||
170 sband->bitrates[i].bitrate == 55 ||
171 sband->bitrates[i].bitrate == 110 ||
172 sband->bitrates[i].bitrate == 60 ||
173 sband->bitrates[i].bitrate == 120 ||
174 sband->bitrates[i].bitrate == 240) {
175 sband->bitrates[i].flags |=
176 IEEE80211_RATE_MANDATORY_G;
177 want--;
178 }
179
180 if (sband->bitrates[i].bitrate != 10 &&
181 sband->bitrates[i].bitrate != 20 &&
182 sband->bitrates[i].bitrate != 55 &&
183 sband->bitrates[i].bitrate != 110)
184 sband->bitrates[i].flags |=
185 IEEE80211_RATE_ERP_G;
186 }
187 WARN_ON(want != 0 && want != 3 && want != 6);
188 break;
189 case NL80211_BAND_60GHZ:
190 /* check for mandatory HT MCS 1..4 */
191 WARN_ON(!sband->ht_cap.ht_supported);
192 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
193 break;
194 case NUM_NL80211_BANDS:
195 WARN_ON(1);
196 break;
197 }
198}
199
200void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
201{
202 enum nl80211_band band;
203
204 for (band = 0; band < NUM_NL80211_BANDS; band++)
205 if (wiphy->bands[band])
206 set_mandatory_flags_band(wiphy->bands[band], band);
207}
208
209bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
210{
211 int i;
212 for (i = 0; i < wiphy->n_cipher_suites; i++)
213 if (cipher == wiphy->cipher_suites[i])
214 return true;
215 return false;
216}
217
218int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
219 struct key_params *params, int key_idx,
220 bool pairwise, const u8 *mac_addr)
221{
222 if (key_idx < 0 || key_idx > 5)
223 return -EINVAL;
224
225 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
226 return -EINVAL;
227
228 if (pairwise && !mac_addr)
229 return -EINVAL;
230
231 switch (params->cipher) {
232 case WLAN_CIPHER_SUITE_TKIP:
233 case WLAN_CIPHER_SUITE_CCMP:
234 case WLAN_CIPHER_SUITE_CCMP_256:
235 case WLAN_CIPHER_SUITE_GCMP:
236 case WLAN_CIPHER_SUITE_GCMP_256:
237 /* Disallow pairwise keys with non-zero index unless it's WEP
238 * or a vendor specific cipher (because current deployments use
239 * pairwise WEP keys with non-zero indices and for vendor
240 * specific ciphers this should be validated in the driver or
241 * hardware level - but 802.11i clearly specifies to use zero)
242 */
243 if (pairwise && key_idx)
244 return -EINVAL;
245 break;
246 case WLAN_CIPHER_SUITE_AES_CMAC:
247 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
248 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
249 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
250 /* Disallow BIP (group-only) cipher as pairwise cipher */
251 if (pairwise)
252 return -EINVAL;
253 if (key_idx < 4)
254 return -EINVAL;
255 break;
256 case WLAN_CIPHER_SUITE_WEP40:
257 case WLAN_CIPHER_SUITE_WEP104:
258 if (key_idx > 3)
259 return -EINVAL;
260 default:
261 break;
262 }
263
264 switch (params->cipher) {
265 case WLAN_CIPHER_SUITE_WEP40:
266 if (params->key_len != WLAN_KEY_LEN_WEP40)
267 return -EINVAL;
268 break;
269 case WLAN_CIPHER_SUITE_TKIP:
270 if (params->key_len != WLAN_KEY_LEN_TKIP)
271 return -EINVAL;
272 break;
273 case WLAN_CIPHER_SUITE_CCMP:
274 if (params->key_len != WLAN_KEY_LEN_CCMP)
275 return -EINVAL;
276 break;
277 case WLAN_CIPHER_SUITE_CCMP_256:
278 if (params->key_len != WLAN_KEY_LEN_CCMP_256)
279 return -EINVAL;
280 break;
281 case WLAN_CIPHER_SUITE_GCMP:
282 if (params->key_len != WLAN_KEY_LEN_GCMP)
283 return -EINVAL;
284 break;
285 case WLAN_CIPHER_SUITE_GCMP_256:
286 if (params->key_len != WLAN_KEY_LEN_GCMP_256)
287 return -EINVAL;
288 break;
289 case WLAN_CIPHER_SUITE_WEP104:
290 if (params->key_len != WLAN_KEY_LEN_WEP104)
291 return -EINVAL;
292 break;
293 case WLAN_CIPHER_SUITE_AES_CMAC:
294 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
295 return -EINVAL;
296 break;
297 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
298 if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
299 return -EINVAL;
300 break;
301 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
302 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
303 return -EINVAL;
304 break;
305 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
306 if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
307 return -EINVAL;
308 break;
309 default:
310 /*
311 * We don't know anything about this algorithm,
312 * allow using it -- but the driver must check
313 * all parameters! We still check below whether
314 * or not the driver supports this algorithm,
315 * of course.
316 */
317 break;
318 }
319
320 if (params->seq) {
321 switch (params->cipher) {
322 case WLAN_CIPHER_SUITE_WEP40:
323 case WLAN_CIPHER_SUITE_WEP104:
324 /* These ciphers do not use key sequence */
325 return -EINVAL;
326 case WLAN_CIPHER_SUITE_TKIP:
327 case WLAN_CIPHER_SUITE_CCMP:
328 case WLAN_CIPHER_SUITE_CCMP_256:
329 case WLAN_CIPHER_SUITE_GCMP:
330 case WLAN_CIPHER_SUITE_GCMP_256:
331 case WLAN_CIPHER_SUITE_AES_CMAC:
332 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
333 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
334 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
335 if (params->seq_len != 6)
336 return -EINVAL;
337 break;
338 }
339 }
340
341 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
342 return -EINVAL;
343
344 return 0;
345}
346
347unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
348{
349 unsigned int hdrlen = 24;
350
351 if (ieee80211_is_data(fc)) {
352 if (ieee80211_has_a4(fc))
353 hdrlen = 30;
354 if (ieee80211_is_data_qos(fc)) {
355 hdrlen += IEEE80211_QOS_CTL_LEN;
356 if (ieee80211_has_order(fc))
357 hdrlen += IEEE80211_HT_CTL_LEN;
358 }
359 goto out;
360 }
361
362 if (ieee80211_is_mgmt(fc)) {
363 if (ieee80211_has_order(fc))
364 hdrlen += IEEE80211_HT_CTL_LEN;
365 goto out;
366 }
367
368 if (ieee80211_is_ctl(fc)) {
369 /*
370 * ACK and CTS are 10 bytes, all others 16. To see how
371 * to get this condition consider
372 * subtype mask: 0b0000000011110000 (0x00F0)
373 * ACK subtype: 0b0000000011010000 (0x00D0)
374 * CTS subtype: 0b0000000011000000 (0x00C0)
375 * bits that matter: ^^^ (0x00E0)
376 * value of those: 0b0000000011000000 (0x00C0)
377 */
378 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
379 hdrlen = 10;
380 else
381 hdrlen = 16;
382 }
383out:
384 return hdrlen;
385}
386EXPORT_SYMBOL(ieee80211_hdrlen);
387
388unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
389{
390 const struct ieee80211_hdr *hdr =
391 (const struct ieee80211_hdr *)skb->data;
392 unsigned int hdrlen;
393
394 if (unlikely(skb->len < 10))
395 return 0;
396 hdrlen = ieee80211_hdrlen(hdr->frame_control);
397 if (unlikely(hdrlen > skb->len))
398 return 0;
399 return hdrlen;
400}
401EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
402
403static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
404{
405 int ae = flags & MESH_FLAGS_AE;
406 /* 802.11-2012, 8.2.4.7.3 */
407 switch (ae) {
408 default:
409 case 0:
410 return 6;
411 case MESH_FLAGS_AE_A4:
412 return 12;
413 case MESH_FLAGS_AE_A5_A6:
414 return 18;
415 }
416}
417
418unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
419{
420 return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
421}
422EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
423
424int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
425 const u8 *addr, enum nl80211_iftype iftype)
426{
427 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
428 struct {
429 u8 hdr[ETH_ALEN] __aligned(2);
430 __be16 proto;
431 } payload;
432 struct ethhdr tmp;
433 u16 hdrlen;
434 u8 mesh_flags = 0;
435
436 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
437 return -1;
438
439 hdrlen = ieee80211_hdrlen(hdr->frame_control);
440 if (skb->len < hdrlen + 8)
441 return -1;
442
443 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
444 * header
445 * IEEE 802.11 address fields:
446 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
447 * 0 0 DA SA BSSID n/a
448 * 0 1 DA BSSID SA n/a
449 * 1 0 BSSID SA DA n/a
450 * 1 1 RA TA DA SA
451 */
452 memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
453 memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
454
455 if (iftype == NL80211_IFTYPE_MESH_POINT)
456 skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
457
458 switch (hdr->frame_control &
459 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
460 case cpu_to_le16(IEEE80211_FCTL_TODS):
461 if (unlikely(iftype != NL80211_IFTYPE_AP &&
462 iftype != NL80211_IFTYPE_AP_VLAN &&
463 iftype != NL80211_IFTYPE_P2P_GO))
464 return -1;
465 break;
466 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
467 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
468 iftype != NL80211_IFTYPE_MESH_POINT &&
469 iftype != NL80211_IFTYPE_AP_VLAN &&
470 iftype != NL80211_IFTYPE_STATION))
471 return -1;
472 if (iftype == NL80211_IFTYPE_MESH_POINT) {
473 if (mesh_flags & MESH_FLAGS_AE_A4)
474 return -1;
475 if (mesh_flags & MESH_FLAGS_AE_A5_A6) {
476 skb_copy_bits(skb, hdrlen +
477 offsetof(struct ieee80211s_hdr, eaddr1),
478 tmp.h_dest, 2 * ETH_ALEN);
479 }
480 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
481 }
482 break;
483 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
484 if ((iftype != NL80211_IFTYPE_STATION &&
485 iftype != NL80211_IFTYPE_P2P_CLIENT &&
486 iftype != NL80211_IFTYPE_MESH_POINT) ||
487 (is_multicast_ether_addr(tmp.h_dest) &&
488 ether_addr_equal(tmp.h_source, addr)))
489 return -1;
490 if (iftype == NL80211_IFTYPE_MESH_POINT) {
491 if (mesh_flags & MESH_FLAGS_AE_A5_A6)
492 return -1;
493 if (mesh_flags & MESH_FLAGS_AE_A4)
494 skb_copy_bits(skb, hdrlen +
495 offsetof(struct ieee80211s_hdr, eaddr1),
496 tmp.h_source, ETH_ALEN);
497 hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
498 }
499 break;
500 case cpu_to_le16(0):
501 if (iftype != NL80211_IFTYPE_ADHOC &&
502 iftype != NL80211_IFTYPE_STATION &&
503 iftype != NL80211_IFTYPE_OCB)
504 return -1;
505 break;
506 }
507
508 skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
509 tmp.h_proto = payload.proto;
510
511 if (likely((ether_addr_equal(payload.hdr, rfc1042_header) &&
512 tmp.h_proto != htons(ETH_P_AARP) &&
513 tmp.h_proto != htons(ETH_P_IPX)) ||
514 ether_addr_equal(payload.hdr, bridge_tunnel_header)))
515 /* remove RFC1042 or Bridge-Tunnel encapsulation and
516 * replace EtherType */
517 hdrlen += ETH_ALEN + 2;
518 else
519 tmp.h_proto = htons(skb->len - hdrlen);
520
521 pskb_pull(skb, hdrlen);
522
523 if (!ehdr)
524 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
525 memcpy(ehdr, &tmp, sizeof(tmp));
526
527 return 0;
528}
529EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
530
531int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
532 enum nl80211_iftype iftype,
533 const u8 *bssid, bool qos)
534{
535 struct ieee80211_hdr hdr;
536 u16 hdrlen, ethertype;
537 __le16 fc;
538 const u8 *encaps_data;
539 int encaps_len, skip_header_bytes;
540 int nh_pos, h_pos;
541 int head_need;
542
543 if (unlikely(skb->len < ETH_HLEN))
544 return -EINVAL;
545
546 nh_pos = skb_network_header(skb) - skb->data;
547 h_pos = skb_transport_header(skb) - skb->data;
548
549 /* convert Ethernet header to proper 802.11 header (based on
550 * operation mode) */
551 ethertype = (skb->data[12] << 8) | skb->data[13];
552 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
553
554 switch (iftype) {
555 case NL80211_IFTYPE_AP:
556 case NL80211_IFTYPE_AP_VLAN:
557 case NL80211_IFTYPE_P2P_GO:
558 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
559 /* DA BSSID SA */
560 memcpy(hdr.addr1, skb->data, ETH_ALEN);
561 memcpy(hdr.addr2, addr, ETH_ALEN);
562 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
563 hdrlen = 24;
564 break;
565 case NL80211_IFTYPE_STATION:
566 case NL80211_IFTYPE_P2P_CLIENT:
567 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
568 /* BSSID SA DA */
569 memcpy(hdr.addr1, bssid, ETH_ALEN);
570 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
571 memcpy(hdr.addr3, skb->data, ETH_ALEN);
572 hdrlen = 24;
573 break;
574 case NL80211_IFTYPE_OCB:
575 case NL80211_IFTYPE_ADHOC:
576 /* DA SA BSSID */
577 memcpy(hdr.addr1, skb->data, ETH_ALEN);
578 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
579 memcpy(hdr.addr3, bssid, ETH_ALEN);
580 hdrlen = 24;
581 break;
582 default:
583 return -EOPNOTSUPP;
584 }
585
586 if (qos) {
587 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
588 hdrlen += 2;
589 }
590
591 hdr.frame_control = fc;
592 hdr.duration_id = 0;
593 hdr.seq_ctrl = 0;
594
595 skip_header_bytes = ETH_HLEN;
596 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
597 encaps_data = bridge_tunnel_header;
598 encaps_len = sizeof(bridge_tunnel_header);
599 skip_header_bytes -= 2;
600 } else if (ethertype >= ETH_P_802_3_MIN) {
601 encaps_data = rfc1042_header;
602 encaps_len = sizeof(rfc1042_header);
603 skip_header_bytes -= 2;
604 } else {
605 encaps_data = NULL;
606 encaps_len = 0;
607 }
608
609 skb_pull(skb, skip_header_bytes);
610 nh_pos -= skip_header_bytes;
611 h_pos -= skip_header_bytes;
612
613 head_need = hdrlen + encaps_len - skb_headroom(skb);
614
615 if (head_need > 0 || skb_cloned(skb)) {
616 head_need = max(head_need, 0);
617 if (head_need)
618 skb_orphan(skb);
619
620 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
621 return -ENOMEM;
622
623 skb->truesize += head_need;
624 }
625
626 if (encaps_data) {
627 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
628 nh_pos += encaps_len;
629 h_pos += encaps_len;
630 }
631
632 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
633
634 nh_pos += hdrlen;
635 h_pos += hdrlen;
636
637 /* Update skb pointers to various headers since this modified frame
638 * is going to go through Linux networking code that may potentially
639 * need things like pointer to IP header. */
640 skb_reset_mac_header(skb);
641 skb_set_network_header(skb, nh_pos);
642 skb_set_transport_header(skb, h_pos);
643
644 return 0;
645}
646EXPORT_SYMBOL(ieee80211_data_from_8023);
647
648static void
649__frame_add_frag(struct sk_buff *skb, struct page *page,
650 void *ptr, int len, int size)
651{
652 struct skb_shared_info *sh = skb_shinfo(skb);
653 int page_offset;
654
655 page_ref_inc(page);
656 page_offset = ptr - page_address(page);
657 skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
658}
659
660static void
661__ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
662 int offset, int len)
663{
664 struct skb_shared_info *sh = skb_shinfo(skb);
665 const skb_frag_t *frag = &sh->frags[-1];
666 struct page *frag_page;
667 void *frag_ptr;
668 int frag_len, frag_size;
669 int head_size = skb->len - skb->data_len;
670 int cur_len;
671
672 frag_page = virt_to_head_page(skb->head);
673 frag_ptr = skb->data;
674 frag_size = head_size;
675
676 while (offset >= frag_size) {
677 offset -= frag_size;
678 frag++;
679 frag_page = skb_frag_page(frag);
680 frag_ptr = skb_frag_address(frag);
681 frag_size = skb_frag_size(frag);
682 }
683
684 frag_ptr += offset;
685 frag_len = frag_size - offset;
686
687 cur_len = min(len, frag_len);
688
689 __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
690 len -= cur_len;
691
692 while (len > 0) {
693 frag++;
694 frag_len = skb_frag_size(frag);
695 cur_len = min(len, frag_len);
696 __frame_add_frag(frame, skb_frag_page(frag),
697 skb_frag_address(frag), cur_len, frag_len);
698 len -= cur_len;
699 }
700}
701
702static struct sk_buff *
703__ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
704 int offset, int len, bool reuse_frag)
705{
706 struct sk_buff *frame;
707 int cur_len = len;
708
709 if (skb->len - offset < len)
710 return NULL;
711
712 /*
713 * When reusing framents, copy some data to the head to simplify
714 * ethernet header handling and speed up protocol header processing
715 * in the stack later.
716 */
717 if (reuse_frag)
718 cur_len = min_t(int, len, 32);
719
720 /*
721 * Allocate and reserve two bytes more for payload
722 * alignment since sizeof(struct ethhdr) is 14.
723 */
724 frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
725 if (!frame)
726 return NULL;
727
728 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
729 skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
730
731 len -= cur_len;
732 if (!len)
733 return frame;
734
735 offset += cur_len;
736 __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
737
738 return frame;
739}
740
741void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
742 const u8 *addr, enum nl80211_iftype iftype,
743 const unsigned int extra_headroom,
744 const u8 *check_da, const u8 *check_sa)
745{
746 unsigned int hlen = ALIGN(extra_headroom, 4);
747 struct sk_buff *frame = NULL;
748 u16 ethertype;
749 u8 *payload;
750 int offset = 0, remaining;
751 struct ethhdr eth;
752 bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
753 bool reuse_skb = false;
754 bool last = false;
755
756 while (!last) {
757 unsigned int subframe_len;
758 int len;
759 u8 padding;
760
761 skb_copy_bits(skb, offset, ð, sizeof(eth));
762 len = ntohs(eth.h_proto);
763 subframe_len = sizeof(struct ethhdr) + len;
764 padding = (4 - subframe_len) & 0x3;
765
766 /* the last MSDU has no padding */
767 remaining = skb->len - offset;
768 if (subframe_len > remaining)
769 goto purge;
770
771 offset += sizeof(struct ethhdr);
772 last = remaining <= subframe_len + padding;
773
774 /* FIXME: should we really accept multicast DA? */
775 if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
776 !ether_addr_equal(check_da, eth.h_dest)) ||
777 (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
778 offset += len + padding;
779 continue;
780 }
781
782 /* reuse skb for the last subframe */
783 if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
784 skb_pull(skb, offset);
785 frame = skb;
786 reuse_skb = true;
787 } else {
788 frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
789 reuse_frag);
790 if (!frame)
791 goto purge;
792
793 offset += len + padding;
794 }
795
796 skb_reset_network_header(frame);
797 frame->dev = skb->dev;
798 frame->priority = skb->priority;
799
800 payload = frame->data;
801 ethertype = (payload[6] << 8) | payload[7];
802 if (likely((ether_addr_equal(payload, rfc1042_header) &&
803 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
804 ether_addr_equal(payload, bridge_tunnel_header))) {
805 eth.h_proto = htons(ethertype);
806 skb_pull(frame, ETH_ALEN + 2);
807 }
808
809 memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth));
810 __skb_queue_tail(list, frame);
811 }
812
813 if (!reuse_skb)
814 dev_kfree_skb(skb);
815
816 return;
817
818 purge:
819 __skb_queue_purge(list);
820 dev_kfree_skb(skb);
821}
822EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
823
824/* Given a data frame determine the 802.1p/1d tag to use. */
825unsigned int cfg80211_classify8021d(struct sk_buff *skb,
826 struct cfg80211_qos_map *qos_map)
827{
828 unsigned int dscp;
829 unsigned char vlan_priority;
830
831 /* skb->priority values from 256->263 are magic values to
832 * directly indicate a specific 802.1d priority. This is used
833 * to allow 802.1d priority to be passed directly in from VLAN
834 * tags, etc.
835 */
836 if (skb->priority >= 256 && skb->priority <= 263)
837 return skb->priority - 256;
838
839 if (skb_vlan_tag_present(skb)) {
840 vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
841 >> VLAN_PRIO_SHIFT;
842 if (vlan_priority > 0)
843 return vlan_priority;
844 }
845
846 switch (skb->protocol) {
847 case htons(ETH_P_IP):
848 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
849 break;
850 case htons(ETH_P_IPV6):
851 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
852 break;
853 case htons(ETH_P_MPLS_UC):
854 case htons(ETH_P_MPLS_MC): {
855 struct mpls_label mpls_tmp, *mpls;
856
857 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
858 sizeof(*mpls), &mpls_tmp);
859 if (!mpls)
860 return 0;
861
862 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
863 >> MPLS_LS_TC_SHIFT;
864 }
865 case htons(ETH_P_80221):
866 /* 802.21 is always network control traffic */
867 return 7;
868 default:
869 return 0;
870 }
871
872 if (qos_map) {
873 unsigned int i, tmp_dscp = dscp >> 2;
874
875 for (i = 0; i < qos_map->num_des; i++) {
876 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
877 return qos_map->dscp_exception[i].up;
878 }
879
880 for (i = 0; i < 8; i++) {
881 if (tmp_dscp >= qos_map->up[i].low &&
882 tmp_dscp <= qos_map->up[i].high)
883 return i;
884 }
885 }
886
887 return dscp >> 5;
888}
889EXPORT_SYMBOL(cfg80211_classify8021d);
890
891const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
892{
893 const struct cfg80211_bss_ies *ies;
894
895 ies = rcu_dereference(bss->ies);
896 if (!ies)
897 return NULL;
898
899 return cfg80211_find_ie(ie, ies->data, ies->len);
900}
901EXPORT_SYMBOL(ieee80211_bss_get_ie);
902
903void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
904{
905 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
906 struct net_device *dev = wdev->netdev;
907 int i;
908
909 if (!wdev->connect_keys)
910 return;
911
912 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
913 if (!wdev->connect_keys->params[i].cipher)
914 continue;
915 if (rdev_add_key(rdev, dev, i, false, NULL,
916 &wdev->connect_keys->params[i])) {
917 netdev_err(dev, "failed to set key %d\n", i);
918 continue;
919 }
920 if (wdev->connect_keys->def == i)
921 if (rdev_set_default_key(rdev, dev, i, true, true)) {
922 netdev_err(dev, "failed to set defkey %d\n", i);
923 continue;
924 }
925 }
926
927 kzfree(wdev->connect_keys);
928 wdev->connect_keys = NULL;
929}
930
931void cfg80211_process_wdev_events(struct wireless_dev *wdev)
932{
933 struct cfg80211_event *ev;
934 unsigned long flags;
935 const u8 *bssid = NULL;
936
937 spin_lock_irqsave(&wdev->event_lock, flags);
938 while (!list_empty(&wdev->event_list)) {
939 ev = list_first_entry(&wdev->event_list,
940 struct cfg80211_event, list);
941 list_del(&ev->list);
942 spin_unlock_irqrestore(&wdev->event_lock, flags);
943
944 wdev_lock(wdev);
945 switch (ev->type) {
946 case EVENT_CONNECT_RESULT:
947 if (!is_zero_ether_addr(ev->cr.bssid))
948 bssid = ev->cr.bssid;
949 __cfg80211_connect_result(
950 wdev->netdev, bssid,
951 ev->cr.req_ie, ev->cr.req_ie_len,
952 ev->cr.resp_ie, ev->cr.resp_ie_len,
953 ev->cr.status,
954 ev->cr.status == WLAN_STATUS_SUCCESS,
955 ev->cr.bss);
956 break;
957 case EVENT_ROAMED:
958 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
959 ev->rm.req_ie_len, ev->rm.resp_ie,
960 ev->rm.resp_ie_len);
961 break;
962 case EVENT_DISCONNECTED:
963 __cfg80211_disconnected(wdev->netdev,
964 ev->dc.ie, ev->dc.ie_len,
965 ev->dc.reason,
966 !ev->dc.locally_generated);
967 break;
968 case EVENT_IBSS_JOINED:
969 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
970 ev->ij.channel);
971 break;
972 case EVENT_STOPPED:
973 __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
974 break;
975 }
976 wdev_unlock(wdev);
977
978 kfree(ev);
979
980 spin_lock_irqsave(&wdev->event_lock, flags);
981 }
982 spin_unlock_irqrestore(&wdev->event_lock, flags);
983}
984
985void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
986{
987 struct wireless_dev *wdev;
988
989 ASSERT_RTNL();
990
991 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
992 cfg80211_process_wdev_events(wdev);
993}
994
995int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
996 struct net_device *dev, enum nl80211_iftype ntype,
997 u32 *flags, struct vif_params *params)
998{
999 int err;
1000 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
1001
1002 ASSERT_RTNL();
1003
1004 /* don't support changing VLANs, you just re-create them */
1005 if (otype == NL80211_IFTYPE_AP_VLAN)
1006 return -EOPNOTSUPP;
1007
1008 /* cannot change into P2P device or NAN */
1009 if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
1010 ntype == NL80211_IFTYPE_NAN)
1011 return -EOPNOTSUPP;
1012
1013 if (!rdev->ops->change_virtual_intf ||
1014 !(rdev->wiphy.interface_modes & (1 << ntype)))
1015 return -EOPNOTSUPP;
1016
1017 /* if it's part of a bridge, reject changing type to station/ibss */
1018 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
1019 (ntype == NL80211_IFTYPE_ADHOC ||
1020 ntype == NL80211_IFTYPE_STATION ||
1021 ntype == NL80211_IFTYPE_P2P_CLIENT))
1022 return -EBUSY;
1023
1024 if (ntype != otype) {
1025 dev->ieee80211_ptr->use_4addr = false;
1026 dev->ieee80211_ptr->mesh_id_up_len = 0;
1027 wdev_lock(dev->ieee80211_ptr);
1028 rdev_set_qos_map(rdev, dev, NULL);
1029 wdev_unlock(dev->ieee80211_ptr);
1030
1031 switch (otype) {
1032 case NL80211_IFTYPE_AP:
1033 cfg80211_stop_ap(rdev, dev, true);
1034 break;
1035 case NL80211_IFTYPE_ADHOC:
1036 cfg80211_leave_ibss(rdev, dev, false);
1037 break;
1038 case NL80211_IFTYPE_STATION:
1039 case NL80211_IFTYPE_P2P_CLIENT:
1040 wdev_lock(dev->ieee80211_ptr);
1041 cfg80211_disconnect(rdev, dev,
1042 WLAN_REASON_DEAUTH_LEAVING, true);
1043 wdev_unlock(dev->ieee80211_ptr);
1044 break;
1045 case NL80211_IFTYPE_MESH_POINT:
1046 /* mesh should be handled? */
1047 break;
1048 default:
1049 break;
1050 }
1051
1052 cfg80211_process_rdev_events(rdev);
1053 }
1054
1055 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
1056
1057 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
1058
1059 if (!err && params && params->use_4addr != -1)
1060 dev->ieee80211_ptr->use_4addr = params->use_4addr;
1061
1062 if (!err) {
1063 dev->priv_flags &= ~IFF_DONT_BRIDGE;
1064 switch (ntype) {
1065 case NL80211_IFTYPE_STATION:
1066 if (dev->ieee80211_ptr->use_4addr)
1067 break;
1068 /* fall through */
1069 case NL80211_IFTYPE_OCB:
1070 case NL80211_IFTYPE_P2P_CLIENT:
1071 case NL80211_IFTYPE_ADHOC:
1072 dev->priv_flags |= IFF_DONT_BRIDGE;
1073 break;
1074 case NL80211_IFTYPE_P2P_GO:
1075 case NL80211_IFTYPE_AP:
1076 case NL80211_IFTYPE_AP_VLAN:
1077 case NL80211_IFTYPE_WDS:
1078 case NL80211_IFTYPE_MESH_POINT:
1079 /* bridging OK */
1080 break;
1081 case NL80211_IFTYPE_MONITOR:
1082 /* monitor can't bridge anyway */
1083 break;
1084 case NL80211_IFTYPE_UNSPECIFIED:
1085 case NUM_NL80211_IFTYPES:
1086 /* not happening */
1087 break;
1088 case NL80211_IFTYPE_P2P_DEVICE:
1089 case NL80211_IFTYPE_NAN:
1090 WARN_ON(1);
1091 break;
1092 }
1093 }
1094
1095 if (!err && ntype != otype && netif_running(dev)) {
1096 cfg80211_update_iface_num(rdev, ntype, 1);
1097 cfg80211_update_iface_num(rdev, otype, -1);
1098 }
1099
1100 return err;
1101}
1102
1103static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
1104{
1105 static const u32 __mcs2bitrate[] = {
1106 /* control PHY */
1107 [0] = 275,
1108 /* SC PHY */
1109 [1] = 3850,
1110 [2] = 7700,
1111 [3] = 9625,
1112 [4] = 11550,
1113 [5] = 12512, /* 1251.25 mbps */
1114 [6] = 15400,
1115 [7] = 19250,
1116 [8] = 23100,
1117 [9] = 25025,
1118 [10] = 30800,
1119 [11] = 38500,
1120 [12] = 46200,
1121 /* OFDM PHY */
1122 [13] = 6930,
1123 [14] = 8662, /* 866.25 mbps */
1124 [15] = 13860,
1125 [16] = 17325,
1126 [17] = 20790,
1127 [18] = 27720,
1128 [19] = 34650,
1129 [20] = 41580,
1130 [21] = 45045,
1131 [22] = 51975,
1132 [23] = 62370,
1133 [24] = 67568, /* 6756.75 mbps */
1134 /* LP-SC PHY */
1135 [25] = 6260,
1136 [26] = 8340,
1137 [27] = 11120,
1138 [28] = 12510,
1139 [29] = 16680,
1140 [30] = 22240,
1141 [31] = 25030,
1142 };
1143
1144 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1145 return 0;
1146
1147 return __mcs2bitrate[rate->mcs];
1148}
1149
1150static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1151{
1152 static const u32 base[4][10] = {
1153 { 6500000,
1154 13000000,
1155 19500000,
1156 26000000,
1157 39000000,
1158 52000000,
1159 58500000,
1160 65000000,
1161 78000000,
1162 /* not in the spec, but some devices use this: */
1163 86500000,
1164 },
1165 { 13500000,
1166 27000000,
1167 40500000,
1168 54000000,
1169 81000000,
1170 108000000,
1171 121500000,
1172 135000000,
1173 162000000,
1174 180000000,
1175 },
1176 { 29300000,
1177 58500000,
1178 87800000,
1179 117000000,
1180 175500000,
1181 234000000,
1182 263300000,
1183 292500000,
1184 351000000,
1185 390000000,
1186 },
1187 { 58500000,
1188 117000000,
1189 175500000,
1190 234000000,
1191 351000000,
1192 468000000,
1193 526500000,
1194 585000000,
1195 702000000,
1196 780000000,
1197 },
1198 };
1199 u32 bitrate;
1200 int idx;
1201
1202 if (WARN_ON_ONCE(rate->mcs > 9))
1203 return 0;
1204
1205 switch (rate->bw) {
1206 case RATE_INFO_BW_160:
1207 idx = 3;
1208 break;
1209 case RATE_INFO_BW_80:
1210 idx = 2;
1211 break;
1212 case RATE_INFO_BW_40:
1213 idx = 1;
1214 break;
1215 case RATE_INFO_BW_5:
1216 case RATE_INFO_BW_10:
1217 default:
1218 WARN_ON(1);
1219 /* fall through */
1220 case RATE_INFO_BW_20:
1221 idx = 0;
1222 }
1223
1224 bitrate = base[idx][rate->mcs];
1225 bitrate *= rate->nss;
1226
1227 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1228 bitrate = (bitrate / 9) * 10;
1229
1230 /* do NOT round down here */
1231 return (bitrate + 50000) / 100000;
1232}
1233
1234u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1235{
1236 int modulation, streams, bitrate;
1237
1238 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1239 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
1240 return rate->legacy;
1241 if (rate->flags & RATE_INFO_FLAGS_60G)
1242 return cfg80211_calculate_bitrate_60g(rate);
1243 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1244 return cfg80211_calculate_bitrate_vht(rate);
1245
1246 /* the formula below does only work for MCS values smaller than 32 */
1247 if (WARN_ON_ONCE(rate->mcs >= 32))
1248 return 0;
1249
1250 modulation = rate->mcs & 7;
1251 streams = (rate->mcs >> 3) + 1;
1252
1253 bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
1254
1255 if (modulation < 4)
1256 bitrate *= (modulation + 1);
1257 else if (modulation == 4)
1258 bitrate *= (modulation + 2);
1259 else
1260 bitrate *= (modulation + 3);
1261
1262 bitrate *= streams;
1263
1264 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1265 bitrate = (bitrate / 9) * 10;
1266
1267 /* do NOT round down here */
1268 return (bitrate + 50000) / 100000;
1269}
1270EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1271
1272int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1273 enum ieee80211_p2p_attr_id attr,
1274 u8 *buf, unsigned int bufsize)
1275{
1276 u8 *out = buf;
1277 u16 attr_remaining = 0;
1278 bool desired_attr = false;
1279 u16 desired_len = 0;
1280
1281 while (len > 0) {
1282 unsigned int iedatalen;
1283 unsigned int copy;
1284 const u8 *iedata;
1285
1286 if (len < 2)
1287 return -EILSEQ;
1288 iedatalen = ies[1];
1289 if (iedatalen + 2 > len)
1290 return -EILSEQ;
1291
1292 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1293 goto cont;
1294
1295 if (iedatalen < 4)
1296 goto cont;
1297
1298 iedata = ies + 2;
1299
1300 /* check WFA OUI, P2P subtype */
1301 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1302 iedata[2] != 0x9a || iedata[3] != 0x09)
1303 goto cont;
1304
1305 iedatalen -= 4;
1306 iedata += 4;
1307
1308 /* check attribute continuation into this IE */
1309 copy = min_t(unsigned int, attr_remaining, iedatalen);
1310 if (copy && desired_attr) {
1311 desired_len += copy;
1312 if (out) {
1313 memcpy(out, iedata, min(bufsize, copy));
1314 out += min(bufsize, copy);
1315 bufsize -= min(bufsize, copy);
1316 }
1317
1318
1319 if (copy == attr_remaining)
1320 return desired_len;
1321 }
1322
1323 attr_remaining -= copy;
1324 if (attr_remaining)
1325 goto cont;
1326
1327 iedatalen -= copy;
1328 iedata += copy;
1329
1330 while (iedatalen > 0) {
1331 u16 attr_len;
1332
1333 /* P2P attribute ID & size must fit */
1334 if (iedatalen < 3)
1335 return -EILSEQ;
1336 desired_attr = iedata[0] == attr;
1337 attr_len = get_unaligned_le16(iedata + 1);
1338 iedatalen -= 3;
1339 iedata += 3;
1340
1341 copy = min_t(unsigned int, attr_len, iedatalen);
1342
1343 if (desired_attr) {
1344 desired_len += copy;
1345 if (out) {
1346 memcpy(out, iedata, min(bufsize, copy));
1347 out += min(bufsize, copy);
1348 bufsize -= min(bufsize, copy);
1349 }
1350
1351 if (copy == attr_len)
1352 return desired_len;
1353 }
1354
1355 iedata += copy;
1356 iedatalen -= copy;
1357 attr_remaining = attr_len - copy;
1358 }
1359
1360 cont:
1361 len -= ies[1] + 2;
1362 ies += ies[1] + 2;
1363 }
1364
1365 if (attr_remaining && desired_attr)
1366 return -EILSEQ;
1367
1368 return -ENOENT;
1369}
1370EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1371
1372static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1373{
1374 int i;
1375
1376 for (i = 0; i < n_ids; i++)
1377 if (ids[i] == id)
1378 return true;
1379 return false;
1380}
1381
1382static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
1383{
1384 /* we assume a validly formed IEs buffer */
1385 u8 len = ies[pos + 1];
1386
1387 pos += 2 + len;
1388
1389 /* the IE itself must have 255 bytes for fragments to follow */
1390 if (len < 255)
1391 return pos;
1392
1393 while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
1394 len = ies[pos + 1];
1395 pos += 2 + len;
1396 }
1397
1398 return pos;
1399}
1400
1401size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1402 const u8 *ids, int n_ids,
1403 const u8 *after_ric, int n_after_ric,
1404 size_t offset)
1405{
1406 size_t pos = offset;
1407
1408 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
1409 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1410 pos = skip_ie(ies, ielen, pos);
1411
1412 while (pos < ielen &&
1413 !ieee80211_id_in_list(after_ric, n_after_ric,
1414 ies[pos]))
1415 pos = skip_ie(ies, ielen, pos);
1416 } else {
1417 pos = skip_ie(ies, ielen, pos);
1418 }
1419 }
1420
1421 return pos;
1422}
1423EXPORT_SYMBOL(ieee80211_ie_split_ric);
1424
1425bool ieee80211_operating_class_to_band(u8 operating_class,
1426 enum nl80211_band *band)
1427{
1428 switch (operating_class) {
1429 case 112:
1430 case 115 ... 127:
1431 case 128 ... 130:
1432 *band = NL80211_BAND_5GHZ;
1433 return true;
1434 case 81:
1435 case 82:
1436 case 83:
1437 case 84:
1438 *band = NL80211_BAND_2GHZ;
1439 return true;
1440 case 180:
1441 *band = NL80211_BAND_60GHZ;
1442 return true;
1443 }
1444
1445 return false;
1446}
1447EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1448
1449bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1450 u8 *op_class)
1451{
1452 u8 vht_opclass;
1453 u16 freq = chandef->center_freq1;
1454
1455 if (freq >= 2412 && freq <= 2472) {
1456 if (chandef->width > NL80211_CHAN_WIDTH_40)
1457 return false;
1458
1459 /* 2.407 GHz, channels 1..13 */
1460 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1461 if (freq > chandef->chan->center_freq)
1462 *op_class = 83; /* HT40+ */
1463 else
1464 *op_class = 84; /* HT40- */
1465 } else {
1466 *op_class = 81;
1467 }
1468
1469 return true;
1470 }
1471
1472 if (freq == 2484) {
1473 if (chandef->width > NL80211_CHAN_WIDTH_40)
1474 return false;
1475
1476 *op_class = 82; /* channel 14 */
1477 return true;
1478 }
1479
1480 switch (chandef->width) {
1481 case NL80211_CHAN_WIDTH_80:
1482 vht_opclass = 128;
1483 break;
1484 case NL80211_CHAN_WIDTH_160:
1485 vht_opclass = 129;
1486 break;
1487 case NL80211_CHAN_WIDTH_80P80:
1488 vht_opclass = 130;
1489 break;
1490 case NL80211_CHAN_WIDTH_10:
1491 case NL80211_CHAN_WIDTH_5:
1492 return false; /* unsupported for now */
1493 default:
1494 vht_opclass = 0;
1495 break;
1496 }
1497
1498 /* 5 GHz, channels 36..48 */
1499 if (freq >= 5180 && freq <= 5240) {
1500 if (vht_opclass) {
1501 *op_class = vht_opclass;
1502 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1503 if (freq > chandef->chan->center_freq)
1504 *op_class = 116;
1505 else
1506 *op_class = 117;
1507 } else {
1508 *op_class = 115;
1509 }
1510
1511 return true;
1512 }
1513
1514 /* 5 GHz, channels 52..64 */
1515 if (freq >= 5260 && freq <= 5320) {
1516 if (vht_opclass) {
1517 *op_class = vht_opclass;
1518 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1519 if (freq > chandef->chan->center_freq)
1520 *op_class = 119;
1521 else
1522 *op_class = 120;
1523 } else {
1524 *op_class = 118;
1525 }
1526
1527 return true;
1528 }
1529
1530 /* 5 GHz, channels 100..144 */
1531 if (freq >= 5500 && freq <= 5720) {
1532 if (vht_opclass) {
1533 *op_class = vht_opclass;
1534 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1535 if (freq > chandef->chan->center_freq)
1536 *op_class = 122;
1537 else
1538 *op_class = 123;
1539 } else {
1540 *op_class = 121;
1541 }
1542
1543 return true;
1544 }
1545
1546 /* 5 GHz, channels 149..169 */
1547 if (freq >= 5745 && freq <= 5845) {
1548 if (vht_opclass) {
1549 *op_class = vht_opclass;
1550 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1551 if (freq > chandef->chan->center_freq)
1552 *op_class = 126;
1553 else
1554 *op_class = 127;
1555 } else if (freq <= 5805) {
1556 *op_class = 124;
1557 } else {
1558 *op_class = 125;
1559 }
1560
1561 return true;
1562 }
1563
1564 /* 56.16 GHz, channel 1..4 */
1565 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
1566 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1567 return false;
1568
1569 *op_class = 180;
1570 return true;
1571 }
1572
1573 /* not supported yet */
1574 return false;
1575}
1576EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1577
1578static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
1579 u32 *beacon_int_gcd,
1580 bool *beacon_int_different)
1581{
1582 struct wireless_dev *wdev;
1583
1584 *beacon_int_gcd = 0;
1585 *beacon_int_different = false;
1586
1587 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
1588 if (!wdev->beacon_interval)
1589 continue;
1590
1591 if (!*beacon_int_gcd) {
1592 *beacon_int_gcd = wdev->beacon_interval;
1593 continue;
1594 }
1595
1596 if (wdev->beacon_interval == *beacon_int_gcd)
1597 continue;
1598
1599 *beacon_int_different = true;
1600 *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
1601 }
1602
1603 if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
1604 if (*beacon_int_gcd)
1605 *beacon_int_different = true;
1606 *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
1607 }
1608}
1609
1610int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1611 enum nl80211_iftype iftype, u32 beacon_int)
1612{
1613 /*
1614 * This is just a basic pre-condition check; if interface combinations
1615 * are possible the driver must already be checking those with a call
1616 * to cfg80211_check_combinations(), in which case we'll validate more
1617 * through the cfg80211_calculate_bi_data() call and code in
1618 * cfg80211_iter_combinations().
1619 */
1620
1621 if (beacon_int < 10 || beacon_int > 10000)
1622 return -EINVAL;
1623
1624 return 0;
1625}
1626
1627int cfg80211_iter_combinations(struct wiphy *wiphy,
1628 struct iface_combination_params *params,
1629 void (*iter)(const struct ieee80211_iface_combination *c,
1630 void *data),
1631 void *data)
1632{
1633 const struct ieee80211_regdomain *regdom;
1634 enum nl80211_dfs_regions region = 0;
1635 int i, j, iftype;
1636 int num_interfaces = 0;
1637 u32 used_iftypes = 0;
1638 u32 beacon_int_gcd;
1639 bool beacon_int_different;
1640
1641 /*
1642 * This is a bit strange, since the iteration used to rely only on
1643 * the data given by the driver, but here it now relies on context,
1644 * in form of the currently operating interfaces.
1645 * This is OK for all current users, and saves us from having to
1646 * push the GCD calculations into all the drivers.
1647 * In the future, this should probably rely more on data that's in
1648 * cfg80211 already - the only thing not would appear to be any new
1649 * interfaces (while being brought up) and channel/radar data.
1650 */
1651 cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
1652 &beacon_int_gcd, &beacon_int_different);
1653
1654 if (params->radar_detect) {
1655 rcu_read_lock();
1656 regdom = rcu_dereference(cfg80211_regdomain);
1657 if (regdom)
1658 region = regdom->dfs_region;
1659 rcu_read_unlock();
1660 }
1661
1662 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1663 num_interfaces += params->iftype_num[iftype];
1664 if (params->iftype_num[iftype] > 0 &&
1665 !(wiphy->software_iftypes & BIT(iftype)))
1666 used_iftypes |= BIT(iftype);
1667 }
1668
1669 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1670 const struct ieee80211_iface_combination *c;
1671 struct ieee80211_iface_limit *limits;
1672 u32 all_iftypes = 0;
1673
1674 c = &wiphy->iface_combinations[i];
1675
1676 if (num_interfaces > c->max_interfaces)
1677 continue;
1678 if (params->num_different_channels > c->num_different_channels)
1679 continue;
1680
1681 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1682 GFP_KERNEL);
1683 if (!limits)
1684 return -ENOMEM;
1685
1686 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1687 if (wiphy->software_iftypes & BIT(iftype))
1688 continue;
1689 for (j = 0; j < c->n_limits; j++) {
1690 all_iftypes |= limits[j].types;
1691 if (!(limits[j].types & BIT(iftype)))
1692 continue;
1693 if (limits[j].max < params->iftype_num[iftype])
1694 goto cont;
1695 limits[j].max -= params->iftype_num[iftype];
1696 }
1697 }
1698
1699 if (params->radar_detect !=
1700 (c->radar_detect_widths & params->radar_detect))
1701 goto cont;
1702
1703 if (params->radar_detect && c->radar_detect_regions &&
1704 !(c->radar_detect_regions & BIT(region)))
1705 goto cont;
1706
1707 /* Finally check that all iftypes that we're currently
1708 * using are actually part of this combination. If they
1709 * aren't then we can't use this combination and have
1710 * to continue to the next.
1711 */
1712 if ((all_iftypes & used_iftypes) != used_iftypes)
1713 goto cont;
1714
1715 if (beacon_int_gcd) {
1716 if (c->beacon_int_min_gcd &&
1717 beacon_int_gcd < c->beacon_int_min_gcd)
1718 goto cont;
1719 if (!c->beacon_int_min_gcd && beacon_int_different)
1720 goto cont;
1721 }
1722
1723 /* This combination covered all interface types and
1724 * supported the requested numbers, so we're good.
1725 */
1726
1727 (*iter)(c, data);
1728 cont:
1729 kfree(limits);
1730 }
1731
1732 return 0;
1733}
1734EXPORT_SYMBOL(cfg80211_iter_combinations);
1735
1736static void
1737cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
1738 void *data)
1739{
1740 int *num = data;
1741 (*num)++;
1742}
1743
1744int cfg80211_check_combinations(struct wiphy *wiphy,
1745 struct iface_combination_params *params)
1746{
1747 int err, num = 0;
1748
1749 err = cfg80211_iter_combinations(wiphy, params,
1750 cfg80211_iter_sum_ifcombs, &num);
1751 if (err)
1752 return err;
1753 if (num == 0)
1754 return -EBUSY;
1755
1756 return 0;
1757}
1758EXPORT_SYMBOL(cfg80211_check_combinations);
1759
1760int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1761 const u8 *rates, unsigned int n_rates,
1762 u32 *mask)
1763{
1764 int i, j;
1765
1766 if (!sband)
1767 return -EINVAL;
1768
1769 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1770 return -EINVAL;
1771
1772 *mask = 0;
1773
1774 for (i = 0; i < n_rates; i++) {
1775 int rate = (rates[i] & 0x7f) * 5;
1776 bool found = false;
1777
1778 for (j = 0; j < sband->n_bitrates; j++) {
1779 if (sband->bitrates[j].bitrate == rate) {
1780 found = true;
1781 *mask |= BIT(j);
1782 break;
1783 }
1784 }
1785 if (!found)
1786 return -EINVAL;
1787 }
1788
1789 /*
1790 * mask must have at least one bit set here since we
1791 * didn't accept a 0-length rates array nor allowed
1792 * entries in the array that didn't exist
1793 */
1794
1795 return 0;
1796}
1797
1798unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1799{
1800 enum nl80211_band band;
1801 unsigned int n_channels = 0;
1802
1803 for (band = 0; band < NUM_NL80211_BANDS; band++)
1804 if (wiphy->bands[band])
1805 n_channels += wiphy->bands[band]->n_channels;
1806
1807 return n_channels;
1808}
1809EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1810
1811int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
1812 struct station_info *sinfo)
1813{
1814 struct cfg80211_registered_device *rdev;
1815 struct wireless_dev *wdev;
1816
1817 wdev = dev->ieee80211_ptr;
1818 if (!wdev)
1819 return -EOPNOTSUPP;
1820
1821 rdev = wiphy_to_rdev(wdev->wiphy);
1822 if (!rdev->ops->get_station)
1823 return -EOPNOTSUPP;
1824
1825 return rdev_get_station(rdev, dev, mac_addr, sinfo);
1826}
1827EXPORT_SYMBOL(cfg80211_get_station);
1828
1829void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
1830{
1831 int i;
1832
1833 if (!f)
1834 return;
1835
1836 kfree(f->serv_spec_info);
1837 kfree(f->srf_bf);
1838 kfree(f->srf_macs);
1839 for (i = 0; i < f->num_rx_filters; i++)
1840 kfree(f->rx_filters[i].filter);
1841
1842 for (i = 0; i < f->num_tx_filters; i++)
1843 kfree(f->tx_filters[i].filter);
1844
1845 kfree(f->rx_filters);
1846 kfree(f->tx_filters);
1847 kfree(f);
1848}
1849EXPORT_SYMBOL(cfg80211_free_nan_func);
1850
1851/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1852/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1853const unsigned char rfc1042_header[] __aligned(2) =
1854 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1855EXPORT_SYMBOL(rfc1042_header);
1856
1857/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1858const unsigned char bridge_tunnel_header[] __aligned(2) =
1859 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1860EXPORT_SYMBOL(bridge_tunnel_header);
1/*
2 * Wireless utility functions
3 *
4 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
5 */
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>
11#include <net/ip.h>
12#include <net/dsfield.h>
13#include <linux/if_vlan.h>
14#include <linux/mpls.h>
15#include "core.h"
16#include "rdev-ops.h"
17
18
19struct ieee80211_rate *
20ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
21 u32 basic_rates, int bitrate)
22{
23 struct ieee80211_rate *result = &sband->bitrates[0];
24 int i;
25
26 for (i = 0; i < sband->n_bitrates; i++) {
27 if (!(basic_rates & BIT(i)))
28 continue;
29 if (sband->bitrates[i].bitrate > bitrate)
30 continue;
31 result = &sband->bitrates[i];
32 }
33
34 return result;
35}
36EXPORT_SYMBOL(ieee80211_get_response_rate);
37
38u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
39 enum nl80211_bss_scan_width scan_width)
40{
41 struct ieee80211_rate *bitrates;
42 u32 mandatory_rates = 0;
43 enum ieee80211_rate_flags mandatory_flag;
44 int i;
45
46 if (WARN_ON(!sband))
47 return 1;
48
49 if (sband->band == IEEE80211_BAND_2GHZ) {
50 if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
51 scan_width == NL80211_BSS_CHAN_WIDTH_10)
52 mandatory_flag = IEEE80211_RATE_MANDATORY_G;
53 else
54 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
55 } else {
56 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
57 }
58
59 bitrates = sband->bitrates;
60 for (i = 0; i < sband->n_bitrates; i++)
61 if (bitrates[i].flags & mandatory_flag)
62 mandatory_rates |= BIT(i);
63 return mandatory_rates;
64}
65EXPORT_SYMBOL(ieee80211_mandatory_rates);
66
67int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
68{
69 /* see 802.11 17.3.8.3.2 and Annex J
70 * there are overlapping channel numbers in 5GHz and 2GHz bands */
71 if (chan <= 0)
72 return 0; /* not supported */
73 switch (band) {
74 case IEEE80211_BAND_2GHZ:
75 if (chan == 14)
76 return 2484;
77 else if (chan < 14)
78 return 2407 + chan * 5;
79 break;
80 case IEEE80211_BAND_5GHZ:
81 if (chan >= 182 && chan <= 196)
82 return 4000 + chan * 5;
83 else
84 return 5000 + chan * 5;
85 break;
86 case IEEE80211_BAND_60GHZ:
87 if (chan < 5)
88 return 56160 + chan * 2160;
89 break;
90 default:
91 ;
92 }
93 return 0; /* not supported */
94}
95EXPORT_SYMBOL(ieee80211_channel_to_frequency);
96
97int ieee80211_frequency_to_channel(int freq)
98{
99 /* see 802.11 17.3.8.3.2 and Annex J */
100 if (freq == 2484)
101 return 14;
102 else if (freq < 2484)
103 return (freq - 2407) / 5;
104 else if (freq >= 4910 && freq <= 4980)
105 return (freq - 4000) / 5;
106 else if (freq <= 45000) /* DMG band lower limit */
107 return (freq - 5000) / 5;
108 else if (freq >= 58320 && freq <= 64800)
109 return (freq - 56160) / 2160;
110 else
111 return 0;
112}
113EXPORT_SYMBOL(ieee80211_frequency_to_channel);
114
115struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
116 int freq)
117{
118 enum ieee80211_band band;
119 struct ieee80211_supported_band *sband;
120 int i;
121
122 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
123 sband = wiphy->bands[band];
124
125 if (!sband)
126 continue;
127
128 for (i = 0; i < sband->n_channels; i++) {
129 if (sband->channels[i].center_freq == freq)
130 return &sband->channels[i];
131 }
132 }
133
134 return NULL;
135}
136EXPORT_SYMBOL(__ieee80211_get_channel);
137
138static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
139 enum ieee80211_band band)
140{
141 int i, want;
142
143 switch (band) {
144 case IEEE80211_BAND_5GHZ:
145 want = 3;
146 for (i = 0; i < sband->n_bitrates; i++) {
147 if (sband->bitrates[i].bitrate == 60 ||
148 sband->bitrates[i].bitrate == 120 ||
149 sband->bitrates[i].bitrate == 240) {
150 sband->bitrates[i].flags |=
151 IEEE80211_RATE_MANDATORY_A;
152 want--;
153 }
154 }
155 WARN_ON(want);
156 break;
157 case IEEE80211_BAND_2GHZ:
158 want = 7;
159 for (i = 0; i < sband->n_bitrates; i++) {
160 if (sband->bitrates[i].bitrate == 10) {
161 sband->bitrates[i].flags |=
162 IEEE80211_RATE_MANDATORY_B |
163 IEEE80211_RATE_MANDATORY_G;
164 want--;
165 }
166
167 if (sband->bitrates[i].bitrate == 20 ||
168 sband->bitrates[i].bitrate == 55 ||
169 sband->bitrates[i].bitrate == 110 ||
170 sband->bitrates[i].bitrate == 60 ||
171 sband->bitrates[i].bitrate == 120 ||
172 sband->bitrates[i].bitrate == 240) {
173 sband->bitrates[i].flags |=
174 IEEE80211_RATE_MANDATORY_G;
175 want--;
176 }
177
178 if (sband->bitrates[i].bitrate != 10 &&
179 sband->bitrates[i].bitrate != 20 &&
180 sband->bitrates[i].bitrate != 55 &&
181 sband->bitrates[i].bitrate != 110)
182 sband->bitrates[i].flags |=
183 IEEE80211_RATE_ERP_G;
184 }
185 WARN_ON(want != 0 && want != 3 && want != 6);
186 break;
187 case IEEE80211_BAND_60GHZ:
188 /* check for mandatory HT MCS 1..4 */
189 WARN_ON(!sband->ht_cap.ht_supported);
190 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
191 break;
192 case IEEE80211_NUM_BANDS:
193 WARN_ON(1);
194 break;
195 }
196}
197
198void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
199{
200 enum ieee80211_band band;
201
202 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
203 if (wiphy->bands[band])
204 set_mandatory_flags_band(wiphy->bands[band], band);
205}
206
207bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
208{
209 int i;
210 for (i = 0; i < wiphy->n_cipher_suites; i++)
211 if (cipher == wiphy->cipher_suites[i])
212 return true;
213 return false;
214}
215
216int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
217 struct key_params *params, int key_idx,
218 bool pairwise, const u8 *mac_addr)
219{
220 if (key_idx > 5)
221 return -EINVAL;
222
223 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
224 return -EINVAL;
225
226 if (pairwise && !mac_addr)
227 return -EINVAL;
228
229 /*
230 * Disallow pairwise keys with non-zero index unless it's WEP
231 * or a vendor specific cipher (because current deployments use
232 * pairwise WEP keys with non-zero indices and for vendor specific
233 * ciphers this should be validated in the driver or hardware level
234 * - but 802.11i clearly specifies to use zero)
235 */
236 if (pairwise && key_idx &&
237 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
238 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
239 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
240 return -EINVAL;
241
242 switch (params->cipher) {
243 case WLAN_CIPHER_SUITE_WEP40:
244 if (params->key_len != WLAN_KEY_LEN_WEP40)
245 return -EINVAL;
246 break;
247 case WLAN_CIPHER_SUITE_TKIP:
248 if (params->key_len != WLAN_KEY_LEN_TKIP)
249 return -EINVAL;
250 break;
251 case WLAN_CIPHER_SUITE_CCMP:
252 if (params->key_len != WLAN_KEY_LEN_CCMP)
253 return -EINVAL;
254 break;
255 case WLAN_CIPHER_SUITE_WEP104:
256 if (params->key_len != WLAN_KEY_LEN_WEP104)
257 return -EINVAL;
258 break;
259 case WLAN_CIPHER_SUITE_AES_CMAC:
260 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
261 return -EINVAL;
262 break;
263 default:
264 /*
265 * We don't know anything about this algorithm,
266 * allow using it -- but the driver must check
267 * all parameters! We still check below whether
268 * or not the driver supports this algorithm,
269 * of course.
270 */
271 break;
272 }
273
274 if (params->seq) {
275 switch (params->cipher) {
276 case WLAN_CIPHER_SUITE_WEP40:
277 case WLAN_CIPHER_SUITE_WEP104:
278 /* These ciphers do not use key sequence */
279 return -EINVAL;
280 case WLAN_CIPHER_SUITE_TKIP:
281 case WLAN_CIPHER_SUITE_CCMP:
282 case WLAN_CIPHER_SUITE_AES_CMAC:
283 if (params->seq_len != 6)
284 return -EINVAL;
285 break;
286 }
287 }
288
289 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
290 return -EINVAL;
291
292 return 0;
293}
294
295unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
296{
297 unsigned int hdrlen = 24;
298
299 if (ieee80211_is_data(fc)) {
300 if (ieee80211_has_a4(fc))
301 hdrlen = 30;
302 if (ieee80211_is_data_qos(fc)) {
303 hdrlen += IEEE80211_QOS_CTL_LEN;
304 if (ieee80211_has_order(fc))
305 hdrlen += IEEE80211_HT_CTL_LEN;
306 }
307 goto out;
308 }
309
310 if (ieee80211_is_ctl(fc)) {
311 /*
312 * ACK and CTS are 10 bytes, all others 16. To see how
313 * to get this condition consider
314 * subtype mask: 0b0000000011110000 (0x00F0)
315 * ACK subtype: 0b0000000011010000 (0x00D0)
316 * CTS subtype: 0b0000000011000000 (0x00C0)
317 * bits that matter: ^^^ (0x00E0)
318 * value of those: 0b0000000011000000 (0x00C0)
319 */
320 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
321 hdrlen = 10;
322 else
323 hdrlen = 16;
324 }
325out:
326 return hdrlen;
327}
328EXPORT_SYMBOL(ieee80211_hdrlen);
329
330unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
331{
332 const struct ieee80211_hdr *hdr =
333 (const struct ieee80211_hdr *)skb->data;
334 unsigned int hdrlen;
335
336 if (unlikely(skb->len < 10))
337 return 0;
338 hdrlen = ieee80211_hdrlen(hdr->frame_control);
339 if (unlikely(hdrlen > skb->len))
340 return 0;
341 return hdrlen;
342}
343EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
344
345unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
346{
347 int ae = meshhdr->flags & MESH_FLAGS_AE;
348 /* 802.11-2012, 8.2.4.7.3 */
349 switch (ae) {
350 default:
351 case 0:
352 return 6;
353 case MESH_FLAGS_AE_A4:
354 return 12;
355 case MESH_FLAGS_AE_A5_A6:
356 return 18;
357 }
358}
359EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
360
361int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
362 enum nl80211_iftype iftype)
363{
364 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
365 u16 hdrlen, ethertype;
366 u8 *payload;
367 u8 dst[ETH_ALEN];
368 u8 src[ETH_ALEN] __aligned(2);
369
370 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
371 return -1;
372
373 hdrlen = ieee80211_hdrlen(hdr->frame_control);
374
375 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
376 * header
377 * IEEE 802.11 address fields:
378 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
379 * 0 0 DA SA BSSID n/a
380 * 0 1 DA BSSID SA n/a
381 * 1 0 BSSID SA DA n/a
382 * 1 1 RA TA DA SA
383 */
384 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
385 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
386
387 switch (hdr->frame_control &
388 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
389 case cpu_to_le16(IEEE80211_FCTL_TODS):
390 if (unlikely(iftype != NL80211_IFTYPE_AP &&
391 iftype != NL80211_IFTYPE_AP_VLAN &&
392 iftype != NL80211_IFTYPE_P2P_GO))
393 return -1;
394 break;
395 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
396 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
397 iftype != NL80211_IFTYPE_MESH_POINT &&
398 iftype != NL80211_IFTYPE_AP_VLAN &&
399 iftype != NL80211_IFTYPE_STATION))
400 return -1;
401 if (iftype == NL80211_IFTYPE_MESH_POINT) {
402 struct ieee80211s_hdr *meshdr =
403 (struct ieee80211s_hdr *) (skb->data + hdrlen);
404 /* make sure meshdr->flags is on the linear part */
405 if (!pskb_may_pull(skb, hdrlen + 1))
406 return -1;
407 if (meshdr->flags & MESH_FLAGS_AE_A4)
408 return -1;
409 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
410 skb_copy_bits(skb, hdrlen +
411 offsetof(struct ieee80211s_hdr, eaddr1),
412 dst, ETH_ALEN);
413 skb_copy_bits(skb, hdrlen +
414 offsetof(struct ieee80211s_hdr, eaddr2),
415 src, ETH_ALEN);
416 }
417 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
418 }
419 break;
420 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
421 if ((iftype != NL80211_IFTYPE_STATION &&
422 iftype != NL80211_IFTYPE_P2P_CLIENT &&
423 iftype != NL80211_IFTYPE_MESH_POINT) ||
424 (is_multicast_ether_addr(dst) &&
425 ether_addr_equal(src, addr)))
426 return -1;
427 if (iftype == NL80211_IFTYPE_MESH_POINT) {
428 struct ieee80211s_hdr *meshdr =
429 (struct ieee80211s_hdr *) (skb->data + hdrlen);
430 /* make sure meshdr->flags is on the linear part */
431 if (!pskb_may_pull(skb, hdrlen + 1))
432 return -1;
433 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
434 return -1;
435 if (meshdr->flags & MESH_FLAGS_AE_A4)
436 skb_copy_bits(skb, hdrlen +
437 offsetof(struct ieee80211s_hdr, eaddr1),
438 src, ETH_ALEN);
439 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
440 }
441 break;
442 case cpu_to_le16(0):
443 if (iftype != NL80211_IFTYPE_ADHOC &&
444 iftype != NL80211_IFTYPE_STATION)
445 return -1;
446 break;
447 }
448
449 if (!pskb_may_pull(skb, hdrlen + 8))
450 return -1;
451
452 payload = skb->data + hdrlen;
453 ethertype = (payload[6] << 8) | payload[7];
454
455 if (likely((ether_addr_equal(payload, rfc1042_header) &&
456 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
457 ether_addr_equal(payload, bridge_tunnel_header))) {
458 /* remove RFC1042 or Bridge-Tunnel encapsulation and
459 * replace EtherType */
460 skb_pull(skb, hdrlen + 6);
461 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
462 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
463 } else {
464 struct ethhdr *ehdr;
465 __be16 len;
466
467 skb_pull(skb, hdrlen);
468 len = htons(skb->len);
469 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
470 memcpy(ehdr->h_dest, dst, ETH_ALEN);
471 memcpy(ehdr->h_source, src, ETH_ALEN);
472 ehdr->h_proto = len;
473 }
474 return 0;
475}
476EXPORT_SYMBOL(ieee80211_data_to_8023);
477
478int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
479 enum nl80211_iftype iftype, u8 *bssid, bool qos)
480{
481 struct ieee80211_hdr hdr;
482 u16 hdrlen, ethertype;
483 __le16 fc;
484 const u8 *encaps_data;
485 int encaps_len, skip_header_bytes;
486 int nh_pos, h_pos;
487 int head_need;
488
489 if (unlikely(skb->len < ETH_HLEN))
490 return -EINVAL;
491
492 nh_pos = skb_network_header(skb) - skb->data;
493 h_pos = skb_transport_header(skb) - skb->data;
494
495 /* convert Ethernet header to proper 802.11 header (based on
496 * operation mode) */
497 ethertype = (skb->data[12] << 8) | skb->data[13];
498 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
499
500 switch (iftype) {
501 case NL80211_IFTYPE_AP:
502 case NL80211_IFTYPE_AP_VLAN:
503 case NL80211_IFTYPE_P2P_GO:
504 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
505 /* DA BSSID SA */
506 memcpy(hdr.addr1, skb->data, ETH_ALEN);
507 memcpy(hdr.addr2, addr, ETH_ALEN);
508 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
509 hdrlen = 24;
510 break;
511 case NL80211_IFTYPE_STATION:
512 case NL80211_IFTYPE_P2P_CLIENT:
513 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
514 /* BSSID SA DA */
515 memcpy(hdr.addr1, bssid, ETH_ALEN);
516 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
517 memcpy(hdr.addr3, skb->data, ETH_ALEN);
518 hdrlen = 24;
519 break;
520 case NL80211_IFTYPE_ADHOC:
521 /* DA SA BSSID */
522 memcpy(hdr.addr1, skb->data, ETH_ALEN);
523 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
524 memcpy(hdr.addr3, bssid, ETH_ALEN);
525 hdrlen = 24;
526 break;
527 default:
528 return -EOPNOTSUPP;
529 }
530
531 if (qos) {
532 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
533 hdrlen += 2;
534 }
535
536 hdr.frame_control = fc;
537 hdr.duration_id = 0;
538 hdr.seq_ctrl = 0;
539
540 skip_header_bytes = ETH_HLEN;
541 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
542 encaps_data = bridge_tunnel_header;
543 encaps_len = sizeof(bridge_tunnel_header);
544 skip_header_bytes -= 2;
545 } else if (ethertype >= ETH_P_802_3_MIN) {
546 encaps_data = rfc1042_header;
547 encaps_len = sizeof(rfc1042_header);
548 skip_header_bytes -= 2;
549 } else {
550 encaps_data = NULL;
551 encaps_len = 0;
552 }
553
554 skb_pull(skb, skip_header_bytes);
555 nh_pos -= skip_header_bytes;
556 h_pos -= skip_header_bytes;
557
558 head_need = hdrlen + encaps_len - skb_headroom(skb);
559
560 if (head_need > 0 || skb_cloned(skb)) {
561 head_need = max(head_need, 0);
562 if (head_need)
563 skb_orphan(skb);
564
565 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
566 return -ENOMEM;
567
568 skb->truesize += head_need;
569 }
570
571 if (encaps_data) {
572 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
573 nh_pos += encaps_len;
574 h_pos += encaps_len;
575 }
576
577 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
578
579 nh_pos += hdrlen;
580 h_pos += hdrlen;
581
582 /* Update skb pointers to various headers since this modified frame
583 * is going to go through Linux networking code that may potentially
584 * need things like pointer to IP header. */
585 skb_set_mac_header(skb, 0);
586 skb_set_network_header(skb, nh_pos);
587 skb_set_transport_header(skb, h_pos);
588
589 return 0;
590}
591EXPORT_SYMBOL(ieee80211_data_from_8023);
592
593
594void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
595 const u8 *addr, enum nl80211_iftype iftype,
596 const unsigned int extra_headroom,
597 bool has_80211_header)
598{
599 struct sk_buff *frame = NULL;
600 u16 ethertype;
601 u8 *payload;
602 const struct ethhdr *eth;
603 int remaining, err;
604 u8 dst[ETH_ALEN], src[ETH_ALEN];
605
606 if (has_80211_header) {
607 err = ieee80211_data_to_8023(skb, addr, iftype);
608 if (err)
609 goto out;
610
611 /* skip the wrapping header */
612 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
613 if (!eth)
614 goto out;
615 } else {
616 eth = (struct ethhdr *) skb->data;
617 }
618
619 while (skb != frame) {
620 u8 padding;
621 __be16 len = eth->h_proto;
622 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
623
624 remaining = skb->len;
625 memcpy(dst, eth->h_dest, ETH_ALEN);
626 memcpy(src, eth->h_source, ETH_ALEN);
627
628 padding = (4 - subframe_len) & 0x3;
629 /* the last MSDU has no padding */
630 if (subframe_len > remaining)
631 goto purge;
632
633 skb_pull(skb, sizeof(struct ethhdr));
634 /* reuse skb for the last subframe */
635 if (remaining <= subframe_len + padding)
636 frame = skb;
637 else {
638 unsigned int hlen = ALIGN(extra_headroom, 4);
639 /*
640 * Allocate and reserve two bytes more for payload
641 * alignment since sizeof(struct ethhdr) is 14.
642 */
643 frame = dev_alloc_skb(hlen + subframe_len + 2);
644 if (!frame)
645 goto purge;
646
647 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
648 memcpy(skb_put(frame, ntohs(len)), skb->data,
649 ntohs(len));
650
651 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
652 padding);
653 if (!eth) {
654 dev_kfree_skb(frame);
655 goto purge;
656 }
657 }
658
659 skb_reset_network_header(frame);
660 frame->dev = skb->dev;
661 frame->priority = skb->priority;
662
663 payload = frame->data;
664 ethertype = (payload[6] << 8) | payload[7];
665
666 if (likely((ether_addr_equal(payload, rfc1042_header) &&
667 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
668 ether_addr_equal(payload, bridge_tunnel_header))) {
669 /* remove RFC1042 or Bridge-Tunnel
670 * encapsulation and replace EtherType */
671 skb_pull(frame, 6);
672 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
673 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
674 } else {
675 memcpy(skb_push(frame, sizeof(__be16)), &len,
676 sizeof(__be16));
677 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
678 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
679 }
680 __skb_queue_tail(list, frame);
681 }
682
683 return;
684
685 purge:
686 __skb_queue_purge(list);
687 out:
688 dev_kfree_skb(skb);
689}
690EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
691
692/* Given a data frame determine the 802.1p/1d tag to use. */
693unsigned int cfg80211_classify8021d(struct sk_buff *skb,
694 struct cfg80211_qos_map *qos_map)
695{
696 unsigned int dscp;
697 unsigned char vlan_priority;
698
699 /* skb->priority values from 256->263 are magic values to
700 * directly indicate a specific 802.1d priority. This is used
701 * to allow 802.1d priority to be passed directly in from VLAN
702 * tags, etc.
703 */
704 if (skb->priority >= 256 && skb->priority <= 263)
705 return skb->priority - 256;
706
707 if (vlan_tx_tag_present(skb)) {
708 vlan_priority = (vlan_tx_tag_get(skb) & VLAN_PRIO_MASK)
709 >> VLAN_PRIO_SHIFT;
710 if (vlan_priority > 0)
711 return vlan_priority;
712 }
713
714 switch (skb->protocol) {
715 case htons(ETH_P_IP):
716 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
717 break;
718 case htons(ETH_P_IPV6):
719 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
720 break;
721 case htons(ETH_P_MPLS_UC):
722 case htons(ETH_P_MPLS_MC): {
723 struct mpls_label mpls_tmp, *mpls;
724
725 mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
726 sizeof(*mpls), &mpls_tmp);
727 if (!mpls)
728 return 0;
729
730 return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
731 >> MPLS_LS_TC_SHIFT;
732 }
733 case htons(ETH_P_80221):
734 /* 802.21 is always network control traffic */
735 return 7;
736 default:
737 return 0;
738 }
739
740 if (qos_map) {
741 unsigned int i, tmp_dscp = dscp >> 2;
742
743 for (i = 0; i < qos_map->num_des; i++) {
744 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
745 return qos_map->dscp_exception[i].up;
746 }
747
748 for (i = 0; i < 8; i++) {
749 if (tmp_dscp >= qos_map->up[i].low &&
750 tmp_dscp <= qos_map->up[i].high)
751 return i;
752 }
753 }
754
755 return dscp >> 5;
756}
757EXPORT_SYMBOL(cfg80211_classify8021d);
758
759const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
760{
761 const struct cfg80211_bss_ies *ies;
762
763 ies = rcu_dereference(bss->ies);
764 if (!ies)
765 return NULL;
766
767 return cfg80211_find_ie(ie, ies->data, ies->len);
768}
769EXPORT_SYMBOL(ieee80211_bss_get_ie);
770
771void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
772{
773 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
774 struct net_device *dev = wdev->netdev;
775 int i;
776
777 if (!wdev->connect_keys)
778 return;
779
780 for (i = 0; i < 6; i++) {
781 if (!wdev->connect_keys->params[i].cipher)
782 continue;
783 if (rdev_add_key(rdev, dev, i, false, NULL,
784 &wdev->connect_keys->params[i])) {
785 netdev_err(dev, "failed to set key %d\n", i);
786 continue;
787 }
788 if (wdev->connect_keys->def == i)
789 if (rdev_set_default_key(rdev, dev, i, true, true)) {
790 netdev_err(dev, "failed to set defkey %d\n", i);
791 continue;
792 }
793 if (wdev->connect_keys->defmgmt == i)
794 if (rdev_set_default_mgmt_key(rdev, dev, i))
795 netdev_err(dev, "failed to set mgtdef %d\n", i);
796 }
797
798 kfree(wdev->connect_keys);
799 wdev->connect_keys = NULL;
800}
801
802void cfg80211_process_wdev_events(struct wireless_dev *wdev)
803{
804 struct cfg80211_event *ev;
805 unsigned long flags;
806 const u8 *bssid = NULL;
807
808 spin_lock_irqsave(&wdev->event_lock, flags);
809 while (!list_empty(&wdev->event_list)) {
810 ev = list_first_entry(&wdev->event_list,
811 struct cfg80211_event, list);
812 list_del(&ev->list);
813 spin_unlock_irqrestore(&wdev->event_lock, flags);
814
815 wdev_lock(wdev);
816 switch (ev->type) {
817 case EVENT_CONNECT_RESULT:
818 if (!is_zero_ether_addr(ev->cr.bssid))
819 bssid = ev->cr.bssid;
820 __cfg80211_connect_result(
821 wdev->netdev, bssid,
822 ev->cr.req_ie, ev->cr.req_ie_len,
823 ev->cr.resp_ie, ev->cr.resp_ie_len,
824 ev->cr.status,
825 ev->cr.status == WLAN_STATUS_SUCCESS,
826 NULL);
827 break;
828 case EVENT_ROAMED:
829 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
830 ev->rm.req_ie_len, ev->rm.resp_ie,
831 ev->rm.resp_ie_len);
832 break;
833 case EVENT_DISCONNECTED:
834 __cfg80211_disconnected(wdev->netdev,
835 ev->dc.ie, ev->dc.ie_len,
836 ev->dc.reason, true);
837 break;
838 case EVENT_IBSS_JOINED:
839 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
840 ev->ij.channel);
841 break;
842 }
843 wdev_unlock(wdev);
844
845 kfree(ev);
846
847 spin_lock_irqsave(&wdev->event_lock, flags);
848 }
849 spin_unlock_irqrestore(&wdev->event_lock, flags);
850}
851
852void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
853{
854 struct wireless_dev *wdev;
855
856 ASSERT_RTNL();
857
858 list_for_each_entry(wdev, &rdev->wdev_list, list)
859 cfg80211_process_wdev_events(wdev);
860}
861
862int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
863 struct net_device *dev, enum nl80211_iftype ntype,
864 u32 *flags, struct vif_params *params)
865{
866 int err;
867 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
868
869 ASSERT_RTNL();
870
871 /* don't support changing VLANs, you just re-create them */
872 if (otype == NL80211_IFTYPE_AP_VLAN)
873 return -EOPNOTSUPP;
874
875 /* cannot change into P2P device type */
876 if (ntype == NL80211_IFTYPE_P2P_DEVICE)
877 return -EOPNOTSUPP;
878
879 if (!rdev->ops->change_virtual_intf ||
880 !(rdev->wiphy.interface_modes & (1 << ntype)))
881 return -EOPNOTSUPP;
882
883 /* if it's part of a bridge, reject changing type to station/ibss */
884 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
885 (ntype == NL80211_IFTYPE_ADHOC ||
886 ntype == NL80211_IFTYPE_STATION ||
887 ntype == NL80211_IFTYPE_P2P_CLIENT))
888 return -EBUSY;
889
890 if (ntype != otype && netif_running(dev)) {
891 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
892 ntype);
893 if (err)
894 return err;
895
896 dev->ieee80211_ptr->use_4addr = false;
897 dev->ieee80211_ptr->mesh_id_up_len = 0;
898 wdev_lock(dev->ieee80211_ptr);
899 rdev_set_qos_map(rdev, dev, NULL);
900 wdev_unlock(dev->ieee80211_ptr);
901
902 switch (otype) {
903 case NL80211_IFTYPE_AP:
904 cfg80211_stop_ap(rdev, dev, true);
905 break;
906 case NL80211_IFTYPE_ADHOC:
907 cfg80211_leave_ibss(rdev, dev, false);
908 break;
909 case NL80211_IFTYPE_STATION:
910 case NL80211_IFTYPE_P2P_CLIENT:
911 wdev_lock(dev->ieee80211_ptr);
912 cfg80211_disconnect(rdev, dev,
913 WLAN_REASON_DEAUTH_LEAVING, true);
914 wdev_unlock(dev->ieee80211_ptr);
915 break;
916 case NL80211_IFTYPE_MESH_POINT:
917 /* mesh should be handled? */
918 break;
919 default:
920 break;
921 }
922
923 cfg80211_process_rdev_events(rdev);
924 }
925
926 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
927
928 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
929
930 if (!err && params && params->use_4addr != -1)
931 dev->ieee80211_ptr->use_4addr = params->use_4addr;
932
933 if (!err) {
934 dev->priv_flags &= ~IFF_DONT_BRIDGE;
935 switch (ntype) {
936 case NL80211_IFTYPE_STATION:
937 if (dev->ieee80211_ptr->use_4addr)
938 break;
939 /* fall through */
940 case NL80211_IFTYPE_P2P_CLIENT:
941 case NL80211_IFTYPE_ADHOC:
942 dev->priv_flags |= IFF_DONT_BRIDGE;
943 break;
944 case NL80211_IFTYPE_P2P_GO:
945 case NL80211_IFTYPE_AP:
946 case NL80211_IFTYPE_AP_VLAN:
947 case NL80211_IFTYPE_WDS:
948 case NL80211_IFTYPE_MESH_POINT:
949 /* bridging OK */
950 break;
951 case NL80211_IFTYPE_MONITOR:
952 /* monitor can't bridge anyway */
953 break;
954 case NL80211_IFTYPE_UNSPECIFIED:
955 case NUM_NL80211_IFTYPES:
956 /* not happening */
957 break;
958 case NL80211_IFTYPE_P2P_DEVICE:
959 WARN_ON(1);
960 break;
961 }
962 }
963
964 if (!err && ntype != otype && netif_running(dev)) {
965 cfg80211_update_iface_num(rdev, ntype, 1);
966 cfg80211_update_iface_num(rdev, otype, -1);
967 }
968
969 return err;
970}
971
972static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
973{
974 static const u32 __mcs2bitrate[] = {
975 /* control PHY */
976 [0] = 275,
977 /* SC PHY */
978 [1] = 3850,
979 [2] = 7700,
980 [3] = 9625,
981 [4] = 11550,
982 [5] = 12512, /* 1251.25 mbps */
983 [6] = 15400,
984 [7] = 19250,
985 [8] = 23100,
986 [9] = 25025,
987 [10] = 30800,
988 [11] = 38500,
989 [12] = 46200,
990 /* OFDM PHY */
991 [13] = 6930,
992 [14] = 8662, /* 866.25 mbps */
993 [15] = 13860,
994 [16] = 17325,
995 [17] = 20790,
996 [18] = 27720,
997 [19] = 34650,
998 [20] = 41580,
999 [21] = 45045,
1000 [22] = 51975,
1001 [23] = 62370,
1002 [24] = 67568, /* 6756.75 mbps */
1003 /* LP-SC PHY */
1004 [25] = 6260,
1005 [26] = 8340,
1006 [27] = 11120,
1007 [28] = 12510,
1008 [29] = 16680,
1009 [30] = 22240,
1010 [31] = 25030,
1011 };
1012
1013 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
1014 return 0;
1015
1016 return __mcs2bitrate[rate->mcs];
1017}
1018
1019static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
1020{
1021 static const u32 base[4][10] = {
1022 { 6500000,
1023 13000000,
1024 19500000,
1025 26000000,
1026 39000000,
1027 52000000,
1028 58500000,
1029 65000000,
1030 78000000,
1031 0,
1032 },
1033 { 13500000,
1034 27000000,
1035 40500000,
1036 54000000,
1037 81000000,
1038 108000000,
1039 121500000,
1040 135000000,
1041 162000000,
1042 180000000,
1043 },
1044 { 29300000,
1045 58500000,
1046 87800000,
1047 117000000,
1048 175500000,
1049 234000000,
1050 263300000,
1051 292500000,
1052 351000000,
1053 390000000,
1054 },
1055 { 58500000,
1056 117000000,
1057 175500000,
1058 234000000,
1059 351000000,
1060 468000000,
1061 526500000,
1062 585000000,
1063 702000000,
1064 780000000,
1065 },
1066 };
1067 u32 bitrate;
1068 int idx;
1069
1070 if (WARN_ON_ONCE(rate->mcs > 9))
1071 return 0;
1072
1073 idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
1074 RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
1075 rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
1076 rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
1077
1078 bitrate = base[idx][rate->mcs];
1079 bitrate *= rate->nss;
1080
1081 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1082 bitrate = (bitrate / 9) * 10;
1083
1084 /* do NOT round down here */
1085 return (bitrate + 50000) / 100000;
1086}
1087
1088u32 cfg80211_calculate_bitrate(struct rate_info *rate)
1089{
1090 int modulation, streams, bitrate;
1091
1092 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
1093 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
1094 return rate->legacy;
1095 if (rate->flags & RATE_INFO_FLAGS_60G)
1096 return cfg80211_calculate_bitrate_60g(rate);
1097 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
1098 return cfg80211_calculate_bitrate_vht(rate);
1099
1100 /* the formula below does only work for MCS values smaller than 32 */
1101 if (WARN_ON_ONCE(rate->mcs >= 32))
1102 return 0;
1103
1104 modulation = rate->mcs & 7;
1105 streams = (rate->mcs >> 3) + 1;
1106
1107 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
1108 13500000 : 6500000;
1109
1110 if (modulation < 4)
1111 bitrate *= (modulation + 1);
1112 else if (modulation == 4)
1113 bitrate *= (modulation + 2);
1114 else
1115 bitrate *= (modulation + 3);
1116
1117 bitrate *= streams;
1118
1119 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1120 bitrate = (bitrate / 9) * 10;
1121
1122 /* do NOT round down here */
1123 return (bitrate + 50000) / 100000;
1124}
1125EXPORT_SYMBOL(cfg80211_calculate_bitrate);
1126
1127int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1128 enum ieee80211_p2p_attr_id attr,
1129 u8 *buf, unsigned int bufsize)
1130{
1131 u8 *out = buf;
1132 u16 attr_remaining = 0;
1133 bool desired_attr = false;
1134 u16 desired_len = 0;
1135
1136 while (len > 0) {
1137 unsigned int iedatalen;
1138 unsigned int copy;
1139 const u8 *iedata;
1140
1141 if (len < 2)
1142 return -EILSEQ;
1143 iedatalen = ies[1];
1144 if (iedatalen + 2 > len)
1145 return -EILSEQ;
1146
1147 if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
1148 goto cont;
1149
1150 if (iedatalen < 4)
1151 goto cont;
1152
1153 iedata = ies + 2;
1154
1155 /* check WFA OUI, P2P subtype */
1156 if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
1157 iedata[2] != 0x9a || iedata[3] != 0x09)
1158 goto cont;
1159
1160 iedatalen -= 4;
1161 iedata += 4;
1162
1163 /* check attribute continuation into this IE */
1164 copy = min_t(unsigned int, attr_remaining, iedatalen);
1165 if (copy && desired_attr) {
1166 desired_len += copy;
1167 if (out) {
1168 memcpy(out, iedata, min(bufsize, copy));
1169 out += min(bufsize, copy);
1170 bufsize -= min(bufsize, copy);
1171 }
1172
1173
1174 if (copy == attr_remaining)
1175 return desired_len;
1176 }
1177
1178 attr_remaining -= copy;
1179 if (attr_remaining)
1180 goto cont;
1181
1182 iedatalen -= copy;
1183 iedata += copy;
1184
1185 while (iedatalen > 0) {
1186 u16 attr_len;
1187
1188 /* P2P attribute ID & size must fit */
1189 if (iedatalen < 3)
1190 return -EILSEQ;
1191 desired_attr = iedata[0] == attr;
1192 attr_len = get_unaligned_le16(iedata + 1);
1193 iedatalen -= 3;
1194 iedata += 3;
1195
1196 copy = min_t(unsigned int, attr_len, iedatalen);
1197
1198 if (desired_attr) {
1199 desired_len += copy;
1200 if (out) {
1201 memcpy(out, iedata, min(bufsize, copy));
1202 out += min(bufsize, copy);
1203 bufsize -= min(bufsize, copy);
1204 }
1205
1206 if (copy == attr_len)
1207 return desired_len;
1208 }
1209
1210 iedata += copy;
1211 iedatalen -= copy;
1212 attr_remaining = attr_len - copy;
1213 }
1214
1215 cont:
1216 len -= ies[1] + 2;
1217 ies += ies[1] + 2;
1218 }
1219
1220 if (attr_remaining && desired_attr)
1221 return -EILSEQ;
1222
1223 return -ENOENT;
1224}
1225EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1226
1227bool ieee80211_operating_class_to_band(u8 operating_class,
1228 enum ieee80211_band *band)
1229{
1230 switch (operating_class) {
1231 case 112:
1232 case 115 ... 127:
1233 *band = IEEE80211_BAND_5GHZ;
1234 return true;
1235 case 81:
1236 case 82:
1237 case 83:
1238 case 84:
1239 *band = IEEE80211_BAND_2GHZ;
1240 return true;
1241 case 180:
1242 *band = IEEE80211_BAND_60GHZ;
1243 return true;
1244 }
1245
1246 return false;
1247}
1248EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1249
1250int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1251 u32 beacon_int)
1252{
1253 struct wireless_dev *wdev;
1254 int res = 0;
1255
1256 if (!beacon_int)
1257 return -EINVAL;
1258
1259 list_for_each_entry(wdev, &rdev->wdev_list, list) {
1260 if (!wdev->beacon_interval)
1261 continue;
1262 if (wdev->beacon_interval != beacon_int) {
1263 res = -EINVAL;
1264 break;
1265 }
1266 }
1267
1268 return res;
1269}
1270
1271int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1272 struct wireless_dev *wdev,
1273 enum nl80211_iftype iftype,
1274 struct ieee80211_channel *chan,
1275 enum cfg80211_chan_mode chanmode,
1276 u8 radar_detect)
1277{
1278 struct wireless_dev *wdev_iter;
1279 u32 used_iftypes = BIT(iftype);
1280 int num[NUM_NL80211_IFTYPES];
1281 struct ieee80211_channel
1282 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1283 struct ieee80211_channel *ch;
1284 enum cfg80211_chan_mode chmode;
1285 int num_different_channels = 0;
1286 int total = 1;
1287 int i, j;
1288
1289 ASSERT_RTNL();
1290
1291 if (WARN_ON(hweight32(radar_detect) > 1))
1292 return -EINVAL;
1293
1294 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
1295 return -EINVAL;
1296
1297 /* Always allow software iftypes */
1298 if (rdev->wiphy.software_iftypes & BIT(iftype)) {
1299 if (radar_detect)
1300 return -EINVAL;
1301 return 0;
1302 }
1303
1304 memset(num, 0, sizeof(num));
1305 memset(used_channels, 0, sizeof(used_channels));
1306
1307 num[iftype] = 1;
1308
1309 switch (chanmode) {
1310 case CHAN_MODE_UNDEFINED:
1311 break;
1312 case CHAN_MODE_SHARED:
1313 WARN_ON(!chan);
1314 used_channels[0] = chan;
1315 num_different_channels++;
1316 break;
1317 case CHAN_MODE_EXCLUSIVE:
1318 num_different_channels++;
1319 break;
1320 }
1321
1322 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
1323 if (wdev_iter == wdev)
1324 continue;
1325 if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
1326 if (!wdev_iter->p2p_started)
1327 continue;
1328 } else if (wdev_iter->netdev) {
1329 if (!netif_running(wdev_iter->netdev))
1330 continue;
1331 } else {
1332 WARN_ON(1);
1333 }
1334
1335 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1336 continue;
1337
1338 /*
1339 * We may be holding the "wdev" mutex, but now need to lock
1340 * wdev_iter. This is OK because once we get here wdev_iter
1341 * is not wdev (tested above), but we need to use the nested
1342 * locking for lockdep.
1343 */
1344 mutex_lock_nested(&wdev_iter->mtx, 1);
1345 __acquire(wdev_iter->mtx);
1346 cfg80211_get_chan_state(wdev_iter, &ch, &chmode, &radar_detect);
1347 wdev_unlock(wdev_iter);
1348
1349 switch (chmode) {
1350 case CHAN_MODE_UNDEFINED:
1351 break;
1352 case CHAN_MODE_SHARED:
1353 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1354 if (!used_channels[i] || used_channels[i] == ch)
1355 break;
1356
1357 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
1358 return -EBUSY;
1359
1360 if (used_channels[i] == NULL) {
1361 used_channels[i] = ch;
1362 num_different_channels++;
1363 }
1364 break;
1365 case CHAN_MODE_EXCLUSIVE:
1366 num_different_channels++;
1367 break;
1368 }
1369
1370 num[wdev_iter->iftype]++;
1371 total++;
1372 used_iftypes |= BIT(wdev_iter->iftype);
1373 }
1374
1375 if (total == 1 && !radar_detect)
1376 return 0;
1377
1378 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1379 const struct ieee80211_iface_combination *c;
1380 struct ieee80211_iface_limit *limits;
1381 u32 all_iftypes = 0;
1382
1383 c = &rdev->wiphy.iface_combinations[i];
1384
1385 if (total > c->max_interfaces)
1386 continue;
1387 if (num_different_channels > c->num_different_channels)
1388 continue;
1389
1390 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1391 GFP_KERNEL);
1392 if (!limits)
1393 return -ENOMEM;
1394
1395 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1396 if (rdev->wiphy.software_iftypes & BIT(iftype))
1397 continue;
1398 for (j = 0; j < c->n_limits; j++) {
1399 all_iftypes |= limits[j].types;
1400 if (!(limits[j].types & BIT(iftype)))
1401 continue;
1402 if (limits[j].max < num[iftype])
1403 goto cont;
1404 limits[j].max -= num[iftype];
1405 }
1406 }
1407
1408 if (radar_detect && !(c->radar_detect_widths & radar_detect))
1409 goto cont;
1410
1411 /*
1412 * Finally check that all iftypes that we're currently
1413 * using are actually part of this combination. If they
1414 * aren't then we can't use this combination and have
1415 * to continue to the next.
1416 */
1417 if ((all_iftypes & used_iftypes) != used_iftypes)
1418 goto cont;
1419
1420 /*
1421 * This combination covered all interface types and
1422 * supported the requested numbers, so we're good.
1423 */
1424 kfree(limits);
1425 return 0;
1426 cont:
1427 kfree(limits);
1428 }
1429
1430 return -EBUSY;
1431}
1432
1433int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1434 const u8 *rates, unsigned int n_rates,
1435 u32 *mask)
1436{
1437 int i, j;
1438
1439 if (!sband)
1440 return -EINVAL;
1441
1442 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1443 return -EINVAL;
1444
1445 *mask = 0;
1446
1447 for (i = 0; i < n_rates; i++) {
1448 int rate = (rates[i] & 0x7f) * 5;
1449 bool found = false;
1450
1451 for (j = 0; j < sband->n_bitrates; j++) {
1452 if (sband->bitrates[j].bitrate == rate) {
1453 found = true;
1454 *mask |= BIT(j);
1455 break;
1456 }
1457 }
1458 if (!found)
1459 return -EINVAL;
1460 }
1461
1462 /*
1463 * mask must have at least one bit set here since we
1464 * didn't accept a 0-length rates array nor allowed
1465 * entries in the array that didn't exist
1466 */
1467
1468 return 0;
1469}
1470
1471unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
1472{
1473 enum ieee80211_band band;
1474 unsigned int n_channels = 0;
1475
1476 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
1477 if (wiphy->bands[band])
1478 n_channels += wiphy->bands[band]->n_channels;
1479
1480 return n_channels;
1481}
1482EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
1483
1484/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1485/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1486const unsigned char rfc1042_header[] __aligned(2) =
1487 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1488EXPORT_SYMBOL(rfc1042_header);
1489
1490/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1491const unsigned char bridge_tunnel_header[] __aligned(2) =
1492 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1493EXPORT_SYMBOL(bridge_tunnel_header);