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