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