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