blob: fb5448f7d55af55b8ee8220ef813880993d0b069 [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:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300186 /*
187 * We don't know anything about this algorithm,
188 * allow using it -- but the driver must check
189 * all parameters! We still check below whether
190 * or not the driver supports this algorithm,
191 * of course.
192 */
193 break;
Johannes Berg08645122009-05-11 13:54:58 +0200194 }
195
Jouni Malinen9f26a952009-05-15 12:38:32 +0300196 if (params->seq) {
197 switch (params->cipher) {
198 case WLAN_CIPHER_SUITE_WEP40:
199 case WLAN_CIPHER_SUITE_WEP104:
200 /* These ciphers do not use key sequence */
201 return -EINVAL;
202 case WLAN_CIPHER_SUITE_TKIP:
203 case WLAN_CIPHER_SUITE_CCMP:
204 case WLAN_CIPHER_SUITE_AES_CMAC:
205 if (params->seq_len != 6)
206 return -EINVAL;
207 break;
208 }
209 }
210
Johannes Bergfffd0932009-07-08 14:22:54 +0200211 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
212 if (params->cipher == rdev->wiphy.cipher_suites[i])
213 break;
214 if (i == rdev->wiphy.n_cipher_suites)
215 return -EINVAL;
216
Johannes Berg08645122009-05-11 13:54:58 +0200217 return 0;
218}
Zhu Yie31a16d2009-05-21 21:47:03 +0800219
220/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
221/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
222const unsigned char rfc1042_header[] __aligned(2) =
223 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
224EXPORT_SYMBOL(rfc1042_header);
225
226/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
227const unsigned char bridge_tunnel_header[] __aligned(2) =
228 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
229EXPORT_SYMBOL(bridge_tunnel_header);
230
Johannes Berg633adf12010-08-12 14:49:58 +0200231unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800232{
233 unsigned int hdrlen = 24;
234
235 if (ieee80211_is_data(fc)) {
236 if (ieee80211_has_a4(fc))
237 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200238 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800239 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200240 if (ieee80211_has_order(fc))
241 hdrlen += IEEE80211_HT_CTL_LEN;
242 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800243 goto out;
244 }
245
246 if (ieee80211_is_ctl(fc)) {
247 /*
248 * ACK and CTS are 10 bytes, all others 16. To see how
249 * to get this condition consider
250 * subtype mask: 0b0000000011110000 (0x00F0)
251 * ACK subtype: 0b0000000011010000 (0x00D0)
252 * CTS subtype: 0b0000000011000000 (0x00C0)
253 * bits that matter: ^^^ (0x00E0)
254 * value of those: 0b0000000011000000 (0x00C0)
255 */
256 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
257 hdrlen = 10;
258 else
259 hdrlen = 16;
260 }
261out:
262 return hdrlen;
263}
264EXPORT_SYMBOL(ieee80211_hdrlen);
265
266unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
267{
268 const struct ieee80211_hdr *hdr =
269 (const struct ieee80211_hdr *)skb->data;
270 unsigned int hdrlen;
271
272 if (unlikely(skb->len < 10))
273 return 0;
274 hdrlen = ieee80211_hdrlen(hdr->frame_control);
275 if (unlikely(hdrlen > skb->len))
276 return 0;
277 return hdrlen;
278}
279EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
280
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400281static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800282{
283 int ae = meshhdr->flags & MESH_FLAGS_AE;
284 /* 7.1.3.5a.2 */
285 switch (ae) {
286 case 0:
287 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700288 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800289 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700290 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800291 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700292 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800293 return 24;
294 default:
295 return 6;
296 }
297}
298
Zhu Yieaf85ca2009-12-01 10:18:37 +0800299int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800300 enum nl80211_iftype iftype)
301{
302 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
303 u16 hdrlen, ethertype;
304 u8 *payload;
305 u8 dst[ETH_ALEN];
306 u8 src[ETH_ALEN] __aligned(2);
307
308 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
309 return -1;
310
311 hdrlen = ieee80211_hdrlen(hdr->frame_control);
312
313 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
314 * header
315 * IEEE 802.11 address fields:
316 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
317 * 0 0 DA SA BSSID n/a
318 * 0 1 DA BSSID SA n/a
319 * 1 0 BSSID SA DA n/a
320 * 1 1 RA TA DA SA
321 */
322 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
323 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
324
325 switch (hdr->frame_control &
326 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
327 case cpu_to_le16(IEEE80211_FCTL_TODS):
328 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200329 iftype != NL80211_IFTYPE_AP_VLAN &&
330 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800331 return -1;
332 break;
333 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
334 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543e2009-11-10 20:10:05 +0100335 iftype != NL80211_IFTYPE_MESH_POINT &&
336 iftype != NL80211_IFTYPE_AP_VLAN &&
337 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800338 return -1;
339 if (iftype == NL80211_IFTYPE_MESH_POINT) {
340 struct ieee80211s_hdr *meshdr =
341 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800342 /* make sure meshdr->flags is on the linear part */
343 if (!pskb_may_pull(skb, hdrlen + 1))
344 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800345 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800346 skb_copy_bits(skb, hdrlen +
347 offsetof(struct ieee80211s_hdr, eaddr1),
348 dst, ETH_ALEN);
349 skb_copy_bits(skb, hdrlen +
350 offsetof(struct ieee80211s_hdr, eaddr2),
351 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800352 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800353 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800354 }
355 break;
356 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700357 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200358 iftype != NL80211_IFTYPE_P2P_CLIENT &&
359 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800360 (is_multicast_ether_addr(dst) &&
361 !compare_ether_addr(src, addr)))
362 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700363 if (iftype == NL80211_IFTYPE_MESH_POINT) {
364 struct ieee80211s_hdr *meshdr =
365 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800366 /* make sure meshdr->flags is on the linear part */
367 if (!pskb_may_pull(skb, hdrlen + 1))
368 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700369 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800370 skb_copy_bits(skb, hdrlen +
371 offsetof(struct ieee80211s_hdr, eaddr1),
372 src, ETH_ALEN);
373 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700374 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800375 break;
376 case cpu_to_le16(0):
377 if (iftype != NL80211_IFTYPE_ADHOC)
378 return -1;
379 break;
380 }
381
Zhu Yie3cf8b32010-03-29 17:35:07 +0800382 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800383 return -1;
384
385 payload = skb->data + hdrlen;
386 ethertype = (payload[6] << 8) | payload[7];
387
388 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
389 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
390 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
391 /* remove RFC1042 or Bridge-Tunnel encapsulation and
392 * replace EtherType */
393 skb_pull(skb, hdrlen + 6);
394 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
395 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
396 } else {
397 struct ethhdr *ehdr;
398 __be16 len;
399
400 skb_pull(skb, hdrlen);
401 len = htons(skb->len);
402 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
403 memcpy(ehdr->h_dest, dst, ETH_ALEN);
404 memcpy(ehdr->h_source, src, ETH_ALEN);
405 ehdr->h_proto = len;
406 }
407 return 0;
408}
409EXPORT_SYMBOL(ieee80211_data_to_8023);
410
Zhu Yieaf85ca2009-12-01 10:18:37 +0800411int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800412 enum nl80211_iftype iftype, u8 *bssid, bool qos)
413{
414 struct ieee80211_hdr hdr;
415 u16 hdrlen, ethertype;
416 __le16 fc;
417 const u8 *encaps_data;
418 int encaps_len, skip_header_bytes;
419 int nh_pos, h_pos;
420 int head_need;
421
422 if (unlikely(skb->len < ETH_HLEN))
423 return -EINVAL;
424
425 nh_pos = skb_network_header(skb) - skb->data;
426 h_pos = skb_transport_header(skb) - skb->data;
427
428 /* convert Ethernet header to proper 802.11 header (based on
429 * operation mode) */
430 ethertype = (skb->data[12] << 8) | skb->data[13];
431 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
432
433 switch (iftype) {
434 case NL80211_IFTYPE_AP:
435 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200436 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800437 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
438 /* DA BSSID SA */
439 memcpy(hdr.addr1, skb->data, ETH_ALEN);
440 memcpy(hdr.addr2, addr, ETH_ALEN);
441 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
442 hdrlen = 24;
443 break;
444 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200445 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800446 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
447 /* BSSID SA DA */
448 memcpy(hdr.addr1, bssid, ETH_ALEN);
449 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
450 memcpy(hdr.addr3, skb->data, ETH_ALEN);
451 hdrlen = 24;
452 break;
453 case NL80211_IFTYPE_ADHOC:
454 /* DA SA BSSID */
455 memcpy(hdr.addr1, skb->data, ETH_ALEN);
456 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
457 memcpy(hdr.addr3, bssid, ETH_ALEN);
458 hdrlen = 24;
459 break;
460 default:
461 return -EOPNOTSUPP;
462 }
463
464 if (qos) {
465 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
466 hdrlen += 2;
467 }
468
469 hdr.frame_control = fc;
470 hdr.duration_id = 0;
471 hdr.seq_ctrl = 0;
472
473 skip_header_bytes = ETH_HLEN;
474 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
475 encaps_data = bridge_tunnel_header;
476 encaps_len = sizeof(bridge_tunnel_header);
477 skip_header_bytes -= 2;
478 } else if (ethertype > 0x600) {
479 encaps_data = rfc1042_header;
480 encaps_len = sizeof(rfc1042_header);
481 skip_header_bytes -= 2;
482 } else {
483 encaps_data = NULL;
484 encaps_len = 0;
485 }
486
487 skb_pull(skb, skip_header_bytes);
488 nh_pos -= skip_header_bytes;
489 h_pos -= skip_header_bytes;
490
491 head_need = hdrlen + encaps_len - skb_headroom(skb);
492
493 if (head_need > 0 || skb_cloned(skb)) {
494 head_need = max(head_need, 0);
495 if (head_need)
496 skb_orphan(skb);
497
498 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
499 printk(KERN_ERR "failed to reallocate Tx buffer\n");
500 return -ENOMEM;
501 }
502 skb->truesize += head_need;
503 }
504
505 if (encaps_data) {
506 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
507 nh_pos += encaps_len;
508 h_pos += encaps_len;
509 }
510
511 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
512
513 nh_pos += hdrlen;
514 h_pos += hdrlen;
515
516 /* Update skb pointers to various headers since this modified frame
517 * is going to go through Linux networking code that may potentially
518 * need things like pointer to IP header. */
519 skb_set_mac_header(skb, 0);
520 skb_set_network_header(skb, nh_pos);
521 skb_set_transport_header(skb, h_pos);
522
523 return 0;
524}
525EXPORT_SYMBOL(ieee80211_data_from_8023);
526
Zhu Yieaf85ca2009-12-01 10:18:37 +0800527
528void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
529 const u8 *addr, enum nl80211_iftype iftype,
530 const unsigned int extra_headroom)
531{
532 struct sk_buff *frame = NULL;
533 u16 ethertype;
534 u8 *payload;
535 const struct ethhdr *eth;
536 int remaining, err;
537 u8 dst[ETH_ALEN], src[ETH_ALEN];
538
539 err = ieee80211_data_to_8023(skb, addr, iftype);
540 if (err)
541 goto out;
542
543 /* skip the wrapping header */
544 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
545 if (!eth)
546 goto out;
547
548 while (skb != frame) {
549 u8 padding;
550 __be16 len = eth->h_proto;
551 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
552
553 remaining = skb->len;
554 memcpy(dst, eth->h_dest, ETH_ALEN);
555 memcpy(src, eth->h_source, ETH_ALEN);
556
557 padding = (4 - subframe_len) & 0x3;
558 /* the last MSDU has no padding */
559 if (subframe_len > remaining)
560 goto purge;
561
562 skb_pull(skb, sizeof(struct ethhdr));
563 /* reuse skb for the last subframe */
564 if (remaining <= subframe_len + padding)
565 frame = skb;
566 else {
567 unsigned int hlen = ALIGN(extra_headroom, 4);
568 /*
569 * Allocate and reserve two bytes more for payload
570 * alignment since sizeof(struct ethhdr) is 14.
571 */
572 frame = dev_alloc_skb(hlen + subframe_len + 2);
573 if (!frame)
574 goto purge;
575
576 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
577 memcpy(skb_put(frame, ntohs(len)), skb->data,
578 ntohs(len));
579
580 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
581 padding);
582 if (!eth) {
583 dev_kfree_skb(frame);
584 goto purge;
585 }
586 }
587
588 skb_reset_network_header(frame);
589 frame->dev = skb->dev;
590 frame->priority = skb->priority;
591
592 payload = frame->data;
593 ethertype = (payload[6] << 8) | payload[7];
594
595 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
596 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
597 compare_ether_addr(payload,
598 bridge_tunnel_header) == 0)) {
599 /* remove RFC1042 or Bridge-Tunnel
600 * encapsulation and replace EtherType */
601 skb_pull(frame, 6);
602 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
603 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
604 } else {
605 memcpy(skb_push(frame, sizeof(__be16)), &len,
606 sizeof(__be16));
607 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
608 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
609 }
610 __skb_queue_tail(list, frame);
611 }
612
613 return;
614
615 purge:
616 __skb_queue_purge(list);
617 out:
618 dev_kfree_skb(skb);
619}
620EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
621
Zhu Yie31a16d2009-05-21 21:47:03 +0800622/* Given a data frame determine the 802.1p/1d tag to use. */
623unsigned int cfg80211_classify8021d(struct sk_buff *skb)
624{
625 unsigned int dscp;
626
627 /* skb->priority values from 256->263 are magic values to
628 * directly indicate a specific 802.1d priority. This is used
629 * to allow 802.1d priority to be passed directly in from VLAN
630 * tags, etc.
631 */
632 if (skb->priority >= 256 && skb->priority <= 263)
633 return skb->priority - 256;
634
635 switch (skb->protocol) {
636 case htons(ETH_P_IP):
637 dscp = ip_hdr(skb)->tos & 0xfc;
638 break;
639 default:
640 return 0;
641 }
642
643 return dscp >> 5;
644}
645EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200646
647const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
648{
649 u8 *end, *pos;
650
651 pos = bss->information_elements;
652 if (pos == NULL)
653 return NULL;
654 end = pos + bss->len_information_elements;
655
656 while (pos + 1 < end) {
657 if (pos + 2 + pos[1] > end)
658 break;
659 if (pos[0] == ie)
660 return pos;
661 pos += 2 + pos[1];
662 }
663
664 return NULL;
665}
666EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200667
668void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
669{
670 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
671 struct net_device *dev = wdev->netdev;
672 int i;
673
674 if (!wdev->connect_keys)
675 return;
676
677 for (i = 0; i < 6; i++) {
678 if (!wdev->connect_keys->params[i].cipher)
679 continue;
680 if (rdev->ops->add_key(wdev->wiphy, dev, i, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800681 &wdev->connect_keys->params[i])) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200682 printk(KERN_ERR "%s: failed to set key %d\n",
683 dev->name, i);
Zhu Yi1e056662009-07-20 16:12:57 +0800684 continue;
685 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200686 if (wdev->connect_keys->def == i)
Zhu Yi1e056662009-07-20 16:12:57 +0800687 if (rdev->ops->set_default_key(wdev->wiphy, dev, i)) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200688 printk(KERN_ERR "%s: failed to set defkey %d\n",
689 dev->name, i);
Zhu Yi1e056662009-07-20 16:12:57 +0800690 continue;
691 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200692 if (wdev->connect_keys->defmgmt == i)
693 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
694 printk(KERN_ERR "%s: failed to set mgtdef %d\n",
695 dev->name, i);
696 }
697
698 kfree(wdev->connect_keys);
699 wdev->connect_keys = NULL;
700}
Johannes Berg3d54d252009-08-21 14:51:05 +0200701
702static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
703{
704 struct cfg80211_event *ev;
705 unsigned long flags;
706 const u8 *bssid = NULL;
707
708 spin_lock_irqsave(&wdev->event_lock, flags);
709 while (!list_empty(&wdev->event_list)) {
710 ev = list_first_entry(&wdev->event_list,
711 struct cfg80211_event, list);
712 list_del(&ev->list);
713 spin_unlock_irqrestore(&wdev->event_lock, flags);
714
715 wdev_lock(wdev);
716 switch (ev->type) {
717 case EVENT_CONNECT_RESULT:
718 if (!is_zero_ether_addr(ev->cr.bssid))
719 bssid = ev->cr.bssid;
720 __cfg80211_connect_result(
721 wdev->netdev, bssid,
722 ev->cr.req_ie, ev->cr.req_ie_len,
723 ev->cr.resp_ie, ev->cr.resp_ie_len,
724 ev->cr.status,
725 ev->cr.status == WLAN_STATUS_SUCCESS,
726 NULL);
727 break;
728 case EVENT_ROAMED:
729 __cfg80211_roamed(wdev, ev->rm.bssid,
730 ev->rm.req_ie, ev->rm.req_ie_len,
731 ev->rm.resp_ie, ev->rm.resp_ie_len);
732 break;
733 case EVENT_DISCONNECTED:
734 __cfg80211_disconnected(wdev->netdev,
735 ev->dc.ie, ev->dc.ie_len,
736 ev->dc.reason, true);
737 break;
738 case EVENT_IBSS_JOINED:
739 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
740 break;
741 }
742 wdev_unlock(wdev);
743
744 kfree(ev);
745
746 spin_lock_irqsave(&wdev->event_lock, flags);
747 }
748 spin_unlock_irqrestore(&wdev->event_lock, flags);
749}
750
751void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
752{
753 struct wireless_dev *wdev;
754
755 ASSERT_RTNL();
756 ASSERT_RDEV_LOCK(rdev);
757
758 mutex_lock(&rdev->devlist_mtx);
759
760 list_for_each_entry(wdev, &rdev->netdev_list, list)
761 cfg80211_process_wdev_events(wdev);
762
763 mutex_unlock(&rdev->devlist_mtx);
764}
765
766int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
767 struct net_device *dev, enum nl80211_iftype ntype,
768 u32 *flags, struct vif_params *params)
769{
770 int err;
771 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
772
773 ASSERT_RDEV_LOCK(rdev);
774
775 /* don't support changing VLANs, you just re-create them */
776 if (otype == NL80211_IFTYPE_AP_VLAN)
777 return -EOPNOTSUPP;
778
779 if (!rdev->ops->change_virtual_intf ||
780 !(rdev->wiphy.interface_modes & (1 << ntype)))
781 return -EOPNOTSUPP;
782
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100783 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a2010-06-15 06:50:45 +0000784 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200785 (ntype == NL80211_IFTYPE_ADHOC ||
786 ntype == NL80211_IFTYPE_STATION ||
787 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100788 return -EBUSY;
789
Johannes Berg3d54d252009-08-21 14:51:05 +0200790 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +0100791 dev->ieee80211_ptr->use_4addr = false;
792
Johannes Berg3d54d252009-08-21 14:51:05 +0200793 switch (otype) {
794 case NL80211_IFTYPE_ADHOC:
795 cfg80211_leave_ibss(rdev, dev, false);
796 break;
797 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200798 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200799 cfg80211_disconnect(rdev, dev,
800 WLAN_REASON_DEAUTH_LEAVING, true);
801 break;
802 case NL80211_IFTYPE_MESH_POINT:
803 /* mesh should be handled? */
804 break;
805 default:
806 break;
807 }
808
809 cfg80211_process_rdev_events(rdev);
810 }
811
812 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
813 ntype, flags, params);
814
815 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
816
Johannes Berg9bc383d2009-11-19 11:55:19 +0100817 if (!err && params && params->use_4addr != -1)
818 dev->ieee80211_ptr->use_4addr = params->use_4addr;
819
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100820 if (!err) {
821 dev->priv_flags &= ~IFF_DONT_BRIDGE;
822 switch (ntype) {
823 case NL80211_IFTYPE_STATION:
824 if (dev->ieee80211_ptr->use_4addr)
825 break;
826 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200827 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100828 case NL80211_IFTYPE_ADHOC:
829 dev->priv_flags |= IFF_DONT_BRIDGE;
830 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200831 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100832 case NL80211_IFTYPE_AP:
833 case NL80211_IFTYPE_AP_VLAN:
834 case NL80211_IFTYPE_WDS:
835 case NL80211_IFTYPE_MESH_POINT:
836 /* bridging OK */
837 break;
838 case NL80211_IFTYPE_MONITOR:
839 /* monitor can't bridge anyway */
840 break;
841 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200842 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100843 /* not happening */
844 break;
845 }
846 }
847
Johannes Berg3d54d252009-08-21 14:51:05 +0200848 return err;
849}
John W. Linville254416a2009-12-09 16:43:52 -0500850
851u16 cfg80211_calculate_bitrate(struct rate_info *rate)
852{
853 int modulation, streams, bitrate;
854
855 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
856 return rate->legacy;
857
858 /* the formula below does only work for MCS values smaller than 32 */
859 if (rate->mcs >= 32)
860 return 0;
861
862 modulation = rate->mcs & 7;
863 streams = (rate->mcs >> 3) + 1;
864
865 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
866 13500000 : 6500000;
867
868 if (modulation < 4)
869 bitrate *= (modulation + 1);
870 else if (modulation == 4)
871 bitrate *= (modulation + 2);
872 else
873 bitrate *= (modulation + 3);
874
875 bitrate *= streams;
876
877 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
878 bitrate = (bitrate / 9) * 10;
879
880 /* do NOT round down here */
881 return (bitrate + 50000) / 100000;
882}