blob: c69e6532ab42a7e09b1e122de09c9c4d7ffca707 [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
Jack Cheung976d06b2012-01-21 12:10:24 -0800153bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
154{
155 int i;
156 for (i = 0; i < wiphy->n_cipher_suites; i++)
157 if (cipher == wiphy->cipher_suites[i])
158 return true;
159 return false;
160}
161
Johannes Bergfffd0932009-07-08 14:22:54 +0200162int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
163 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200164 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200165{
166 if (key_idx > 5)
167 return -EINVAL;
168
Johannes Berge31b8212010-10-05 19:39:30 +0200169 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
170 return -EINVAL;
171
172 if (pairwise && !mac_addr)
173 return -EINVAL;
174
Johannes Berg08645122009-05-11 13:54:58 +0200175 /*
176 * Disallow pairwise keys with non-zero index unless it's WEP
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200177 * or a vendor specific cipher (because current deployments use
178 * pairwise WEP keys with non-zero indices and for vendor specific
179 * ciphers this should be validated in the driver or hardware level
180 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200181 */
Johannes Berge31b8212010-10-05 19:39:30 +0200182 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200183 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
184 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
185 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200186 return -EINVAL;
187
Johannes Berg08645122009-05-11 13:54:58 +0200188 switch (params->cipher) {
189 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200190 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200191 return -EINVAL;
192 break;
193 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200194 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200195 return -EINVAL;
196 break;
197 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200198 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200199 return -EINVAL;
200 break;
201 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200202 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200203 return -EINVAL;
204 break;
205 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200206 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200207 return -EINVAL;
208 break;
Deepthi Gowri5b26a952011-11-29 11:22:42 +0530209 case WLAN_CIPHER_SUITE_SMS4:
210 if (params->key_len != WLAN_KEY_LEN_WAPI_SMS4)
211 return -EINVAL;
212 break;
Johannes Berg08645122009-05-11 13:54:58 +0200213 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300214 /*
215 * We don't know anything about this algorithm,
216 * allow using it -- but the driver must check
217 * all parameters! We still check below whether
218 * or not the driver supports this algorithm,
219 * of course.
220 */
221 break;
Johannes Berg08645122009-05-11 13:54:58 +0200222 }
223
Jouni Malinen9f26a952009-05-15 12:38:32 +0300224 if (params->seq) {
225 switch (params->cipher) {
226 case WLAN_CIPHER_SUITE_WEP40:
227 case WLAN_CIPHER_SUITE_WEP104:
228 /* These ciphers do not use key sequence */
229 return -EINVAL;
230 case WLAN_CIPHER_SUITE_TKIP:
231 case WLAN_CIPHER_SUITE_CCMP:
232 case WLAN_CIPHER_SUITE_AES_CMAC:
233 if (params->seq_len != 6)
234 return -EINVAL;
235 break;
236 }
237 }
238
Jack Cheung976d06b2012-01-21 12:10:24 -0800239 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200240 return -EINVAL;
241
Johannes Berg08645122009-05-11 13:54:58 +0200242 return 0;
243}
Zhu Yie31a16d2009-05-21 21:47:03 +0800244
245/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
246/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
247const unsigned char rfc1042_header[] __aligned(2) =
248 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
249EXPORT_SYMBOL(rfc1042_header);
250
251/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
252const unsigned char bridge_tunnel_header[] __aligned(2) =
253 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
254EXPORT_SYMBOL(bridge_tunnel_header);
255
Johannes Berg633adf12010-08-12 14:49:58 +0200256unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800257{
258 unsigned int hdrlen = 24;
259
260 if (ieee80211_is_data(fc)) {
261 if (ieee80211_has_a4(fc))
262 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200263 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800264 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200265 if (ieee80211_has_order(fc))
266 hdrlen += IEEE80211_HT_CTL_LEN;
267 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800268 goto out;
269 }
270
271 if (ieee80211_is_ctl(fc)) {
272 /*
273 * ACK and CTS are 10 bytes, all others 16. To see how
274 * to get this condition consider
275 * subtype mask: 0b0000000011110000 (0x00F0)
276 * ACK subtype: 0b0000000011010000 (0x00D0)
277 * CTS subtype: 0b0000000011000000 (0x00C0)
278 * bits that matter: ^^^ (0x00E0)
279 * value of those: 0b0000000011000000 (0x00C0)
280 */
281 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
282 hdrlen = 10;
283 else
284 hdrlen = 16;
285 }
286out:
287 return hdrlen;
288}
289EXPORT_SYMBOL(ieee80211_hdrlen);
290
291unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
292{
293 const struct ieee80211_hdr *hdr =
294 (const struct ieee80211_hdr *)skb->data;
295 unsigned int hdrlen;
296
297 if (unlikely(skb->len < 10))
298 return 0;
299 hdrlen = ieee80211_hdrlen(hdr->frame_control);
300 if (unlikely(hdrlen > skb->len))
301 return 0;
302 return hdrlen;
303}
304EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
305
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400306static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800307{
308 int ae = meshhdr->flags & MESH_FLAGS_AE;
309 /* 7.1.3.5a.2 */
310 switch (ae) {
311 case 0:
312 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700313 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800314 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700315 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800316 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700317 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800318 return 24;
319 default:
320 return 6;
321 }
322}
323
Zhu Yieaf85ca2009-12-01 10:18:37 +0800324int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800325 enum nl80211_iftype iftype)
326{
327 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
328 u16 hdrlen, ethertype;
329 u8 *payload;
330 u8 dst[ETH_ALEN];
331 u8 src[ETH_ALEN] __aligned(2);
332
333 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
334 return -1;
335
336 hdrlen = ieee80211_hdrlen(hdr->frame_control);
337
338 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
339 * header
340 * IEEE 802.11 address fields:
341 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
342 * 0 0 DA SA BSSID n/a
343 * 0 1 DA BSSID SA n/a
344 * 1 0 BSSID SA DA n/a
345 * 1 1 RA TA DA SA
346 */
347 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
348 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
349
350 switch (hdr->frame_control &
351 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
352 case cpu_to_le16(IEEE80211_FCTL_TODS):
353 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200354 iftype != NL80211_IFTYPE_AP_VLAN &&
355 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800356 return -1;
357 break;
358 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
359 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543e2009-11-10 20:10:05 +0100360 iftype != NL80211_IFTYPE_MESH_POINT &&
361 iftype != NL80211_IFTYPE_AP_VLAN &&
362 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800363 return -1;
364 if (iftype == NL80211_IFTYPE_MESH_POINT) {
365 struct ieee80211s_hdr *meshdr =
366 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800367 /* make sure meshdr->flags is on the linear part */
368 if (!pskb_may_pull(skb, hdrlen + 1))
369 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800370 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800371 skb_copy_bits(skb, hdrlen +
372 offsetof(struct ieee80211s_hdr, eaddr1),
373 dst, ETH_ALEN);
374 skb_copy_bits(skb, hdrlen +
375 offsetof(struct ieee80211s_hdr, eaddr2),
376 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800377 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800378 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800379 }
380 break;
381 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700382 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200383 iftype != NL80211_IFTYPE_P2P_CLIENT &&
384 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800385 (is_multicast_ether_addr(dst) &&
386 !compare_ether_addr(src, addr)))
387 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700388 if (iftype == NL80211_IFTYPE_MESH_POINT) {
389 struct ieee80211s_hdr *meshdr =
390 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800391 /* make sure meshdr->flags is on the linear part */
392 if (!pskb_may_pull(skb, hdrlen + 1))
393 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700394 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800395 skb_copy_bits(skb, hdrlen +
396 offsetof(struct ieee80211s_hdr, eaddr1),
397 src, ETH_ALEN);
398 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700399 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800400 break;
401 case cpu_to_le16(0):
402 if (iftype != NL80211_IFTYPE_ADHOC)
403 return -1;
404 break;
405 }
406
Zhu Yie3cf8b32010-03-29 17:35:07 +0800407 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800408 return -1;
409
410 payload = skb->data + hdrlen;
411 ethertype = (payload[6] << 8) | payload[7];
412
413 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
414 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
415 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
416 /* remove RFC1042 or Bridge-Tunnel encapsulation and
417 * replace EtherType */
418 skb_pull(skb, hdrlen + 6);
419 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
420 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
421 } else {
422 struct ethhdr *ehdr;
423 __be16 len;
424
425 skb_pull(skb, hdrlen);
426 len = htons(skb->len);
427 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
428 memcpy(ehdr->h_dest, dst, ETH_ALEN);
429 memcpy(ehdr->h_source, src, ETH_ALEN);
430 ehdr->h_proto = len;
431 }
432 return 0;
433}
434EXPORT_SYMBOL(ieee80211_data_to_8023);
435
Zhu Yieaf85ca2009-12-01 10:18:37 +0800436int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800437 enum nl80211_iftype iftype, u8 *bssid, bool qos)
438{
439 struct ieee80211_hdr hdr;
440 u16 hdrlen, ethertype;
441 __le16 fc;
442 const u8 *encaps_data;
443 int encaps_len, skip_header_bytes;
444 int nh_pos, h_pos;
445 int head_need;
446
447 if (unlikely(skb->len < ETH_HLEN))
448 return -EINVAL;
449
450 nh_pos = skb_network_header(skb) - skb->data;
451 h_pos = skb_transport_header(skb) - skb->data;
452
453 /* convert Ethernet header to proper 802.11 header (based on
454 * operation mode) */
455 ethertype = (skb->data[12] << 8) | skb->data[13];
456 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
457
458 switch (iftype) {
459 case NL80211_IFTYPE_AP:
460 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200461 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800462 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
463 /* DA BSSID SA */
464 memcpy(hdr.addr1, skb->data, ETH_ALEN);
465 memcpy(hdr.addr2, addr, ETH_ALEN);
466 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
467 hdrlen = 24;
468 break;
469 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200470 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800471 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
472 /* BSSID SA DA */
473 memcpy(hdr.addr1, bssid, ETH_ALEN);
474 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
475 memcpy(hdr.addr3, skb->data, ETH_ALEN);
476 hdrlen = 24;
477 break;
478 case NL80211_IFTYPE_ADHOC:
479 /* DA SA BSSID */
480 memcpy(hdr.addr1, skb->data, ETH_ALEN);
481 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
482 memcpy(hdr.addr3, bssid, ETH_ALEN);
483 hdrlen = 24;
484 break;
485 default:
486 return -EOPNOTSUPP;
487 }
488
489 if (qos) {
490 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
491 hdrlen += 2;
492 }
493
494 hdr.frame_control = fc;
495 hdr.duration_id = 0;
496 hdr.seq_ctrl = 0;
497
498 skip_header_bytes = ETH_HLEN;
499 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
500 encaps_data = bridge_tunnel_header;
501 encaps_len = sizeof(bridge_tunnel_header);
502 skip_header_bytes -= 2;
503 } else if (ethertype > 0x600) {
504 encaps_data = rfc1042_header;
505 encaps_len = sizeof(rfc1042_header);
506 skip_header_bytes -= 2;
507 } else {
508 encaps_data = NULL;
509 encaps_len = 0;
510 }
511
512 skb_pull(skb, skip_header_bytes);
513 nh_pos -= skip_header_bytes;
514 h_pos -= skip_header_bytes;
515
516 head_need = hdrlen + encaps_len - skb_headroom(skb);
517
518 if (head_need > 0 || skb_cloned(skb)) {
519 head_need = max(head_need, 0);
520 if (head_need)
521 skb_orphan(skb);
522
523 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800524 pr_err("failed to reallocate Tx buffer\n");
Zhu Yie31a16d2009-05-21 21:47:03 +0800525 return -ENOMEM;
526 }
527 skb->truesize += head_need;
528 }
529
530 if (encaps_data) {
531 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
532 nh_pos += encaps_len;
533 h_pos += encaps_len;
534 }
535
536 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
537
538 nh_pos += hdrlen;
539 h_pos += hdrlen;
540
541 /* Update skb pointers to various headers since this modified frame
542 * is going to go through Linux networking code that may potentially
543 * need things like pointer to IP header. */
544 skb_set_mac_header(skb, 0);
545 skb_set_network_header(skb, nh_pos);
546 skb_set_transport_header(skb, h_pos);
547
548 return 0;
549}
550EXPORT_SYMBOL(ieee80211_data_from_8023);
551
Zhu Yieaf85ca2009-12-01 10:18:37 +0800552
553void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
554 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700555 const unsigned int extra_headroom,
556 bool has_80211_header)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800557{
558 struct sk_buff *frame = NULL;
559 u16 ethertype;
560 u8 *payload;
561 const struct ethhdr *eth;
562 int remaining, err;
563 u8 dst[ETH_ALEN], src[ETH_ALEN];
564
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700565 if (has_80211_header) {
566 err = ieee80211_data_to_8023(skb, addr, iftype);
567 if (err)
568 goto out;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800569
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700570 /* skip the wrapping header */
571 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
572 if (!eth)
573 goto out;
574 } else {
575 eth = (struct ethhdr *) skb->data;
576 }
Zhu Yieaf85ca2009-12-01 10:18:37 +0800577
578 while (skb != frame) {
579 u8 padding;
580 __be16 len = eth->h_proto;
581 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
582
583 remaining = skb->len;
584 memcpy(dst, eth->h_dest, ETH_ALEN);
585 memcpy(src, eth->h_source, ETH_ALEN);
586
587 padding = (4 - subframe_len) & 0x3;
588 /* the last MSDU has no padding */
589 if (subframe_len > remaining)
590 goto purge;
591
592 skb_pull(skb, sizeof(struct ethhdr));
593 /* reuse skb for the last subframe */
594 if (remaining <= subframe_len + padding)
595 frame = skb;
596 else {
597 unsigned int hlen = ALIGN(extra_headroom, 4);
598 /*
599 * Allocate and reserve two bytes more for payload
600 * alignment since sizeof(struct ethhdr) is 14.
601 */
602 frame = dev_alloc_skb(hlen + subframe_len + 2);
603 if (!frame)
604 goto purge;
605
606 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
607 memcpy(skb_put(frame, ntohs(len)), skb->data,
608 ntohs(len));
609
610 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
611 padding);
612 if (!eth) {
613 dev_kfree_skb(frame);
614 goto purge;
615 }
616 }
617
618 skb_reset_network_header(frame);
619 frame->dev = skb->dev;
620 frame->priority = skb->priority;
621
622 payload = frame->data;
623 ethertype = (payload[6] << 8) | payload[7];
624
625 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
626 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
627 compare_ether_addr(payload,
628 bridge_tunnel_header) == 0)) {
629 /* remove RFC1042 or Bridge-Tunnel
630 * encapsulation and replace EtherType */
631 skb_pull(frame, 6);
632 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
633 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
634 } else {
635 memcpy(skb_push(frame, sizeof(__be16)), &len,
636 sizeof(__be16));
637 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
638 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
639 }
640 __skb_queue_tail(list, frame);
641 }
642
643 return;
644
645 purge:
646 __skb_queue_purge(list);
647 out:
648 dev_kfree_skb(skb);
649}
650EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
651
Zhu Yie31a16d2009-05-21 21:47:03 +0800652/* Given a data frame determine the 802.1p/1d tag to use. */
653unsigned int cfg80211_classify8021d(struct sk_buff *skb)
654{
655 unsigned int dscp;
656
657 /* skb->priority values from 256->263 are magic values to
658 * directly indicate a specific 802.1d priority. This is used
659 * to allow 802.1d priority to be passed directly in from VLAN
660 * tags, etc.
661 */
662 if (skb->priority >= 256 && skb->priority <= 263)
663 return skb->priority - 256;
664
665 switch (skb->protocol) {
666 case htons(ETH_P_IP):
667 dscp = ip_hdr(skb)->tos & 0xfc;
668 break;
669 default:
670 return 0;
671 }
672
673 return dscp >> 5;
674}
675EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200676
677const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
678{
679 u8 *end, *pos;
680
681 pos = bss->information_elements;
682 if (pos == NULL)
683 return NULL;
684 end = pos + bss->len_information_elements;
685
686 while (pos + 1 < end) {
687 if (pos + 2 + pos[1] > end)
688 break;
689 if (pos[0] == ie)
690 return pos;
691 pos += 2 + pos[1];
692 }
693
694 return NULL;
695}
696EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200697
698void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
699{
700 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
701 struct net_device *dev = wdev->netdev;
702 int i;
703
704 if (!wdev->connect_keys)
705 return;
706
707 for (i = 0; i < 6; i++) {
708 if (!wdev->connect_keys->params[i].cipher)
709 continue;
Johannes Berge31b8212010-10-05 19:39:30 +0200710 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800711 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800712 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800713 continue;
714 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200715 if (wdev->connect_keys->def == i)
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100716 if (rdev->ops->set_default_key(wdev->wiphy, dev,
717 i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800718 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800719 continue;
720 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200721 if (wdev->connect_keys->defmgmt == i)
722 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800723 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200724 }
725
726 kfree(wdev->connect_keys);
727 wdev->connect_keys = NULL;
728}
Johannes Berg3d54d252009-08-21 14:51:05 +0200729
730static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
731{
732 struct cfg80211_event *ev;
733 unsigned long flags;
734 const u8 *bssid = NULL;
735
736 spin_lock_irqsave(&wdev->event_lock, flags);
737 while (!list_empty(&wdev->event_list)) {
738 ev = list_first_entry(&wdev->event_list,
739 struct cfg80211_event, list);
740 list_del(&ev->list);
741 spin_unlock_irqrestore(&wdev->event_lock, flags);
742
743 wdev_lock(wdev);
744 switch (ev->type) {
745 case EVENT_CONNECT_RESULT:
746 if (!is_zero_ether_addr(ev->cr.bssid))
747 bssid = ev->cr.bssid;
748 __cfg80211_connect_result(
749 wdev->netdev, bssid,
750 ev->cr.req_ie, ev->cr.req_ie_len,
751 ev->cr.resp_ie, ev->cr.resp_ie_len,
752 ev->cr.status,
753 ev->cr.status == WLAN_STATUS_SUCCESS,
754 NULL);
755 break;
756 case EVENT_ROAMED:
Jouni Malinened9d0102011-05-16 19:40:15 +0300757 __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid,
Johannes Berg3d54d252009-08-21 14:51:05 +0200758 ev->rm.req_ie, ev->rm.req_ie_len,
759 ev->rm.resp_ie, ev->rm.resp_ie_len);
760 break;
761 case EVENT_DISCONNECTED:
762 __cfg80211_disconnected(wdev->netdev,
763 ev->dc.ie, ev->dc.ie_len,
764 ev->dc.reason, true);
765 break;
766 case EVENT_IBSS_JOINED:
767 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
768 break;
769 }
770 wdev_unlock(wdev);
771
772 kfree(ev);
773
774 spin_lock_irqsave(&wdev->event_lock, flags);
775 }
776 spin_unlock_irqrestore(&wdev->event_lock, flags);
777}
778
779void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
780{
781 struct wireless_dev *wdev;
782
783 ASSERT_RTNL();
784 ASSERT_RDEV_LOCK(rdev);
785
786 mutex_lock(&rdev->devlist_mtx);
787
788 list_for_each_entry(wdev, &rdev->netdev_list, list)
789 cfg80211_process_wdev_events(wdev);
790
791 mutex_unlock(&rdev->devlist_mtx);
792}
793
794int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
795 struct net_device *dev, enum nl80211_iftype ntype,
796 u32 *flags, struct vif_params *params)
797{
798 int err;
799 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
800
801 ASSERT_RDEV_LOCK(rdev);
802
803 /* don't support changing VLANs, you just re-create them */
804 if (otype == NL80211_IFTYPE_AP_VLAN)
805 return -EOPNOTSUPP;
806
807 if (!rdev->ops->change_virtual_intf ||
808 !(rdev->wiphy.interface_modes & (1 << ntype)))
809 return -EOPNOTSUPP;
810
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100811 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a2010-06-15 06:50:45 +0000812 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200813 (ntype == NL80211_IFTYPE_ADHOC ||
814 ntype == NL80211_IFTYPE_STATION ||
815 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100816 return -EBUSY;
817
Johannes Berg3d54d252009-08-21 14:51:05 +0200818 if (ntype != otype) {
Johannes Berg7527a782011-05-13 10:58:57 +0200819 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
820 ntype);
821 if (err)
822 return err;
823
Johannes Berg9bc383d2009-11-19 11:55:19 +0100824 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100825 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100826
Johannes Berg3d54d252009-08-21 14:51:05 +0200827 switch (otype) {
828 case NL80211_IFTYPE_ADHOC:
829 cfg80211_leave_ibss(rdev, dev, false);
830 break;
831 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200832 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200833 cfg80211_disconnect(rdev, dev,
834 WLAN_REASON_DEAUTH_LEAVING, true);
835 break;
836 case NL80211_IFTYPE_MESH_POINT:
837 /* mesh should be handled? */
838 break;
839 default:
840 break;
841 }
842
843 cfg80211_process_rdev_events(rdev);
844 }
845
846 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
847 ntype, flags, params);
848
849 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
850
Johannes Berg9bc383d2009-11-19 11:55:19 +0100851 if (!err && params && params->use_4addr != -1)
852 dev->ieee80211_ptr->use_4addr = params->use_4addr;
853
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100854 if (!err) {
855 dev->priv_flags &= ~IFF_DONT_BRIDGE;
856 switch (ntype) {
857 case NL80211_IFTYPE_STATION:
858 if (dev->ieee80211_ptr->use_4addr)
859 break;
860 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200861 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100862 case NL80211_IFTYPE_ADHOC:
863 dev->priv_flags |= IFF_DONT_BRIDGE;
864 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200865 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100866 case NL80211_IFTYPE_AP:
867 case NL80211_IFTYPE_AP_VLAN:
868 case NL80211_IFTYPE_WDS:
869 case NL80211_IFTYPE_MESH_POINT:
870 /* bridging OK */
871 break;
872 case NL80211_IFTYPE_MONITOR:
873 /* monitor can't bridge anyway */
874 break;
875 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200876 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100877 /* not happening */
878 break;
879 }
880 }
881
Johannes Berg3d54d252009-08-21 14:51:05 +0200882 return err;
883}
John W. Linville254416a2009-12-09 16:43:52 -0500884
885u16 cfg80211_calculate_bitrate(struct rate_info *rate)
886{
887 int modulation, streams, bitrate;
888
889 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
890 return rate->legacy;
891
892 /* the formula below does only work for MCS values smaller than 32 */
893 if (rate->mcs >= 32)
894 return 0;
895
896 modulation = rate->mcs & 7;
897 streams = (rate->mcs >> 3) + 1;
898
899 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
900 13500000 : 6500000;
901
902 if (modulation < 4)
903 bitrate *= (modulation + 1);
904 else if (modulation == 4)
905 bitrate *= (modulation + 2);
906 else
907 bitrate *= (modulation + 3);
908
909 bitrate *= streams;
910
911 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
912 bitrate = (bitrate / 9) * 10;
913
914 /* do NOT round down here */
915 return (bitrate + 50000) / 100000;
916}
Johannes Berg56d18932011-05-09 18:41:15 +0200917
918int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
919 u32 beacon_int)
920{
921 struct wireless_dev *wdev;
922 int res = 0;
923
924 if (!beacon_int)
925 return -EINVAL;
926
927 mutex_lock(&rdev->devlist_mtx);
928
929 list_for_each_entry(wdev, &rdev->netdev_list, list) {
930 if (!wdev->beacon_interval)
931 continue;
932 if (wdev->beacon_interval != beacon_int) {
933 res = -EINVAL;
934 break;
935 }
936 }
937
938 mutex_unlock(&rdev->devlist_mtx);
939
940 return res;
941}
Johannes Berg7527a782011-05-13 10:58:57 +0200942
943int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
944 struct wireless_dev *wdev,
945 enum nl80211_iftype iftype)
946{
947 struct wireless_dev *wdev_iter;
948 int num[NUM_NL80211_IFTYPES];
949 int total = 1;
950 int i, j;
951
952 ASSERT_RTNL();
953
954 /* Always allow software iftypes */
955 if (rdev->wiphy.software_iftypes & BIT(iftype))
956 return 0;
957
958 /*
959 * Drivers will gradually all set this flag, until all
960 * have it we only enforce for those that set it.
961 */
962 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
963 return 0;
964
965 memset(num, 0, sizeof(num));
966
967 num[iftype] = 1;
968
969 mutex_lock(&rdev->devlist_mtx);
970 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
971 if (wdev_iter == wdev)
972 continue;
973 if (!netif_running(wdev_iter->netdev))
974 continue;
975
976 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
977 continue;
978
979 num[wdev_iter->iftype]++;
980 total++;
981 }
982 mutex_unlock(&rdev->devlist_mtx);
983
984 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
985 const struct ieee80211_iface_combination *c;
986 struct ieee80211_iface_limit *limits;
987
988 c = &rdev->wiphy.iface_combinations[i];
989
990 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
991 GFP_KERNEL);
992 if (!limits)
993 return -ENOMEM;
994 if (total > c->max_interfaces)
995 goto cont;
996
997 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
998 if (rdev->wiphy.software_iftypes & BIT(iftype))
999 continue;
1000 for (j = 0; j < c->n_limits; j++) {
1001 if (!(limits[j].types & iftype))
1002 continue;
1003 if (limits[j].max < num[iftype])
1004 goto cont;
1005 limits[j].max -= num[iftype];
1006 }
1007 }
1008 /* yay, it fits */
1009 kfree(limits);
1010 return 0;
1011 cont:
1012 kfree(limits);
1013 }
1014
1015 return -EBUSY;
1016}