blob: 343f13c1d31d66f576708ccc6d0ff4d0f047102b [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>
Dave Tähtb1565792011-12-22 12:55:08 -080012#include <net/dsfield.h>
Johannes Berg8318d782008-01-24 19:38:38 +010013#include "core.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030014#include "rdev-ops.h"
15
Johannes Berg8318d782008-01-24 19:38:38 +010016
Johannes Bergbd815252008-10-29 20:00:45 +010017struct ieee80211_rate *
18ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010019 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010020{
21 struct ieee80211_rate *result = &sband->bitrates[0];
22 int i;
23
24 for (i = 0; i < sband->n_bitrates; i++) {
25 if (!(basic_rates & BIT(i)))
26 continue;
27 if (sband->bitrates[i].bitrate > bitrate)
28 continue;
29 result = &sband->bitrates[i];
30 }
31
32 return result;
33}
34EXPORT_SYMBOL(ieee80211_get_response_rate);
35
Bruno Randolf59eb21a2011-01-17 13:37:28 +090036int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010037{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090038 /* see 802.11 17.3.8.3.2 and Annex J
39 * there are overlapping channel numbers in 5GHz and 2GHz bands */
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030040 if (chan <= 0)
41 return 0; /* not supported */
42 switch (band) {
43 case IEEE80211_BAND_2GHZ:
Bruno Randolf59eb21a2011-01-17 13:37:28 +090044 if (chan == 14)
45 return 2484;
46 else if (chan < 14)
47 return 2407 + chan * 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030048 break;
49 case IEEE80211_BAND_5GHZ:
50 if (chan >= 182 && chan <= 196)
51 return 4000 + chan * 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090052 else
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030053 return 5000 + chan * 5;
54 break;
55 case IEEE80211_BAND_60GHZ:
56 if (chan < 5)
57 return 56160 + chan * 2160;
58 break;
59 default:
60 ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090061 }
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030062 return 0; /* not supported */
Johannes Berg8318d782008-01-24 19:38:38 +010063}
64EXPORT_SYMBOL(ieee80211_channel_to_frequency);
65
66int ieee80211_frequency_to_channel(int freq)
67{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090068 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +010069 if (freq == 2484)
70 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090071 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +010072 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090073 else if (freq >= 4910 && freq <= 4980)
74 return (freq - 4000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030075 else if (freq <= 45000) /* DMG band lower limit */
Bruno Randolf59eb21a2011-01-17 13:37:28 +090076 return (freq - 5000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030077 else if (freq >= 58320 && freq <= 64800)
78 return (freq - 56160) / 2160;
79 else
80 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +010081}
82EXPORT_SYMBOL(ieee80211_frequency_to_channel);
83
Johannes Berg6c507cd2008-03-26 14:14:55 +010084struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
85 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +010086{
87 enum ieee80211_band band;
88 struct ieee80211_supported_band *sband;
89 int i;
90
91 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
92 sband = wiphy->bands[band];
93
94 if (!sband)
95 continue;
96
97 for (i = 0; i < sband->n_channels; i++) {
98 if (sband->channels[i].center_freq == freq)
99 return &sband->channels[i];
100 }
101 }
102
103 return NULL;
104}
Johannes Berg6c507cd2008-03-26 14:14:55 +0100105EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +0100106
Johannes Berg8318d782008-01-24 19:38:38 +0100107static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
108 enum ieee80211_band band)
109{
110 int i, want;
111
112 switch (band) {
113 case IEEE80211_BAND_5GHZ:
114 want = 3;
115 for (i = 0; i < sband->n_bitrates; i++) {
116 if (sband->bitrates[i].bitrate == 60 ||
117 sband->bitrates[i].bitrate == 120 ||
118 sband->bitrates[i].bitrate == 240) {
119 sband->bitrates[i].flags |=
120 IEEE80211_RATE_MANDATORY_A;
121 want--;
122 }
123 }
124 WARN_ON(want);
125 break;
126 case IEEE80211_BAND_2GHZ:
127 want = 7;
128 for (i = 0; i < sband->n_bitrates; i++) {
129 if (sband->bitrates[i].bitrate == 10) {
130 sband->bitrates[i].flags |=
131 IEEE80211_RATE_MANDATORY_B |
132 IEEE80211_RATE_MANDATORY_G;
133 want--;
134 }
135
136 if (sband->bitrates[i].bitrate == 20 ||
137 sband->bitrates[i].bitrate == 55 ||
138 sband->bitrates[i].bitrate == 110 ||
139 sband->bitrates[i].bitrate == 60 ||
140 sband->bitrates[i].bitrate == 120 ||
141 sband->bitrates[i].bitrate == 240) {
142 sband->bitrates[i].flags |=
143 IEEE80211_RATE_MANDATORY_G;
144 want--;
145 }
146
Johannes Bergaac09fb2008-01-30 17:36:10 +0100147 if (sband->bitrates[i].bitrate != 10 &&
148 sband->bitrates[i].bitrate != 20 &&
149 sband->bitrates[i].bitrate != 55 &&
150 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100151 sband->bitrates[i].flags |=
152 IEEE80211_RATE_ERP_G;
153 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100154 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100155 break;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300156 case IEEE80211_BAND_60GHZ:
157 /* check for mandatory HT MCS 1..4 */
158 WARN_ON(!sband->ht_cap.ht_supported);
159 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
160 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100161 case IEEE80211_NUM_BANDS:
162 WARN_ON(1);
163 break;
164 }
165}
166
167void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
168{
169 enum ieee80211_band band;
170
171 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
172 if (wiphy->bands[band])
173 set_mandatory_flags_band(wiphy->bands[band], band);
174}
Johannes Berg08645122009-05-11 13:54:58 +0200175
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300176bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
177{
178 int i;
179 for (i = 0; i < wiphy->n_cipher_suites; i++)
180 if (cipher == wiphy->cipher_suites[i])
181 return true;
182 return false;
183}
184
Johannes Bergfffd0932009-07-08 14:22:54 +0200185int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
186 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200187 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200188{
189 if (key_idx > 5)
190 return -EINVAL;
191
Johannes Berge31b8212010-10-05 19:39:30 +0200192 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
193 return -EINVAL;
194
195 if (pairwise && !mac_addr)
196 return -EINVAL;
197
Johannes Berg08645122009-05-11 13:54:58 +0200198 /*
199 * Disallow pairwise keys with non-zero index unless it's WEP
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200200 * or a vendor specific cipher (because current deployments use
201 * pairwise WEP keys with non-zero indices and for vendor specific
202 * ciphers this should be validated in the driver or hardware level
203 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200204 */
Johannes Berge31b8212010-10-05 19:39:30 +0200205 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200206 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
207 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
208 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200209 return -EINVAL;
210
Johannes Berg08645122009-05-11 13:54:58 +0200211 switch (params->cipher) {
212 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200213 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200214 return -EINVAL;
215 break;
216 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200217 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200218 return -EINVAL;
219 break;
220 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200221 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200222 return -EINVAL;
223 break;
224 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200225 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200226 return -EINVAL;
227 break;
228 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200229 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200230 return -EINVAL;
231 break;
232 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300233 /*
234 * We don't know anything about this algorithm,
235 * allow using it -- but the driver must check
236 * all parameters! We still check below whether
237 * or not the driver supports this algorithm,
238 * of course.
239 */
240 break;
Johannes Berg08645122009-05-11 13:54:58 +0200241 }
242
Jouni Malinen9f26a952009-05-15 12:38:32 +0300243 if (params->seq) {
244 switch (params->cipher) {
245 case WLAN_CIPHER_SUITE_WEP40:
246 case WLAN_CIPHER_SUITE_WEP104:
247 /* These ciphers do not use key sequence */
248 return -EINVAL;
249 case WLAN_CIPHER_SUITE_TKIP:
250 case WLAN_CIPHER_SUITE_CCMP:
251 case WLAN_CIPHER_SUITE_AES_CMAC:
252 if (params->seq_len != 6)
253 return -EINVAL;
254 break;
255 }
256 }
257
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300258 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200259 return -EINVAL;
260
Johannes Berg08645122009-05-11 13:54:58 +0200261 return 0;
262}
Zhu Yie31a16d2009-05-21 21:47:03 +0800263
Johannes Berg633adf12010-08-12 14:49:58 +0200264unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800265{
266 unsigned int hdrlen = 24;
267
268 if (ieee80211_is_data(fc)) {
269 if (ieee80211_has_a4(fc))
270 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200271 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800272 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200273 if (ieee80211_has_order(fc))
274 hdrlen += IEEE80211_HT_CTL_LEN;
275 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800276 goto out;
277 }
278
279 if (ieee80211_is_ctl(fc)) {
280 /*
281 * ACK and CTS are 10 bytes, all others 16. To see how
282 * to get this condition consider
283 * subtype mask: 0b0000000011110000 (0x00F0)
284 * ACK subtype: 0b0000000011010000 (0x00D0)
285 * CTS subtype: 0b0000000011000000 (0x00C0)
286 * bits that matter: ^^^ (0x00E0)
287 * value of those: 0b0000000011000000 (0x00C0)
288 */
289 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
290 hdrlen = 10;
291 else
292 hdrlen = 16;
293 }
294out:
295 return hdrlen;
296}
297EXPORT_SYMBOL(ieee80211_hdrlen);
298
299unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
300{
301 const struct ieee80211_hdr *hdr =
302 (const struct ieee80211_hdr *)skb->data;
303 unsigned int hdrlen;
304
305 if (unlikely(skb->len < 10))
306 return 0;
307 hdrlen = ieee80211_hdrlen(hdr->frame_control);
308 if (unlikely(hdrlen > skb->len))
309 return 0;
310 return hdrlen;
311}
312EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
313
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400314static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800315{
316 int ae = meshhdr->flags & MESH_FLAGS_AE;
317 /* 7.1.3.5a.2 */
318 switch (ae) {
319 case 0:
320 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700321 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800322 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700323 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800324 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700325 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800326 return 24;
327 default:
328 return 6;
329 }
330}
331
Zhu Yieaf85ca2009-12-01 10:18:37 +0800332int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800333 enum nl80211_iftype iftype)
334{
335 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
336 u16 hdrlen, ethertype;
337 u8 *payload;
338 u8 dst[ETH_ALEN];
339 u8 src[ETH_ALEN] __aligned(2);
340
341 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
342 return -1;
343
344 hdrlen = ieee80211_hdrlen(hdr->frame_control);
345
346 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
347 * header
348 * IEEE 802.11 address fields:
349 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
350 * 0 0 DA SA BSSID n/a
351 * 0 1 DA BSSID SA n/a
352 * 1 0 BSSID SA DA n/a
353 * 1 1 RA TA DA SA
354 */
355 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
356 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
357
358 switch (hdr->frame_control &
359 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
360 case cpu_to_le16(IEEE80211_FCTL_TODS):
361 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200362 iftype != NL80211_IFTYPE_AP_VLAN &&
363 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800364 return -1;
365 break;
366 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
367 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100368 iftype != NL80211_IFTYPE_MESH_POINT &&
369 iftype != NL80211_IFTYPE_AP_VLAN &&
370 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800371 return -1;
372 if (iftype == NL80211_IFTYPE_MESH_POINT) {
373 struct ieee80211s_hdr *meshdr =
374 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800375 /* make sure meshdr->flags is on the linear part */
376 if (!pskb_may_pull(skb, hdrlen + 1))
377 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800378 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800379 skb_copy_bits(skb, hdrlen +
380 offsetof(struct ieee80211s_hdr, eaddr1),
381 dst, ETH_ALEN);
382 skb_copy_bits(skb, hdrlen +
383 offsetof(struct ieee80211s_hdr, eaddr2),
384 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800385 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800386 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800387 }
388 break;
389 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700390 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200391 iftype != NL80211_IFTYPE_P2P_CLIENT &&
392 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800393 (is_multicast_ether_addr(dst) &&
Joe Perches4c764722012-05-08 18:56:56 +0000394 ether_addr_equal(src, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800395 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700396 if (iftype == NL80211_IFTYPE_MESH_POINT) {
397 struct ieee80211s_hdr *meshdr =
398 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800399 /* make sure meshdr->flags is on the linear part */
400 if (!pskb_may_pull(skb, hdrlen + 1))
401 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700402 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800403 skb_copy_bits(skb, hdrlen +
404 offsetof(struct ieee80211s_hdr, eaddr1),
405 src, ETH_ALEN);
406 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700407 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800408 break;
409 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300410 if (iftype != NL80211_IFTYPE_ADHOC &&
411 iftype != NL80211_IFTYPE_STATION)
412 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800413 break;
414 }
415
Zhu Yie3cf8b32010-03-29 17:35:07 +0800416 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800417 return -1;
418
419 payload = skb->data + hdrlen;
420 ethertype = (payload[6] << 8) | payload[7];
421
Joe Perches4c764722012-05-08 18:56:56 +0000422 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yie31a16d2009-05-21 21:47:03 +0800423 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perches4c764722012-05-08 18:56:56 +0000424 ether_addr_equal(payload, bridge_tunnel_header))) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800425 /* remove RFC1042 or Bridge-Tunnel encapsulation and
426 * replace EtherType */
427 skb_pull(skb, hdrlen + 6);
428 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
429 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
430 } else {
431 struct ethhdr *ehdr;
432 __be16 len;
433
434 skb_pull(skb, hdrlen);
435 len = htons(skb->len);
436 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
437 memcpy(ehdr->h_dest, dst, ETH_ALEN);
438 memcpy(ehdr->h_source, src, ETH_ALEN);
439 ehdr->h_proto = len;
440 }
441 return 0;
442}
443EXPORT_SYMBOL(ieee80211_data_to_8023);
444
Zhu Yieaf85ca2009-12-01 10:18:37 +0800445int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800446 enum nl80211_iftype iftype, u8 *bssid, bool qos)
447{
448 struct ieee80211_hdr hdr;
449 u16 hdrlen, ethertype;
450 __le16 fc;
451 const u8 *encaps_data;
452 int encaps_len, skip_header_bytes;
453 int nh_pos, h_pos;
454 int head_need;
455
456 if (unlikely(skb->len < ETH_HLEN))
457 return -EINVAL;
458
459 nh_pos = skb_network_header(skb) - skb->data;
460 h_pos = skb_transport_header(skb) - skb->data;
461
462 /* convert Ethernet header to proper 802.11 header (based on
463 * operation mode) */
464 ethertype = (skb->data[12] << 8) | skb->data[13];
465 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
466
467 switch (iftype) {
468 case NL80211_IFTYPE_AP:
469 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200470 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800471 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
472 /* DA BSSID SA */
473 memcpy(hdr.addr1, skb->data, ETH_ALEN);
474 memcpy(hdr.addr2, addr, ETH_ALEN);
475 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
476 hdrlen = 24;
477 break;
478 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200479 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800480 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
481 /* BSSID SA DA */
482 memcpy(hdr.addr1, bssid, ETH_ALEN);
483 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
484 memcpy(hdr.addr3, skb->data, ETH_ALEN);
485 hdrlen = 24;
486 break;
487 case NL80211_IFTYPE_ADHOC:
488 /* DA SA BSSID */
489 memcpy(hdr.addr1, skb->data, ETH_ALEN);
490 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
491 memcpy(hdr.addr3, bssid, ETH_ALEN);
492 hdrlen = 24;
493 break;
494 default:
495 return -EOPNOTSUPP;
496 }
497
498 if (qos) {
499 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
500 hdrlen += 2;
501 }
502
503 hdr.frame_control = fc;
504 hdr.duration_id = 0;
505 hdr.seq_ctrl = 0;
506
507 skip_header_bytes = ETH_HLEN;
508 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
509 encaps_data = bridge_tunnel_header;
510 encaps_len = sizeof(bridge_tunnel_header);
511 skip_header_bytes -= 2;
512 } else if (ethertype > 0x600) {
513 encaps_data = rfc1042_header;
514 encaps_len = sizeof(rfc1042_header);
515 skip_header_bytes -= 2;
516 } else {
517 encaps_data = NULL;
518 encaps_len = 0;
519 }
520
521 skb_pull(skb, skip_header_bytes);
522 nh_pos -= skip_header_bytes;
523 h_pos -= skip_header_bytes;
524
525 head_need = hdrlen + encaps_len - skb_headroom(skb);
526
527 if (head_need > 0 || skb_cloned(skb)) {
528 head_need = max(head_need, 0);
529 if (head_need)
530 skb_orphan(skb);
531
Joe Perches24616152011-08-29 14:17:41 -0700532 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
Zhu Yie31a16d2009-05-21 21:47:03 +0800533 return -ENOMEM;
Joe Perches24616152011-08-29 14:17:41 -0700534
Zhu Yie31a16d2009-05-21 21:47:03 +0800535 skb->truesize += head_need;
536 }
537
538 if (encaps_data) {
539 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
540 nh_pos += encaps_len;
541 h_pos += encaps_len;
542 }
543
544 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
545
546 nh_pos += hdrlen;
547 h_pos += hdrlen;
548
549 /* Update skb pointers to various headers since this modified frame
550 * is going to go through Linux networking code that may potentially
551 * need things like pointer to IP header. */
552 skb_set_mac_header(skb, 0);
553 skb_set_network_header(skb, nh_pos);
554 skb_set_transport_header(skb, h_pos);
555
556 return 0;
557}
558EXPORT_SYMBOL(ieee80211_data_from_8023);
559
Zhu Yieaf85ca2009-12-01 10:18:37 +0800560
561void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
562 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700563 const unsigned int extra_headroom,
564 bool has_80211_header)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800565{
566 struct sk_buff *frame = NULL;
567 u16 ethertype;
568 u8 *payload;
569 const struct ethhdr *eth;
570 int remaining, err;
571 u8 dst[ETH_ALEN], src[ETH_ALEN];
572
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700573 if (has_80211_header) {
574 err = ieee80211_data_to_8023(skb, addr, iftype);
575 if (err)
576 goto out;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800577
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700578 /* skip the wrapping header */
579 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
580 if (!eth)
581 goto out;
582 } else {
583 eth = (struct ethhdr *) skb->data;
584 }
Zhu Yieaf85ca2009-12-01 10:18:37 +0800585
586 while (skb != frame) {
587 u8 padding;
588 __be16 len = eth->h_proto;
589 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
590
591 remaining = skb->len;
592 memcpy(dst, eth->h_dest, ETH_ALEN);
593 memcpy(src, eth->h_source, ETH_ALEN);
594
595 padding = (4 - subframe_len) & 0x3;
596 /* the last MSDU has no padding */
597 if (subframe_len > remaining)
598 goto purge;
599
600 skb_pull(skb, sizeof(struct ethhdr));
601 /* reuse skb for the last subframe */
602 if (remaining <= subframe_len + padding)
603 frame = skb;
604 else {
605 unsigned int hlen = ALIGN(extra_headroom, 4);
606 /*
607 * Allocate and reserve two bytes more for payload
608 * alignment since sizeof(struct ethhdr) is 14.
609 */
610 frame = dev_alloc_skb(hlen + subframe_len + 2);
611 if (!frame)
612 goto purge;
613
614 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
615 memcpy(skb_put(frame, ntohs(len)), skb->data,
616 ntohs(len));
617
618 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
619 padding);
620 if (!eth) {
621 dev_kfree_skb(frame);
622 goto purge;
623 }
624 }
625
626 skb_reset_network_header(frame);
627 frame->dev = skb->dev;
628 frame->priority = skb->priority;
629
630 payload = frame->data;
631 ethertype = (payload[6] << 8) | payload[7];
632
Joe Perchesac422d32012-05-08 18:56:55 +0000633 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800634 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000635 ether_addr_equal(payload, bridge_tunnel_header))) {
Zhu Yieaf85ca2009-12-01 10:18:37 +0800636 /* remove RFC1042 or Bridge-Tunnel
637 * encapsulation and replace EtherType */
638 skb_pull(frame, 6);
639 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
640 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
641 } else {
642 memcpy(skb_push(frame, sizeof(__be16)), &len,
643 sizeof(__be16));
644 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
645 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
646 }
647 __skb_queue_tail(list, frame);
648 }
649
650 return;
651
652 purge:
653 __skb_queue_purge(list);
654 out:
655 dev_kfree_skb(skb);
656}
657EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
658
Zhu Yie31a16d2009-05-21 21:47:03 +0800659/* Given a data frame determine the 802.1p/1d tag to use. */
660unsigned int cfg80211_classify8021d(struct sk_buff *skb)
661{
662 unsigned int dscp;
663
664 /* skb->priority values from 256->263 are magic values to
665 * directly indicate a specific 802.1d priority. This is used
666 * to allow 802.1d priority to be passed directly in from VLAN
667 * tags, etc.
668 */
669 if (skb->priority >= 256 && skb->priority <= 263)
670 return skb->priority - 256;
671
672 switch (skb->protocol) {
673 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800674 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
675 break;
676 case htons(ETH_P_IPV6):
677 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800678 break;
679 default:
680 return 0;
681 }
682
683 return dscp >> 5;
684}
685EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200686
687const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
688{
Vladimir Kondratieve2176892012-07-25 13:56:53 +0300689 if (bss->information_elements == NULL)
Johannes Berg517357c2009-07-02 17:18:40 +0200690 return NULL;
Vladimir Kondratieve2176892012-07-25 13:56:53 +0300691 return cfg80211_find_ie(ie, bss->information_elements,
692 bss->len_information_elements);
Johannes Berg517357c2009-07-02 17:18:40 +0200693}
694EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200695
696void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
697{
698 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
699 struct net_device *dev = wdev->netdev;
700 int i;
701
702 if (!wdev->connect_keys)
703 return;
704
705 for (i = 0; i < 6; i++) {
706 if (!wdev->connect_keys->params[i].cipher)
707 continue;
Hila Gonene35e4d22012-06-27 17:19:42 +0300708 if (rdev_add_key(rdev, dev, i, false, NULL,
709 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800710 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800711 continue;
712 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200713 if (wdev->connect_keys->def == i)
Hila Gonene35e4d22012-06-27 17:19:42 +0300714 if (rdev_set_default_key(rdev, dev, i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800715 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800716 continue;
717 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200718 if (wdev->connect_keys->defmgmt == i)
Hila Gonene35e4d22012-06-27 17:19:42 +0300719 if (rdev_set_default_mgmt_key(rdev, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800720 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200721 }
722
723 kfree(wdev->connect_keys);
724 wdev->connect_keys = NULL;
725}
Johannes Berg3d54d252009-08-21 14:51:05 +0200726
Daniel Drake1f6fc432012-08-02 18:41:48 +0100727void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200728{
729 struct cfg80211_event *ev;
730 unsigned long flags;
731 const u8 *bssid = NULL;
732
733 spin_lock_irqsave(&wdev->event_lock, flags);
734 while (!list_empty(&wdev->event_list)) {
735 ev = list_first_entry(&wdev->event_list,
736 struct cfg80211_event, list);
737 list_del(&ev->list);
738 spin_unlock_irqrestore(&wdev->event_lock, flags);
739
740 wdev_lock(wdev);
741 switch (ev->type) {
742 case EVENT_CONNECT_RESULT:
743 if (!is_zero_ether_addr(ev->cr.bssid))
744 bssid = ev->cr.bssid;
745 __cfg80211_connect_result(
746 wdev->netdev, bssid,
747 ev->cr.req_ie, ev->cr.req_ie_len,
748 ev->cr.resp_ie, ev->cr.resp_ie_len,
749 ev->cr.status,
750 ev->cr.status == WLAN_STATUS_SUCCESS,
751 NULL);
752 break;
753 case EVENT_ROAMED:
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530754 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
755 ev->rm.req_ie_len, ev->rm.resp_ie,
756 ev->rm.resp_ie_len);
Johannes Berg3d54d252009-08-21 14:51:05 +0200757 break;
758 case EVENT_DISCONNECTED:
759 __cfg80211_disconnected(wdev->netdev,
760 ev->dc.ie, ev->dc.ie_len,
761 ev->dc.reason, true);
762 break;
763 case EVENT_IBSS_JOINED:
764 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
765 break;
766 }
767 wdev_unlock(wdev);
768
769 kfree(ev);
770
771 spin_lock_irqsave(&wdev->event_lock, flags);
772 }
773 spin_unlock_irqrestore(&wdev->event_lock, flags);
774}
775
776void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
777{
778 struct wireless_dev *wdev;
779
780 ASSERT_RTNL();
781 ASSERT_RDEV_LOCK(rdev);
782
783 mutex_lock(&rdev->devlist_mtx);
784
Johannes Berg89a54e42012-06-15 14:33:17 +0200785 list_for_each_entry(wdev, &rdev->wdev_list, list)
Johannes Berg3d54d252009-08-21 14:51:05 +0200786 cfg80211_process_wdev_events(wdev);
787
788 mutex_unlock(&rdev->devlist_mtx);
789}
790
791int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
792 struct net_device *dev, enum nl80211_iftype ntype,
793 u32 *flags, struct vif_params *params)
794{
795 int err;
796 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
797
798 ASSERT_RDEV_LOCK(rdev);
799
800 /* don't support changing VLANs, you just re-create them */
801 if (otype == NL80211_IFTYPE_AP_VLAN)
802 return -EOPNOTSUPP;
803
Johannes Berg98104fde2012-06-16 00:19:54 +0200804 /* cannot change into P2P device type */
805 if (ntype == NL80211_IFTYPE_P2P_DEVICE)
806 return -EOPNOTSUPP;
807
Johannes Berg3d54d252009-08-21 14:51:05 +0200808 if (!rdev->ops->change_virtual_intf ||
809 !(rdev->wiphy.interface_modes & (1 << ntype)))
810 return -EOPNOTSUPP;
811
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100812 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000813 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200814 (ntype == NL80211_IFTYPE_ADHOC ||
815 ntype == NL80211_IFTYPE_STATION ||
816 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100817 return -EBUSY;
818
Michal Kaziorf8cdddb2012-06-08 10:55:44 +0200819 if (ntype != otype && netif_running(dev)) {
Michal Kaziore4e32452012-06-29 12:47:08 +0200820 mutex_lock(&rdev->devlist_mtx);
Johannes Berg7527a782011-05-13 10:58:57 +0200821 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
822 ntype);
Michal Kaziore4e32452012-06-29 12:47:08 +0200823 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg7527a782011-05-13 10:58:57 +0200824 if (err)
825 return err;
826
Johannes Berg9bc383d2009-11-19 11:55:19 +0100827 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100828 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100829
Johannes Berg3d54d252009-08-21 14:51:05 +0200830 switch (otype) {
Michal Kaziorac800142012-06-29 12:46:57 +0200831 case NL80211_IFTYPE_AP:
832 cfg80211_stop_ap(rdev, dev);
833 break;
Johannes Berg3d54d252009-08-21 14:51:05 +0200834 case NL80211_IFTYPE_ADHOC:
835 cfg80211_leave_ibss(rdev, dev, false);
836 break;
837 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200838 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200839 cfg80211_disconnect(rdev, dev,
840 WLAN_REASON_DEAUTH_LEAVING, true);
841 break;
842 case NL80211_IFTYPE_MESH_POINT:
843 /* mesh should be handled? */
844 break;
845 default:
846 break;
847 }
848
849 cfg80211_process_rdev_events(rdev);
850 }
851
Hila Gonene35e4d22012-06-27 17:19:42 +0300852 err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params);
Johannes Berg3d54d252009-08-21 14:51:05 +0200853
854 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
855
Johannes Berg9bc383d2009-11-19 11:55:19 +0100856 if (!err && params && params->use_4addr != -1)
857 dev->ieee80211_ptr->use_4addr = params->use_4addr;
858
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100859 if (!err) {
860 dev->priv_flags &= ~IFF_DONT_BRIDGE;
861 switch (ntype) {
862 case NL80211_IFTYPE_STATION:
863 if (dev->ieee80211_ptr->use_4addr)
864 break;
865 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200866 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100867 case NL80211_IFTYPE_ADHOC:
868 dev->priv_flags |= IFF_DONT_BRIDGE;
869 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200870 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100871 case NL80211_IFTYPE_AP:
872 case NL80211_IFTYPE_AP_VLAN:
873 case NL80211_IFTYPE_WDS:
874 case NL80211_IFTYPE_MESH_POINT:
875 /* bridging OK */
876 break;
877 case NL80211_IFTYPE_MONITOR:
878 /* monitor can't bridge anyway */
879 break;
880 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200881 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100882 /* not happening */
883 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200884 case NL80211_IFTYPE_P2P_DEVICE:
885 WARN_ON(1);
886 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100887 }
888 }
889
Michal Kaziordbbae262012-06-29 12:47:01 +0200890 if (!err && ntype != otype && netif_running(dev)) {
891 cfg80211_update_iface_num(rdev, ntype, 1);
892 cfg80211_update_iface_num(rdev, otype, -1);
893 }
894
Johannes Berg3d54d252009-08-21 14:51:05 +0200895 return err;
896}
John W. Linville254416a2009-12-09 16:43:52 -0500897
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +0300898static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
899{
900 static const u32 __mcs2bitrate[] = {
901 /* control PHY */
902 [0] = 275,
903 /* SC PHY */
904 [1] = 3850,
905 [2] = 7700,
906 [3] = 9625,
907 [4] = 11550,
908 [5] = 12512, /* 1251.25 mbps */
909 [6] = 15400,
910 [7] = 19250,
911 [8] = 23100,
912 [9] = 25025,
913 [10] = 30800,
914 [11] = 38500,
915 [12] = 46200,
916 /* OFDM PHY */
917 [13] = 6930,
918 [14] = 8662, /* 866.25 mbps */
919 [15] = 13860,
920 [16] = 17325,
921 [17] = 20790,
922 [18] = 27720,
923 [19] = 34650,
924 [20] = 41580,
925 [21] = 45045,
926 [22] = 51975,
927 [23] = 62370,
928 [24] = 67568, /* 6756.75 mbps */
929 /* LP-SC PHY */
930 [25] = 6260,
931 [26] = 8340,
932 [27] = 11120,
933 [28] = 12510,
934 [29] = 16680,
935 [30] = 22240,
936 [31] = 25030,
937 };
938
939 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
940 return 0;
941
942 return __mcs2bitrate[rate->mcs];
943}
944
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +0300945u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -0500946{
947 int modulation, streams, bitrate;
948
949 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
950 return rate->legacy;
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +0300951 if (rate->flags & RATE_INFO_FLAGS_60G)
952 return cfg80211_calculate_bitrate_60g(rate);
John W. Linville254416a2009-12-09 16:43:52 -0500953
954 /* the formula below does only work for MCS values smaller than 32 */
Johannes Berg2615f372012-05-08 21:36:20 +0200955 if (WARN_ON_ONCE(rate->mcs >= 32))
John W. Linville254416a2009-12-09 16:43:52 -0500956 return 0;
957
958 modulation = rate->mcs & 7;
959 streams = (rate->mcs >> 3) + 1;
960
961 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
962 13500000 : 6500000;
963
964 if (modulation < 4)
965 bitrate *= (modulation + 1);
966 else if (modulation == 4)
967 bitrate *= (modulation + 2);
968 else
969 bitrate *= (modulation + 3);
970
971 bitrate *= streams;
972
973 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
974 bitrate = (bitrate / 9) * 10;
975
976 /* do NOT round down here */
977 return (bitrate + 50000) / 100000;
978}
Thomas Pedersen8097e142012-03-05 15:31:47 -0800979EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +0200980
981int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
982 u32 beacon_int)
983{
984 struct wireless_dev *wdev;
985 int res = 0;
986
987 if (!beacon_int)
988 return -EINVAL;
989
990 mutex_lock(&rdev->devlist_mtx);
991
Johannes Berg89a54e42012-06-15 14:33:17 +0200992 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Berg56d18932011-05-09 18:41:15 +0200993 if (!wdev->beacon_interval)
994 continue;
995 if (wdev->beacon_interval != beacon_int) {
996 res = -EINVAL;
997 break;
998 }
999 }
1000
1001 mutex_unlock(&rdev->devlist_mtx);
1002
1003 return res;
1004}
Johannes Berg7527a782011-05-13 10:58:57 +02001005
Michal Kaziord4e50c52012-06-29 12:47:07 +02001006int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1007 struct wireless_dev *wdev,
1008 enum nl80211_iftype iftype,
1009 struct ieee80211_channel *chan,
1010 enum cfg80211_chan_mode chanmode)
Johannes Berg7527a782011-05-13 10:58:57 +02001011{
1012 struct wireless_dev *wdev_iter;
Johannes Berg463454b2012-06-05 12:16:50 +02001013 u32 used_iftypes = BIT(iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001014 int num[NUM_NL80211_IFTYPES];
Michal Kaziord4e50c52012-06-29 12:47:07 +02001015 struct ieee80211_channel
1016 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1017 struct ieee80211_channel *ch;
1018 enum cfg80211_chan_mode chmode;
1019 int num_different_channels = 0;
Johannes Berg7527a782011-05-13 10:58:57 +02001020 int total = 1;
1021 int i, j;
1022
1023 ASSERT_RTNL();
Michal Kaziore4e32452012-06-29 12:47:08 +02001024 lockdep_assert_held(&rdev->devlist_mtx);
Johannes Berg7527a782011-05-13 10:58:57 +02001025
1026 /* Always allow software iftypes */
1027 if (rdev->wiphy.software_iftypes & BIT(iftype))
1028 return 0;
1029
Johannes Berg7527a782011-05-13 10:58:57 +02001030 memset(num, 0, sizeof(num));
Michal Kaziord4e50c52012-06-29 12:47:07 +02001031 memset(used_channels, 0, sizeof(used_channels));
Johannes Berg7527a782011-05-13 10:58:57 +02001032
1033 num[iftype] = 1;
1034
Michal Kaziord4e50c52012-06-29 12:47:07 +02001035 switch (chanmode) {
1036 case CHAN_MODE_UNDEFINED:
1037 break;
1038 case CHAN_MODE_SHARED:
1039 WARN_ON(!chan);
1040 used_channels[0] = chan;
1041 num_different_channels++;
1042 break;
1043 case CHAN_MODE_EXCLUSIVE:
1044 num_different_channels++;
1045 break;
1046 }
1047
Johannes Berg89a54e42012-06-15 14:33:17 +02001048 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
Johannes Berg7527a782011-05-13 10:58:57 +02001049 if (wdev_iter == wdev)
1050 continue;
Johannes Berg98104fde2012-06-16 00:19:54 +02001051 if (wdev_iter->netdev) {
1052 if (!netif_running(wdev_iter->netdev))
1053 continue;
1054 } else if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
1055 if (!wdev_iter->p2p_started)
1056 continue;
1057 } else {
1058 WARN_ON(1);
1059 }
Johannes Berg7527a782011-05-13 10:58:57 +02001060
1061 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1062 continue;
1063
Johannes Berg8e95ea42012-07-10 19:39:02 +02001064 /*
1065 * We may be holding the "wdev" mutex, but now need to lock
1066 * wdev_iter. This is OK because once we get here wdev_iter
1067 * is not wdev (tested above), but we need to use the nested
1068 * locking for lockdep.
1069 */
1070 mutex_lock_nested(&wdev_iter->mtx, 1);
1071 __acquire(wdev_iter->mtx);
1072 cfg80211_get_chan_state(wdev_iter, &ch, &chmode);
1073 wdev_unlock(wdev_iter);
Michal Kaziord4e50c52012-06-29 12:47:07 +02001074
1075 switch (chmode) {
1076 case CHAN_MODE_UNDEFINED:
1077 break;
1078 case CHAN_MODE_SHARED:
1079 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1080 if (!used_channels[i] || used_channels[i] == ch)
1081 break;
1082
Michal Kaziore4e32452012-06-29 12:47:08 +02001083 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
Michal Kaziord4e50c52012-06-29 12:47:07 +02001084 return -EBUSY;
Michal Kaziord4e50c52012-06-29 12:47:07 +02001085
1086 if (used_channels[i] == NULL) {
1087 used_channels[i] = ch;
1088 num_different_channels++;
1089 }
1090 break;
1091 case CHAN_MODE_EXCLUSIVE:
1092 num_different_channels++;
1093 break;
1094 }
1095
Johannes Berg7527a782011-05-13 10:58:57 +02001096 num[wdev_iter->iftype]++;
1097 total++;
Johannes Berg463454b2012-06-05 12:16:50 +02001098 used_iftypes |= BIT(wdev_iter->iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001099 }
Johannes Berg7527a782011-05-13 10:58:57 +02001100
Johannes Berg8e8b41f2012-03-15 10:16:16 +01001101 if (total == 1)
1102 return 0;
1103
Johannes Berg7527a782011-05-13 10:58:57 +02001104 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1105 const struct ieee80211_iface_combination *c;
1106 struct ieee80211_iface_limit *limits;
Johannes Berg463454b2012-06-05 12:16:50 +02001107 u32 all_iftypes = 0;
Johannes Berg7527a782011-05-13 10:58:57 +02001108
1109 c = &rdev->wiphy.iface_combinations[i];
1110
Michal Kaziord4e50c52012-06-29 12:47:07 +02001111 if (total > c->max_interfaces)
1112 continue;
1113 if (num_different_channels > c->num_different_channels)
1114 continue;
1115
Johannes Berg7527a782011-05-13 10:58:57 +02001116 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1117 GFP_KERNEL);
1118 if (!limits)
1119 return -ENOMEM;
Johannes Berg7527a782011-05-13 10:58:57 +02001120
1121 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1122 if (rdev->wiphy.software_iftypes & BIT(iftype))
1123 continue;
1124 for (j = 0; j < c->n_limits; j++) {
Johannes Berg463454b2012-06-05 12:16:50 +02001125 all_iftypes |= limits[j].types;
Lukasz Kucharczyke55a4042012-04-11 14:55:10 +02001126 if (!(limits[j].types & BIT(iftype)))
Johannes Berg7527a782011-05-13 10:58:57 +02001127 continue;
1128 if (limits[j].max < num[iftype])
1129 goto cont;
1130 limits[j].max -= num[iftype];
1131 }
1132 }
Johannes Berg463454b2012-06-05 12:16:50 +02001133
1134 /*
1135 * Finally check that all iftypes that we're currently
1136 * using are actually part of this combination. If they
1137 * aren't then we can't use this combination and have
1138 * to continue to the next.
1139 */
1140 if ((all_iftypes & used_iftypes) != used_iftypes)
1141 goto cont;
1142
1143 /*
1144 * This combination covered all interface types and
1145 * supported the requested numbers, so we're good.
1146 */
Johannes Berg7527a782011-05-13 10:58:57 +02001147 kfree(limits);
1148 return 0;
1149 cont:
1150 kfree(limits);
1151 }
1152
1153 return -EBUSY;
1154}
Johannes Berg34850ab2011-07-18 18:08:35 +02001155
1156int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1157 const u8 *rates, unsigned int n_rates,
1158 u32 *mask)
1159{
1160 int i, j;
1161
Johannes Berga401d2b2011-07-20 00:52:16 +02001162 if (!sband)
1163 return -EINVAL;
1164
Johannes Berg34850ab2011-07-18 18:08:35 +02001165 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1166 return -EINVAL;
1167
1168 *mask = 0;
1169
1170 for (i = 0; i < n_rates; i++) {
1171 int rate = (rates[i] & 0x7f) * 5;
1172 bool found = false;
1173
1174 for (j = 0; j < sband->n_bitrates; j++) {
1175 if (sband->bitrates[j].bitrate == rate) {
1176 found = true;
1177 *mask |= BIT(j);
1178 break;
1179 }
1180 }
1181 if (!found)
1182 return -EINVAL;
1183 }
1184
1185 /*
1186 * mask must have at least one bit set here since we
1187 * didn't accept a 0-length rates array nor allowed
1188 * entries in the array that didn't exist
1189 */
1190
1191 return 0;
1192}
Johannes Berg11a2a352011-11-21 11:09:22 +01001193
1194/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1195/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1196const unsigned char rfc1042_header[] __aligned(2) =
1197 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1198EXPORT_SYMBOL(rfc1042_header);
1199
1200/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1201const unsigned char bridge_tunnel_header[] __aligned(2) =
1202 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1203EXPORT_SYMBOL(bridge_tunnel_header);