blob: 4ed065d8bb515ae2594b113c9be34daa58895197 [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
Bruno Randolf59eb21a2011-01-17 13:37:28 +090032int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010033{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090034 /* see 802.11 17.3.8.3.2 and Annex J
35 * there are overlapping channel numbers in 5GHz and 2GHz bands */
36 if (band == IEEE80211_BAND_5GHZ) {
37 if (chan >= 182 && chan <= 196)
38 return 4000 + chan * 5;
39 else
40 return 5000 + chan * 5;
41 } else { /* IEEE80211_BAND_2GHZ */
42 if (chan == 14)
43 return 2484;
44 else if (chan < 14)
45 return 2407 + chan * 5;
46 else
47 return 0; /* not supported */
48 }
Johannes Berg8318d782008-01-24 19:38:38 +010049}
50EXPORT_SYMBOL(ieee80211_channel_to_frequency);
51
52int ieee80211_frequency_to_channel(int freq)
53{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090054 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +010055 if (freq == 2484)
56 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090057 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +010058 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090059 else if (freq >= 4910 && freq <= 4980)
60 return (freq - 4000) / 5;
61 else
62 return (freq - 5000) / 5;
Johannes Berg8318d782008-01-24 19:38:38 +010063}
64EXPORT_SYMBOL(ieee80211_frequency_to_channel);
65
Johannes Berg6c507cd2008-03-26 14:14:55 +010066struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
67 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +010068{
69 enum ieee80211_band band;
70 struct ieee80211_supported_band *sband;
71 int i;
72
73 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
74 sband = wiphy->bands[band];
75
76 if (!sband)
77 continue;
78
79 for (i = 0; i < sband->n_channels; i++) {
80 if (sband->channels[i].center_freq == freq)
81 return &sband->channels[i];
82 }
83 }
84
85 return NULL;
86}
Johannes Berg6c507cd2008-03-26 14:14:55 +010087EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +010088
Johannes Berg8318d782008-01-24 19:38:38 +010089static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
90 enum ieee80211_band band)
91{
92 int i, want;
93
94 switch (band) {
95 case IEEE80211_BAND_5GHZ:
96 want = 3;
97 for (i = 0; i < sband->n_bitrates; i++) {
98 if (sband->bitrates[i].bitrate == 60 ||
99 sband->bitrates[i].bitrate == 120 ||
100 sband->bitrates[i].bitrate == 240) {
101 sband->bitrates[i].flags |=
102 IEEE80211_RATE_MANDATORY_A;
103 want--;
104 }
105 }
106 WARN_ON(want);
107 break;
108 case IEEE80211_BAND_2GHZ:
109 want = 7;
110 for (i = 0; i < sband->n_bitrates; i++) {
111 if (sband->bitrates[i].bitrate == 10) {
112 sband->bitrates[i].flags |=
113 IEEE80211_RATE_MANDATORY_B |
114 IEEE80211_RATE_MANDATORY_G;
115 want--;
116 }
117
118 if (sband->bitrates[i].bitrate == 20 ||
119 sband->bitrates[i].bitrate == 55 ||
120 sband->bitrates[i].bitrate == 110 ||
121 sband->bitrates[i].bitrate == 60 ||
122 sband->bitrates[i].bitrate == 120 ||
123 sband->bitrates[i].bitrate == 240) {
124 sband->bitrates[i].flags |=
125 IEEE80211_RATE_MANDATORY_G;
126 want--;
127 }
128
Johannes Bergaac09fb2008-01-30 17:36:10 +0100129 if (sband->bitrates[i].bitrate != 10 &&
130 sband->bitrates[i].bitrate != 20 &&
131 sband->bitrates[i].bitrate != 55 &&
132 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100133 sband->bitrates[i].flags |=
134 IEEE80211_RATE_ERP_G;
135 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100136 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100137 break;
138 case IEEE80211_NUM_BANDS:
139 WARN_ON(1);
140 break;
141 }
142}
143
144void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
145{
146 enum ieee80211_band band;
147
148 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
149 if (wiphy->bands[band])
150 set_mandatory_flags_band(wiphy->bands[band], band);
151}
Johannes Berg08645122009-05-11 13:54:58 +0200152
Johannes Bergfffd0932009-07-08 14:22:54 +0200153int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
154 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200155 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200156{
Johannes Bergfffd0932009-07-08 14:22:54 +0200157 int i;
158
Johannes Berg08645122009-05-11 13:54:58 +0200159 if (key_idx > 5)
160 return -EINVAL;
161
Johannes Berge31b8212010-10-05 19:39:30 +0200162 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
163 return -EINVAL;
164
165 if (pairwise && !mac_addr)
166 return -EINVAL;
167
Johannes Berg08645122009-05-11 13:54:58 +0200168 /*
169 * Disallow pairwise keys with non-zero index unless it's WEP
170 * (because current deployments use pairwise WEP keys with
171 * non-zero indizes but 802.11i clearly specifies to use zero)
172 */
Johannes Berge31b8212010-10-05 19:39:30 +0200173 if (pairwise && key_idx &&
Johannes Berg08645122009-05-11 13:54:58 +0200174 params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
175 params->cipher != WLAN_CIPHER_SUITE_WEP104)
176 return -EINVAL;
177
Johannes Berg08645122009-05-11 13:54:58 +0200178 switch (params->cipher) {
179 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200180 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200181 return -EINVAL;
182 break;
183 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200184 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200185 return -EINVAL;
186 break;
187 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200188 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200189 return -EINVAL;
190 break;
191 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200192 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200193 return -EINVAL;
194 break;
195 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200196 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200197 return -EINVAL;
198 break;
199 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300200 /*
201 * We don't know anything about this algorithm,
202 * allow using it -- but the driver must check
203 * all parameters! We still check below whether
204 * or not the driver supports this algorithm,
205 * of course.
206 */
207 break;
Johannes Berg08645122009-05-11 13:54:58 +0200208 }
209
Jouni Malinen9f26a952009-05-15 12:38:32 +0300210 if (params->seq) {
211 switch (params->cipher) {
212 case WLAN_CIPHER_SUITE_WEP40:
213 case WLAN_CIPHER_SUITE_WEP104:
214 /* These ciphers do not use key sequence */
215 return -EINVAL;
216 case WLAN_CIPHER_SUITE_TKIP:
217 case WLAN_CIPHER_SUITE_CCMP:
218 case WLAN_CIPHER_SUITE_AES_CMAC:
219 if (params->seq_len != 6)
220 return -EINVAL;
221 break;
222 }
223 }
224
Johannes Bergfffd0932009-07-08 14:22:54 +0200225 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
226 if (params->cipher == rdev->wiphy.cipher_suites[i])
227 break;
228 if (i == rdev->wiphy.n_cipher_suites)
229 return -EINVAL;
230
Johannes Berg08645122009-05-11 13:54:58 +0200231 return 0;
232}
Zhu Yie31a16d2009-05-21 21:47:03 +0800233
234/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
235/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
236const unsigned char rfc1042_header[] __aligned(2) =
237 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
238EXPORT_SYMBOL(rfc1042_header);
239
240/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
241const unsigned char bridge_tunnel_header[] __aligned(2) =
242 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
243EXPORT_SYMBOL(bridge_tunnel_header);
244
Johannes Berg633adf12010-08-12 14:49:58 +0200245unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800246{
247 unsigned int hdrlen = 24;
248
249 if (ieee80211_is_data(fc)) {
250 if (ieee80211_has_a4(fc))
251 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200252 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800253 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200254 if (ieee80211_has_order(fc))
255 hdrlen += IEEE80211_HT_CTL_LEN;
256 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800257 goto out;
258 }
259
260 if (ieee80211_is_ctl(fc)) {
261 /*
262 * ACK and CTS are 10 bytes, all others 16. To see how
263 * to get this condition consider
264 * subtype mask: 0b0000000011110000 (0x00F0)
265 * ACK subtype: 0b0000000011010000 (0x00D0)
266 * CTS subtype: 0b0000000011000000 (0x00C0)
267 * bits that matter: ^^^ (0x00E0)
268 * value of those: 0b0000000011000000 (0x00C0)
269 */
270 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
271 hdrlen = 10;
272 else
273 hdrlen = 16;
274 }
275out:
276 return hdrlen;
277}
278EXPORT_SYMBOL(ieee80211_hdrlen);
279
280unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
281{
282 const struct ieee80211_hdr *hdr =
283 (const struct ieee80211_hdr *)skb->data;
284 unsigned int hdrlen;
285
286 if (unlikely(skb->len < 10))
287 return 0;
288 hdrlen = ieee80211_hdrlen(hdr->frame_control);
289 if (unlikely(hdrlen > skb->len))
290 return 0;
291 return hdrlen;
292}
293EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
294
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400295static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800296{
297 int ae = meshhdr->flags & MESH_FLAGS_AE;
298 /* 7.1.3.5a.2 */
299 switch (ae) {
300 case 0:
301 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700302 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800303 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700304 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800305 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700306 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800307 return 24;
308 default:
309 return 6;
310 }
311}
312
Zhu Yieaf85ca2009-12-01 10:18:37 +0800313int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800314 enum nl80211_iftype iftype)
315{
316 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
317 u16 hdrlen, ethertype;
318 u8 *payload;
319 u8 dst[ETH_ALEN];
320 u8 src[ETH_ALEN] __aligned(2);
321
322 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
323 return -1;
324
325 hdrlen = ieee80211_hdrlen(hdr->frame_control);
326
327 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
328 * header
329 * IEEE 802.11 address fields:
330 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
331 * 0 0 DA SA BSSID n/a
332 * 0 1 DA BSSID SA n/a
333 * 1 0 BSSID SA DA n/a
334 * 1 1 RA TA DA SA
335 */
336 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
337 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
338
339 switch (hdr->frame_control &
340 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
341 case cpu_to_le16(IEEE80211_FCTL_TODS):
342 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200343 iftype != NL80211_IFTYPE_AP_VLAN &&
344 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800345 return -1;
346 break;
347 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
348 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100349 iftype != NL80211_IFTYPE_MESH_POINT &&
350 iftype != NL80211_IFTYPE_AP_VLAN &&
351 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800352 return -1;
353 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;
Zhu Yie31a16d2009-05-21 21:47:03 +0800359 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800360 skb_copy_bits(skb, hdrlen +
361 offsetof(struct ieee80211s_hdr, eaddr1),
362 dst, ETH_ALEN);
363 skb_copy_bits(skb, hdrlen +
364 offsetof(struct ieee80211s_hdr, eaddr2),
365 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800366 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800367 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800368 }
369 break;
370 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700371 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200372 iftype != NL80211_IFTYPE_P2P_CLIENT &&
373 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800374 (is_multicast_ether_addr(dst) &&
375 !compare_ether_addr(src, addr)))
376 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700377 if (iftype == NL80211_IFTYPE_MESH_POINT) {
378 struct ieee80211s_hdr *meshdr =
379 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800380 /* make sure meshdr->flags is on the linear part */
381 if (!pskb_may_pull(skb, hdrlen + 1))
382 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700383 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800384 skb_copy_bits(skb, hdrlen +
385 offsetof(struct ieee80211s_hdr, eaddr1),
386 src, ETH_ALEN);
387 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700388 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800389 break;
390 case cpu_to_le16(0):
391 if (iftype != NL80211_IFTYPE_ADHOC)
392 return -1;
393 break;
394 }
395
Zhu Yie3cf8b32010-03-29 17:35:07 +0800396 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800397 return -1;
398
399 payload = skb->data + hdrlen;
400 ethertype = (payload[6] << 8) | payload[7];
401
402 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
403 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
404 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
405 /* remove RFC1042 or Bridge-Tunnel encapsulation and
406 * replace EtherType */
407 skb_pull(skb, hdrlen + 6);
408 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
409 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
410 } else {
411 struct ethhdr *ehdr;
412 __be16 len;
413
414 skb_pull(skb, hdrlen);
415 len = htons(skb->len);
416 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
417 memcpy(ehdr->h_dest, dst, ETH_ALEN);
418 memcpy(ehdr->h_source, src, ETH_ALEN);
419 ehdr->h_proto = len;
420 }
421 return 0;
422}
423EXPORT_SYMBOL(ieee80211_data_to_8023);
424
Zhu Yieaf85ca2009-12-01 10:18:37 +0800425int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800426 enum nl80211_iftype iftype, u8 *bssid, bool qos)
427{
428 struct ieee80211_hdr hdr;
429 u16 hdrlen, ethertype;
430 __le16 fc;
431 const u8 *encaps_data;
432 int encaps_len, skip_header_bytes;
433 int nh_pos, h_pos;
434 int head_need;
435
436 if (unlikely(skb->len < ETH_HLEN))
437 return -EINVAL;
438
439 nh_pos = skb_network_header(skb) - skb->data;
440 h_pos = skb_transport_header(skb) - skb->data;
441
442 /* convert Ethernet header to proper 802.11 header (based on
443 * operation mode) */
444 ethertype = (skb->data[12] << 8) | skb->data[13];
445 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
446
447 switch (iftype) {
448 case NL80211_IFTYPE_AP:
449 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200450 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800451 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
452 /* DA BSSID SA */
453 memcpy(hdr.addr1, skb->data, ETH_ALEN);
454 memcpy(hdr.addr2, addr, ETH_ALEN);
455 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
456 hdrlen = 24;
457 break;
458 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200459 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800460 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
461 /* BSSID SA DA */
462 memcpy(hdr.addr1, bssid, ETH_ALEN);
463 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
464 memcpy(hdr.addr3, skb->data, ETH_ALEN);
465 hdrlen = 24;
466 break;
467 case NL80211_IFTYPE_ADHOC:
468 /* DA SA BSSID */
469 memcpy(hdr.addr1, skb->data, ETH_ALEN);
470 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
471 memcpy(hdr.addr3, bssid, ETH_ALEN);
472 hdrlen = 24;
473 break;
474 default:
475 return -EOPNOTSUPP;
476 }
477
478 if (qos) {
479 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
480 hdrlen += 2;
481 }
482
483 hdr.frame_control = fc;
484 hdr.duration_id = 0;
485 hdr.seq_ctrl = 0;
486
487 skip_header_bytes = ETH_HLEN;
488 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
489 encaps_data = bridge_tunnel_header;
490 encaps_len = sizeof(bridge_tunnel_header);
491 skip_header_bytes -= 2;
492 } else if (ethertype > 0x600) {
493 encaps_data = rfc1042_header;
494 encaps_len = sizeof(rfc1042_header);
495 skip_header_bytes -= 2;
496 } else {
497 encaps_data = NULL;
498 encaps_len = 0;
499 }
500
501 skb_pull(skb, skip_header_bytes);
502 nh_pos -= skip_header_bytes;
503 h_pos -= skip_header_bytes;
504
505 head_need = hdrlen + encaps_len - skb_headroom(skb);
506
507 if (head_need > 0 || skb_cloned(skb)) {
508 head_need = max(head_need, 0);
509 if (head_need)
510 skb_orphan(skb);
511
512 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800513 pr_err("failed to reallocate Tx buffer\n");
Zhu Yie31a16d2009-05-21 21:47:03 +0800514 return -ENOMEM;
515 }
516 skb->truesize += head_need;
517 }
518
519 if (encaps_data) {
520 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
521 nh_pos += encaps_len;
522 h_pos += encaps_len;
523 }
524
525 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
526
527 nh_pos += hdrlen;
528 h_pos += hdrlen;
529
530 /* Update skb pointers to various headers since this modified frame
531 * is going to go through Linux networking code that may potentially
532 * need things like pointer to IP header. */
533 skb_set_mac_header(skb, 0);
534 skb_set_network_header(skb, nh_pos);
535 skb_set_transport_header(skb, h_pos);
536
537 return 0;
538}
539EXPORT_SYMBOL(ieee80211_data_from_8023);
540
Zhu Yieaf85ca2009-12-01 10:18:37 +0800541
542void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
543 const u8 *addr, enum nl80211_iftype iftype,
544 const unsigned int extra_headroom)
545{
546 struct sk_buff *frame = NULL;
547 u16 ethertype;
548 u8 *payload;
549 const struct ethhdr *eth;
550 int remaining, err;
551 u8 dst[ETH_ALEN], src[ETH_ALEN];
552
553 err = ieee80211_data_to_8023(skb, addr, iftype);
554 if (err)
555 goto out;
556
557 /* skip the wrapping header */
558 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
559 if (!eth)
560 goto out;
561
562 while (skb != frame) {
563 u8 padding;
564 __be16 len = eth->h_proto;
565 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
566
567 remaining = skb->len;
568 memcpy(dst, eth->h_dest, ETH_ALEN);
569 memcpy(src, eth->h_source, ETH_ALEN);
570
571 padding = (4 - subframe_len) & 0x3;
572 /* the last MSDU has no padding */
573 if (subframe_len > remaining)
574 goto purge;
575
576 skb_pull(skb, sizeof(struct ethhdr));
577 /* reuse skb for the last subframe */
578 if (remaining <= subframe_len + padding)
579 frame = skb;
580 else {
581 unsigned int hlen = ALIGN(extra_headroom, 4);
582 /*
583 * Allocate and reserve two bytes more for payload
584 * alignment since sizeof(struct ethhdr) is 14.
585 */
586 frame = dev_alloc_skb(hlen + subframe_len + 2);
587 if (!frame)
588 goto purge;
589
590 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
591 memcpy(skb_put(frame, ntohs(len)), skb->data,
592 ntohs(len));
593
594 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
595 padding);
596 if (!eth) {
597 dev_kfree_skb(frame);
598 goto purge;
599 }
600 }
601
602 skb_reset_network_header(frame);
603 frame->dev = skb->dev;
604 frame->priority = skb->priority;
605
606 payload = frame->data;
607 ethertype = (payload[6] << 8) | payload[7];
608
609 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
610 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
611 compare_ether_addr(payload,
612 bridge_tunnel_header) == 0)) {
613 /* remove RFC1042 or Bridge-Tunnel
614 * encapsulation and replace EtherType */
615 skb_pull(frame, 6);
616 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
617 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
618 } else {
619 memcpy(skb_push(frame, sizeof(__be16)), &len,
620 sizeof(__be16));
621 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
622 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
623 }
624 __skb_queue_tail(list, frame);
625 }
626
627 return;
628
629 purge:
630 __skb_queue_purge(list);
631 out:
632 dev_kfree_skb(skb);
633}
634EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
635
Zhu Yie31a16d2009-05-21 21:47:03 +0800636/* Given a data frame determine the 802.1p/1d tag to use. */
637unsigned int cfg80211_classify8021d(struct sk_buff *skb)
638{
639 unsigned int dscp;
640
641 /* skb->priority values from 256->263 are magic values to
642 * directly indicate a specific 802.1d priority. This is used
643 * to allow 802.1d priority to be passed directly in from VLAN
644 * tags, etc.
645 */
646 if (skb->priority >= 256 && skb->priority <= 263)
647 return skb->priority - 256;
648
649 switch (skb->protocol) {
650 case htons(ETH_P_IP):
651 dscp = ip_hdr(skb)->tos & 0xfc;
652 break;
653 default:
654 return 0;
655 }
656
657 return dscp >> 5;
658}
659EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200660
661const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
662{
663 u8 *end, *pos;
664
665 pos = bss->information_elements;
666 if (pos == NULL)
667 return NULL;
668 end = pos + bss->len_information_elements;
669
670 while (pos + 1 < end) {
671 if (pos + 2 + pos[1] > end)
672 break;
673 if (pos[0] == ie)
674 return pos;
675 pos += 2 + pos[1];
676 }
677
678 return NULL;
679}
680EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200681
682void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
683{
684 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
685 struct net_device *dev = wdev->netdev;
686 int i;
687
688 if (!wdev->connect_keys)
689 return;
690
691 for (i = 0; i < 6; i++) {
692 if (!wdev->connect_keys->params[i].cipher)
693 continue;
Johannes Berge31b8212010-10-05 19:39:30 +0200694 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800695 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800696 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800697 continue;
698 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200699 if (wdev->connect_keys->def == i)
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100700 if (rdev->ops->set_default_key(wdev->wiphy, dev,
701 i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800702 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800703 continue;
704 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200705 if (wdev->connect_keys->defmgmt == i)
706 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800707 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200708 }
709
710 kfree(wdev->connect_keys);
711 wdev->connect_keys = NULL;
712}
Johannes Berg3d54d252009-08-21 14:51:05 +0200713
714static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
715{
716 struct cfg80211_event *ev;
717 unsigned long flags;
718 const u8 *bssid = NULL;
719
720 spin_lock_irqsave(&wdev->event_lock, flags);
721 while (!list_empty(&wdev->event_list)) {
722 ev = list_first_entry(&wdev->event_list,
723 struct cfg80211_event, list);
724 list_del(&ev->list);
725 spin_unlock_irqrestore(&wdev->event_lock, flags);
726
727 wdev_lock(wdev);
728 switch (ev->type) {
729 case EVENT_CONNECT_RESULT:
730 if (!is_zero_ether_addr(ev->cr.bssid))
731 bssid = ev->cr.bssid;
732 __cfg80211_connect_result(
733 wdev->netdev, bssid,
734 ev->cr.req_ie, ev->cr.req_ie_len,
735 ev->cr.resp_ie, ev->cr.resp_ie_len,
736 ev->cr.status,
737 ev->cr.status == WLAN_STATUS_SUCCESS,
738 NULL);
739 break;
740 case EVENT_ROAMED:
741 __cfg80211_roamed(wdev, ev->rm.bssid,
742 ev->rm.req_ie, ev->rm.req_ie_len,
743 ev->rm.resp_ie, ev->rm.resp_ie_len);
744 break;
745 case EVENT_DISCONNECTED:
746 __cfg80211_disconnected(wdev->netdev,
747 ev->dc.ie, ev->dc.ie_len,
748 ev->dc.reason, true);
749 break;
750 case EVENT_IBSS_JOINED:
751 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
752 break;
753 }
754 wdev_unlock(wdev);
755
756 kfree(ev);
757
758 spin_lock_irqsave(&wdev->event_lock, flags);
759 }
760 spin_unlock_irqrestore(&wdev->event_lock, flags);
761}
762
763void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
764{
765 struct wireless_dev *wdev;
766
767 ASSERT_RTNL();
768 ASSERT_RDEV_LOCK(rdev);
769
770 mutex_lock(&rdev->devlist_mtx);
771
772 list_for_each_entry(wdev, &rdev->netdev_list, list)
773 cfg80211_process_wdev_events(wdev);
774
775 mutex_unlock(&rdev->devlist_mtx);
776}
777
778int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
779 struct net_device *dev, enum nl80211_iftype ntype,
780 u32 *flags, struct vif_params *params)
781{
782 int err;
783 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
784
785 ASSERT_RDEV_LOCK(rdev);
786
787 /* don't support changing VLANs, you just re-create them */
788 if (otype == NL80211_IFTYPE_AP_VLAN)
789 return -EOPNOTSUPP;
790
791 if (!rdev->ops->change_virtual_intf ||
792 !(rdev->wiphy.interface_modes & (1 << ntype)))
793 return -EOPNOTSUPP;
794
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100795 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000796 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200797 (ntype == NL80211_IFTYPE_ADHOC ||
798 ntype == NL80211_IFTYPE_STATION ||
799 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100800 return -EBUSY;
801
Johannes Berg3d54d252009-08-21 14:51:05 +0200802 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +0100803 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100804 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100805
Johannes Berg3d54d252009-08-21 14:51:05 +0200806 switch (otype) {
807 case NL80211_IFTYPE_ADHOC:
808 cfg80211_leave_ibss(rdev, dev, false);
809 break;
810 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200811 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200812 cfg80211_disconnect(rdev, dev,
813 WLAN_REASON_DEAUTH_LEAVING, true);
814 break;
815 case NL80211_IFTYPE_MESH_POINT:
816 /* mesh should be handled? */
817 break;
818 default:
819 break;
820 }
821
822 cfg80211_process_rdev_events(rdev);
823 }
824
825 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
826 ntype, flags, params);
827
828 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
829
Johannes Berg9bc383d2009-11-19 11:55:19 +0100830 if (!err && params && params->use_4addr != -1)
831 dev->ieee80211_ptr->use_4addr = params->use_4addr;
832
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100833 if (!err) {
834 dev->priv_flags &= ~IFF_DONT_BRIDGE;
835 switch (ntype) {
836 case NL80211_IFTYPE_STATION:
837 if (dev->ieee80211_ptr->use_4addr)
838 break;
839 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200840 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100841 case NL80211_IFTYPE_ADHOC:
842 dev->priv_flags |= IFF_DONT_BRIDGE;
843 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200844 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100845 case NL80211_IFTYPE_AP:
846 case NL80211_IFTYPE_AP_VLAN:
847 case NL80211_IFTYPE_WDS:
848 case NL80211_IFTYPE_MESH_POINT:
849 /* bridging OK */
850 break;
851 case NL80211_IFTYPE_MONITOR:
852 /* monitor can't bridge anyway */
853 break;
854 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200855 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100856 /* not happening */
857 break;
858 }
859 }
860
Johannes Berg3d54d252009-08-21 14:51:05 +0200861 return err;
862}
John W. Linville254416a2009-12-09 16:43:52 -0500863
864u16 cfg80211_calculate_bitrate(struct rate_info *rate)
865{
866 int modulation, streams, bitrate;
867
868 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
869 return rate->legacy;
870
871 /* the formula below does only work for MCS values smaller than 32 */
872 if (rate->mcs >= 32)
873 return 0;
874
875 modulation = rate->mcs & 7;
876 streams = (rate->mcs >> 3) + 1;
877
878 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
879 13500000 : 6500000;
880
881 if (modulation < 4)
882 bitrate *= (modulation + 1);
883 else if (modulation == 4)
884 bitrate *= (modulation + 2);
885 else
886 bitrate *= (modulation + 3);
887
888 bitrate *= streams;
889
890 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
891 bitrate = (bitrate / 9) * 10;
892
893 /* do NOT round down here */
894 return (bitrate + 50000) / 100000;
895}