blob: e77df75850048c3e5b4eb19f788581f7226c0c34 [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 */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04006#include <linux/export.h>
Johannes Bergd3236552009-04-20 14:31:42 +02007#include <linux/bitops.h>
Zhu Yie31a16d2009-05-21 21:47:03 +08008#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Johannes Bergd3236552009-04-20 14:31:42 +020010#include <net/cfg80211.h>
Zhu Yie31a16d2009-05-21 21:47:03 +080011#include <net/ip.h>
Johannes Berg8318d782008-01-24 19:38:38 +010012#include "core.h"
13
Johannes Bergbd815252008-10-29 20:00:45 +010014struct ieee80211_rate *
15ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010016 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010017{
18 struct ieee80211_rate *result = &sband->bitrates[0];
19 int i;
20
21 for (i = 0; i < sband->n_bitrates; i++) {
22 if (!(basic_rates & BIT(i)))
23 continue;
24 if (sband->bitrates[i].bitrate > bitrate)
25 continue;
26 result = &sband->bitrates[i];
27 }
28
29 return result;
30}
31EXPORT_SYMBOL(ieee80211_get_response_rate);
32
Bruno Randolf59eb21a2011-01-17 13:37:28 +090033int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010034{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090035 /* see 802.11 17.3.8.3.2 and Annex J
36 * there are overlapping channel numbers in 5GHz and 2GHz bands */
37 if (band == IEEE80211_BAND_5GHZ) {
38 if (chan >= 182 && chan <= 196)
39 return 4000 + chan * 5;
40 else
41 return 5000 + chan * 5;
42 } else { /* IEEE80211_BAND_2GHZ */
43 if (chan == 14)
44 return 2484;
45 else if (chan < 14)
46 return 2407 + chan * 5;
47 else
48 return 0; /* not supported */
49 }
Johannes Berg8318d782008-01-24 19:38:38 +010050}
51EXPORT_SYMBOL(ieee80211_channel_to_frequency);
52
53int ieee80211_frequency_to_channel(int freq)
54{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090055 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +010056 if (freq == 2484)
57 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090058 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +010059 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090060 else if (freq >= 4910 && freq <= 4980)
61 return (freq - 4000) / 5;
62 else
63 return (freq - 5000) / 5;
Johannes Berg8318d782008-01-24 19:38:38 +010064}
65EXPORT_SYMBOL(ieee80211_frequency_to_channel);
66
Johannes Berg6c507cd2008-03-26 14:14:55 +010067struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
68 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +010069{
70 enum ieee80211_band band;
71 struct ieee80211_supported_band *sband;
72 int i;
73
74 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
75 sband = wiphy->bands[band];
76
77 if (!sband)
78 continue;
79
80 for (i = 0; i < sband->n_channels; i++) {
81 if (sband->channels[i].center_freq == freq)
82 return &sband->channels[i];
83 }
84 }
85
86 return NULL;
87}
Johannes Berg6c507cd2008-03-26 14:14:55 +010088EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +010089
Johannes Berg8318d782008-01-24 19:38:38 +010090static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
91 enum ieee80211_band band)
92{
93 int i, want;
94
95 switch (band) {
96 case IEEE80211_BAND_5GHZ:
97 want = 3;
98 for (i = 0; i < sband->n_bitrates; i++) {
99 if (sband->bitrates[i].bitrate == 60 ||
100 sband->bitrates[i].bitrate == 120 ||
101 sband->bitrates[i].bitrate == 240) {
102 sband->bitrates[i].flags |=
103 IEEE80211_RATE_MANDATORY_A;
104 want--;
105 }
106 }
107 WARN_ON(want);
108 break;
109 case IEEE80211_BAND_2GHZ:
110 want = 7;
111 for (i = 0; i < sband->n_bitrates; i++) {
112 if (sband->bitrates[i].bitrate == 10) {
113 sband->bitrates[i].flags |=
114 IEEE80211_RATE_MANDATORY_B |
115 IEEE80211_RATE_MANDATORY_G;
116 want--;
117 }
118
119 if (sband->bitrates[i].bitrate == 20 ||
120 sband->bitrates[i].bitrate == 55 ||
121 sband->bitrates[i].bitrate == 110 ||
122 sband->bitrates[i].bitrate == 60 ||
123 sband->bitrates[i].bitrate == 120 ||
124 sband->bitrates[i].bitrate == 240) {
125 sband->bitrates[i].flags |=
126 IEEE80211_RATE_MANDATORY_G;
127 want--;
128 }
129
Johannes Bergaac09fb2008-01-30 17:36:10 +0100130 if (sband->bitrates[i].bitrate != 10 &&
131 sband->bitrates[i].bitrate != 20 &&
132 sband->bitrates[i].bitrate != 55 &&
133 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100134 sband->bitrates[i].flags |=
135 IEEE80211_RATE_ERP_G;
136 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100137 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100138 break;
139 case IEEE80211_NUM_BANDS:
140 WARN_ON(1);
141 break;
142 }
143}
144
145void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
146{
147 enum ieee80211_band band;
148
149 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
150 if (wiphy->bands[band])
151 set_mandatory_flags_band(wiphy->bands[band], band);
152}
Johannes Berg08645122009-05-11 13:54:58 +0200153
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300154bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
155{
156 int i;
157 for (i = 0; i < wiphy->n_cipher_suites; i++)
158 if (cipher == wiphy->cipher_suites[i])
159 return true;
160 return false;
161}
162
Johannes Bergfffd0932009-07-08 14:22:54 +0200163int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
164 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200165 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200166{
167 if (key_idx > 5)
168 return -EINVAL;
169
Johannes Berge31b8212010-10-05 19:39:30 +0200170 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
171 return -EINVAL;
172
173 if (pairwise && !mac_addr)
174 return -EINVAL;
175
Johannes Berg08645122009-05-11 13:54:58 +0200176 /*
177 * Disallow pairwise keys with non-zero index unless it's WEP
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200178 * or a vendor specific cipher (because current deployments use
179 * pairwise WEP keys with non-zero indices and for vendor specific
180 * ciphers this should be validated in the driver or hardware level
181 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200182 */
Johannes Berge31b8212010-10-05 19:39:30 +0200183 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200184 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
185 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
186 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200187 return -EINVAL;
188
Johannes Berg08645122009-05-11 13:54:58 +0200189 switch (params->cipher) {
190 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200191 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200192 return -EINVAL;
193 break;
194 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200195 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200196 return -EINVAL;
197 break;
198 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200199 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200200 return -EINVAL;
201 break;
202 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200203 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200204 return -EINVAL;
205 break;
206 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200207 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200208 return -EINVAL;
209 break;
210 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300211 /*
212 * We don't know anything about this algorithm,
213 * allow using it -- but the driver must check
214 * all parameters! We still check below whether
215 * or not the driver supports this algorithm,
216 * of course.
217 */
218 break;
Johannes Berg08645122009-05-11 13:54:58 +0200219 }
220
Jouni Malinen9f26a952009-05-15 12:38:32 +0300221 if (params->seq) {
222 switch (params->cipher) {
223 case WLAN_CIPHER_SUITE_WEP40:
224 case WLAN_CIPHER_SUITE_WEP104:
225 /* These ciphers do not use key sequence */
226 return -EINVAL;
227 case WLAN_CIPHER_SUITE_TKIP:
228 case WLAN_CIPHER_SUITE_CCMP:
229 case WLAN_CIPHER_SUITE_AES_CMAC:
230 if (params->seq_len != 6)
231 return -EINVAL;
232 break;
233 }
234 }
235
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300236 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200237 return -EINVAL;
238
Johannes Berg08645122009-05-11 13:54:58 +0200239 return 0;
240}
Zhu Yie31a16d2009-05-21 21:47:03 +0800241
Johannes Berg633adf12010-08-12 14:49:58 +0200242unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800243{
244 unsigned int hdrlen = 24;
245
246 if (ieee80211_is_data(fc)) {
247 if (ieee80211_has_a4(fc))
248 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200249 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800250 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200251 if (ieee80211_has_order(fc))
252 hdrlen += IEEE80211_HT_CTL_LEN;
253 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800254 goto out;
255 }
256
257 if (ieee80211_is_ctl(fc)) {
258 /*
259 * ACK and CTS are 10 bytes, all others 16. To see how
260 * to get this condition consider
261 * subtype mask: 0b0000000011110000 (0x00F0)
262 * ACK subtype: 0b0000000011010000 (0x00D0)
263 * CTS subtype: 0b0000000011000000 (0x00C0)
264 * bits that matter: ^^^ (0x00E0)
265 * value of those: 0b0000000011000000 (0x00C0)
266 */
267 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
268 hdrlen = 10;
269 else
270 hdrlen = 16;
271 }
272out:
273 return hdrlen;
274}
275EXPORT_SYMBOL(ieee80211_hdrlen);
276
277unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
278{
279 const struct ieee80211_hdr *hdr =
280 (const struct ieee80211_hdr *)skb->data;
281 unsigned int hdrlen;
282
283 if (unlikely(skb->len < 10))
284 return 0;
285 hdrlen = ieee80211_hdrlen(hdr->frame_control);
286 if (unlikely(hdrlen > skb->len))
287 return 0;
288 return hdrlen;
289}
290EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
291
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400292static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800293{
294 int ae = meshhdr->flags & MESH_FLAGS_AE;
295 /* 7.1.3.5a.2 */
296 switch (ae) {
297 case 0:
298 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700299 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800300 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700301 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800302 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700303 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800304 return 24;
305 default:
306 return 6;
307 }
308}
309
Zhu Yieaf85ca2009-12-01 10:18:37 +0800310int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800311 enum nl80211_iftype iftype)
312{
313 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
314 u16 hdrlen, ethertype;
315 u8 *payload;
316 u8 dst[ETH_ALEN];
317 u8 src[ETH_ALEN] __aligned(2);
318
319 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
320 return -1;
321
322 hdrlen = ieee80211_hdrlen(hdr->frame_control);
323
324 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
325 * header
326 * IEEE 802.11 address fields:
327 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
328 * 0 0 DA SA BSSID n/a
329 * 0 1 DA BSSID SA n/a
330 * 1 0 BSSID SA DA n/a
331 * 1 1 RA TA DA SA
332 */
333 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
334 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
335
336 switch (hdr->frame_control &
337 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
338 case cpu_to_le16(IEEE80211_FCTL_TODS):
339 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200340 iftype != NL80211_IFTYPE_AP_VLAN &&
341 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800342 return -1;
343 break;
344 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
345 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543e2009-11-10 20:10:05 +0100346 iftype != NL80211_IFTYPE_MESH_POINT &&
347 iftype != NL80211_IFTYPE_AP_VLAN &&
348 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800349 return -1;
350 if (iftype == NL80211_IFTYPE_MESH_POINT) {
351 struct ieee80211s_hdr *meshdr =
352 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800353 /* make sure meshdr->flags is on the linear part */
354 if (!pskb_may_pull(skb, hdrlen + 1))
355 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800356 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800357 skb_copy_bits(skb, hdrlen +
358 offsetof(struct ieee80211s_hdr, eaddr1),
359 dst, ETH_ALEN);
360 skb_copy_bits(skb, hdrlen +
361 offsetof(struct ieee80211s_hdr, eaddr2),
362 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800363 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800364 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800365 }
366 break;
367 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700368 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200369 iftype != NL80211_IFTYPE_P2P_CLIENT &&
370 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800371 (is_multicast_ether_addr(dst) &&
372 !compare_ether_addr(src, addr)))
373 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700374 if (iftype == NL80211_IFTYPE_MESH_POINT) {
375 struct ieee80211s_hdr *meshdr =
376 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800377 /* make sure meshdr->flags is on the linear part */
378 if (!pskb_may_pull(skb, hdrlen + 1))
379 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700380 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800381 skb_copy_bits(skb, hdrlen +
382 offsetof(struct ieee80211s_hdr, eaddr1),
383 src, ETH_ALEN);
384 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700385 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800386 break;
387 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300388 if (iftype != NL80211_IFTYPE_ADHOC &&
389 iftype != NL80211_IFTYPE_STATION)
390 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800391 break;
392 }
393
Zhu Yie3cf8b32010-03-29 17:35:07 +0800394 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800395 return -1;
396
397 payload = skb->data + hdrlen;
398 ethertype = (payload[6] << 8) | payload[7];
399
400 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
401 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
402 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
403 /* remove RFC1042 or Bridge-Tunnel encapsulation and
404 * replace EtherType */
405 skb_pull(skb, hdrlen + 6);
406 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
407 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
408 } else {
409 struct ethhdr *ehdr;
410 __be16 len;
411
412 skb_pull(skb, hdrlen);
413 len = htons(skb->len);
414 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
415 memcpy(ehdr->h_dest, dst, ETH_ALEN);
416 memcpy(ehdr->h_source, src, ETH_ALEN);
417 ehdr->h_proto = len;
418 }
419 return 0;
420}
421EXPORT_SYMBOL(ieee80211_data_to_8023);
422
Zhu Yieaf85ca2009-12-01 10:18:37 +0800423int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800424 enum nl80211_iftype iftype, u8 *bssid, bool qos)
425{
426 struct ieee80211_hdr hdr;
427 u16 hdrlen, ethertype;
428 __le16 fc;
429 const u8 *encaps_data;
430 int encaps_len, skip_header_bytes;
431 int nh_pos, h_pos;
432 int head_need;
433
434 if (unlikely(skb->len < ETH_HLEN))
435 return -EINVAL;
436
437 nh_pos = skb_network_header(skb) - skb->data;
438 h_pos = skb_transport_header(skb) - skb->data;
439
440 /* convert Ethernet header to proper 802.11 header (based on
441 * operation mode) */
442 ethertype = (skb->data[12] << 8) | skb->data[13];
443 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
444
445 switch (iftype) {
446 case NL80211_IFTYPE_AP:
447 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200448 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800449 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
450 /* DA BSSID SA */
451 memcpy(hdr.addr1, skb->data, ETH_ALEN);
452 memcpy(hdr.addr2, addr, ETH_ALEN);
453 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
454 hdrlen = 24;
455 break;
456 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200457 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800458 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
459 /* BSSID SA DA */
460 memcpy(hdr.addr1, bssid, ETH_ALEN);
461 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
462 memcpy(hdr.addr3, skb->data, ETH_ALEN);
463 hdrlen = 24;
464 break;
465 case NL80211_IFTYPE_ADHOC:
466 /* DA SA BSSID */
467 memcpy(hdr.addr1, skb->data, ETH_ALEN);
468 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
469 memcpy(hdr.addr3, bssid, ETH_ALEN);
470 hdrlen = 24;
471 break;
472 default:
473 return -EOPNOTSUPP;
474 }
475
476 if (qos) {
477 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
478 hdrlen += 2;
479 }
480
481 hdr.frame_control = fc;
482 hdr.duration_id = 0;
483 hdr.seq_ctrl = 0;
484
485 skip_header_bytes = ETH_HLEN;
486 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
487 encaps_data = bridge_tunnel_header;
488 encaps_len = sizeof(bridge_tunnel_header);
489 skip_header_bytes -= 2;
490 } else if (ethertype > 0x600) {
491 encaps_data = rfc1042_header;
492 encaps_len = sizeof(rfc1042_header);
493 skip_header_bytes -= 2;
494 } else {
495 encaps_data = NULL;
496 encaps_len = 0;
497 }
498
499 skb_pull(skb, skip_header_bytes);
500 nh_pos -= skip_header_bytes;
501 h_pos -= skip_header_bytes;
502
503 head_need = hdrlen + encaps_len - skb_headroom(skb);
504
505 if (head_need > 0 || skb_cloned(skb)) {
506 head_need = max(head_need, 0);
507 if (head_need)
508 skb_orphan(skb);
509
Joe Perches24616152011-08-29 14:17:41 -0700510 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
Zhu Yie31a16d2009-05-21 21:47:03 +0800511 return -ENOMEM;
Joe Perches24616152011-08-29 14:17:41 -0700512
Zhu Yie31a16d2009-05-21 21:47:03 +0800513 skb->truesize += head_need;
514 }
515
516 if (encaps_data) {
517 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
518 nh_pos += encaps_len;
519 h_pos += encaps_len;
520 }
521
522 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
523
524 nh_pos += hdrlen;
525 h_pos += hdrlen;
526
527 /* Update skb pointers to various headers since this modified frame
528 * is going to go through Linux networking code that may potentially
529 * need things like pointer to IP header. */
530 skb_set_mac_header(skb, 0);
531 skb_set_network_header(skb, nh_pos);
532 skb_set_transport_header(skb, h_pos);
533
534 return 0;
535}
536EXPORT_SYMBOL(ieee80211_data_from_8023);
537
Zhu Yieaf85ca2009-12-01 10:18:37 +0800538
539void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
540 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700541 const unsigned int extra_headroom,
542 bool has_80211_header)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800543{
544 struct sk_buff *frame = NULL;
545 u16 ethertype;
546 u8 *payload;
547 const struct ethhdr *eth;
548 int remaining, err;
549 u8 dst[ETH_ALEN], src[ETH_ALEN];
550
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700551 if (has_80211_header) {
552 err = ieee80211_data_to_8023(skb, addr, iftype);
553 if (err)
554 goto out;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800555
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700556 /* skip the wrapping header */
557 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
558 if (!eth)
559 goto out;
560 } else {
561 eth = (struct ethhdr *) skb->data;
562 }
Zhu Yieaf85ca2009-12-01 10:18:37 +0800563
564 while (skb != frame) {
565 u8 padding;
566 __be16 len = eth->h_proto;
567 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
568
569 remaining = skb->len;
570 memcpy(dst, eth->h_dest, ETH_ALEN);
571 memcpy(src, eth->h_source, ETH_ALEN);
572
573 padding = (4 - subframe_len) & 0x3;
574 /* the last MSDU has no padding */
575 if (subframe_len > remaining)
576 goto purge;
577
578 skb_pull(skb, sizeof(struct ethhdr));
579 /* reuse skb for the last subframe */
580 if (remaining <= subframe_len + padding)
581 frame = skb;
582 else {
583 unsigned int hlen = ALIGN(extra_headroom, 4);
584 /*
585 * Allocate and reserve two bytes more for payload
586 * alignment since sizeof(struct ethhdr) is 14.
587 */
588 frame = dev_alloc_skb(hlen + subframe_len + 2);
589 if (!frame)
590 goto purge;
591
592 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
593 memcpy(skb_put(frame, ntohs(len)), skb->data,
594 ntohs(len));
595
596 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
597 padding);
598 if (!eth) {
599 dev_kfree_skb(frame);
600 goto purge;
601 }
602 }
603
604 skb_reset_network_header(frame);
605 frame->dev = skb->dev;
606 frame->priority = skb->priority;
607
608 payload = frame->data;
609 ethertype = (payload[6] << 8) | payload[7];
610
611 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
612 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
613 compare_ether_addr(payload,
614 bridge_tunnel_header) == 0)) {
615 /* remove RFC1042 or Bridge-Tunnel
616 * encapsulation and replace EtherType */
617 skb_pull(frame, 6);
618 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
619 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
620 } else {
621 memcpy(skb_push(frame, sizeof(__be16)), &len,
622 sizeof(__be16));
623 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
624 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
625 }
626 __skb_queue_tail(list, frame);
627 }
628
629 return;
630
631 purge:
632 __skb_queue_purge(list);
633 out:
634 dev_kfree_skb(skb);
635}
636EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
637
Zhu Yie31a16d2009-05-21 21:47:03 +0800638/* Given a data frame determine the 802.1p/1d tag to use. */
639unsigned int cfg80211_classify8021d(struct sk_buff *skb)
640{
641 unsigned int dscp;
642
643 /* skb->priority values from 256->263 are magic values to
644 * directly indicate a specific 802.1d priority. This is used
645 * to allow 802.1d priority to be passed directly in from VLAN
646 * tags, etc.
647 */
648 if (skb->priority >= 256 && skb->priority <= 263)
649 return skb->priority - 256;
650
651 switch (skb->protocol) {
652 case htons(ETH_P_IP):
653 dscp = ip_hdr(skb)->tos & 0xfc;
654 break;
655 default:
656 return 0;
657 }
658
659 return dscp >> 5;
660}
661EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200662
663const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
664{
665 u8 *end, *pos;
666
667 pos = bss->information_elements;
668 if (pos == NULL)
669 return NULL;
670 end = pos + bss->len_information_elements;
671
672 while (pos + 1 < end) {
673 if (pos + 2 + pos[1] > end)
674 break;
675 if (pos[0] == ie)
676 return pos;
677 pos += 2 + pos[1];
678 }
679
680 return NULL;
681}
682EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200683
684void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
685{
686 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
687 struct net_device *dev = wdev->netdev;
688 int i;
689
690 if (!wdev->connect_keys)
691 return;
692
693 for (i = 0; i < 6; i++) {
694 if (!wdev->connect_keys->params[i].cipher)
695 continue;
Johannes Berge31b8212010-10-05 19:39:30 +0200696 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800697 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800698 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800699 continue;
700 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200701 if (wdev->connect_keys->def == i)
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100702 if (rdev->ops->set_default_key(wdev->wiphy, dev,
703 i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800704 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800705 continue;
706 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200707 if (wdev->connect_keys->defmgmt == i)
708 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800709 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200710 }
711
712 kfree(wdev->connect_keys);
713 wdev->connect_keys = NULL;
714}
Johannes Berg3d54d252009-08-21 14:51:05 +0200715
716static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
717{
718 struct cfg80211_event *ev;
719 unsigned long flags;
720 const u8 *bssid = NULL;
721
722 spin_lock_irqsave(&wdev->event_lock, flags);
723 while (!list_empty(&wdev->event_list)) {
724 ev = list_first_entry(&wdev->event_list,
725 struct cfg80211_event, list);
726 list_del(&ev->list);
727 spin_unlock_irqrestore(&wdev->event_lock, flags);
728
729 wdev_lock(wdev);
730 switch (ev->type) {
731 case EVENT_CONNECT_RESULT:
732 if (!is_zero_ether_addr(ev->cr.bssid))
733 bssid = ev->cr.bssid;
734 __cfg80211_connect_result(
735 wdev->netdev, bssid,
736 ev->cr.req_ie, ev->cr.req_ie_len,
737 ev->cr.resp_ie, ev->cr.resp_ie_len,
738 ev->cr.status,
739 ev->cr.status == WLAN_STATUS_SUCCESS,
740 NULL);
741 break;
742 case EVENT_ROAMED:
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530743 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
744 ev->rm.req_ie_len, ev->rm.resp_ie,
745 ev->rm.resp_ie_len);
Johannes Berg3d54d252009-08-21 14:51:05 +0200746 break;
747 case EVENT_DISCONNECTED:
748 __cfg80211_disconnected(wdev->netdev,
749 ev->dc.ie, ev->dc.ie_len,
750 ev->dc.reason, true);
751 break;
752 case EVENT_IBSS_JOINED:
753 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
754 break;
755 }
756 wdev_unlock(wdev);
757
758 kfree(ev);
759
760 spin_lock_irqsave(&wdev->event_lock, flags);
761 }
762 spin_unlock_irqrestore(&wdev->event_lock, flags);
763}
764
765void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
766{
767 struct wireless_dev *wdev;
768
769 ASSERT_RTNL();
770 ASSERT_RDEV_LOCK(rdev);
771
772 mutex_lock(&rdev->devlist_mtx);
773
774 list_for_each_entry(wdev, &rdev->netdev_list, list)
775 cfg80211_process_wdev_events(wdev);
776
777 mutex_unlock(&rdev->devlist_mtx);
778}
779
780int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
781 struct net_device *dev, enum nl80211_iftype ntype,
782 u32 *flags, struct vif_params *params)
783{
784 int err;
785 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
786
787 ASSERT_RDEV_LOCK(rdev);
788
789 /* don't support changing VLANs, you just re-create them */
790 if (otype == NL80211_IFTYPE_AP_VLAN)
791 return -EOPNOTSUPP;
792
793 if (!rdev->ops->change_virtual_intf ||
794 !(rdev->wiphy.interface_modes & (1 << ntype)))
795 return -EOPNOTSUPP;
796
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100797 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a2010-06-15 06:50:45 +0000798 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200799 (ntype == NL80211_IFTYPE_ADHOC ||
800 ntype == NL80211_IFTYPE_STATION ||
801 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100802 return -EBUSY;
803
Johannes Berg3d54d252009-08-21 14:51:05 +0200804 if (ntype != otype) {
Johannes Berg7527a782011-05-13 10:58:57 +0200805 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
806 ntype);
807 if (err)
808 return err;
809
Johannes Berg9bc383d2009-11-19 11:55:19 +0100810 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100811 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100812
Johannes Berg3d54d252009-08-21 14:51:05 +0200813 switch (otype) {
814 case NL80211_IFTYPE_ADHOC:
815 cfg80211_leave_ibss(rdev, dev, false);
816 break;
817 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200818 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200819 cfg80211_disconnect(rdev, dev,
820 WLAN_REASON_DEAUTH_LEAVING, true);
821 break;
822 case NL80211_IFTYPE_MESH_POINT:
823 /* mesh should be handled? */
824 break;
825 default:
826 break;
827 }
828
829 cfg80211_process_rdev_events(rdev);
830 }
831
832 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
833 ntype, flags, params);
834
835 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
836
Johannes Berg9bc383d2009-11-19 11:55:19 +0100837 if (!err && params && params->use_4addr != -1)
838 dev->ieee80211_ptr->use_4addr = params->use_4addr;
839
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100840 if (!err) {
841 dev->priv_flags &= ~IFF_DONT_BRIDGE;
842 switch (ntype) {
843 case NL80211_IFTYPE_STATION:
844 if (dev->ieee80211_ptr->use_4addr)
845 break;
846 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200847 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100848 case NL80211_IFTYPE_ADHOC:
849 dev->priv_flags |= IFF_DONT_BRIDGE;
850 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200851 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100852 case NL80211_IFTYPE_AP:
853 case NL80211_IFTYPE_AP_VLAN:
854 case NL80211_IFTYPE_WDS:
855 case NL80211_IFTYPE_MESH_POINT:
856 /* bridging OK */
857 break;
858 case NL80211_IFTYPE_MONITOR:
859 /* monitor can't bridge anyway */
860 break;
861 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200862 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100863 /* not happening */
864 break;
865 }
866 }
867
Johannes Berg3d54d252009-08-21 14:51:05 +0200868 return err;
869}
John W. Linville254416a2009-12-09 16:43:52 -0500870
871u16 cfg80211_calculate_bitrate(struct rate_info *rate)
872{
873 int modulation, streams, bitrate;
874
875 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
876 return rate->legacy;
877
878 /* the formula below does only work for MCS values smaller than 32 */
879 if (rate->mcs >= 32)
880 return 0;
881
882 modulation = rate->mcs & 7;
883 streams = (rate->mcs >> 3) + 1;
884
885 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
886 13500000 : 6500000;
887
888 if (modulation < 4)
889 bitrate *= (modulation + 1);
890 else if (modulation == 4)
891 bitrate *= (modulation + 2);
892 else
893 bitrate *= (modulation + 3);
894
895 bitrate *= streams;
896
897 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
898 bitrate = (bitrate / 9) * 10;
899
900 /* do NOT round down here */
901 return (bitrate + 50000) / 100000;
902}
Johannes Berg56d18932011-05-09 18:41:15 +0200903
904int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
905 u32 beacon_int)
906{
907 struct wireless_dev *wdev;
908 int res = 0;
909
910 if (!beacon_int)
911 return -EINVAL;
912
913 mutex_lock(&rdev->devlist_mtx);
914
915 list_for_each_entry(wdev, &rdev->netdev_list, list) {
916 if (!wdev->beacon_interval)
917 continue;
918 if (wdev->beacon_interval != beacon_int) {
919 res = -EINVAL;
920 break;
921 }
922 }
923
924 mutex_unlock(&rdev->devlist_mtx);
925
926 return res;
927}
Johannes Berg7527a782011-05-13 10:58:57 +0200928
929int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
930 struct wireless_dev *wdev,
931 enum nl80211_iftype iftype)
932{
933 struct wireless_dev *wdev_iter;
934 int num[NUM_NL80211_IFTYPES];
935 int total = 1;
936 int i, j;
937
938 ASSERT_RTNL();
939
940 /* Always allow software iftypes */
941 if (rdev->wiphy.software_iftypes & BIT(iftype))
942 return 0;
943
944 /*
945 * Drivers will gradually all set this flag, until all
946 * have it we only enforce for those that set it.
947 */
948 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
949 return 0;
950
951 memset(num, 0, sizeof(num));
952
953 num[iftype] = 1;
954
955 mutex_lock(&rdev->devlist_mtx);
956 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
957 if (wdev_iter == wdev)
958 continue;
959 if (!netif_running(wdev_iter->netdev))
960 continue;
961
962 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
963 continue;
964
965 num[wdev_iter->iftype]++;
966 total++;
967 }
968 mutex_unlock(&rdev->devlist_mtx);
969
970 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
971 const struct ieee80211_iface_combination *c;
972 struct ieee80211_iface_limit *limits;
973
974 c = &rdev->wiphy.iface_combinations[i];
975
976 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
977 GFP_KERNEL);
978 if (!limits)
979 return -ENOMEM;
980 if (total > c->max_interfaces)
981 goto cont;
982
983 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
984 if (rdev->wiphy.software_iftypes & BIT(iftype))
985 continue;
986 for (j = 0; j < c->n_limits; j++) {
987 if (!(limits[j].types & iftype))
988 continue;
989 if (limits[j].max < num[iftype])
990 goto cont;
991 limits[j].max -= num[iftype];
992 }
993 }
994 /* yay, it fits */
995 kfree(limits);
996 return 0;
997 cont:
998 kfree(limits);
999 }
1000
1001 return -EBUSY;
1002}
Johannes Berg34850ab2011-07-18 18:08:35 +02001003
1004int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1005 const u8 *rates, unsigned int n_rates,
1006 u32 *mask)
1007{
1008 int i, j;
1009
Johannes Berga401d2b2011-07-20 00:52:16 +02001010 if (!sband)
1011 return -EINVAL;
1012
Johannes Berg34850ab2011-07-18 18:08:35 +02001013 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1014 return -EINVAL;
1015
1016 *mask = 0;
1017
1018 for (i = 0; i < n_rates; i++) {
1019 int rate = (rates[i] & 0x7f) * 5;
1020 bool found = false;
1021
1022 for (j = 0; j < sband->n_bitrates; j++) {
1023 if (sband->bitrates[j].bitrate == rate) {
1024 found = true;
1025 *mask |= BIT(j);
1026 break;
1027 }
1028 }
1029 if (!found)
1030 return -EINVAL;
1031 }
1032
1033 /*
1034 * mask must have at least one bit set here since we
1035 * didn't accept a 0-length rates array nor allowed
1036 * entries in the array that didn't exist
1037 */
1038
1039 return 0;
1040}
Johannes Berg11a2a352011-11-21 11:09:22 +01001041
1042/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1043/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1044const unsigned char rfc1042_header[] __aligned(2) =
1045 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1046EXPORT_SYMBOL(rfc1042_header);
1047
1048/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1049const unsigned char bridge_tunnel_header[] __aligned(2) =
1050 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1051EXPORT_SYMBOL(bridge_tunnel_header);