blob: 7acb81b9675dc66597f2f1048ab2675542191939 [file] [log] [blame]
Johannes Berg8318d782008-01-24 19:38:38 +01001/*
2 * Wireless utility functions
3 *
Johannes Bergd3236552009-04-20 14:31:42 +02004 * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg8318d782008-01-24 19:38:38 +01005 */
Johannes Bergd3236552009-04-20 14:31:42 +02006#include <linux/bitops.h>
Zhu Yie31a16d2009-05-21 21:47:03 +08007#include <linux/etherdevice.h>
Johannes Bergd3236552009-04-20 14:31:42 +02008#include <net/cfg80211.h>
Zhu Yie31a16d2009-05-21 21:47:03 +08009#include <net/ip.h>
Johannes Berg8318d782008-01-24 19:38:38 +010010#include "core.h"
11
Johannes Bergbd815252008-10-29 20:00:45 +010012struct ieee80211_rate *
13ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010014 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010015{
16 struct ieee80211_rate *result = &sband->bitrates[0];
17 int i;
18
19 for (i = 0; i < sband->n_bitrates; i++) {
20 if (!(basic_rates & BIT(i)))
21 continue;
22 if (sband->bitrates[i].bitrate > bitrate)
23 continue;
24 result = &sband->bitrates[i];
25 }
26
27 return result;
28}
29EXPORT_SYMBOL(ieee80211_get_response_rate);
30
Johannes Berg8318d782008-01-24 19:38:38 +010031int ieee80211_channel_to_frequency(int chan)
32{
33 if (chan < 14)
34 return 2407 + chan * 5;
35
36 if (chan == 14)
37 return 2484;
38
39 /* FIXME: 802.11j 17.3.8.3.2 */
40 return (chan + 1000) * 5;
41}
42EXPORT_SYMBOL(ieee80211_channel_to_frequency);
43
44int ieee80211_frequency_to_channel(int freq)
45{
46 if (freq == 2484)
47 return 14;
48
49 if (freq < 2484)
50 return (freq - 2407) / 5;
51
52 /* FIXME: 802.11j 17.3.8.3.2 */
53 return freq/5 - 1000;
54}
55EXPORT_SYMBOL(ieee80211_frequency_to_channel);
56
Johannes Berg6c507cd2008-03-26 14:14:55 +010057struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
58 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +010059{
60 enum ieee80211_band band;
61 struct ieee80211_supported_band *sband;
62 int i;
63
64 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
65 sband = wiphy->bands[band];
66
67 if (!sband)
68 continue;
69
70 for (i = 0; i < sband->n_channels; i++) {
71 if (sband->channels[i].center_freq == freq)
72 return &sband->channels[i];
73 }
74 }
75
76 return NULL;
77}
Johannes Berg6c507cd2008-03-26 14:14:55 +010078EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +010079
Johannes Berg8318d782008-01-24 19:38:38 +010080static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
81 enum ieee80211_band band)
82{
83 int i, want;
84
85 switch (band) {
86 case IEEE80211_BAND_5GHZ:
87 want = 3;
88 for (i = 0; i < sband->n_bitrates; i++) {
89 if (sband->bitrates[i].bitrate == 60 ||
90 sband->bitrates[i].bitrate == 120 ||
91 sband->bitrates[i].bitrate == 240) {
92 sband->bitrates[i].flags |=
93 IEEE80211_RATE_MANDATORY_A;
94 want--;
95 }
96 }
97 WARN_ON(want);
98 break;
99 case IEEE80211_BAND_2GHZ:
100 want = 7;
101 for (i = 0; i < sband->n_bitrates; i++) {
102 if (sband->bitrates[i].bitrate == 10) {
103 sband->bitrates[i].flags |=
104 IEEE80211_RATE_MANDATORY_B |
105 IEEE80211_RATE_MANDATORY_G;
106 want--;
107 }
108
109 if (sband->bitrates[i].bitrate == 20 ||
110 sband->bitrates[i].bitrate == 55 ||
111 sband->bitrates[i].bitrate == 110 ||
112 sband->bitrates[i].bitrate == 60 ||
113 sband->bitrates[i].bitrate == 120 ||
114 sband->bitrates[i].bitrate == 240) {
115 sband->bitrates[i].flags |=
116 IEEE80211_RATE_MANDATORY_G;
117 want--;
118 }
119
Johannes Bergaac09fb2008-01-30 17:36:10 +0100120 if (sband->bitrates[i].bitrate != 10 &&
121 sband->bitrates[i].bitrate != 20 &&
122 sband->bitrates[i].bitrate != 55 &&
123 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100124 sband->bitrates[i].flags |=
125 IEEE80211_RATE_ERP_G;
126 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100127 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100128 break;
129 case IEEE80211_NUM_BANDS:
130 WARN_ON(1);
131 break;
132 }
133}
134
135void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
136{
137 enum ieee80211_band band;
138
139 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
140 if (wiphy->bands[band])
141 set_mandatory_flags_band(wiphy->bands[band], band);
142}
Johannes Berg08645122009-05-11 13:54:58 +0200143
Johannes Bergfffd0932009-07-08 14:22:54 +0200144int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
145 struct key_params *params, int key_idx,
Johannes Berg08645122009-05-11 13:54:58 +0200146 const u8 *mac_addr)
147{
Johannes Bergfffd0932009-07-08 14:22:54 +0200148 int i;
149
Johannes Berg08645122009-05-11 13:54:58 +0200150 if (key_idx > 5)
151 return -EINVAL;
152
153 /*
154 * Disallow pairwise keys with non-zero index unless it's WEP
155 * (because current deployments use pairwise WEP keys with
156 * non-zero indizes but 802.11i clearly specifies to use zero)
157 */
158 if (mac_addr && key_idx &&
159 params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
160 params->cipher != WLAN_CIPHER_SUITE_WEP104)
161 return -EINVAL;
162
Johannes Berg08645122009-05-11 13:54:58 +0200163 switch (params->cipher) {
164 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200165 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200166 return -EINVAL;
167 break;
168 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200169 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200170 return -EINVAL;
171 break;
172 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200173 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200174 return -EINVAL;
175 break;
176 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200177 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200178 return -EINVAL;
179 break;
180 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200181 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200182 return -EINVAL;
183 break;
184 default:
185 return -EINVAL;
186 }
187
Jouni Malinen9f26a952009-05-15 12:38:32 +0300188 if (params->seq) {
189 switch (params->cipher) {
190 case WLAN_CIPHER_SUITE_WEP40:
191 case WLAN_CIPHER_SUITE_WEP104:
192 /* These ciphers do not use key sequence */
193 return -EINVAL;
194 case WLAN_CIPHER_SUITE_TKIP:
195 case WLAN_CIPHER_SUITE_CCMP:
196 case WLAN_CIPHER_SUITE_AES_CMAC:
197 if (params->seq_len != 6)
198 return -EINVAL;
199 break;
200 }
201 }
202
Johannes Bergfffd0932009-07-08 14:22:54 +0200203 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
204 if (params->cipher == rdev->wiphy.cipher_suites[i])
205 break;
206 if (i == rdev->wiphy.n_cipher_suites)
207 return -EINVAL;
208
Johannes Berg08645122009-05-11 13:54:58 +0200209 return 0;
210}
Zhu Yie31a16d2009-05-21 21:47:03 +0800211
212/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
213/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
214const unsigned char rfc1042_header[] __aligned(2) =
215 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
216EXPORT_SYMBOL(rfc1042_header);
217
218/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
219const unsigned char bridge_tunnel_header[] __aligned(2) =
220 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
221EXPORT_SYMBOL(bridge_tunnel_header);
222
223unsigned int ieee80211_hdrlen(__le16 fc)
224{
225 unsigned int hdrlen = 24;
226
227 if (ieee80211_is_data(fc)) {
228 if (ieee80211_has_a4(fc))
229 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200230 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800231 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200232 if (ieee80211_has_order(fc))
233 hdrlen += IEEE80211_HT_CTL_LEN;
234 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800235 goto out;
236 }
237
238 if (ieee80211_is_ctl(fc)) {
239 /*
240 * ACK and CTS are 10 bytes, all others 16. To see how
241 * to get this condition consider
242 * subtype mask: 0b0000000011110000 (0x00F0)
243 * ACK subtype: 0b0000000011010000 (0x00D0)
244 * CTS subtype: 0b0000000011000000 (0x00C0)
245 * bits that matter: ^^^ (0x00E0)
246 * value of those: 0b0000000011000000 (0x00C0)
247 */
248 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
249 hdrlen = 10;
250 else
251 hdrlen = 16;
252 }
253out:
254 return hdrlen;
255}
256EXPORT_SYMBOL(ieee80211_hdrlen);
257
258unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
259{
260 const struct ieee80211_hdr *hdr =
261 (const struct ieee80211_hdr *)skb->data;
262 unsigned int hdrlen;
263
264 if (unlikely(skb->len < 10))
265 return 0;
266 hdrlen = ieee80211_hdrlen(hdr->frame_control);
267 if (unlikely(hdrlen > skb->len))
268 return 0;
269 return hdrlen;
270}
271EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
272
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400273static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800274{
275 int ae = meshhdr->flags & MESH_FLAGS_AE;
276 /* 7.1.3.5a.2 */
277 switch (ae) {
278 case 0:
279 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700280 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800281 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700282 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800283 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700284 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800285 return 24;
286 default:
287 return 6;
288 }
289}
290
Zhu Yieaf85ca2009-12-01 10:18:37 +0800291int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800292 enum nl80211_iftype iftype)
293{
294 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
295 u16 hdrlen, ethertype;
296 u8 *payload;
297 u8 dst[ETH_ALEN];
298 u8 src[ETH_ALEN] __aligned(2);
299
300 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
301 return -1;
302
303 hdrlen = ieee80211_hdrlen(hdr->frame_control);
304
305 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
306 * header
307 * IEEE 802.11 address fields:
308 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
309 * 0 0 DA SA BSSID n/a
310 * 0 1 DA BSSID SA n/a
311 * 1 0 BSSID SA DA n/a
312 * 1 1 RA TA DA SA
313 */
314 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
315 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
316
317 switch (hdr->frame_control &
318 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
319 case cpu_to_le16(IEEE80211_FCTL_TODS):
320 if (unlikely(iftype != NL80211_IFTYPE_AP &&
321 iftype != NL80211_IFTYPE_AP_VLAN))
322 return -1;
323 break;
324 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
325 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100326 iftype != NL80211_IFTYPE_MESH_POINT &&
327 iftype != NL80211_IFTYPE_AP_VLAN &&
328 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800329 return -1;
330 if (iftype == NL80211_IFTYPE_MESH_POINT) {
331 struct ieee80211s_hdr *meshdr =
332 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800333 /* make sure meshdr->flags is on the linear part */
334 if (!pskb_may_pull(skb, hdrlen + 1))
335 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800336 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800337 skb_copy_bits(skb, hdrlen +
338 offsetof(struct ieee80211s_hdr, eaddr1),
339 dst, ETH_ALEN);
340 skb_copy_bits(skb, hdrlen +
341 offsetof(struct ieee80211s_hdr, eaddr2),
342 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800343 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800344 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800345 }
346 break;
347 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700348 if ((iftype != NL80211_IFTYPE_STATION &&
349 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800350 (is_multicast_ether_addr(dst) &&
351 !compare_ether_addr(src, addr)))
352 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700353 if (iftype == NL80211_IFTYPE_MESH_POINT) {
354 struct ieee80211s_hdr *meshdr =
355 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800356 /* make sure meshdr->flags is on the linear part */
357 if (!pskb_may_pull(skb, hdrlen + 1))
358 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700359 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800360 skb_copy_bits(skb, hdrlen +
361 offsetof(struct ieee80211s_hdr, eaddr1),
362 src, ETH_ALEN);
363 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700364 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800365 break;
366 case cpu_to_le16(0):
367 if (iftype != NL80211_IFTYPE_ADHOC)
368 return -1;
369 break;
370 }
371
Zhu Yie3cf8b32010-03-29 17:35:07 +0800372 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800373 return -1;
374
375 payload = skb->data + hdrlen;
376 ethertype = (payload[6] << 8) | payload[7];
377
378 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
379 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
380 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
381 /* remove RFC1042 or Bridge-Tunnel encapsulation and
382 * replace EtherType */
383 skb_pull(skb, hdrlen + 6);
384 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
385 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
386 } else {
387 struct ethhdr *ehdr;
388 __be16 len;
389
390 skb_pull(skb, hdrlen);
391 len = htons(skb->len);
392 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
393 memcpy(ehdr->h_dest, dst, ETH_ALEN);
394 memcpy(ehdr->h_source, src, ETH_ALEN);
395 ehdr->h_proto = len;
396 }
397 return 0;
398}
399EXPORT_SYMBOL(ieee80211_data_to_8023);
400
Zhu Yieaf85ca2009-12-01 10:18:37 +0800401int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800402 enum nl80211_iftype iftype, u8 *bssid, bool qos)
403{
404 struct ieee80211_hdr hdr;
405 u16 hdrlen, ethertype;
406 __le16 fc;
407 const u8 *encaps_data;
408 int encaps_len, skip_header_bytes;
409 int nh_pos, h_pos;
410 int head_need;
411
412 if (unlikely(skb->len < ETH_HLEN))
413 return -EINVAL;
414
415 nh_pos = skb_network_header(skb) - skb->data;
416 h_pos = skb_transport_header(skb) - skb->data;
417
418 /* convert Ethernet header to proper 802.11 header (based on
419 * operation mode) */
420 ethertype = (skb->data[12] << 8) | skb->data[13];
421 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
422
423 switch (iftype) {
424 case NL80211_IFTYPE_AP:
425 case NL80211_IFTYPE_AP_VLAN:
426 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
427 /* DA BSSID SA */
428 memcpy(hdr.addr1, skb->data, ETH_ALEN);
429 memcpy(hdr.addr2, addr, ETH_ALEN);
430 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
431 hdrlen = 24;
432 break;
433 case NL80211_IFTYPE_STATION:
434 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
435 /* BSSID SA DA */
436 memcpy(hdr.addr1, bssid, ETH_ALEN);
437 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
438 memcpy(hdr.addr3, skb->data, ETH_ALEN);
439 hdrlen = 24;
440 break;
441 case NL80211_IFTYPE_ADHOC:
442 /* DA SA BSSID */
443 memcpy(hdr.addr1, skb->data, ETH_ALEN);
444 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
445 memcpy(hdr.addr3, bssid, ETH_ALEN);
446 hdrlen = 24;
447 break;
448 default:
449 return -EOPNOTSUPP;
450 }
451
452 if (qos) {
453 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
454 hdrlen += 2;
455 }
456
457 hdr.frame_control = fc;
458 hdr.duration_id = 0;
459 hdr.seq_ctrl = 0;
460
461 skip_header_bytes = ETH_HLEN;
462 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
463 encaps_data = bridge_tunnel_header;
464 encaps_len = sizeof(bridge_tunnel_header);
465 skip_header_bytes -= 2;
466 } else if (ethertype > 0x600) {
467 encaps_data = rfc1042_header;
468 encaps_len = sizeof(rfc1042_header);
469 skip_header_bytes -= 2;
470 } else {
471 encaps_data = NULL;
472 encaps_len = 0;
473 }
474
475 skb_pull(skb, skip_header_bytes);
476 nh_pos -= skip_header_bytes;
477 h_pos -= skip_header_bytes;
478
479 head_need = hdrlen + encaps_len - skb_headroom(skb);
480
481 if (head_need > 0 || skb_cloned(skb)) {
482 head_need = max(head_need, 0);
483 if (head_need)
484 skb_orphan(skb);
485
486 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
487 printk(KERN_ERR "failed to reallocate Tx buffer\n");
488 return -ENOMEM;
489 }
490 skb->truesize += head_need;
491 }
492
493 if (encaps_data) {
494 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
495 nh_pos += encaps_len;
496 h_pos += encaps_len;
497 }
498
499 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
500
501 nh_pos += hdrlen;
502 h_pos += hdrlen;
503
504 /* Update skb pointers to various headers since this modified frame
505 * is going to go through Linux networking code that may potentially
506 * need things like pointer to IP header. */
507 skb_set_mac_header(skb, 0);
508 skb_set_network_header(skb, nh_pos);
509 skb_set_transport_header(skb, h_pos);
510
511 return 0;
512}
513EXPORT_SYMBOL(ieee80211_data_from_8023);
514
Zhu Yieaf85ca2009-12-01 10:18:37 +0800515
516void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
517 const u8 *addr, enum nl80211_iftype iftype,
518 const unsigned int extra_headroom)
519{
520 struct sk_buff *frame = NULL;
521 u16 ethertype;
522 u8 *payload;
523 const struct ethhdr *eth;
524 int remaining, err;
525 u8 dst[ETH_ALEN], src[ETH_ALEN];
526
527 err = ieee80211_data_to_8023(skb, addr, iftype);
528 if (err)
529 goto out;
530
531 /* skip the wrapping header */
532 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
533 if (!eth)
534 goto out;
535
536 while (skb != frame) {
537 u8 padding;
538 __be16 len = eth->h_proto;
539 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
540
541 remaining = skb->len;
542 memcpy(dst, eth->h_dest, ETH_ALEN);
543 memcpy(src, eth->h_source, ETH_ALEN);
544
545 padding = (4 - subframe_len) & 0x3;
546 /* the last MSDU has no padding */
547 if (subframe_len > remaining)
548 goto purge;
549
550 skb_pull(skb, sizeof(struct ethhdr));
551 /* reuse skb for the last subframe */
552 if (remaining <= subframe_len + padding)
553 frame = skb;
554 else {
555 unsigned int hlen = ALIGN(extra_headroom, 4);
556 /*
557 * Allocate and reserve two bytes more for payload
558 * alignment since sizeof(struct ethhdr) is 14.
559 */
560 frame = dev_alloc_skb(hlen + subframe_len + 2);
561 if (!frame)
562 goto purge;
563
564 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
565 memcpy(skb_put(frame, ntohs(len)), skb->data,
566 ntohs(len));
567
568 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
569 padding);
570 if (!eth) {
571 dev_kfree_skb(frame);
572 goto purge;
573 }
574 }
575
576 skb_reset_network_header(frame);
577 frame->dev = skb->dev;
578 frame->priority = skb->priority;
579
580 payload = frame->data;
581 ethertype = (payload[6] << 8) | payload[7];
582
583 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
584 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
585 compare_ether_addr(payload,
586 bridge_tunnel_header) == 0)) {
587 /* remove RFC1042 or Bridge-Tunnel
588 * encapsulation and replace EtherType */
589 skb_pull(frame, 6);
590 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
591 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
592 } else {
593 memcpy(skb_push(frame, sizeof(__be16)), &len,
594 sizeof(__be16));
595 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
596 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
597 }
598 __skb_queue_tail(list, frame);
599 }
600
601 return;
602
603 purge:
604 __skb_queue_purge(list);
605 out:
606 dev_kfree_skb(skb);
607}
608EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
609
Zhu Yie31a16d2009-05-21 21:47:03 +0800610/* Given a data frame determine the 802.1p/1d tag to use. */
611unsigned int cfg80211_classify8021d(struct sk_buff *skb)
612{
613 unsigned int dscp;
614
615 /* skb->priority values from 256->263 are magic values to
616 * directly indicate a specific 802.1d priority. This is used
617 * to allow 802.1d priority to be passed directly in from VLAN
618 * tags, etc.
619 */
620 if (skb->priority >= 256 && skb->priority <= 263)
621 return skb->priority - 256;
622
623 switch (skb->protocol) {
624 case htons(ETH_P_IP):
625 dscp = ip_hdr(skb)->tos & 0xfc;
626 break;
627 default:
628 return 0;
629 }
630
631 return dscp >> 5;
632}
633EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200634
635const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
636{
637 u8 *end, *pos;
638
639 pos = bss->information_elements;
640 if (pos == NULL)
641 return NULL;
642 end = pos + bss->len_information_elements;
643
644 while (pos + 1 < end) {
645 if (pos + 2 + pos[1] > end)
646 break;
647 if (pos[0] == ie)
648 return pos;
649 pos += 2 + pos[1];
650 }
651
652 return NULL;
653}
654EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200655
656void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
657{
658 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
659 struct net_device *dev = wdev->netdev;
660 int i;
661
662 if (!wdev->connect_keys)
663 return;
664
665 for (i = 0; i < 6; i++) {
666 if (!wdev->connect_keys->params[i].cipher)
667 continue;
668 if (rdev->ops->add_key(wdev->wiphy, dev, i, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800669 &wdev->connect_keys->params[i])) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200670 printk(KERN_ERR "%s: failed to set key %d\n",
671 dev->name, i);
Zhu Yi1e056662009-07-20 16:12:57 +0800672 continue;
673 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200674 if (wdev->connect_keys->def == i)
Zhu Yi1e056662009-07-20 16:12:57 +0800675 if (rdev->ops->set_default_key(wdev->wiphy, dev, i)) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200676 printk(KERN_ERR "%s: failed to set defkey %d\n",
677 dev->name, i);
Zhu Yi1e056662009-07-20 16:12:57 +0800678 continue;
679 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200680 if (wdev->connect_keys->defmgmt == i)
681 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
682 printk(KERN_ERR "%s: failed to set mgtdef %d\n",
683 dev->name, i);
684 }
685
686 kfree(wdev->connect_keys);
687 wdev->connect_keys = NULL;
688}
Johannes Berg3d54d252009-08-21 14:51:05 +0200689
690static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
691{
692 struct cfg80211_event *ev;
693 unsigned long flags;
694 const u8 *bssid = NULL;
695
696 spin_lock_irqsave(&wdev->event_lock, flags);
697 while (!list_empty(&wdev->event_list)) {
698 ev = list_first_entry(&wdev->event_list,
699 struct cfg80211_event, list);
700 list_del(&ev->list);
701 spin_unlock_irqrestore(&wdev->event_lock, flags);
702
703 wdev_lock(wdev);
704 switch (ev->type) {
705 case EVENT_CONNECT_RESULT:
706 if (!is_zero_ether_addr(ev->cr.bssid))
707 bssid = ev->cr.bssid;
708 __cfg80211_connect_result(
709 wdev->netdev, bssid,
710 ev->cr.req_ie, ev->cr.req_ie_len,
711 ev->cr.resp_ie, ev->cr.resp_ie_len,
712 ev->cr.status,
713 ev->cr.status == WLAN_STATUS_SUCCESS,
714 NULL);
715 break;
716 case EVENT_ROAMED:
717 __cfg80211_roamed(wdev, ev->rm.bssid,
718 ev->rm.req_ie, ev->rm.req_ie_len,
719 ev->rm.resp_ie, ev->rm.resp_ie_len);
720 break;
721 case EVENT_DISCONNECTED:
722 __cfg80211_disconnected(wdev->netdev,
723 ev->dc.ie, ev->dc.ie_len,
724 ev->dc.reason, true);
725 break;
726 case EVENT_IBSS_JOINED:
727 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
728 break;
729 }
730 wdev_unlock(wdev);
731
732 kfree(ev);
733
734 spin_lock_irqsave(&wdev->event_lock, flags);
735 }
736 spin_unlock_irqrestore(&wdev->event_lock, flags);
737}
738
739void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
740{
741 struct wireless_dev *wdev;
742
743 ASSERT_RTNL();
744 ASSERT_RDEV_LOCK(rdev);
745
746 mutex_lock(&rdev->devlist_mtx);
747
748 list_for_each_entry(wdev, &rdev->netdev_list, list)
749 cfg80211_process_wdev_events(wdev);
750
751 mutex_unlock(&rdev->devlist_mtx);
752}
753
754int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
755 struct net_device *dev, enum nl80211_iftype ntype,
756 u32 *flags, struct vif_params *params)
757{
758 int err;
759 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
760
761 ASSERT_RDEV_LOCK(rdev);
762
763 /* don't support changing VLANs, you just re-create them */
764 if (otype == NL80211_IFTYPE_AP_VLAN)
765 return -EOPNOTSUPP;
766
767 if (!rdev->ops->change_virtual_intf ||
768 !(rdev->wiphy.interface_modes & (1 << ntype)))
769 return -EOPNOTSUPP;
770
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100771 /* if it's part of a bridge, reject changing type to station/ibss */
772 if (dev->br_port && (ntype == NL80211_IFTYPE_ADHOC ||
773 ntype == NL80211_IFTYPE_STATION))
774 return -EBUSY;
775
Johannes Berg3d54d252009-08-21 14:51:05 +0200776 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +0100777 dev->ieee80211_ptr->use_4addr = false;
778
Johannes Berg3d54d252009-08-21 14:51:05 +0200779 switch (otype) {
780 case NL80211_IFTYPE_ADHOC:
781 cfg80211_leave_ibss(rdev, dev, false);
782 break;
783 case NL80211_IFTYPE_STATION:
784 cfg80211_disconnect(rdev, dev,
785 WLAN_REASON_DEAUTH_LEAVING, true);
786 break;
787 case NL80211_IFTYPE_MESH_POINT:
788 /* mesh should be handled? */
789 break;
790 default:
791 break;
792 }
793
794 cfg80211_process_rdev_events(rdev);
795 }
796
797 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
798 ntype, flags, params);
799
800 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
801
Johannes Berg9bc383d2009-11-19 11:55:19 +0100802 if (!err && params && params->use_4addr != -1)
803 dev->ieee80211_ptr->use_4addr = params->use_4addr;
804
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100805 if (!err) {
806 dev->priv_flags &= ~IFF_DONT_BRIDGE;
807 switch (ntype) {
808 case NL80211_IFTYPE_STATION:
809 if (dev->ieee80211_ptr->use_4addr)
810 break;
811 /* fall through */
812 case NL80211_IFTYPE_ADHOC:
813 dev->priv_flags |= IFF_DONT_BRIDGE;
814 break;
815 case NL80211_IFTYPE_AP:
816 case NL80211_IFTYPE_AP_VLAN:
817 case NL80211_IFTYPE_WDS:
818 case NL80211_IFTYPE_MESH_POINT:
819 /* bridging OK */
820 break;
821 case NL80211_IFTYPE_MONITOR:
822 /* monitor can't bridge anyway */
823 break;
824 case NL80211_IFTYPE_UNSPECIFIED:
825 case __NL80211_IFTYPE_AFTER_LAST:
826 /* not happening */
827 break;
828 }
829 }
830
Johannes Berg3d54d252009-08-21 14:51:05 +0200831 return err;
832}
John W. Linville254416a2009-12-09 16:43:52 -0500833
834u16 cfg80211_calculate_bitrate(struct rate_info *rate)
835{
836 int modulation, streams, bitrate;
837
838 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
839 return rate->legacy;
840
841 /* the formula below does only work for MCS values smaller than 32 */
842 if (rate->mcs >= 32)
843 return 0;
844
845 modulation = rate->mcs & 7;
846 streams = (rate->mcs >> 3) + 1;
847
848 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
849 13500000 : 6500000;
850
851 if (modulation < 4)
852 bitrate *= (modulation + 1);
853 else if (modulation == 4)
854 bitrate *= (modulation + 2);
855 else
856 bitrate *= (modulation + 3);
857
858 bitrate *= streams;
859
860 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
861 bitrate = (bitrate / 9) * 10;
862
863 /* do NOT round down here */
864 return (bitrate + 50000) / 100000;
865}