]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/wireless/util.c
mac80211: allow calling ieee80211_ap_probereq_get() during auth/assoc
[mirror_ubuntu-bionic-kernel.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>
8318d78a
JB
13#include "core.h"
14
bd815252
JB
15struct ieee80211_rate *
16ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
881d948c 17 u32 basic_rates, int bitrate)
bd815252
JB
18{
19 struct ieee80211_rate *result = &sband->bitrates[0];
20 int i;
21
22 for (i = 0; i < sband->n_bitrates; i++) {
23 if (!(basic_rates & BIT(i)))
24 continue;
25 if (sband->bitrates[i].bitrate > bitrate)
26 continue;
27 result = &sband->bitrates[i];
28 }
29
30 return result;
31}
32EXPORT_SYMBOL(ieee80211_get_response_rate);
33
59eb21a6 34int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
8318d78a 35{
59eb21a6
BR
36 /* see 802.11 17.3.8.3.2 and Annex J
37 * there are overlapping channel numbers in 5GHz and 2GHz bands */
38 if (band == IEEE80211_BAND_5GHZ) {
39 if (chan >= 182 && chan <= 196)
40 return 4000 + chan * 5;
41 else
42 return 5000 + chan * 5;
43 } else { /* IEEE80211_BAND_2GHZ */
44 if (chan == 14)
45 return 2484;
46 else if (chan < 14)
47 return 2407 + chan * 5;
48 else
49 return 0; /* not supported */
50 }
8318d78a
JB
51}
52EXPORT_SYMBOL(ieee80211_channel_to_frequency);
53
54int ieee80211_frequency_to_channel(int freq)
55{
59eb21a6 56 /* see 802.11 17.3.8.3.2 and Annex J */
8318d78a
JB
57 if (freq == 2484)
58 return 14;
59eb21a6 59 else if (freq < 2484)
8318d78a 60 return (freq - 2407) / 5;
59eb21a6
BR
61 else if (freq >= 4910 && freq <= 4980)
62 return (freq - 4000) / 5;
63 else
64 return (freq - 5000) / 5;
8318d78a
JB
65}
66EXPORT_SYMBOL(ieee80211_frequency_to_channel);
67
6c507cd0
JB
68struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
69 int freq)
906c730a
JB
70{
71 enum ieee80211_band band;
72 struct ieee80211_supported_band *sband;
73 int i;
74
75 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
76 sband = wiphy->bands[band];
77
78 if (!sband)
79 continue;
80
81 for (i = 0; i < sband->n_channels; i++) {
82 if (sband->channels[i].center_freq == freq)
83 return &sband->channels[i];
84 }
85 }
86
87 return NULL;
88}
6c507cd0 89EXPORT_SYMBOL(__ieee80211_get_channel);
906c730a 90
8318d78a
JB
91static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
92 enum ieee80211_band band)
93{
94 int i, want;
95
96 switch (band) {
97 case IEEE80211_BAND_5GHZ:
98 want = 3;
99 for (i = 0; i < sband->n_bitrates; i++) {
100 if (sband->bitrates[i].bitrate == 60 ||
101 sband->bitrates[i].bitrate == 120 ||
102 sband->bitrates[i].bitrate == 240) {
103 sband->bitrates[i].flags |=
104 IEEE80211_RATE_MANDATORY_A;
105 want--;
106 }
107 }
108 WARN_ON(want);
109 break;
110 case IEEE80211_BAND_2GHZ:
111 want = 7;
112 for (i = 0; i < sband->n_bitrates; i++) {
113 if (sband->bitrates[i].bitrate == 10) {
114 sband->bitrates[i].flags |=
115 IEEE80211_RATE_MANDATORY_B |
116 IEEE80211_RATE_MANDATORY_G;
117 want--;
118 }
119
120 if (sband->bitrates[i].bitrate == 20 ||
121 sband->bitrates[i].bitrate == 55 ||
122 sband->bitrates[i].bitrate == 110 ||
123 sband->bitrates[i].bitrate == 60 ||
124 sband->bitrates[i].bitrate == 120 ||
125 sband->bitrates[i].bitrate == 240) {
126 sband->bitrates[i].flags |=
127 IEEE80211_RATE_MANDATORY_G;
128 want--;
129 }
130
aac09fbf
JB
131 if (sband->bitrates[i].bitrate != 10 &&
132 sband->bitrates[i].bitrate != 20 &&
133 sband->bitrates[i].bitrate != 55 &&
134 sband->bitrates[i].bitrate != 110)
8318d78a
JB
135 sband->bitrates[i].flags |=
136 IEEE80211_RATE_ERP_G;
137 }
406f2388 138 WARN_ON(want != 0 && want != 3 && want != 6);
8318d78a
JB
139 break;
140 case IEEE80211_NUM_BANDS:
141 WARN_ON(1);
142 break;
143 }
144}
145
146void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
147{
148 enum ieee80211_band band;
149
150 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
151 if (wiphy->bands[band])
152 set_mandatory_flags_band(wiphy->bands[band], band);
153}
08645126 154
38ba3c57
JM
155bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
156{
157 int i;
158 for (i = 0; i < wiphy->n_cipher_suites; i++)
159 if (cipher == wiphy->cipher_suites[i])
160 return true;
161 return false;
162}
163
fffd0934
JB
164int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
165 struct key_params *params, int key_idx,
e31b8213 166 bool pairwise, const u8 *mac_addr)
08645126
JB
167{
168 if (key_idx > 5)
169 return -EINVAL;
170
e31b8213
JB
171 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
172 return -EINVAL;
173
174 if (pairwise && !mac_addr)
175 return -EINVAL;
176
08645126
JB
177 /*
178 * Disallow pairwise keys with non-zero index unless it's WEP
45cbad6a
JO
179 * or a vendor specific cipher (because current deployments use
180 * pairwise WEP keys with non-zero indices and for vendor specific
181 * ciphers this should be validated in the driver or hardware level
182 * - but 802.11i clearly specifies to use zero)
08645126 183 */
e31b8213 184 if (pairwise && key_idx &&
45cbad6a
JO
185 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
186 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
187 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
08645126
JB
188 return -EINVAL;
189
08645126
JB
190 switch (params->cipher) {
191 case WLAN_CIPHER_SUITE_WEP40:
8fc0fee0 192 if (params->key_len != WLAN_KEY_LEN_WEP40)
08645126
JB
193 return -EINVAL;
194 break;
195 case WLAN_CIPHER_SUITE_TKIP:
8fc0fee0 196 if (params->key_len != WLAN_KEY_LEN_TKIP)
08645126
JB
197 return -EINVAL;
198 break;
199 case WLAN_CIPHER_SUITE_CCMP:
8fc0fee0 200 if (params->key_len != WLAN_KEY_LEN_CCMP)
08645126
JB
201 return -EINVAL;
202 break;
203 case WLAN_CIPHER_SUITE_WEP104:
8fc0fee0 204 if (params->key_len != WLAN_KEY_LEN_WEP104)
08645126
JB
205 return -EINVAL;
206 break;
207 case WLAN_CIPHER_SUITE_AES_CMAC:
8fc0fee0 208 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
08645126
JB
209 return -EINVAL;
210 break;
211 default:
7d64b7cc
JB
212 /*
213 * We don't know anything about this algorithm,
214 * allow using it -- but the driver must check
215 * all parameters! We still check below whether
216 * or not the driver supports this algorithm,
217 * of course.
218 */
219 break;
08645126
JB
220 }
221
9f26a952
JM
222 if (params->seq) {
223 switch (params->cipher) {
224 case WLAN_CIPHER_SUITE_WEP40:
225 case WLAN_CIPHER_SUITE_WEP104:
226 /* These ciphers do not use key sequence */
227 return -EINVAL;
228 case WLAN_CIPHER_SUITE_TKIP:
229 case WLAN_CIPHER_SUITE_CCMP:
230 case WLAN_CIPHER_SUITE_AES_CMAC:
231 if (params->seq_len != 6)
232 return -EINVAL;
233 break;
234 }
235 }
236
38ba3c57 237 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
fffd0934
JB
238 return -EINVAL;
239
08645126
JB
240 return 0;
241}
e31a16d6 242
633adf1a 243unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
e31a16d6
ZY
244{
245 unsigned int hdrlen = 24;
246
247 if (ieee80211_is_data(fc)) {
248 if (ieee80211_has_a4(fc))
249 hdrlen = 30;
d0dd2de0 250 if (ieee80211_is_data_qos(fc)) {
e31a16d6 251 hdrlen += IEEE80211_QOS_CTL_LEN;
d0dd2de0
AT
252 if (ieee80211_has_order(fc))
253 hdrlen += IEEE80211_HT_CTL_LEN;
254 }
e31a16d6
ZY
255 goto out;
256 }
257
258 if (ieee80211_is_ctl(fc)) {
259 /*
260 * ACK and CTS are 10 bytes, all others 16. To see how
261 * to get this condition consider
262 * subtype mask: 0b0000000011110000 (0x00F0)
263 * ACK subtype: 0b0000000011010000 (0x00D0)
264 * CTS subtype: 0b0000000011000000 (0x00C0)
265 * bits that matter: ^^^ (0x00E0)
266 * value of those: 0b0000000011000000 (0x00C0)
267 */
268 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
269 hdrlen = 10;
270 else
271 hdrlen = 16;
272 }
273out:
274 return hdrlen;
275}
276EXPORT_SYMBOL(ieee80211_hdrlen);
277
278unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
279{
280 const struct ieee80211_hdr *hdr =
281 (const struct ieee80211_hdr *)skb->data;
282 unsigned int hdrlen;
283
284 if (unlikely(skb->len < 10))
285 return 0;
286 hdrlen = ieee80211_hdrlen(hdr->frame_control);
287 if (unlikely(hdrlen > skb->len))
288 return 0;
289 return hdrlen;
290}
291EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
292
60fd2b67 293static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
e31a16d6
ZY
294{
295 int ae = meshhdr->flags & MESH_FLAGS_AE;
296 /* 7.1.3.5a.2 */
297 switch (ae) {
298 case 0:
299 return 6;
3c5772a5 300 case MESH_FLAGS_AE_A4:
e31a16d6 301 return 12;
3c5772a5 302 case MESH_FLAGS_AE_A5_A6:
e31a16d6 303 return 18;
3c5772a5 304 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
e31a16d6
ZY
305 return 24;
306 default:
307 return 6;
308 }
309}
310
eaf85ca7 311int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
e31a16d6
ZY
312 enum nl80211_iftype iftype)
313{
314 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
315 u16 hdrlen, ethertype;
316 u8 *payload;
317 u8 dst[ETH_ALEN];
318 u8 src[ETH_ALEN] __aligned(2);
319
320 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
321 return -1;
322
323 hdrlen = ieee80211_hdrlen(hdr->frame_control);
324
325 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
326 * header
327 * IEEE 802.11 address fields:
328 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
329 * 0 0 DA SA BSSID n/a
330 * 0 1 DA BSSID SA n/a
331 * 1 0 BSSID SA DA n/a
332 * 1 1 RA TA DA SA
333 */
334 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
335 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
336
337 switch (hdr->frame_control &
338 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
339 case cpu_to_le16(IEEE80211_FCTL_TODS):
340 if (unlikely(iftype != NL80211_IFTYPE_AP &&
074ac8df
JB
341 iftype != NL80211_IFTYPE_AP_VLAN &&
342 iftype != NL80211_IFTYPE_P2P_GO))
e31a16d6
ZY
343 return -1;
344 break;
345 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
346 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
f14543ee
FF
347 iftype != NL80211_IFTYPE_MESH_POINT &&
348 iftype != NL80211_IFTYPE_AP_VLAN &&
349 iftype != NL80211_IFTYPE_STATION))
e31a16d6
ZY
350 return -1;
351 if (iftype == NL80211_IFTYPE_MESH_POINT) {
352 struct ieee80211s_hdr *meshdr =
353 (struct ieee80211s_hdr *) (skb->data + hdrlen);
e3cf8b3f
ZY
354 /* make sure meshdr->flags is on the linear part */
355 if (!pskb_may_pull(skb, hdrlen + 1))
356 return -1;
e31a16d6 357 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
e3cf8b3f
ZY
358 skb_copy_bits(skb, hdrlen +
359 offsetof(struct ieee80211s_hdr, eaddr1),
360 dst, ETH_ALEN);
361 skb_copy_bits(skb, hdrlen +
362 offsetof(struct ieee80211s_hdr, eaddr2),
363 src, ETH_ALEN);
e31a16d6 364 }
e3cf8b3f 365 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
e31a16d6
ZY
366 }
367 break;
368 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
3c5772a5 369 if ((iftype != NL80211_IFTYPE_STATION &&
074ac8df
JB
370 iftype != NL80211_IFTYPE_P2P_CLIENT &&
371 iftype != NL80211_IFTYPE_MESH_POINT) ||
e31a16d6 372 (is_multicast_ether_addr(dst) &&
4c764729 373 ether_addr_equal(src, addr)))
e31a16d6 374 return -1;
3c5772a5
JC
375 if (iftype == NL80211_IFTYPE_MESH_POINT) {
376 struct ieee80211s_hdr *meshdr =
377 (struct ieee80211s_hdr *) (skb->data + hdrlen);
e3cf8b3f
ZY
378 /* make sure meshdr->flags is on the linear part */
379 if (!pskb_may_pull(skb, hdrlen + 1))
380 return -1;
3c5772a5 381 if (meshdr->flags & MESH_FLAGS_AE_A4)
e3cf8b3f
ZY
382 skb_copy_bits(skb, hdrlen +
383 offsetof(struct ieee80211s_hdr, eaddr1),
384 src, ETH_ALEN);
385 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
3c5772a5 386 }
e31a16d6
ZY
387 break;
388 case cpu_to_le16(0):
941c93cd
AN
389 if (iftype != NL80211_IFTYPE_ADHOC &&
390 iftype != NL80211_IFTYPE_STATION)
391 return -1;
e31a16d6
ZY
392 break;
393 }
394
e3cf8b3f 395 if (!pskb_may_pull(skb, hdrlen + 8))
e31a16d6
ZY
396 return -1;
397
398 payload = skb->data + hdrlen;
399 ethertype = (payload[6] << 8) | payload[7];
400
4c764729 401 if (likely((ether_addr_equal(payload, rfc1042_header) &&
e31a16d6 402 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
4c764729 403 ether_addr_equal(payload, bridge_tunnel_header))) {
e31a16d6
ZY
404 /* remove RFC1042 or Bridge-Tunnel encapsulation and
405 * replace EtherType */
406 skb_pull(skb, hdrlen + 6);
407 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
408 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
409 } else {
410 struct ethhdr *ehdr;
411 __be16 len;
412
413 skb_pull(skb, hdrlen);
414 len = htons(skb->len);
415 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
416 memcpy(ehdr->h_dest, dst, ETH_ALEN);
417 memcpy(ehdr->h_source, src, ETH_ALEN);
418 ehdr->h_proto = len;
419 }
420 return 0;
421}
422EXPORT_SYMBOL(ieee80211_data_to_8023);
423
eaf85ca7 424int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
e31a16d6
ZY
425 enum nl80211_iftype iftype, u8 *bssid, bool qos)
426{
427 struct ieee80211_hdr hdr;
428 u16 hdrlen, ethertype;
429 __le16 fc;
430 const u8 *encaps_data;
431 int encaps_len, skip_header_bytes;
432 int nh_pos, h_pos;
433 int head_need;
434
435 if (unlikely(skb->len < ETH_HLEN))
436 return -EINVAL;
437
438 nh_pos = skb_network_header(skb) - skb->data;
439 h_pos = skb_transport_header(skb) - skb->data;
440
441 /* convert Ethernet header to proper 802.11 header (based on
442 * operation mode) */
443 ethertype = (skb->data[12] << 8) | skb->data[13];
444 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
445
446 switch (iftype) {
447 case NL80211_IFTYPE_AP:
448 case NL80211_IFTYPE_AP_VLAN:
074ac8df 449 case NL80211_IFTYPE_P2P_GO:
e31a16d6
ZY
450 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
451 /* DA BSSID SA */
452 memcpy(hdr.addr1, skb->data, ETH_ALEN);
453 memcpy(hdr.addr2, addr, ETH_ALEN);
454 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
455 hdrlen = 24;
456 break;
457 case NL80211_IFTYPE_STATION:
074ac8df 458 case NL80211_IFTYPE_P2P_CLIENT:
e31a16d6
ZY
459 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
460 /* BSSID SA DA */
461 memcpy(hdr.addr1, bssid, ETH_ALEN);
462 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
463 memcpy(hdr.addr3, skb->data, ETH_ALEN);
464 hdrlen = 24;
465 break;
466 case NL80211_IFTYPE_ADHOC:
467 /* DA SA BSSID */
468 memcpy(hdr.addr1, skb->data, ETH_ALEN);
469 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
470 memcpy(hdr.addr3, bssid, ETH_ALEN);
471 hdrlen = 24;
472 break;
473 default:
474 return -EOPNOTSUPP;
475 }
476
477 if (qos) {
478 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
479 hdrlen += 2;
480 }
481
482 hdr.frame_control = fc;
483 hdr.duration_id = 0;
484 hdr.seq_ctrl = 0;
485
486 skip_header_bytes = ETH_HLEN;
487 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
488 encaps_data = bridge_tunnel_header;
489 encaps_len = sizeof(bridge_tunnel_header);
490 skip_header_bytes -= 2;
491 } else if (ethertype > 0x600) {
492 encaps_data = rfc1042_header;
493 encaps_len = sizeof(rfc1042_header);
494 skip_header_bytes -= 2;
495 } else {
496 encaps_data = NULL;
497 encaps_len = 0;
498 }
499
500 skb_pull(skb, skip_header_bytes);
501 nh_pos -= skip_header_bytes;
502 h_pos -= skip_header_bytes;
503
504 head_need = hdrlen + encaps_len - skb_headroom(skb);
505
506 if (head_need > 0 || skb_cloned(skb)) {
507 head_need = max(head_need, 0);
508 if (head_need)
509 skb_orphan(skb);
510
24616152 511 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
e31a16d6 512 return -ENOMEM;
24616152 513
e31a16d6
ZY
514 skb->truesize += head_need;
515 }
516
517 if (encaps_data) {
518 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
519 nh_pos += encaps_len;
520 h_pos += encaps_len;
521 }
522
523 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
524
525 nh_pos += hdrlen;
526 h_pos += hdrlen;
527
528 /* Update skb pointers to various headers since this modified frame
529 * is going to go through Linux networking code that may potentially
530 * need things like pointer to IP header. */
531 skb_set_mac_header(skb, 0);
532 skb_set_network_header(skb, nh_pos);
533 skb_set_transport_header(skb, h_pos);
534
535 return 0;
536}
537EXPORT_SYMBOL(ieee80211_data_from_8023);
538
eaf85ca7
ZY
539
540void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
541 const u8 *addr, enum nl80211_iftype iftype,
8b3becad
YAP
542 const unsigned int extra_headroom,
543 bool has_80211_header)
eaf85ca7
ZY
544{
545 struct sk_buff *frame = NULL;
546 u16 ethertype;
547 u8 *payload;
548 const struct ethhdr *eth;
549 int remaining, err;
550 u8 dst[ETH_ALEN], src[ETH_ALEN];
551
8b3becad
YAP
552 if (has_80211_header) {
553 err = ieee80211_data_to_8023(skb, addr, iftype);
554 if (err)
555 goto out;
eaf85ca7 556
8b3becad
YAP
557 /* skip the wrapping header */
558 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
559 if (!eth)
560 goto out;
561 } else {
562 eth = (struct ethhdr *) skb->data;
563 }
eaf85ca7
ZY
564
565 while (skb != frame) {
566 u8 padding;
567 __be16 len = eth->h_proto;
568 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
569
570 remaining = skb->len;
571 memcpy(dst, eth->h_dest, ETH_ALEN);
572 memcpy(src, eth->h_source, ETH_ALEN);
573
574 padding = (4 - subframe_len) & 0x3;
575 /* the last MSDU has no padding */
576 if (subframe_len > remaining)
577 goto purge;
578
579 skb_pull(skb, sizeof(struct ethhdr));
580 /* reuse skb for the last subframe */
581 if (remaining <= subframe_len + padding)
582 frame = skb;
583 else {
584 unsigned int hlen = ALIGN(extra_headroom, 4);
585 /*
586 * Allocate and reserve two bytes more for payload
587 * alignment since sizeof(struct ethhdr) is 14.
588 */
589 frame = dev_alloc_skb(hlen + subframe_len + 2);
590 if (!frame)
591 goto purge;
592
593 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
594 memcpy(skb_put(frame, ntohs(len)), skb->data,
595 ntohs(len));
596
597 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
598 padding);
599 if (!eth) {
600 dev_kfree_skb(frame);
601 goto purge;
602 }
603 }
604
605 skb_reset_network_header(frame);
606 frame->dev = skb->dev;
607 frame->priority = skb->priority;
608
609 payload = frame->data;
610 ethertype = (payload[6] << 8) | payload[7];
611
ac422d3c 612 if (likely((ether_addr_equal(payload, rfc1042_header) &&
eaf85ca7 613 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
ac422d3c 614 ether_addr_equal(payload, bridge_tunnel_header))) {
eaf85ca7
ZY
615 /* remove RFC1042 or Bridge-Tunnel
616 * encapsulation and replace EtherType */
617 skb_pull(frame, 6);
618 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
619 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
620 } else {
621 memcpy(skb_push(frame, sizeof(__be16)), &len,
622 sizeof(__be16));
623 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
624 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
625 }
626 __skb_queue_tail(list, frame);
627 }
628
629 return;
630
631 purge:
632 __skb_queue_purge(list);
633 out:
634 dev_kfree_skb(skb);
635}
636EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
637
e31a16d6
ZY
638/* Given a data frame determine the 802.1p/1d tag to use. */
639unsigned int cfg80211_classify8021d(struct sk_buff *skb)
640{
641 unsigned int dscp;
642
643 /* skb->priority values from 256->263 are magic values to
644 * directly indicate a specific 802.1d priority. This is used
645 * to allow 802.1d priority to be passed directly in from VLAN
646 * tags, etc.
647 */
648 if (skb->priority >= 256 && skb->priority <= 263)
649 return skb->priority - 256;
650
651 switch (skb->protocol) {
652 case htons(ETH_P_IP):
b156579b
DT
653 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
654 break;
655 case htons(ETH_P_IPV6):
656 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
e31a16d6
ZY
657 break;
658 default:
659 return 0;
660 }
661
662 return dscp >> 5;
663}
664EXPORT_SYMBOL(cfg80211_classify8021d);
517357c6
JB
665
666const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
667{
668 u8 *end, *pos;
669
670 pos = bss->information_elements;
671 if (pos == NULL)
672 return NULL;
673 end = pos + bss->len_information_elements;
674
675 while (pos + 1 < end) {
676 if (pos + 2 + pos[1] > end)
677 break;
678 if (pos[0] == ie)
679 return pos;
680 pos += 2 + pos[1];
681 }
682
683 return NULL;
684}
685EXPORT_SYMBOL(ieee80211_bss_get_ie);
fffd0934
JB
686
687void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
688{
689 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
690 struct net_device *dev = wdev->netdev;
691 int i;
692
693 if (!wdev->connect_keys)
694 return;
695
696 for (i = 0; i < 6; i++) {
697 if (!wdev->connect_keys->params[i].cipher)
698 continue;
e31b8213 699 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
1e056665 700 &wdev->connect_keys->params[i])) {
e9c0268f 701 netdev_err(dev, "failed to set key %d\n", i);
1e056665
ZY
702 continue;
703 }
fffd0934 704 if (wdev->connect_keys->def == i)
dbd2fd65
JB
705 if (rdev->ops->set_default_key(wdev->wiphy, dev,
706 i, true, true)) {
e9c0268f 707 netdev_err(dev, "failed to set defkey %d\n", i);
1e056665
ZY
708 continue;
709 }
fffd0934
JB
710 if (wdev->connect_keys->defmgmt == i)
711 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
e9c0268f 712 netdev_err(dev, "failed to set mgtdef %d\n", i);
fffd0934
JB
713 }
714
715 kfree(wdev->connect_keys);
716 wdev->connect_keys = NULL;
717}
3d54d255
JB
718
719static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
720{
721 struct cfg80211_event *ev;
722 unsigned long flags;
723 const u8 *bssid = NULL;
724
725 spin_lock_irqsave(&wdev->event_lock, flags);
726 while (!list_empty(&wdev->event_list)) {
727 ev = list_first_entry(&wdev->event_list,
728 struct cfg80211_event, list);
729 list_del(&ev->list);
730 spin_unlock_irqrestore(&wdev->event_lock, flags);
731
732 wdev_lock(wdev);
733 switch (ev->type) {
734 case EVENT_CONNECT_RESULT:
735 if (!is_zero_ether_addr(ev->cr.bssid))
736 bssid = ev->cr.bssid;
737 __cfg80211_connect_result(
738 wdev->netdev, bssid,
739 ev->cr.req_ie, ev->cr.req_ie_len,
740 ev->cr.resp_ie, ev->cr.resp_ie_len,
741 ev->cr.status,
742 ev->cr.status == WLAN_STATUS_SUCCESS,
743 NULL);
744 break;
745 case EVENT_ROAMED:
adbde344
VT
746 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
747 ev->rm.req_ie_len, ev->rm.resp_ie,
748 ev->rm.resp_ie_len);
3d54d255
JB
749 break;
750 case EVENT_DISCONNECTED:
751 __cfg80211_disconnected(wdev->netdev,
752 ev->dc.ie, ev->dc.ie_len,
753 ev->dc.reason, true);
754 break;
755 case EVENT_IBSS_JOINED:
756 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
757 break;
758 }
759 wdev_unlock(wdev);
760
761 kfree(ev);
762
763 spin_lock_irqsave(&wdev->event_lock, flags);
764 }
765 spin_unlock_irqrestore(&wdev->event_lock, flags);
766}
767
768void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
769{
770 struct wireless_dev *wdev;
771
772 ASSERT_RTNL();
773 ASSERT_RDEV_LOCK(rdev);
774
775 mutex_lock(&rdev->devlist_mtx);
776
777 list_for_each_entry(wdev, &rdev->netdev_list, list)
778 cfg80211_process_wdev_events(wdev);
779
780 mutex_unlock(&rdev->devlist_mtx);
781}
782
783int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
784 struct net_device *dev, enum nl80211_iftype ntype,
785 u32 *flags, struct vif_params *params)
786{
787 int err;
788 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
789
790 ASSERT_RDEV_LOCK(rdev);
791
792 /* don't support changing VLANs, you just re-create them */
793 if (otype == NL80211_IFTYPE_AP_VLAN)
794 return -EOPNOTSUPP;
795
796 if (!rdev->ops->change_virtual_intf ||
797 !(rdev->wiphy.interface_modes & (1 << ntype)))
798 return -EOPNOTSUPP;
799
ad4bb6f8 800 /* if it's part of a bridge, reject changing type to station/ibss */
f350a0a8 801 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
074ac8df
JB
802 (ntype == NL80211_IFTYPE_ADHOC ||
803 ntype == NL80211_IFTYPE_STATION ||
804 ntype == NL80211_IFTYPE_P2P_CLIENT))
ad4bb6f8
JB
805 return -EBUSY;
806
f8cdddb8 807 if (ntype != otype && netif_running(dev)) {
e4e32459 808 mutex_lock(&rdev->devlist_mtx);
7527a782
JB
809 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
810 ntype);
e4e32459 811 mutex_unlock(&rdev->devlist_mtx);
7527a782
JB
812 if (err)
813 return err;
814
9bc383de 815 dev->ieee80211_ptr->use_4addr = false;
29cbe68c 816 dev->ieee80211_ptr->mesh_id_up_len = 0;
9bc383de 817
3d54d255 818 switch (otype) {
ac800140
MK
819 case NL80211_IFTYPE_AP:
820 cfg80211_stop_ap(rdev, dev);
821 break;
3d54d255
JB
822 case NL80211_IFTYPE_ADHOC:
823 cfg80211_leave_ibss(rdev, dev, false);
824 break;
825 case NL80211_IFTYPE_STATION:
074ac8df 826 case NL80211_IFTYPE_P2P_CLIENT:
3d54d255
JB
827 cfg80211_disconnect(rdev, dev,
828 WLAN_REASON_DEAUTH_LEAVING, true);
829 break;
830 case NL80211_IFTYPE_MESH_POINT:
831 /* mesh should be handled? */
832 break;
833 default:
834 break;
835 }
836
837 cfg80211_process_rdev_events(rdev);
838 }
839
840 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
841 ntype, flags, params);
842
843 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
844
9bc383de
JB
845 if (!err && params && params->use_4addr != -1)
846 dev->ieee80211_ptr->use_4addr = params->use_4addr;
847
ad4bb6f8
JB
848 if (!err) {
849 dev->priv_flags &= ~IFF_DONT_BRIDGE;
850 switch (ntype) {
851 case NL80211_IFTYPE_STATION:
852 if (dev->ieee80211_ptr->use_4addr)
853 break;
854 /* fall through */
074ac8df 855 case NL80211_IFTYPE_P2P_CLIENT:
ad4bb6f8
JB
856 case NL80211_IFTYPE_ADHOC:
857 dev->priv_flags |= IFF_DONT_BRIDGE;
858 break;
074ac8df 859 case NL80211_IFTYPE_P2P_GO:
ad4bb6f8
JB
860 case NL80211_IFTYPE_AP:
861 case NL80211_IFTYPE_AP_VLAN:
862 case NL80211_IFTYPE_WDS:
863 case NL80211_IFTYPE_MESH_POINT:
864 /* bridging OK */
865 break;
866 case NL80211_IFTYPE_MONITOR:
867 /* monitor can't bridge anyway */
868 break;
869 case NL80211_IFTYPE_UNSPECIFIED:
2e161f78 870 case NUM_NL80211_IFTYPES:
ad4bb6f8
JB
871 /* not happening */
872 break;
873 }
874 }
875
dbbae26a
MK
876 if (!err && ntype != otype && netif_running(dev)) {
877 cfg80211_update_iface_num(rdev, ntype, 1);
878 cfg80211_update_iface_num(rdev, otype, -1);
879 }
880
3d54d255
JB
881 return err;
882}
254416aa
JL
883
884u16 cfg80211_calculate_bitrate(struct rate_info *rate)
885{
886 int modulation, streams, bitrate;
887
888 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
889 return rate->legacy;
890
891 /* the formula below does only work for MCS values smaller than 32 */
2615f375 892 if (WARN_ON_ONCE(rate->mcs >= 32))
254416aa
JL
893 return 0;
894
895 modulation = rate->mcs & 7;
896 streams = (rate->mcs >> 3) + 1;
897
898 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
899 13500000 : 6500000;
900
901 if (modulation < 4)
902 bitrate *= (modulation + 1);
903 else if (modulation == 4)
904 bitrate *= (modulation + 2);
905 else
906 bitrate *= (modulation + 3);
907
908 bitrate *= streams;
909
910 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
911 bitrate = (bitrate / 9) * 10;
912
913 /* do NOT round down here */
914 return (bitrate + 50000) / 100000;
915}
8097e149 916EXPORT_SYMBOL(cfg80211_calculate_bitrate);
56d1893d
JB
917
918int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
919 u32 beacon_int)
920{
921 struct wireless_dev *wdev;
922 int res = 0;
923
924 if (!beacon_int)
925 return -EINVAL;
926
927 mutex_lock(&rdev->devlist_mtx);
928
929 list_for_each_entry(wdev, &rdev->netdev_list, list) {
930 if (!wdev->beacon_interval)
931 continue;
932 if (wdev->beacon_interval != beacon_int) {
933 res = -EINVAL;
934 break;
935 }
936 }
937
938 mutex_unlock(&rdev->devlist_mtx);
939
940 return res;
941}
7527a782 942
d4e50c59
MK
943int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
944 struct wireless_dev *wdev,
945 enum nl80211_iftype iftype,
946 struct ieee80211_channel *chan,
947 enum cfg80211_chan_mode chanmode)
7527a782
JB
948{
949 struct wireless_dev *wdev_iter;
463454b5 950 u32 used_iftypes = BIT(iftype);
7527a782 951 int num[NUM_NL80211_IFTYPES];
d4e50c59
MK
952 struct ieee80211_channel
953 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
954 struct ieee80211_channel *ch;
955 enum cfg80211_chan_mode chmode;
956 int num_different_channels = 0;
7527a782
JB
957 int total = 1;
958 int i, j;
959
960 ASSERT_RTNL();
e4e32459 961 lockdep_assert_held(&rdev->devlist_mtx);
7527a782
JB
962
963 /* Always allow software iftypes */
964 if (rdev->wiphy.software_iftypes & BIT(iftype))
965 return 0;
966
7527a782 967 memset(num, 0, sizeof(num));
d4e50c59 968 memset(used_channels, 0, sizeof(used_channels));
7527a782
JB
969
970 num[iftype] = 1;
971
d4e50c59
MK
972 switch (chanmode) {
973 case CHAN_MODE_UNDEFINED:
974 break;
975 case CHAN_MODE_SHARED:
976 WARN_ON(!chan);
977 used_channels[0] = chan;
978 num_different_channels++;
979 break;
980 case CHAN_MODE_EXCLUSIVE:
981 num_different_channels++;
982 break;
983 }
984
7527a782
JB
985 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
986 if (wdev_iter == wdev)
987 continue;
988 if (!netif_running(wdev_iter->netdev))
989 continue;
990
991 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
992 continue;
993
d4e50c59
MK
994 cfg80211_get_chan_state(rdev, wdev_iter, &ch, &chmode);
995
996 switch (chmode) {
997 case CHAN_MODE_UNDEFINED:
998 break;
999 case CHAN_MODE_SHARED:
1000 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1001 if (!used_channels[i] || used_channels[i] == ch)
1002 break;
1003
e4e32459 1004 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
d4e50c59 1005 return -EBUSY;
d4e50c59
MK
1006
1007 if (used_channels[i] == NULL) {
1008 used_channels[i] = ch;
1009 num_different_channels++;
1010 }
1011 break;
1012 case CHAN_MODE_EXCLUSIVE:
1013 num_different_channels++;
1014 break;
1015 }
1016
7527a782
JB
1017 num[wdev_iter->iftype]++;
1018 total++;
463454b5 1019 used_iftypes |= BIT(wdev_iter->iftype);
7527a782 1020 }
7527a782 1021
8e8b41f9
JB
1022 if (total == 1)
1023 return 0;
1024
7527a782
JB
1025 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1026 const struct ieee80211_iface_combination *c;
1027 struct ieee80211_iface_limit *limits;
463454b5 1028 u32 all_iftypes = 0;
7527a782
JB
1029
1030 c = &rdev->wiphy.iface_combinations[i];
1031
d4e50c59
MK
1032 if (total > c->max_interfaces)
1033 continue;
1034 if (num_different_channels > c->num_different_channels)
1035 continue;
1036
7527a782
JB
1037 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1038 GFP_KERNEL);
1039 if (!limits)
1040 return -ENOMEM;
7527a782
JB
1041
1042 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1043 if (rdev->wiphy.software_iftypes & BIT(iftype))
1044 continue;
1045 for (j = 0; j < c->n_limits; j++) {
463454b5 1046 all_iftypes |= limits[j].types;
e55a4046 1047 if (!(limits[j].types & BIT(iftype)))
7527a782
JB
1048 continue;
1049 if (limits[j].max < num[iftype])
1050 goto cont;
1051 limits[j].max -= num[iftype];
1052 }
1053 }
463454b5
JB
1054
1055 /*
1056 * Finally check that all iftypes that we're currently
1057 * using are actually part of this combination. If they
1058 * aren't then we can't use this combination and have
1059 * to continue to the next.
1060 */
1061 if ((all_iftypes & used_iftypes) != used_iftypes)
1062 goto cont;
1063
1064 /*
1065 * This combination covered all interface types and
1066 * supported the requested numbers, so we're good.
1067 */
7527a782
JB
1068 kfree(limits);
1069 return 0;
1070 cont:
1071 kfree(limits);
1072 }
1073
1074 return -EBUSY;
1075}
34850ab2
JB
1076
1077int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1078 const u8 *rates, unsigned int n_rates,
1079 u32 *mask)
1080{
1081 int i, j;
1082
a401d2bb
JB
1083 if (!sband)
1084 return -EINVAL;
1085
34850ab2
JB
1086 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1087 return -EINVAL;
1088
1089 *mask = 0;
1090
1091 for (i = 0; i < n_rates; i++) {
1092 int rate = (rates[i] & 0x7f) * 5;
1093 bool found = false;
1094
1095 for (j = 0; j < sband->n_bitrates; j++) {
1096 if (sband->bitrates[j].bitrate == rate) {
1097 found = true;
1098 *mask |= BIT(j);
1099 break;
1100 }
1101 }
1102 if (!found)
1103 return -EINVAL;
1104 }
1105
1106 /*
1107 * mask must have at least one bit set here since we
1108 * didn't accept a 0-length rates array nor allowed
1109 * entries in the array that didn't exist
1110 */
1111
1112 return 0;
1113}
11a2a357
JB
1114
1115/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1116/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1117const unsigned char rfc1042_header[] __aligned(2) =
1118 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1119EXPORT_SYMBOL(rfc1042_header);
1120
1121/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1122const unsigned char bridge_tunnel_header[] __aligned(2) =
1123 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1124EXPORT_SYMBOL(bridge_tunnel_header);