blob: b50e60e4485ee79153875e935e74485fcd5e9598 [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
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300153bool 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;
209 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300210 /*
211 * We don't know anything about this algorithm,
212 * allow using it -- but the driver must check
213 * all parameters! We still check below whether
214 * or not the driver supports this algorithm,
215 * of course.
216 */
217 break;
Johannes Berg08645122009-05-11 13:54:58 +0200218 }
219
Jouni Malinen9f26a952009-05-15 12:38:32 +0300220 if (params->seq) {
221 switch (params->cipher) {
222 case WLAN_CIPHER_SUITE_WEP40:
223 case WLAN_CIPHER_SUITE_WEP104:
224 /* These ciphers do not use key sequence */
225 return -EINVAL;
226 case WLAN_CIPHER_SUITE_TKIP:
227 case WLAN_CIPHER_SUITE_CCMP:
228 case WLAN_CIPHER_SUITE_AES_CMAC:
229 if (params->seq_len != 6)
230 return -EINVAL;
231 break;
232 }
233 }
234
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300235 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200236 return -EINVAL;
237
Johannes Berg08645122009-05-11 13:54:58 +0200238 return 0;
239}
Zhu Yie31a16d2009-05-21 21:47:03 +0800240
Johannes Berg633adf12010-08-12 14:49:58 +0200241unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800242{
243 unsigned int hdrlen = 24;
244
245 if (ieee80211_is_data(fc)) {
246 if (ieee80211_has_a4(fc))
247 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200248 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800249 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200250 if (ieee80211_has_order(fc))
251 hdrlen += IEEE80211_HT_CTL_LEN;
252 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800253 goto out;
254 }
255
256 if (ieee80211_is_ctl(fc)) {
257 /*
258 * ACK and CTS are 10 bytes, all others 16. To see how
259 * to get this condition consider
260 * subtype mask: 0b0000000011110000 (0x00F0)
261 * ACK subtype: 0b0000000011010000 (0x00D0)
262 * CTS subtype: 0b0000000011000000 (0x00C0)
263 * bits that matter: ^^^ (0x00E0)
264 * value of those: 0b0000000011000000 (0x00C0)
265 */
266 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
267 hdrlen = 10;
268 else
269 hdrlen = 16;
270 }
271out:
272 return hdrlen;
273}
274EXPORT_SYMBOL(ieee80211_hdrlen);
275
276unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
277{
278 const struct ieee80211_hdr *hdr =
279 (const struct ieee80211_hdr *)skb->data;
280 unsigned int hdrlen;
281
282 if (unlikely(skb->len < 10))
283 return 0;
284 hdrlen = ieee80211_hdrlen(hdr->frame_control);
285 if (unlikely(hdrlen > skb->len))
286 return 0;
287 return hdrlen;
288}
289EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
290
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400291static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800292{
293 int ae = meshhdr->flags & MESH_FLAGS_AE;
294 /* 7.1.3.5a.2 */
295 switch (ae) {
296 case 0:
297 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700298 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800299 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700300 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800301 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700302 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800303 return 24;
304 default:
305 return 6;
306 }
307}
308
Zhu Yieaf85ca2009-12-01 10:18:37 +0800309int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800310 enum nl80211_iftype iftype)
311{
312 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
313 u16 hdrlen, ethertype;
314 u8 *payload;
315 u8 dst[ETH_ALEN];
316 u8 src[ETH_ALEN] __aligned(2);
317
318 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
319 return -1;
320
321 hdrlen = ieee80211_hdrlen(hdr->frame_control);
322
323 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
324 * header
325 * IEEE 802.11 address fields:
326 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
327 * 0 0 DA SA BSSID n/a
328 * 0 1 DA BSSID SA n/a
329 * 1 0 BSSID SA DA n/a
330 * 1 1 RA TA DA SA
331 */
332 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
333 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
334
335 switch (hdr->frame_control &
336 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
337 case cpu_to_le16(IEEE80211_FCTL_TODS):
338 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200339 iftype != NL80211_IFTYPE_AP_VLAN &&
340 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800341 return -1;
342 break;
343 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
344 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543e2009-11-10 20:10:05 +0100345 iftype != NL80211_IFTYPE_MESH_POINT &&
346 iftype != NL80211_IFTYPE_AP_VLAN &&
347 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800348 return -1;
349 if (iftype == NL80211_IFTYPE_MESH_POINT) {
350 struct ieee80211s_hdr *meshdr =
351 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800352 /* make sure meshdr->flags is on the linear part */
353 if (!pskb_may_pull(skb, hdrlen + 1))
354 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800355 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800356 skb_copy_bits(skb, hdrlen +
357 offsetof(struct ieee80211s_hdr, eaddr1),
358 dst, ETH_ALEN);
359 skb_copy_bits(skb, hdrlen +
360 offsetof(struct ieee80211s_hdr, eaddr2),
361 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800362 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800363 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800364 }
365 break;
366 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700367 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200368 iftype != NL80211_IFTYPE_P2P_CLIENT &&
369 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800370 (is_multicast_ether_addr(dst) &&
371 !compare_ether_addr(src, addr)))
372 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700373 if (iftype == NL80211_IFTYPE_MESH_POINT) {
374 struct ieee80211s_hdr *meshdr =
375 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800376 /* make sure meshdr->flags is on the linear part */
377 if (!pskb_may_pull(skb, hdrlen + 1))
378 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700379 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800380 skb_copy_bits(skb, hdrlen +
381 offsetof(struct ieee80211s_hdr, eaddr1),
382 src, ETH_ALEN);
383 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700384 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800385 break;
386 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300387 if (iftype != NL80211_IFTYPE_ADHOC &&
388 iftype != NL80211_IFTYPE_STATION)
389 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800390 break;
391 }
392
Zhu Yie3cf8b32010-03-29 17:35:07 +0800393 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800394 return -1;
395
396 payload = skb->data + hdrlen;
397 ethertype = (payload[6] << 8) | payload[7];
398
399 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
400 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
401 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
402 /* remove RFC1042 or Bridge-Tunnel encapsulation and
403 * replace EtherType */
404 skb_pull(skb, hdrlen + 6);
405 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
406 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
407 } else {
408 struct ethhdr *ehdr;
409 __be16 len;
410
411 skb_pull(skb, hdrlen);
412 len = htons(skb->len);
413 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
414 memcpy(ehdr->h_dest, dst, ETH_ALEN);
415 memcpy(ehdr->h_source, src, ETH_ALEN);
416 ehdr->h_proto = len;
417 }
418 return 0;
419}
420EXPORT_SYMBOL(ieee80211_data_to_8023);
421
Zhu Yieaf85ca2009-12-01 10:18:37 +0800422int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800423 enum nl80211_iftype iftype, u8 *bssid, bool qos)
424{
425 struct ieee80211_hdr hdr;
426 u16 hdrlen, ethertype;
427 __le16 fc;
428 const u8 *encaps_data;
429 int encaps_len, skip_header_bytes;
430 int nh_pos, h_pos;
431 int head_need;
432
433 if (unlikely(skb->len < ETH_HLEN))
434 return -EINVAL;
435
436 nh_pos = skb_network_header(skb) - skb->data;
437 h_pos = skb_transport_header(skb) - skb->data;
438
439 /* convert Ethernet header to proper 802.11 header (based on
440 * operation mode) */
441 ethertype = (skb->data[12] << 8) | skb->data[13];
442 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
443
444 switch (iftype) {
445 case NL80211_IFTYPE_AP:
446 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200447 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800448 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
449 /* DA BSSID SA */
450 memcpy(hdr.addr1, skb->data, ETH_ALEN);
451 memcpy(hdr.addr2, addr, ETH_ALEN);
452 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
453 hdrlen = 24;
454 break;
455 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200456 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800457 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
458 /* BSSID SA DA */
459 memcpy(hdr.addr1, bssid, ETH_ALEN);
460 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
461 memcpy(hdr.addr3, skb->data, ETH_ALEN);
462 hdrlen = 24;
463 break;
464 case NL80211_IFTYPE_ADHOC:
465 /* DA SA BSSID */
466 memcpy(hdr.addr1, skb->data, ETH_ALEN);
467 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
468 memcpy(hdr.addr3, bssid, ETH_ALEN);
469 hdrlen = 24;
470 break;
471 default:
472 return -EOPNOTSUPP;
473 }
474
475 if (qos) {
476 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
477 hdrlen += 2;
478 }
479
480 hdr.frame_control = fc;
481 hdr.duration_id = 0;
482 hdr.seq_ctrl = 0;
483
484 skip_header_bytes = ETH_HLEN;
485 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
486 encaps_data = bridge_tunnel_header;
487 encaps_len = sizeof(bridge_tunnel_header);
488 skip_header_bytes -= 2;
489 } else if (ethertype > 0x600) {
490 encaps_data = rfc1042_header;
491 encaps_len = sizeof(rfc1042_header);
492 skip_header_bytes -= 2;
493 } else {
494 encaps_data = NULL;
495 encaps_len = 0;
496 }
497
498 skb_pull(skb, skip_header_bytes);
499 nh_pos -= skip_header_bytes;
500 h_pos -= skip_header_bytes;
501
502 head_need = hdrlen + encaps_len - skb_headroom(skb);
503
504 if (head_need > 0 || skb_cloned(skb)) {
505 head_need = max(head_need, 0);
506 if (head_need)
507 skb_orphan(skb);
508
Joe Perches24616152011-08-29 14:17:41 -0700509 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
Zhu Yie31a16d2009-05-21 21:47:03 +0800510 return -ENOMEM;
Joe Perches24616152011-08-29 14:17:41 -0700511
Zhu Yie31a16d2009-05-21 21:47:03 +0800512 skb->truesize += head_need;
513 }
514
515 if (encaps_data) {
516 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
517 nh_pos += encaps_len;
518 h_pos += encaps_len;
519 }
520
521 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
522
523 nh_pos += hdrlen;
524 h_pos += hdrlen;
525
526 /* Update skb pointers to various headers since this modified frame
527 * is going to go through Linux networking code that may potentially
528 * need things like pointer to IP header. */
529 skb_set_mac_header(skb, 0);
530 skb_set_network_header(skb, nh_pos);
531 skb_set_transport_header(skb, h_pos);
532
533 return 0;
534}
535EXPORT_SYMBOL(ieee80211_data_from_8023);
536
Zhu Yieaf85ca2009-12-01 10:18:37 +0800537
538void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
539 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700540 const unsigned int extra_headroom,
541 bool has_80211_header)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800542{
543 struct sk_buff *frame = NULL;
544 u16 ethertype;
545 u8 *payload;
546 const struct ethhdr *eth;
547 int remaining, err;
548 u8 dst[ETH_ALEN], src[ETH_ALEN];
549
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700550 if (has_80211_header) {
551 err = ieee80211_data_to_8023(skb, addr, iftype);
552 if (err)
553 goto out;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800554
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700555 /* skip the wrapping header */
556 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
557 if (!eth)
558 goto out;
559 } else {
560 eth = (struct ethhdr *) skb->data;
561 }
Zhu Yieaf85ca2009-12-01 10:18:37 +0800562
563 while (skb != frame) {
564 u8 padding;
565 __be16 len = eth->h_proto;
566 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
567
568 remaining = skb->len;
569 memcpy(dst, eth->h_dest, ETH_ALEN);
570 memcpy(src, eth->h_source, ETH_ALEN);
571
572 padding = (4 - subframe_len) & 0x3;
573 /* the last MSDU has no padding */
574 if (subframe_len > remaining)
575 goto purge;
576
577 skb_pull(skb, sizeof(struct ethhdr));
578 /* reuse skb for the last subframe */
579 if (remaining <= subframe_len + padding)
580 frame = skb;
581 else {
582 unsigned int hlen = ALIGN(extra_headroom, 4);
583 /*
584 * Allocate and reserve two bytes more for payload
585 * alignment since sizeof(struct ethhdr) is 14.
586 */
587 frame = dev_alloc_skb(hlen + subframe_len + 2);
588 if (!frame)
589 goto purge;
590
591 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
592 memcpy(skb_put(frame, ntohs(len)), skb->data,
593 ntohs(len));
594
595 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
596 padding);
597 if (!eth) {
598 dev_kfree_skb(frame);
599 goto purge;
600 }
601 }
602
603 skb_reset_network_header(frame);
604 frame->dev = skb->dev;
605 frame->priority = skb->priority;
606
607 payload = frame->data;
608 ethertype = (payload[6] << 8) | payload[7];
609
610 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
611 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
612 compare_ether_addr(payload,
613 bridge_tunnel_header) == 0)) {
614 /* remove RFC1042 or Bridge-Tunnel
615 * encapsulation and replace EtherType */
616 skb_pull(frame, 6);
617 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
618 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
619 } else {
620 memcpy(skb_push(frame, sizeof(__be16)), &len,
621 sizeof(__be16));
622 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
623 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
624 }
625 __skb_queue_tail(list, frame);
626 }
627
628 return;
629
630 purge:
631 __skb_queue_purge(list);
632 out:
633 dev_kfree_skb(skb);
634}
635EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
636
Zhu Yie31a16d2009-05-21 21:47:03 +0800637/* Given a data frame determine the 802.1p/1d tag to use. */
638unsigned int cfg80211_classify8021d(struct sk_buff *skb)
639{
640 unsigned int dscp;
641
642 /* skb->priority values from 256->263 are magic values to
643 * directly indicate a specific 802.1d priority. This is used
644 * to allow 802.1d priority to be passed directly in from VLAN
645 * tags, etc.
646 */
647 if (skb->priority >= 256 && skb->priority <= 263)
648 return skb->priority - 256;
649
650 switch (skb->protocol) {
651 case htons(ETH_P_IP):
652 dscp = ip_hdr(skb)->tos & 0xfc;
653 break;
654 default:
655 return 0;
656 }
657
658 return dscp >> 5;
659}
660EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200661
662const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
663{
664 u8 *end, *pos;
665
666 pos = bss->information_elements;
667 if (pos == NULL)
668 return NULL;
669 end = pos + bss->len_information_elements;
670
671 while (pos + 1 < end) {
672 if (pos + 2 + pos[1] > end)
673 break;
674 if (pos[0] == ie)
675 return pos;
676 pos += 2 + pos[1];
677 }
678
679 return NULL;
680}
681EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200682
683void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
684{
685 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
686 struct net_device *dev = wdev->netdev;
687 int i;
688
689 if (!wdev->connect_keys)
690 return;
691
692 for (i = 0; i < 6; i++) {
693 if (!wdev->connect_keys->params[i].cipher)
694 continue;
Johannes Berge31b8212010-10-05 19:39:30 +0200695 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800696 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800697 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800698 continue;
699 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200700 if (wdev->connect_keys->def == i)
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100701 if (rdev->ops->set_default_key(wdev->wiphy, dev,
702 i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800703 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800704 continue;
705 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200706 if (wdev->connect_keys->defmgmt == i)
707 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800708 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200709 }
710
711 kfree(wdev->connect_keys);
712 wdev->connect_keys = NULL;
713}
Johannes Berg3d54d252009-08-21 14:51:05 +0200714
715static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
716{
717 struct cfg80211_event *ev;
718 unsigned long flags;
719 const u8 *bssid = NULL;
720
721 spin_lock_irqsave(&wdev->event_lock, flags);
722 while (!list_empty(&wdev->event_list)) {
723 ev = list_first_entry(&wdev->event_list,
724 struct cfg80211_event, list);
725 list_del(&ev->list);
726 spin_unlock_irqrestore(&wdev->event_lock, flags);
727
728 wdev_lock(wdev);
729 switch (ev->type) {
730 case EVENT_CONNECT_RESULT:
731 if (!is_zero_ether_addr(ev->cr.bssid))
732 bssid = ev->cr.bssid;
733 __cfg80211_connect_result(
734 wdev->netdev, bssid,
735 ev->cr.req_ie, ev->cr.req_ie_len,
736 ev->cr.resp_ie, ev->cr.resp_ie_len,
737 ev->cr.status,
738 ev->cr.status == WLAN_STATUS_SUCCESS,
739 NULL);
740 break;
741 case EVENT_ROAMED:
Jouni Malinened9d0102011-05-16 19:40:15 +0300742 __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid,
Johannes Berg3d54d252009-08-21 14:51:05 +0200743 ev->rm.req_ie, ev->rm.req_ie_len,
744 ev->rm.resp_ie, ev->rm.resp_ie_len);
745 break;
746 case EVENT_DISCONNECTED:
747 __cfg80211_disconnected(wdev->netdev,
748 ev->dc.ie, ev->dc.ie_len,
749 ev->dc.reason, true);
750 break;
751 case EVENT_IBSS_JOINED:
752 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
753 break;
754 }
755 wdev_unlock(wdev);
756
757 kfree(ev);
758
759 spin_lock_irqsave(&wdev->event_lock, flags);
760 }
761 spin_unlock_irqrestore(&wdev->event_lock, flags);
762}
763
764void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
765{
766 struct wireless_dev *wdev;
767
768 ASSERT_RTNL();
769 ASSERT_RDEV_LOCK(rdev);
770
771 mutex_lock(&rdev->devlist_mtx);
772
773 list_for_each_entry(wdev, &rdev->netdev_list, list)
774 cfg80211_process_wdev_events(wdev);
775
776 mutex_unlock(&rdev->devlist_mtx);
777}
778
779int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
780 struct net_device *dev, enum nl80211_iftype ntype,
781 u32 *flags, struct vif_params *params)
782{
783 int err;
784 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
785
786 ASSERT_RDEV_LOCK(rdev);
787
788 /* don't support changing VLANs, you just re-create them */
789 if (otype == NL80211_IFTYPE_AP_VLAN)
790 return -EOPNOTSUPP;
791
792 if (!rdev->ops->change_virtual_intf ||
793 !(rdev->wiphy.interface_modes & (1 << ntype)))
794 return -EOPNOTSUPP;
795
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100796 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a2010-06-15 06:50:45 +0000797 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200798 (ntype == NL80211_IFTYPE_ADHOC ||
799 ntype == NL80211_IFTYPE_STATION ||
800 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100801 return -EBUSY;
802
Johannes Berg3d54d252009-08-21 14:51:05 +0200803 if (ntype != otype) {
Johannes Berg7527a782011-05-13 10:58:57 +0200804 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
805 ntype);
806 if (err)
807 return err;
808
Johannes Berg9bc383d2009-11-19 11:55:19 +0100809 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100810 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100811
Johannes Berg3d54d252009-08-21 14:51:05 +0200812 switch (otype) {
813 case NL80211_IFTYPE_ADHOC:
814 cfg80211_leave_ibss(rdev, dev, false);
815 break;
816 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200817 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200818 cfg80211_disconnect(rdev, dev,
819 WLAN_REASON_DEAUTH_LEAVING, true);
820 break;
821 case NL80211_IFTYPE_MESH_POINT:
822 /* mesh should be handled? */
823 break;
824 default:
825 break;
826 }
827
828 cfg80211_process_rdev_events(rdev);
829 }
830
831 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
832 ntype, flags, params);
833
834 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
835
Johannes Berg9bc383d2009-11-19 11:55:19 +0100836 if (!err && params && params->use_4addr != -1)
837 dev->ieee80211_ptr->use_4addr = params->use_4addr;
838
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100839 if (!err) {
840 dev->priv_flags &= ~IFF_DONT_BRIDGE;
841 switch (ntype) {
842 case NL80211_IFTYPE_STATION:
843 if (dev->ieee80211_ptr->use_4addr)
844 break;
845 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200846 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100847 case NL80211_IFTYPE_ADHOC:
848 dev->priv_flags |= IFF_DONT_BRIDGE;
849 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200850 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100851 case NL80211_IFTYPE_AP:
852 case NL80211_IFTYPE_AP_VLAN:
853 case NL80211_IFTYPE_WDS:
854 case NL80211_IFTYPE_MESH_POINT:
855 /* bridging OK */
856 break;
857 case NL80211_IFTYPE_MONITOR:
858 /* monitor can't bridge anyway */
859 break;
860 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200861 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100862 /* not happening */
863 break;
864 }
865 }
866
Johannes Berg3d54d252009-08-21 14:51:05 +0200867 return err;
868}
John W. Linville254416a2009-12-09 16:43:52 -0500869
870u16 cfg80211_calculate_bitrate(struct rate_info *rate)
871{
872 int modulation, streams, bitrate;
873
874 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
875 return rate->legacy;
876
877 /* the formula below does only work for MCS values smaller than 32 */
878 if (rate->mcs >= 32)
879 return 0;
880
881 modulation = rate->mcs & 7;
882 streams = (rate->mcs >> 3) + 1;
883
884 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
885 13500000 : 6500000;
886
887 if (modulation < 4)
888 bitrate *= (modulation + 1);
889 else if (modulation == 4)
890 bitrate *= (modulation + 2);
891 else
892 bitrate *= (modulation + 3);
893
894 bitrate *= streams;
895
896 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
897 bitrate = (bitrate / 9) * 10;
898
899 /* do NOT round down here */
900 return (bitrate + 50000) / 100000;
901}
Johannes Berg56d18932011-05-09 18:41:15 +0200902
903int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
904 u32 beacon_int)
905{
906 struct wireless_dev *wdev;
907 int res = 0;
908
909 if (!beacon_int)
910 return -EINVAL;
911
912 mutex_lock(&rdev->devlist_mtx);
913
914 list_for_each_entry(wdev, &rdev->netdev_list, list) {
915 if (!wdev->beacon_interval)
916 continue;
917 if (wdev->beacon_interval != beacon_int) {
918 res = -EINVAL;
919 break;
920 }
921 }
922
923 mutex_unlock(&rdev->devlist_mtx);
924
925 return res;
926}
Johannes Berg7527a782011-05-13 10:58:57 +0200927
928int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
929 struct wireless_dev *wdev,
930 enum nl80211_iftype iftype)
931{
932 struct wireless_dev *wdev_iter;
933 int num[NUM_NL80211_IFTYPES];
934 int total = 1;
935 int i, j;
936
937 ASSERT_RTNL();
938
939 /* Always allow software iftypes */
940 if (rdev->wiphy.software_iftypes & BIT(iftype))
941 return 0;
942
943 /*
944 * Drivers will gradually all set this flag, until all
945 * have it we only enforce for those that set it.
946 */
947 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
948 return 0;
949
950 memset(num, 0, sizeof(num));
951
952 num[iftype] = 1;
953
954 mutex_lock(&rdev->devlist_mtx);
955 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
956 if (wdev_iter == wdev)
957 continue;
958 if (!netif_running(wdev_iter->netdev))
959 continue;
960
961 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
962 continue;
963
964 num[wdev_iter->iftype]++;
965 total++;
966 }
967 mutex_unlock(&rdev->devlist_mtx);
968
969 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
970 const struct ieee80211_iface_combination *c;
971 struct ieee80211_iface_limit *limits;
972
973 c = &rdev->wiphy.iface_combinations[i];
974
975 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
976 GFP_KERNEL);
977 if (!limits)
978 return -ENOMEM;
979 if (total > c->max_interfaces)
980 goto cont;
981
982 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
983 if (rdev->wiphy.software_iftypes & BIT(iftype))
984 continue;
985 for (j = 0; j < c->n_limits; j++) {
986 if (!(limits[j].types & iftype))
987 continue;
988 if (limits[j].max < num[iftype])
989 goto cont;
990 limits[j].max -= num[iftype];
991 }
992 }
993 /* yay, it fits */
994 kfree(limits);
995 return 0;
996 cont:
997 kfree(limits);
998 }
999
1000 return -EBUSY;
1001}
Johannes Berg34850ab2011-07-18 18:08:35 +02001002
1003int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1004 const u8 *rates, unsigned int n_rates,
1005 u32 *mask)
1006{
1007 int i, j;
1008
Johannes Berga401d2b2011-07-20 00:52:16 +02001009 if (!sband)
1010 return -EINVAL;
1011
Johannes Berg34850ab2011-07-18 18:08:35 +02001012 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1013 return -EINVAL;
1014
1015 *mask = 0;
1016
1017 for (i = 0; i < n_rates; i++) {
1018 int rate = (rates[i] & 0x7f) * 5;
1019 bool found = false;
1020
1021 for (j = 0; j < sband->n_bitrates; j++) {
1022 if (sband->bitrates[j].bitrate == rate) {
1023 found = true;
1024 *mask |= BIT(j);
1025 break;
1026 }
1027 }
1028 if (!found)
1029 return -EINVAL;
1030 }
1031
1032 /*
1033 * mask must have at least one bit set here since we
1034 * didn't accept a 0-length rates array nor allowed
1035 * entries in the array that didn't exist
1036 */
1037
1038 return 0;
1039}
Johannes Berg11a2a352011-11-21 11:09:22 +01001040
1041/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1042/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1043const unsigned char rfc1042_header[] __aligned(2) =
1044 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1045EXPORT_SYMBOL(rfc1042_header);
1046
1047/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1048const unsigned char bridge_tunnel_header[] __aligned(2) =
1049 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1050EXPORT_SYMBOL(bridge_tunnel_header);