blob: 45a09de1ffe3c1388a23968c268e26ab05e724b6 [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"
14
Johannes Bergbd815252008-10-29 20:00:45 +010015struct ieee80211_rate *
16ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
Johannes Berg881d9482009-01-21 15:13:48 +010017 u32 basic_rates, int bitrate)
Johannes Bergbd815252008-10-29 20:00:45 +010018{
19 struct ieee80211_rate *result = &sband->bitrates[0];
20 int i;
21
22 for (i = 0; i < sband->n_bitrates; i++) {
23 if (!(basic_rates & BIT(i)))
24 continue;
25 if (sband->bitrates[i].bitrate > bitrate)
26 continue;
27 result = &sband->bitrates[i];
28 }
29
30 return result;
31}
32EXPORT_SYMBOL(ieee80211_get_response_rate);
33
Bruno Randolf59eb21a2011-01-17 13:37:28 +090034int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band)
Johannes Berg8318d782008-01-24 19:38:38 +010035{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090036 /* see 802.11 17.3.8.3.2 and Annex J
37 * there are overlapping channel numbers in 5GHz and 2GHz bands */
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030038 if (chan <= 0)
39 return 0; /* not supported */
40 switch (band) {
41 case IEEE80211_BAND_2GHZ:
Bruno Randolf59eb21a2011-01-17 13:37:28 +090042 if (chan == 14)
43 return 2484;
44 else if (chan < 14)
45 return 2407 + chan * 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030046 break;
47 case IEEE80211_BAND_5GHZ:
48 if (chan >= 182 && chan <= 196)
49 return 4000 + chan * 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090050 else
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030051 return 5000 + chan * 5;
52 break;
53 case IEEE80211_BAND_60GHZ:
54 if (chan < 5)
55 return 56160 + chan * 2160;
56 break;
57 default:
58 ;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090059 }
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030060 return 0; /* not supported */
Johannes Berg8318d782008-01-24 19:38:38 +010061}
62EXPORT_SYMBOL(ieee80211_channel_to_frequency);
63
64int ieee80211_frequency_to_channel(int freq)
65{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090066 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +010067 if (freq == 2484)
68 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090069 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +010070 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090071 else if (freq >= 4910 && freq <= 4980)
72 return (freq - 4000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030073 else if (freq <= 45000) /* DMG band lower limit */
Bruno Randolf59eb21a2011-01-17 13:37:28 +090074 return (freq - 5000) / 5;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +030075 else if (freq >= 58320 && freq <= 64800)
76 return (freq - 56160) / 2160;
77 else
78 return 0;
Johannes Berg8318d782008-01-24 19:38:38 +010079}
80EXPORT_SYMBOL(ieee80211_frequency_to_channel);
81
Johannes Berg6c507cd2008-03-26 14:14:55 +010082struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
83 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +010084{
85 enum ieee80211_band band;
86 struct ieee80211_supported_band *sband;
87 int i;
88
89 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
90 sband = wiphy->bands[band];
91
92 if (!sband)
93 continue;
94
95 for (i = 0; i < sband->n_channels; i++) {
96 if (sband->channels[i].center_freq == freq)
97 return &sband->channels[i];
98 }
99 }
100
101 return NULL;
102}
Johannes Berg6c507cd2008-03-26 14:14:55 +0100103EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +0100104
Johannes Berg8318d782008-01-24 19:38:38 +0100105static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
106 enum ieee80211_band band)
107{
108 int i, want;
109
110 switch (band) {
111 case IEEE80211_BAND_5GHZ:
112 want = 3;
113 for (i = 0; i < sband->n_bitrates; i++) {
114 if (sband->bitrates[i].bitrate == 60 ||
115 sband->bitrates[i].bitrate == 120 ||
116 sband->bitrates[i].bitrate == 240) {
117 sband->bitrates[i].flags |=
118 IEEE80211_RATE_MANDATORY_A;
119 want--;
120 }
121 }
122 WARN_ON(want);
123 break;
124 case IEEE80211_BAND_2GHZ:
125 want = 7;
126 for (i = 0; i < sband->n_bitrates; i++) {
127 if (sband->bitrates[i].bitrate == 10) {
128 sband->bitrates[i].flags |=
129 IEEE80211_RATE_MANDATORY_B |
130 IEEE80211_RATE_MANDATORY_G;
131 want--;
132 }
133
134 if (sband->bitrates[i].bitrate == 20 ||
135 sband->bitrates[i].bitrate == 55 ||
136 sband->bitrates[i].bitrate == 110 ||
137 sband->bitrates[i].bitrate == 60 ||
138 sband->bitrates[i].bitrate == 120 ||
139 sband->bitrates[i].bitrate == 240) {
140 sband->bitrates[i].flags |=
141 IEEE80211_RATE_MANDATORY_G;
142 want--;
143 }
144
Johannes Bergaac09fb2008-01-30 17:36:10 +0100145 if (sband->bitrates[i].bitrate != 10 &&
146 sband->bitrates[i].bitrate != 20 &&
147 sband->bitrates[i].bitrate != 55 &&
148 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100149 sband->bitrates[i].flags |=
150 IEEE80211_RATE_ERP_G;
151 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100152 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100153 break;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300154 case IEEE80211_BAND_60GHZ:
155 /* check for mandatory HT MCS 1..4 */
156 WARN_ON(!sband->ht_cap.ht_supported);
157 WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
158 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100159 case IEEE80211_NUM_BANDS:
160 WARN_ON(1);
161 break;
162 }
163}
164
165void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
166{
167 enum ieee80211_band band;
168
169 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
170 if (wiphy->bands[band])
171 set_mandatory_flags_band(wiphy->bands[band], band);
172}
Johannes Berg08645122009-05-11 13:54:58 +0200173
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300174bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
175{
176 int i;
177 for (i = 0; i < wiphy->n_cipher_suites; i++)
178 if (cipher == wiphy->cipher_suites[i])
179 return true;
180 return false;
181}
182
Johannes Bergfffd0932009-07-08 14:22:54 +0200183int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
184 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200185 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200186{
187 if (key_idx > 5)
188 return -EINVAL;
189
Johannes Berge31b8212010-10-05 19:39:30 +0200190 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
191 return -EINVAL;
192
193 if (pairwise && !mac_addr)
194 return -EINVAL;
195
Johannes Berg08645122009-05-11 13:54:58 +0200196 /*
197 * Disallow pairwise keys with non-zero index unless it's WEP
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200198 * or a vendor specific cipher (because current deployments use
199 * pairwise WEP keys with non-zero indices and for vendor specific
200 * ciphers this should be validated in the driver or hardware level
201 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200202 */
Johannes Berge31b8212010-10-05 19:39:30 +0200203 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200204 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
205 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
206 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200207 return -EINVAL;
208
Johannes Berg08645122009-05-11 13:54:58 +0200209 switch (params->cipher) {
210 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200211 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200212 return -EINVAL;
213 break;
214 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200215 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200216 return -EINVAL;
217 break;
218 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200219 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200220 return -EINVAL;
221 break;
222 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200223 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200224 return -EINVAL;
225 break;
226 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200227 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200228 return -EINVAL;
229 break;
230 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300231 /*
232 * We don't know anything about this algorithm,
233 * allow using it -- but the driver must check
234 * all parameters! We still check below whether
235 * or not the driver supports this algorithm,
236 * of course.
237 */
238 break;
Johannes Berg08645122009-05-11 13:54:58 +0200239 }
240
Jouni Malinen9f26a952009-05-15 12:38:32 +0300241 if (params->seq) {
242 switch (params->cipher) {
243 case WLAN_CIPHER_SUITE_WEP40:
244 case WLAN_CIPHER_SUITE_WEP104:
245 /* These ciphers do not use key sequence */
246 return -EINVAL;
247 case WLAN_CIPHER_SUITE_TKIP:
248 case WLAN_CIPHER_SUITE_CCMP:
249 case WLAN_CIPHER_SUITE_AES_CMAC:
250 if (params->seq_len != 6)
251 return -EINVAL;
252 break;
253 }
254 }
255
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300256 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200257 return -EINVAL;
258
Johannes Berg08645122009-05-11 13:54:58 +0200259 return 0;
260}
Zhu Yie31a16d2009-05-21 21:47:03 +0800261
Johannes Berg633adf12010-08-12 14:49:58 +0200262unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800263{
264 unsigned int hdrlen = 24;
265
266 if (ieee80211_is_data(fc)) {
267 if (ieee80211_has_a4(fc))
268 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200269 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800270 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200271 if (ieee80211_has_order(fc))
272 hdrlen += IEEE80211_HT_CTL_LEN;
273 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800274 goto out;
275 }
276
277 if (ieee80211_is_ctl(fc)) {
278 /*
279 * ACK and CTS are 10 bytes, all others 16. To see how
280 * to get this condition consider
281 * subtype mask: 0b0000000011110000 (0x00F0)
282 * ACK subtype: 0b0000000011010000 (0x00D0)
283 * CTS subtype: 0b0000000011000000 (0x00C0)
284 * bits that matter: ^^^ (0x00E0)
285 * value of those: 0b0000000011000000 (0x00C0)
286 */
287 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
288 hdrlen = 10;
289 else
290 hdrlen = 16;
291 }
292out:
293 return hdrlen;
294}
295EXPORT_SYMBOL(ieee80211_hdrlen);
296
297unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
298{
299 const struct ieee80211_hdr *hdr =
300 (const struct ieee80211_hdr *)skb->data;
301 unsigned int hdrlen;
302
303 if (unlikely(skb->len < 10))
304 return 0;
305 hdrlen = ieee80211_hdrlen(hdr->frame_control);
306 if (unlikely(hdrlen > skb->len))
307 return 0;
308 return hdrlen;
309}
310EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
311
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400312static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800313{
314 int ae = meshhdr->flags & MESH_FLAGS_AE;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200315 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800316 switch (ae) {
Johannes Berg7dd111e2012-10-25 21:51:59 +0200317 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800318 case 0:
319 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700320 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800321 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700322 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800323 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800324 }
325}
326
Zhu Yieaf85ca2009-12-01 10:18:37 +0800327int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800328 enum nl80211_iftype iftype)
329{
330 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
331 u16 hdrlen, ethertype;
332 u8 *payload;
333 u8 dst[ETH_ALEN];
334 u8 src[ETH_ALEN] __aligned(2);
335
336 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
337 return -1;
338
339 hdrlen = ieee80211_hdrlen(hdr->frame_control);
340
341 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
342 * header
343 * IEEE 802.11 address fields:
344 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
345 * 0 0 DA SA BSSID n/a
346 * 0 1 DA BSSID SA n/a
347 * 1 0 BSSID SA DA n/a
348 * 1 1 RA TA DA SA
349 */
350 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
351 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
352
353 switch (hdr->frame_control &
354 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
355 case cpu_to_le16(IEEE80211_FCTL_TODS):
356 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200357 iftype != NL80211_IFTYPE_AP_VLAN &&
358 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800359 return -1;
360 break;
361 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
362 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543ee2009-11-10 20:10:05 +0100363 iftype != NL80211_IFTYPE_MESH_POINT &&
364 iftype != NL80211_IFTYPE_AP_VLAN &&
365 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800366 return -1;
367 if (iftype == NL80211_IFTYPE_MESH_POINT) {
368 struct ieee80211s_hdr *meshdr =
369 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800370 /* make sure meshdr->flags is on the linear part */
371 if (!pskb_may_pull(skb, hdrlen + 1))
372 return -1;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200373 if (meshdr->flags & MESH_FLAGS_AE_A4)
374 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800375 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800376 skb_copy_bits(skb, hdrlen +
377 offsetof(struct ieee80211s_hdr, eaddr1),
378 dst, ETH_ALEN);
379 skb_copy_bits(skb, hdrlen +
380 offsetof(struct ieee80211s_hdr, eaddr2),
381 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800382 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800383 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800384 }
385 break;
386 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700387 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200388 iftype != NL80211_IFTYPE_P2P_CLIENT &&
389 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800390 (is_multicast_ether_addr(dst) &&
Joe Perches4c764722012-05-08 18:56:56 +0000391 ether_addr_equal(src, addr)))
Zhu Yie31a16d2009-05-21 21:47:03 +0800392 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700393 if (iftype == NL80211_IFTYPE_MESH_POINT) {
394 struct ieee80211s_hdr *meshdr =
395 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800396 /* make sure meshdr->flags is on the linear part */
397 if (!pskb_may_pull(skb, hdrlen + 1))
398 return -1;
Johannes Berg7dd111e2012-10-25 21:51:59 +0200399 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
400 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700401 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800402 skb_copy_bits(skb, hdrlen +
403 offsetof(struct ieee80211s_hdr, eaddr1),
404 src, ETH_ALEN);
405 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700406 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800407 break;
408 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300409 if (iftype != NL80211_IFTYPE_ADHOC &&
410 iftype != NL80211_IFTYPE_STATION)
411 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800412 break;
413 }
414
Zhu Yie3cf8b32010-03-29 17:35:07 +0800415 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800416 return -1;
417
418 payload = skb->data + hdrlen;
419 ethertype = (payload[6] << 8) | payload[7];
420
Joe Perches4c764722012-05-08 18:56:56 +0000421 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yie31a16d2009-05-21 21:47:03 +0800422 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perches4c764722012-05-08 18:56:56 +0000423 ether_addr_equal(payload, bridge_tunnel_header))) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800424 /* remove RFC1042 or Bridge-Tunnel encapsulation and
425 * replace EtherType */
426 skb_pull(skb, hdrlen + 6);
427 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
428 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
429 } else {
430 struct ethhdr *ehdr;
431 __be16 len;
432
433 skb_pull(skb, hdrlen);
434 len = htons(skb->len);
435 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
436 memcpy(ehdr->h_dest, dst, ETH_ALEN);
437 memcpy(ehdr->h_source, src, ETH_ALEN);
438 ehdr->h_proto = len;
439 }
440 return 0;
441}
442EXPORT_SYMBOL(ieee80211_data_to_8023);
443
Zhu Yieaf85ca2009-12-01 10:18:37 +0800444int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800445 enum nl80211_iftype iftype, u8 *bssid, bool qos)
446{
447 struct ieee80211_hdr hdr;
448 u16 hdrlen, ethertype;
449 __le16 fc;
450 const u8 *encaps_data;
451 int encaps_len, skip_header_bytes;
452 int nh_pos, h_pos;
453 int head_need;
454
455 if (unlikely(skb->len < ETH_HLEN))
456 return -EINVAL;
457
458 nh_pos = skb_network_header(skb) - skb->data;
459 h_pos = skb_transport_header(skb) - skb->data;
460
461 /* convert Ethernet header to proper 802.11 header (based on
462 * operation mode) */
463 ethertype = (skb->data[12] << 8) | skb->data[13];
464 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
465
466 switch (iftype) {
467 case NL80211_IFTYPE_AP:
468 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200469 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800470 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
471 /* DA BSSID SA */
472 memcpy(hdr.addr1, skb->data, ETH_ALEN);
473 memcpy(hdr.addr2, addr, ETH_ALEN);
474 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
475 hdrlen = 24;
476 break;
477 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200478 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800479 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
480 /* BSSID SA DA */
481 memcpy(hdr.addr1, bssid, ETH_ALEN);
482 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
483 memcpy(hdr.addr3, skb->data, ETH_ALEN);
484 hdrlen = 24;
485 break;
486 case NL80211_IFTYPE_ADHOC:
487 /* DA SA BSSID */
488 memcpy(hdr.addr1, skb->data, ETH_ALEN);
489 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
490 memcpy(hdr.addr3, bssid, ETH_ALEN);
491 hdrlen = 24;
492 break;
493 default:
494 return -EOPNOTSUPP;
495 }
496
497 if (qos) {
498 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
499 hdrlen += 2;
500 }
501
502 hdr.frame_control = fc;
503 hdr.duration_id = 0;
504 hdr.seq_ctrl = 0;
505
506 skip_header_bytes = ETH_HLEN;
507 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
508 encaps_data = bridge_tunnel_header;
509 encaps_len = sizeof(bridge_tunnel_header);
510 skip_header_bytes -= 2;
511 } else if (ethertype > 0x600) {
512 encaps_data = rfc1042_header;
513 encaps_len = sizeof(rfc1042_header);
514 skip_header_bytes -= 2;
515 } else {
516 encaps_data = NULL;
517 encaps_len = 0;
518 }
519
520 skb_pull(skb, skip_header_bytes);
521 nh_pos -= skip_header_bytes;
522 h_pos -= skip_header_bytes;
523
524 head_need = hdrlen + encaps_len - skb_headroom(skb);
525
526 if (head_need > 0 || skb_cloned(skb)) {
527 head_need = max(head_need, 0);
528 if (head_need)
529 skb_orphan(skb);
530
Joe Perches24616152011-08-29 14:17:41 -0700531 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
Zhu Yie31a16d2009-05-21 21:47:03 +0800532 return -ENOMEM;
Joe Perches24616152011-08-29 14:17:41 -0700533
Zhu Yie31a16d2009-05-21 21:47:03 +0800534 skb->truesize += head_need;
535 }
536
537 if (encaps_data) {
538 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
539 nh_pos += encaps_len;
540 h_pos += encaps_len;
541 }
542
543 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
544
545 nh_pos += hdrlen;
546 h_pos += hdrlen;
547
548 /* Update skb pointers to various headers since this modified frame
549 * is going to go through Linux networking code that may potentially
550 * need things like pointer to IP header. */
551 skb_set_mac_header(skb, 0);
552 skb_set_network_header(skb, nh_pos);
553 skb_set_transport_header(skb, h_pos);
554
555 return 0;
556}
557EXPORT_SYMBOL(ieee80211_data_from_8023);
558
Zhu Yieaf85ca2009-12-01 10:18:37 +0800559
560void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
561 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700562 const unsigned int extra_headroom,
563 bool has_80211_header)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800564{
565 struct sk_buff *frame = NULL;
566 u16 ethertype;
567 u8 *payload;
568 const struct ethhdr *eth;
569 int remaining, err;
570 u8 dst[ETH_ALEN], src[ETH_ALEN];
571
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700572 if (has_80211_header) {
573 err = ieee80211_data_to_8023(skb, addr, iftype);
574 if (err)
575 goto out;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800576
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700577 /* skip the wrapping header */
578 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
579 if (!eth)
580 goto out;
581 } else {
582 eth = (struct ethhdr *) skb->data;
583 }
Zhu Yieaf85ca2009-12-01 10:18:37 +0800584
585 while (skb != frame) {
586 u8 padding;
587 __be16 len = eth->h_proto;
588 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
589
590 remaining = skb->len;
591 memcpy(dst, eth->h_dest, ETH_ALEN);
592 memcpy(src, eth->h_source, ETH_ALEN);
593
594 padding = (4 - subframe_len) & 0x3;
595 /* the last MSDU has no padding */
596 if (subframe_len > remaining)
597 goto purge;
598
599 skb_pull(skb, sizeof(struct ethhdr));
600 /* reuse skb for the last subframe */
601 if (remaining <= subframe_len + padding)
602 frame = skb;
603 else {
604 unsigned int hlen = ALIGN(extra_headroom, 4);
605 /*
606 * Allocate and reserve two bytes more for payload
607 * alignment since sizeof(struct ethhdr) is 14.
608 */
609 frame = dev_alloc_skb(hlen + subframe_len + 2);
610 if (!frame)
611 goto purge;
612
613 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
614 memcpy(skb_put(frame, ntohs(len)), skb->data,
615 ntohs(len));
616
617 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
618 padding);
619 if (!eth) {
620 dev_kfree_skb(frame);
621 goto purge;
622 }
623 }
624
625 skb_reset_network_header(frame);
626 frame->dev = skb->dev;
627 frame->priority = skb->priority;
628
629 payload = frame->data;
630 ethertype = (payload[6] << 8) | payload[7];
631
Joe Perchesac422d32012-05-08 18:56:55 +0000632 if (likely((ether_addr_equal(payload, rfc1042_header) &&
Zhu Yieaf85ca2009-12-01 10:18:37 +0800633 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
Joe Perchesac422d32012-05-08 18:56:55 +0000634 ether_addr_equal(payload, bridge_tunnel_header))) {
Zhu Yieaf85ca2009-12-01 10:18:37 +0800635 /* remove RFC1042 or Bridge-Tunnel
636 * encapsulation and replace EtherType */
637 skb_pull(frame, 6);
638 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
639 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
640 } else {
641 memcpy(skb_push(frame, sizeof(__be16)), &len,
642 sizeof(__be16));
643 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
644 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
645 }
646 __skb_queue_tail(list, frame);
647 }
648
649 return;
650
651 purge:
652 __skb_queue_purge(list);
653 out:
654 dev_kfree_skb(skb);
655}
656EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
657
Zhu Yie31a16d2009-05-21 21:47:03 +0800658/* Given a data frame determine the 802.1p/1d tag to use. */
659unsigned int cfg80211_classify8021d(struct sk_buff *skb)
660{
661 unsigned int dscp;
662
663 /* skb->priority values from 256->263 are magic values to
664 * directly indicate a specific 802.1d priority. This is used
665 * to allow 802.1d priority to be passed directly in from VLAN
666 * tags, etc.
667 */
668 if (skb->priority >= 256 && skb->priority <= 263)
669 return skb->priority - 256;
670
671 switch (skb->protocol) {
672 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800673 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
674 break;
675 case htons(ETH_P_IPV6):
676 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800677 break;
678 default:
679 return 0;
680 }
681
682 return dscp >> 5;
683}
684EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200685
686const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
687{
Vladimir Kondratieve2176892012-07-25 13:56:53 +0300688 if (bss->information_elements == NULL)
Johannes Berg517357c2009-07-02 17:18:40 +0200689 return NULL;
Vladimir Kondratieve2176892012-07-25 13:56:53 +0300690 return cfg80211_find_ie(ie, bss->information_elements,
691 bss->len_information_elements);
Johannes Berg517357c2009-07-02 17:18:40 +0200692}
693EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200694
695void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
696{
697 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
698 struct net_device *dev = wdev->netdev;
699 int i;
700
701 if (!wdev->connect_keys)
702 return;
703
704 for (i = 0; i < 6; i++) {
705 if (!wdev->connect_keys->params[i].cipher)
706 continue;
Johannes Berge31b8212010-10-05 19:39:30 +0200707 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800708 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800709 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800710 continue;
711 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200712 if (wdev->connect_keys->def == i)
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100713 if (rdev->ops->set_default_key(wdev->wiphy, dev,
714 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)
719 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, 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
852 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
853 ntype, flags, params);
854
855 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
856
Johannes Berg9bc383d2009-11-19 11:55:19 +0100857 if (!err && params && params->use_4addr != -1)
858 dev->ieee80211_ptr->use_4addr = params->use_4addr;
859
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100860 if (!err) {
861 dev->priv_flags &= ~IFF_DONT_BRIDGE;
862 switch (ntype) {
863 case NL80211_IFTYPE_STATION:
864 if (dev->ieee80211_ptr->use_4addr)
865 break;
866 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200867 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100868 case NL80211_IFTYPE_ADHOC:
869 dev->priv_flags |= IFF_DONT_BRIDGE;
870 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200871 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100872 case NL80211_IFTYPE_AP:
873 case NL80211_IFTYPE_AP_VLAN:
874 case NL80211_IFTYPE_WDS:
875 case NL80211_IFTYPE_MESH_POINT:
876 /* bridging OK */
877 break;
878 case NL80211_IFTYPE_MONITOR:
879 /* monitor can't bridge anyway */
880 break;
881 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200882 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100883 /* not happening */
884 break;
Johannes Berg98104fde2012-06-16 00:19:54 +0200885 case NL80211_IFTYPE_P2P_DEVICE:
886 WARN_ON(1);
887 break;
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100888 }
889 }
890
Michal Kaziordbbae262012-06-29 12:47:01 +0200891 if (!err && ntype != otype && netif_running(dev)) {
892 cfg80211_update_iface_num(rdev, ntype, 1);
893 cfg80211_update_iface_num(rdev, otype, -1);
894 }
895
Johannes Berg3d54d252009-08-21 14:51:05 +0200896 return err;
897}
John W. Linville254416a2009-12-09 16:43:52 -0500898
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +0300899static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
900{
901 static const u32 __mcs2bitrate[] = {
902 /* control PHY */
903 [0] = 275,
904 /* SC PHY */
905 [1] = 3850,
906 [2] = 7700,
907 [3] = 9625,
908 [4] = 11550,
909 [5] = 12512, /* 1251.25 mbps */
910 [6] = 15400,
911 [7] = 19250,
912 [8] = 23100,
913 [9] = 25025,
914 [10] = 30800,
915 [11] = 38500,
916 [12] = 46200,
917 /* OFDM PHY */
918 [13] = 6930,
919 [14] = 8662, /* 866.25 mbps */
920 [15] = 13860,
921 [16] = 17325,
922 [17] = 20790,
923 [18] = 27720,
924 [19] = 34650,
925 [20] = 41580,
926 [21] = 45045,
927 [22] = 51975,
928 [23] = 62370,
929 [24] = 67568, /* 6756.75 mbps */
930 /* LP-SC PHY */
931 [25] = 6260,
932 [26] = 8340,
933 [27] = 11120,
934 [28] = 12510,
935 [29] = 16680,
936 [30] = 22240,
937 [31] = 25030,
938 };
939
940 if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
941 return 0;
942
943 return __mcs2bitrate[rate->mcs];
944}
945
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +0300946u32 cfg80211_calculate_bitrate(struct rate_info *rate)
John W. Linville254416a2009-12-09 16:43:52 -0500947{
948 int modulation, streams, bitrate;
949
950 if (!(rate->flags & RATE_INFO_FLAGS_MCS))
951 return rate->legacy;
Vladimir Kondratiev95ddc1f2012-07-05 14:25:50 +0300952 if (rate->flags & RATE_INFO_FLAGS_60G)
953 return cfg80211_calculate_bitrate_60g(rate);
John W. Linville254416a2009-12-09 16:43:52 -0500954
955 /* the formula below does only work for MCS values smaller than 32 */
Johannes Berg2615f372012-05-08 21:36:20 +0200956 if (WARN_ON_ONCE(rate->mcs >= 32))
John W. Linville254416a2009-12-09 16:43:52 -0500957 return 0;
958
959 modulation = rate->mcs & 7;
960 streams = (rate->mcs >> 3) + 1;
961
962 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
963 13500000 : 6500000;
964
965 if (modulation < 4)
966 bitrate *= (modulation + 1);
967 else if (modulation == 4)
968 bitrate *= (modulation + 2);
969 else
970 bitrate *= (modulation + 3);
971
972 bitrate *= streams;
973
974 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
975 bitrate = (bitrate / 9) * 10;
976
977 /* do NOT round down here */
978 return (bitrate + 50000) / 100000;
979}
Thomas Pedersen8097e142012-03-05 15:31:47 -0800980EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +0200981
982int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
983 u32 beacon_int)
984{
985 struct wireless_dev *wdev;
986 int res = 0;
987
988 if (!beacon_int)
989 return -EINVAL;
990
991 mutex_lock(&rdev->devlist_mtx);
992
Johannes Berg89a54e42012-06-15 14:33:17 +0200993 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Berg56d18932011-05-09 18:41:15 +0200994 if (!wdev->beacon_interval)
995 continue;
996 if (wdev->beacon_interval != beacon_int) {
997 res = -EINVAL;
998 break;
999 }
1000 }
1001
1002 mutex_unlock(&rdev->devlist_mtx);
1003
1004 return res;
1005}
Johannes Berg7527a782011-05-13 10:58:57 +02001006
Michal Kaziord4e50c52012-06-29 12:47:07 +02001007int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1008 struct wireless_dev *wdev,
1009 enum nl80211_iftype iftype,
1010 struct ieee80211_channel *chan,
1011 enum cfg80211_chan_mode chanmode)
Johannes Berg7527a782011-05-13 10:58:57 +02001012{
1013 struct wireless_dev *wdev_iter;
Johannes Berg463454b2012-06-05 12:16:50 +02001014 u32 used_iftypes = BIT(iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001015 int num[NUM_NL80211_IFTYPES];
Michal Kaziord4e50c52012-06-29 12:47:07 +02001016 struct ieee80211_channel
1017 *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS];
1018 struct ieee80211_channel *ch;
1019 enum cfg80211_chan_mode chmode;
1020 int num_different_channels = 0;
Johannes Berg7527a782011-05-13 10:58:57 +02001021 int total = 1;
1022 int i, j;
1023
1024 ASSERT_RTNL();
Michal Kaziore4e32452012-06-29 12:47:08 +02001025 lockdep_assert_held(&rdev->devlist_mtx);
Johannes Berg7527a782011-05-13 10:58:57 +02001026
1027 /* Always allow software iftypes */
1028 if (rdev->wiphy.software_iftypes & BIT(iftype))
1029 return 0;
1030
Johannes Berg7527a782011-05-13 10:58:57 +02001031 memset(num, 0, sizeof(num));
Michal Kaziord4e50c52012-06-29 12:47:07 +02001032 memset(used_channels, 0, sizeof(used_channels));
Johannes Berg7527a782011-05-13 10:58:57 +02001033
1034 num[iftype] = 1;
1035
Michal Kaziord4e50c52012-06-29 12:47:07 +02001036 switch (chanmode) {
1037 case CHAN_MODE_UNDEFINED:
1038 break;
1039 case CHAN_MODE_SHARED:
1040 WARN_ON(!chan);
1041 used_channels[0] = chan;
1042 num_different_channels++;
1043 break;
1044 case CHAN_MODE_EXCLUSIVE:
1045 num_different_channels++;
1046 break;
1047 }
1048
Johannes Berg89a54e42012-06-15 14:33:17 +02001049 list_for_each_entry(wdev_iter, &rdev->wdev_list, list) {
Johannes Berg7527a782011-05-13 10:58:57 +02001050 if (wdev_iter == wdev)
1051 continue;
Johannes Berg98104fde2012-06-16 00:19:54 +02001052 if (wdev_iter->netdev) {
1053 if (!netif_running(wdev_iter->netdev))
1054 continue;
1055 } else if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) {
1056 if (!wdev_iter->p2p_started)
1057 continue;
1058 } else {
1059 WARN_ON(1);
1060 }
Johannes Berg7527a782011-05-13 10:58:57 +02001061
1062 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1063 continue;
1064
Johannes Berg8e95ea42012-07-10 19:39:02 +02001065 /*
1066 * We may be holding the "wdev" mutex, but now need to lock
1067 * wdev_iter. This is OK because once we get here wdev_iter
1068 * is not wdev (tested above), but we need to use the nested
1069 * locking for lockdep.
1070 */
1071 mutex_lock_nested(&wdev_iter->mtx, 1);
1072 __acquire(wdev_iter->mtx);
1073 cfg80211_get_chan_state(wdev_iter, &ch, &chmode);
1074 wdev_unlock(wdev_iter);
Michal Kaziord4e50c52012-06-29 12:47:07 +02001075
1076 switch (chmode) {
1077 case CHAN_MODE_UNDEFINED:
1078 break;
1079 case CHAN_MODE_SHARED:
1080 for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++)
1081 if (!used_channels[i] || used_channels[i] == ch)
1082 break;
1083
Michal Kaziore4e32452012-06-29 12:47:08 +02001084 if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS)
Michal Kaziord4e50c52012-06-29 12:47:07 +02001085 return -EBUSY;
Michal Kaziord4e50c52012-06-29 12:47:07 +02001086
1087 if (used_channels[i] == NULL) {
1088 used_channels[i] = ch;
1089 num_different_channels++;
1090 }
1091 break;
1092 case CHAN_MODE_EXCLUSIVE:
1093 num_different_channels++;
1094 break;
1095 }
1096
Johannes Berg7527a782011-05-13 10:58:57 +02001097 num[wdev_iter->iftype]++;
1098 total++;
Johannes Berg463454b2012-06-05 12:16:50 +02001099 used_iftypes |= BIT(wdev_iter->iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001100 }
Johannes Berg7527a782011-05-13 10:58:57 +02001101
Johannes Berg8e8b41f2012-03-15 10:16:16 +01001102 if (total == 1)
1103 return 0;
1104
Johannes Berg7527a782011-05-13 10:58:57 +02001105 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1106 const struct ieee80211_iface_combination *c;
1107 struct ieee80211_iface_limit *limits;
Johannes Berg463454b2012-06-05 12:16:50 +02001108 u32 all_iftypes = 0;
Johannes Berg7527a782011-05-13 10:58:57 +02001109
1110 c = &rdev->wiphy.iface_combinations[i];
1111
Michal Kaziord4e50c52012-06-29 12:47:07 +02001112 if (total > c->max_interfaces)
1113 continue;
1114 if (num_different_channels > c->num_different_channels)
1115 continue;
1116
Johannes Berg7527a782011-05-13 10:58:57 +02001117 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1118 GFP_KERNEL);
1119 if (!limits)
1120 return -ENOMEM;
Johannes Berg7527a782011-05-13 10:58:57 +02001121
1122 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1123 if (rdev->wiphy.software_iftypes & BIT(iftype))
1124 continue;
1125 for (j = 0; j < c->n_limits; j++) {
Johannes Berg463454b2012-06-05 12:16:50 +02001126 all_iftypes |= limits[j].types;
Lukasz Kucharczyke55a4042012-04-11 14:55:10 +02001127 if (!(limits[j].types & BIT(iftype)))
Johannes Berg7527a782011-05-13 10:58:57 +02001128 continue;
1129 if (limits[j].max < num[iftype])
1130 goto cont;
1131 limits[j].max -= num[iftype];
1132 }
1133 }
Johannes Berg463454b2012-06-05 12:16:50 +02001134
1135 /*
1136 * Finally check that all iftypes that we're currently
1137 * using are actually part of this combination. If they
1138 * aren't then we can't use this combination and have
1139 * to continue to the next.
1140 */
1141 if ((all_iftypes & used_iftypes) != used_iftypes)
1142 goto cont;
1143
1144 /*
1145 * This combination covered all interface types and
1146 * supported the requested numbers, so we're good.
1147 */
Johannes Berg7527a782011-05-13 10:58:57 +02001148 kfree(limits);
1149 return 0;
1150 cont:
1151 kfree(limits);
1152 }
1153
1154 return -EBUSY;
1155}
Johannes Berg34850ab2011-07-18 18:08:35 +02001156
1157int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1158 const u8 *rates, unsigned int n_rates,
1159 u32 *mask)
1160{
1161 int i, j;
1162
Johannes Berga401d2b2011-07-20 00:52:16 +02001163 if (!sband)
1164 return -EINVAL;
1165
Johannes Berg34850ab2011-07-18 18:08:35 +02001166 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1167 return -EINVAL;
1168
1169 *mask = 0;
1170
1171 for (i = 0; i < n_rates; i++) {
1172 int rate = (rates[i] & 0x7f) * 5;
1173 bool found = false;
1174
1175 for (j = 0; j < sband->n_bitrates; j++) {
1176 if (sband->bitrates[j].bitrate == rate) {
1177 found = true;
1178 *mask |= BIT(j);
1179 break;
1180 }
1181 }
1182 if (!found)
1183 return -EINVAL;
1184 }
1185
1186 /*
1187 * mask must have at least one bit set here since we
1188 * didn't accept a 0-length rates array nor allowed
1189 * entries in the array that didn't exist
1190 */
1191
1192 return 0;
1193}
Johannes Berg11a2a352011-11-21 11:09:22 +01001194
1195/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1196/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1197const unsigned char rfc1042_header[] __aligned(2) =
1198 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1199EXPORT_SYMBOL(rfc1042_header);
1200
1201/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1202const unsigned char bridge_tunnel_header[] __aligned(2) =
1203 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1204EXPORT_SYMBOL(bridge_tunnel_header);