blob: 6a750bc6bcfe3cf3fddc82202a7d7ddeabf28720 [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
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200170 * or a vendor specific cipher (because current deployments use
171 * pairwise WEP keys with non-zero indices and for vendor specific
172 * ciphers this should be validated in the driver or hardware level
173 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200174 */
Johannes Berge31b8212010-10-05 19:39:30 +0200175 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200176 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
177 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
178 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200179 return -EINVAL;
180
Johannes Berg08645122009-05-11 13:54:58 +0200181 switch (params->cipher) {
182 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200183 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200184 return -EINVAL;
185 break;
186 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200187 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200188 return -EINVAL;
189 break;
190 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200191 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200192 return -EINVAL;
193 break;
194 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200195 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200196 return -EINVAL;
197 break;
198 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200199 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200200 return -EINVAL;
201 break;
202 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300203 /*
204 * We don't know anything about this algorithm,
205 * allow using it -- but the driver must check
206 * all parameters! We still check below whether
207 * or not the driver supports this algorithm,
208 * of course.
209 */
210 break;
Johannes Berg08645122009-05-11 13:54:58 +0200211 }
212
Jouni Malinen9f26a952009-05-15 12:38:32 +0300213 if (params->seq) {
214 switch (params->cipher) {
215 case WLAN_CIPHER_SUITE_WEP40:
216 case WLAN_CIPHER_SUITE_WEP104:
217 /* These ciphers do not use key sequence */
218 return -EINVAL;
219 case WLAN_CIPHER_SUITE_TKIP:
220 case WLAN_CIPHER_SUITE_CCMP:
221 case WLAN_CIPHER_SUITE_AES_CMAC:
222 if (params->seq_len != 6)
223 return -EINVAL;
224 break;
225 }
226 }
227
Johannes Bergfffd0932009-07-08 14:22:54 +0200228 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++)
229 if (params->cipher == rdev->wiphy.cipher_suites[i])
230 break;
231 if (i == rdev->wiphy.n_cipher_suites)
232 return -EINVAL;
233
Johannes Berg08645122009-05-11 13:54:58 +0200234 return 0;
235}
Zhu Yie31a16d2009-05-21 21:47:03 +0800236
237/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
238/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
239const unsigned char rfc1042_header[] __aligned(2) =
240 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
241EXPORT_SYMBOL(rfc1042_header);
242
243/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
244const unsigned char bridge_tunnel_header[] __aligned(2) =
245 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
246EXPORT_SYMBOL(bridge_tunnel_header);
247
Johannes Berg633adf12010-08-12 14:49:58 +0200248unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800249{
250 unsigned int hdrlen = 24;
251
252 if (ieee80211_is_data(fc)) {
253 if (ieee80211_has_a4(fc))
254 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200255 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800256 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200257 if (ieee80211_has_order(fc))
258 hdrlen += IEEE80211_HT_CTL_LEN;
259 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800260 goto out;
261 }
262
263 if (ieee80211_is_ctl(fc)) {
264 /*
265 * ACK and CTS are 10 bytes, all others 16. To see how
266 * to get this condition consider
267 * subtype mask: 0b0000000011110000 (0x00F0)
268 * ACK subtype: 0b0000000011010000 (0x00D0)
269 * CTS subtype: 0b0000000011000000 (0x00C0)
270 * bits that matter: ^^^ (0x00E0)
271 * value of those: 0b0000000011000000 (0x00C0)
272 */
273 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
274 hdrlen = 10;
275 else
276 hdrlen = 16;
277 }
278out:
279 return hdrlen;
280}
281EXPORT_SYMBOL(ieee80211_hdrlen);
282
283unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
284{
285 const struct ieee80211_hdr *hdr =
286 (const struct ieee80211_hdr *)skb->data;
287 unsigned int hdrlen;
288
289 if (unlikely(skb->len < 10))
290 return 0;
291 hdrlen = ieee80211_hdrlen(hdr->frame_control);
292 if (unlikely(hdrlen > skb->len))
293 return 0;
294 return hdrlen;
295}
296EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
297
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400298static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800299{
300 int ae = meshhdr->flags & MESH_FLAGS_AE;
301 /* 7.1.3.5a.2 */
302 switch (ae) {
303 case 0:
304 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700305 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800306 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700307 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800308 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700309 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800310 return 24;
311 default:
312 return 6;
313 }
314}
315
Zhu Yieaf85ca2009-12-01 10:18:37 +0800316int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800317 enum nl80211_iftype iftype)
318{
319 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
320 u16 hdrlen, ethertype;
321 u8 *payload;
322 u8 dst[ETH_ALEN];
323 u8 src[ETH_ALEN] __aligned(2);
324
325 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
326 return -1;
327
328 hdrlen = ieee80211_hdrlen(hdr->frame_control);
329
330 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
331 * header
332 * IEEE 802.11 address fields:
333 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
334 * 0 0 DA SA BSSID n/a
335 * 0 1 DA BSSID SA n/a
336 * 1 0 BSSID SA DA n/a
337 * 1 1 RA TA DA SA
338 */
339 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
340 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
341
342 switch (hdr->frame_control &
343 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
344 case cpu_to_le16(IEEE80211_FCTL_TODS):
345 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200346 iftype != NL80211_IFTYPE_AP_VLAN &&
347 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800348 return -1;
349 break;
350 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
351 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543e2009-11-10 20:10:05 +0100352 iftype != NL80211_IFTYPE_MESH_POINT &&
353 iftype != NL80211_IFTYPE_AP_VLAN &&
354 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800355 return -1;
356 if (iftype == NL80211_IFTYPE_MESH_POINT) {
357 struct ieee80211s_hdr *meshdr =
358 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800359 /* make sure meshdr->flags is on the linear part */
360 if (!pskb_may_pull(skb, hdrlen + 1))
361 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800362 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800363 skb_copy_bits(skb, hdrlen +
364 offsetof(struct ieee80211s_hdr, eaddr1),
365 dst, ETH_ALEN);
366 skb_copy_bits(skb, hdrlen +
367 offsetof(struct ieee80211s_hdr, eaddr2),
368 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800369 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800370 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800371 }
372 break;
373 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700374 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200375 iftype != NL80211_IFTYPE_P2P_CLIENT &&
376 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800377 (is_multicast_ether_addr(dst) &&
378 !compare_ether_addr(src, addr)))
379 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700380 if (iftype == NL80211_IFTYPE_MESH_POINT) {
381 struct ieee80211s_hdr *meshdr =
382 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800383 /* make sure meshdr->flags is on the linear part */
384 if (!pskb_may_pull(skb, hdrlen + 1))
385 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700386 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800387 skb_copy_bits(skb, hdrlen +
388 offsetof(struct ieee80211s_hdr, eaddr1),
389 src, ETH_ALEN);
390 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700391 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800392 break;
393 case cpu_to_le16(0):
394 if (iftype != NL80211_IFTYPE_ADHOC)
395 return -1;
396 break;
397 }
398
Zhu Yie3cf8b32010-03-29 17:35:07 +0800399 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800400 return -1;
401
402 payload = skb->data + hdrlen;
403 ethertype = (payload[6] << 8) | payload[7];
404
405 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
406 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
407 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
408 /* remove RFC1042 or Bridge-Tunnel encapsulation and
409 * replace EtherType */
410 skb_pull(skb, hdrlen + 6);
411 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
412 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
413 } else {
414 struct ethhdr *ehdr;
415 __be16 len;
416
417 skb_pull(skb, hdrlen);
418 len = htons(skb->len);
419 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
420 memcpy(ehdr->h_dest, dst, ETH_ALEN);
421 memcpy(ehdr->h_source, src, ETH_ALEN);
422 ehdr->h_proto = len;
423 }
424 return 0;
425}
426EXPORT_SYMBOL(ieee80211_data_to_8023);
427
Zhu Yieaf85ca2009-12-01 10:18:37 +0800428int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800429 enum nl80211_iftype iftype, u8 *bssid, bool qos)
430{
431 struct ieee80211_hdr hdr;
432 u16 hdrlen, ethertype;
433 __le16 fc;
434 const u8 *encaps_data;
435 int encaps_len, skip_header_bytes;
436 int nh_pos, h_pos;
437 int head_need;
438
439 if (unlikely(skb->len < ETH_HLEN))
440 return -EINVAL;
441
442 nh_pos = skb_network_header(skb) - skb->data;
443 h_pos = skb_transport_header(skb) - skb->data;
444
445 /* convert Ethernet header to proper 802.11 header (based on
446 * operation mode) */
447 ethertype = (skb->data[12] << 8) | skb->data[13];
448 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
449
450 switch (iftype) {
451 case NL80211_IFTYPE_AP:
452 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200453 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800454 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
455 /* DA BSSID SA */
456 memcpy(hdr.addr1, skb->data, ETH_ALEN);
457 memcpy(hdr.addr2, addr, ETH_ALEN);
458 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
459 hdrlen = 24;
460 break;
461 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200462 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800463 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
464 /* BSSID SA DA */
465 memcpy(hdr.addr1, bssid, ETH_ALEN);
466 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
467 memcpy(hdr.addr3, skb->data, ETH_ALEN);
468 hdrlen = 24;
469 break;
470 case NL80211_IFTYPE_ADHOC:
471 /* DA SA BSSID */
472 memcpy(hdr.addr1, skb->data, ETH_ALEN);
473 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
474 memcpy(hdr.addr3, bssid, ETH_ALEN);
475 hdrlen = 24;
476 break;
477 default:
478 return -EOPNOTSUPP;
479 }
480
481 if (qos) {
482 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
483 hdrlen += 2;
484 }
485
486 hdr.frame_control = fc;
487 hdr.duration_id = 0;
488 hdr.seq_ctrl = 0;
489
490 skip_header_bytes = ETH_HLEN;
491 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
492 encaps_data = bridge_tunnel_header;
493 encaps_len = sizeof(bridge_tunnel_header);
494 skip_header_bytes -= 2;
495 } else if (ethertype > 0x600) {
496 encaps_data = rfc1042_header;
497 encaps_len = sizeof(rfc1042_header);
498 skip_header_bytes -= 2;
499 } else {
500 encaps_data = NULL;
501 encaps_len = 0;
502 }
503
504 skb_pull(skb, skip_header_bytes);
505 nh_pos -= skip_header_bytes;
506 h_pos -= skip_header_bytes;
507
508 head_need = hdrlen + encaps_len - skb_headroom(skb);
509
510 if (head_need > 0 || skb_cloned(skb)) {
511 head_need = max(head_need, 0);
512 if (head_need)
513 skb_orphan(skb);
514
515 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800516 pr_err("failed to reallocate Tx buffer\n");
Zhu Yie31a16d2009-05-21 21:47:03 +0800517 return -ENOMEM;
518 }
519 skb->truesize += head_need;
520 }
521
522 if (encaps_data) {
523 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
524 nh_pos += encaps_len;
525 h_pos += encaps_len;
526 }
527
528 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
529
530 nh_pos += hdrlen;
531 h_pos += hdrlen;
532
533 /* Update skb pointers to various headers since this modified frame
534 * is going to go through Linux networking code that may potentially
535 * need things like pointer to IP header. */
536 skb_set_mac_header(skb, 0);
537 skb_set_network_header(skb, nh_pos);
538 skb_set_transport_header(skb, h_pos);
539
540 return 0;
541}
542EXPORT_SYMBOL(ieee80211_data_from_8023);
543
Zhu Yieaf85ca2009-12-01 10:18:37 +0800544
545void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
546 const u8 *addr, enum nl80211_iftype iftype,
547 const unsigned int extra_headroom)
548{
549 struct sk_buff *frame = NULL;
550 u16 ethertype;
551 u8 *payload;
552 const struct ethhdr *eth;
553 int remaining, err;
554 u8 dst[ETH_ALEN], src[ETH_ALEN];
555
556 err = ieee80211_data_to_8023(skb, addr, iftype);
557 if (err)
558 goto out;
559
560 /* skip the wrapping header */
561 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
562 if (!eth)
563 goto out;
564
565 while (skb != frame) {
566 u8 padding;
567 __be16 len = eth->h_proto;
568 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
569
570 remaining = skb->len;
571 memcpy(dst, eth->h_dest, ETH_ALEN);
572 memcpy(src, eth->h_source, ETH_ALEN);
573
574 padding = (4 - subframe_len) & 0x3;
575 /* the last MSDU has no padding */
576 if (subframe_len > remaining)
577 goto purge;
578
579 skb_pull(skb, sizeof(struct ethhdr));
580 /* reuse skb for the last subframe */
581 if (remaining <= subframe_len + padding)
582 frame = skb;
583 else {
584 unsigned int hlen = ALIGN(extra_headroom, 4);
585 /*
586 * Allocate and reserve two bytes more for payload
587 * alignment since sizeof(struct ethhdr) is 14.
588 */
589 frame = dev_alloc_skb(hlen + subframe_len + 2);
590 if (!frame)
591 goto purge;
592
593 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
594 memcpy(skb_put(frame, ntohs(len)), skb->data,
595 ntohs(len));
596
597 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
598 padding);
599 if (!eth) {
600 dev_kfree_skb(frame);
601 goto purge;
602 }
603 }
604
605 skb_reset_network_header(frame);
606 frame->dev = skb->dev;
607 frame->priority = skb->priority;
608
609 payload = frame->data;
610 ethertype = (payload[6] << 8) | payload[7];
611
612 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
613 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
614 compare_ether_addr(payload,
615 bridge_tunnel_header) == 0)) {
616 /* remove RFC1042 or Bridge-Tunnel
617 * encapsulation and replace EtherType */
618 skb_pull(frame, 6);
619 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
620 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
621 } else {
622 memcpy(skb_push(frame, sizeof(__be16)), &len,
623 sizeof(__be16));
624 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
625 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
626 }
627 __skb_queue_tail(list, frame);
628 }
629
630 return;
631
632 purge:
633 __skb_queue_purge(list);
634 out:
635 dev_kfree_skb(skb);
636}
637EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
638
Zhu Yie31a16d2009-05-21 21:47:03 +0800639/* Given a data frame determine the 802.1p/1d tag to use. */
640unsigned int cfg80211_classify8021d(struct sk_buff *skb)
641{
642 unsigned int dscp;
643
644 /* skb->priority values from 256->263 are magic values to
645 * directly indicate a specific 802.1d priority. This is used
646 * to allow 802.1d priority to be passed directly in from VLAN
647 * tags, etc.
648 */
649 if (skb->priority >= 256 && skb->priority <= 263)
650 return skb->priority - 256;
651
652 switch (skb->protocol) {
653 case htons(ETH_P_IP):
654 dscp = ip_hdr(skb)->tos & 0xfc;
655 break;
656 default:
657 return 0;
658 }
659
660 return dscp >> 5;
661}
662EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200663
664const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
665{
666 u8 *end, *pos;
667
668 pos = bss->information_elements;
669 if (pos == NULL)
670 return NULL;
671 end = pos + bss->len_information_elements;
672
673 while (pos + 1 < end) {
674 if (pos + 2 + pos[1] > end)
675 break;
676 if (pos[0] == ie)
677 return pos;
678 pos += 2 + pos[1];
679 }
680
681 return NULL;
682}
683EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200684
685void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
686{
687 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
688 struct net_device *dev = wdev->netdev;
689 int i;
690
691 if (!wdev->connect_keys)
692 return;
693
694 for (i = 0; i < 6; i++) {
695 if (!wdev->connect_keys->params[i].cipher)
696 continue;
Johannes Berge31b8212010-10-05 19:39:30 +0200697 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800698 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800699 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800700 continue;
701 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200702 if (wdev->connect_keys->def == i)
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100703 if (rdev->ops->set_default_key(wdev->wiphy, dev,
704 i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800705 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800706 continue;
707 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200708 if (wdev->connect_keys->defmgmt == i)
709 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800710 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200711 }
712
713 kfree(wdev->connect_keys);
714 wdev->connect_keys = NULL;
715}
Johannes Berg3d54d252009-08-21 14:51:05 +0200716
717static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
718{
719 struct cfg80211_event *ev;
720 unsigned long flags;
721 const u8 *bssid = NULL;
722
723 spin_lock_irqsave(&wdev->event_lock, flags);
724 while (!list_empty(&wdev->event_list)) {
725 ev = list_first_entry(&wdev->event_list,
726 struct cfg80211_event, list);
727 list_del(&ev->list);
728 spin_unlock_irqrestore(&wdev->event_lock, flags);
729
730 wdev_lock(wdev);
731 switch (ev->type) {
732 case EVENT_CONNECT_RESULT:
733 if (!is_zero_ether_addr(ev->cr.bssid))
734 bssid = ev->cr.bssid;
735 __cfg80211_connect_result(
736 wdev->netdev, bssid,
737 ev->cr.req_ie, ev->cr.req_ie_len,
738 ev->cr.resp_ie, ev->cr.resp_ie_len,
739 ev->cr.status,
740 ev->cr.status == WLAN_STATUS_SUCCESS,
741 NULL);
742 break;
743 case EVENT_ROAMED:
744 __cfg80211_roamed(wdev, ev->rm.bssid,
745 ev->rm.req_ie, ev->rm.req_ie_len,
746 ev->rm.resp_ie, ev->rm.resp_ie_len);
747 break;
748 case EVENT_DISCONNECTED:
749 __cfg80211_disconnected(wdev->netdev,
750 ev->dc.ie, ev->dc.ie_len,
751 ev->dc.reason, true);
752 break;
753 case EVENT_IBSS_JOINED:
754 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
755 break;
756 }
757 wdev_unlock(wdev);
758
759 kfree(ev);
760
761 spin_lock_irqsave(&wdev->event_lock, flags);
762 }
763 spin_unlock_irqrestore(&wdev->event_lock, flags);
764}
765
766void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
767{
768 struct wireless_dev *wdev;
769
770 ASSERT_RTNL();
771 ASSERT_RDEV_LOCK(rdev);
772
773 mutex_lock(&rdev->devlist_mtx);
774
775 list_for_each_entry(wdev, &rdev->netdev_list, list)
776 cfg80211_process_wdev_events(wdev);
777
778 mutex_unlock(&rdev->devlist_mtx);
779}
780
781int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
782 struct net_device *dev, enum nl80211_iftype ntype,
783 u32 *flags, struct vif_params *params)
784{
785 int err;
786 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
787
788 ASSERT_RDEV_LOCK(rdev);
789
790 /* don't support changing VLANs, you just re-create them */
791 if (otype == NL80211_IFTYPE_AP_VLAN)
792 return -EOPNOTSUPP;
793
794 if (!rdev->ops->change_virtual_intf ||
795 !(rdev->wiphy.interface_modes & (1 << ntype)))
796 return -EOPNOTSUPP;
797
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100798 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a2010-06-15 06:50:45 +0000799 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200800 (ntype == NL80211_IFTYPE_ADHOC ||
801 ntype == NL80211_IFTYPE_STATION ||
802 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100803 return -EBUSY;
804
Johannes Berg3d54d252009-08-21 14:51:05 +0200805 if (ntype != otype) {
Johannes Berg9bc383d2009-11-19 11:55:19 +0100806 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100807 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100808
Johannes Berg3d54d252009-08-21 14:51:05 +0200809 switch (otype) {
810 case NL80211_IFTYPE_ADHOC:
811 cfg80211_leave_ibss(rdev, dev, false);
812 break;
813 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200814 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200815 cfg80211_disconnect(rdev, dev,
816 WLAN_REASON_DEAUTH_LEAVING, true);
817 break;
818 case NL80211_IFTYPE_MESH_POINT:
819 /* mesh should be handled? */
820 break;
821 default:
822 break;
823 }
824
825 cfg80211_process_rdev_events(rdev);
826 }
827
828 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
829 ntype, flags, params);
830
831 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
832
Johannes Berg9bc383d2009-11-19 11:55:19 +0100833 if (!err && params && params->use_4addr != -1)
834 dev->ieee80211_ptr->use_4addr = params->use_4addr;
835
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100836 if (!err) {
837 dev->priv_flags &= ~IFF_DONT_BRIDGE;
838 switch (ntype) {
839 case NL80211_IFTYPE_STATION:
840 if (dev->ieee80211_ptr->use_4addr)
841 break;
842 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200843 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100844 case NL80211_IFTYPE_ADHOC:
845 dev->priv_flags |= IFF_DONT_BRIDGE;
846 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200847 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100848 case NL80211_IFTYPE_AP:
849 case NL80211_IFTYPE_AP_VLAN:
850 case NL80211_IFTYPE_WDS:
851 case NL80211_IFTYPE_MESH_POINT:
852 /* bridging OK */
853 break;
854 case NL80211_IFTYPE_MONITOR:
855 /* monitor can't bridge anyway */
856 break;
857 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200858 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100859 /* not happening */
860 break;
861 }
862 }
863
Johannes Berg3d54d252009-08-21 14:51:05 +0200864 return err;
865}
John W. Linville254416a2009-12-09 16:43:52 -0500866
867u16 cfg80211_calculate_bitrate(struct rate_info *rate)
868{
869 int modulation, streams, bitrate;
870
871 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
872 return rate->legacy;
873
874 /* the formula below does only work for MCS values smaller than 32 */
875 if (rate->mcs >= 32)
876 return 0;
877
878 modulation = rate->mcs & 7;
879 streams = (rate->mcs >> 3) + 1;
880
881 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
882 13500000 : 6500000;
883
884 if (modulation < 4)
885 bitrate *= (modulation + 1);
886 else if (modulation == 4)
887 bitrate *= (modulation + 2);
888 else
889 bitrate *= (modulation + 3);
890
891 bitrate *= streams;
892
893 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
894 bitrate = (bitrate / 9) * 10;
895
896 /* do NOT round down here */
897 return (bitrate + 50000) / 100000;
898}