blob: 02504cfab1a4b5042d214e4c8649ed3c2b32eef0 [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 */
38 if (band == IEEE80211_BAND_5GHZ) {
39 if (chan >= 182 && chan <= 196)
40 return 4000 + chan * 5;
41 else
42 return 5000 + chan * 5;
43 } else { /* IEEE80211_BAND_2GHZ */
44 if (chan == 14)
45 return 2484;
46 else if (chan < 14)
47 return 2407 + chan * 5;
48 else
49 return 0; /* not supported */
50 }
Johannes Berg8318d782008-01-24 19:38:38 +010051}
52EXPORT_SYMBOL(ieee80211_channel_to_frequency);
53
54int ieee80211_frequency_to_channel(int freq)
55{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090056 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +010057 if (freq == 2484)
58 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090059 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +010060 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090061 else if (freq >= 4910 && freq <= 4980)
62 return (freq - 4000) / 5;
63 else
64 return (freq - 5000) / 5;
Johannes Berg8318d782008-01-24 19:38:38 +010065}
66EXPORT_SYMBOL(ieee80211_frequency_to_channel);
67
Johannes Berg6c507cd2008-03-26 14:14:55 +010068struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
69 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +010070{
71 enum ieee80211_band band;
72 struct ieee80211_supported_band *sband;
73 int i;
74
75 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
76 sband = wiphy->bands[band];
77
78 if (!sband)
79 continue;
80
81 for (i = 0; i < sband->n_channels; i++) {
82 if (sband->channels[i].center_freq == freq)
83 return &sband->channels[i];
84 }
85 }
86
87 return NULL;
88}
Johannes Berg6c507cd2008-03-26 14:14:55 +010089EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +010090
Johannes Berg8318d782008-01-24 19:38:38 +010091static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
92 enum ieee80211_band band)
93{
94 int i, want;
95
96 switch (band) {
97 case IEEE80211_BAND_5GHZ:
98 want = 3;
99 for (i = 0; i < sband->n_bitrates; i++) {
100 if (sband->bitrates[i].bitrate == 60 ||
101 sband->bitrates[i].bitrate == 120 ||
102 sband->bitrates[i].bitrate == 240) {
103 sband->bitrates[i].flags |=
104 IEEE80211_RATE_MANDATORY_A;
105 want--;
106 }
107 }
108 WARN_ON(want);
109 break;
110 case IEEE80211_BAND_2GHZ:
111 want = 7;
112 for (i = 0; i < sband->n_bitrates; i++) {
113 if (sband->bitrates[i].bitrate == 10) {
114 sband->bitrates[i].flags |=
115 IEEE80211_RATE_MANDATORY_B |
116 IEEE80211_RATE_MANDATORY_G;
117 want--;
118 }
119
120 if (sband->bitrates[i].bitrate == 20 ||
121 sband->bitrates[i].bitrate == 55 ||
122 sband->bitrates[i].bitrate == 110 ||
123 sband->bitrates[i].bitrate == 60 ||
124 sband->bitrates[i].bitrate == 120 ||
125 sband->bitrates[i].bitrate == 240) {
126 sband->bitrates[i].flags |=
127 IEEE80211_RATE_MANDATORY_G;
128 want--;
129 }
130
Johannes Bergaac09fb2008-01-30 17:36:10 +0100131 if (sband->bitrates[i].bitrate != 10 &&
132 sband->bitrates[i].bitrate != 20 &&
133 sband->bitrates[i].bitrate != 55 &&
134 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100135 sband->bitrates[i].flags |=
136 IEEE80211_RATE_ERP_G;
137 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100138 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100139 break;
140 case IEEE80211_NUM_BANDS:
141 WARN_ON(1);
142 break;
143 }
144}
145
146void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
147{
148 enum ieee80211_band band;
149
150 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
151 if (wiphy->bands[band])
152 set_mandatory_flags_band(wiphy->bands[band], band);
153}
Johannes Berg08645122009-05-11 13:54:58 +0200154
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300155bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
156{
157 int i;
158 for (i = 0; i < wiphy->n_cipher_suites; i++)
159 if (cipher == wiphy->cipher_suites[i])
160 return true;
161 return false;
162}
163
Johannes Bergfffd0932009-07-08 14:22:54 +0200164int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
165 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200166 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200167{
168 if (key_idx > 5)
169 return -EINVAL;
170
Johannes Berge31b8212010-10-05 19:39:30 +0200171 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
172 return -EINVAL;
173
174 if (pairwise && !mac_addr)
175 return -EINVAL;
176
Johannes Berg08645122009-05-11 13:54:58 +0200177 /*
178 * Disallow pairwise keys with non-zero index unless it's WEP
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200179 * or a vendor specific cipher (because current deployments use
180 * pairwise WEP keys with non-zero indices and for vendor specific
181 * ciphers this should be validated in the driver or hardware level
182 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200183 */
Johannes Berge31b8212010-10-05 19:39:30 +0200184 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200185 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
186 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
187 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200188 return -EINVAL;
189
Johannes Berg08645122009-05-11 13:54:58 +0200190 switch (params->cipher) {
191 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200192 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200193 return -EINVAL;
194 break;
195 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200196 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200197 return -EINVAL;
198 break;
199 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200200 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200201 return -EINVAL;
202 break;
203 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200204 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200205 return -EINVAL;
206 break;
207 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200208 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200209 return -EINVAL;
210 break;
Deepthi Gowri5b26a952011-11-29 11:22:42 +0530211 case WLAN_CIPHER_SUITE_SMS4:
212 if (params->key_len != WLAN_KEY_LEN_WAPI_SMS4)
213 return -EINVAL;
214 break;
Johannes Berg08645122009-05-11 13:54:58 +0200215 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300216 /*
217 * We don't know anything about this algorithm,
218 * allow using it -- but the driver must check
219 * all parameters! We still check below whether
220 * or not the driver supports this algorithm,
221 * of course.
222 */
223 break;
Johannes Berg08645122009-05-11 13:54:58 +0200224 }
225
Jouni Malinen9f26a952009-05-15 12:38:32 +0300226 if (params->seq) {
227 switch (params->cipher) {
228 case WLAN_CIPHER_SUITE_WEP40:
229 case WLAN_CIPHER_SUITE_WEP104:
230 /* These ciphers do not use key sequence */
231 return -EINVAL;
232 case WLAN_CIPHER_SUITE_TKIP:
233 case WLAN_CIPHER_SUITE_CCMP:
234 case WLAN_CIPHER_SUITE_AES_CMAC:
235 if (params->seq_len != 6)
236 return -EINVAL;
237 break;
238 }
239 }
240
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300241 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200242 return -EINVAL;
243
Johannes Berg08645122009-05-11 13:54:58 +0200244 return 0;
245}
Zhu Yie31a16d2009-05-21 21:47:03 +0800246
Johannes Berg633adf12010-08-12 14:49:58 +0200247unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800248{
249 unsigned int hdrlen = 24;
250
251 if (ieee80211_is_data(fc)) {
252 if (ieee80211_has_a4(fc))
253 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200254 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800255 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200256 if (ieee80211_has_order(fc))
257 hdrlen += IEEE80211_HT_CTL_LEN;
258 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800259 goto out;
260 }
261
262 if (ieee80211_is_ctl(fc)) {
263 /*
264 * ACK and CTS are 10 bytes, all others 16. To see how
265 * to get this condition consider
266 * subtype mask: 0b0000000011110000 (0x00F0)
267 * ACK subtype: 0b0000000011010000 (0x00D0)
268 * CTS subtype: 0b0000000011000000 (0x00C0)
269 * bits that matter: ^^^ (0x00E0)
270 * value of those: 0b0000000011000000 (0x00C0)
271 */
272 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
273 hdrlen = 10;
274 else
275 hdrlen = 16;
276 }
277out:
278 return hdrlen;
279}
280EXPORT_SYMBOL(ieee80211_hdrlen);
281
282unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
283{
284 const struct ieee80211_hdr *hdr =
285 (const struct ieee80211_hdr *)skb->data;
286 unsigned int hdrlen;
287
288 if (unlikely(skb->len < 10))
289 return 0;
290 hdrlen = ieee80211_hdrlen(hdr->frame_control);
291 if (unlikely(hdrlen > skb->len))
292 return 0;
293 return hdrlen;
294}
295EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
296
Johannes Berg44359902012-10-26 00:36:40 +0200297unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800298{
299 int ae = meshhdr->flags & MESH_FLAGS_AE;
Johannes Berg2404ce32012-10-25 21:51:59 +0200300 /* 802.11-2012, 8.2.4.7.3 */
Zhu Yie31a16d2009-05-21 21:47:03 +0800301 switch (ae) {
Johannes Berg2404ce32012-10-25 21:51:59 +0200302 default:
Zhu Yie31a16d2009-05-21 21:47:03 +0800303 case 0:
304 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700305 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800306 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700307 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800308 return 18;
Zhu Yie31a16d2009-05-21 21:47:03 +0800309 }
310}
Johannes Berg44359902012-10-26 00:36:40 +0200311EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
Zhu Yie31a16d2009-05-21 21:47:03 +0800312
Zhu Yieaf85ca2009-12-01 10:18:37 +0800313int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800314 enum nl80211_iftype iftype)
315{
316 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
317 u16 hdrlen, ethertype;
318 u8 *payload;
319 u8 dst[ETH_ALEN];
320 u8 src[ETH_ALEN] __aligned(2);
321
322 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
323 return -1;
324
325 hdrlen = ieee80211_hdrlen(hdr->frame_control);
326
327 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
328 * header
329 * IEEE 802.11 address fields:
330 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
331 * 0 0 DA SA BSSID n/a
332 * 0 1 DA BSSID SA n/a
333 * 1 0 BSSID SA DA n/a
334 * 1 1 RA TA DA SA
335 */
336 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
337 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
338
339 switch (hdr->frame_control &
340 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
341 case cpu_to_le16(IEEE80211_FCTL_TODS):
342 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200343 iftype != NL80211_IFTYPE_AP_VLAN &&
344 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800345 return -1;
346 break;
347 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
348 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543e2009-11-10 20:10:05 +0100349 iftype != NL80211_IFTYPE_MESH_POINT &&
350 iftype != NL80211_IFTYPE_AP_VLAN &&
351 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800352 return -1;
353 if (iftype == NL80211_IFTYPE_MESH_POINT) {
354 struct ieee80211s_hdr *meshdr =
355 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800356 /* make sure meshdr->flags is on the linear part */
357 if (!pskb_may_pull(skb, hdrlen + 1))
358 return -1;
Johannes Berg2404ce32012-10-25 21:51:59 +0200359 if (meshdr->flags & MESH_FLAGS_AE_A4)
360 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800361 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800362 skb_copy_bits(skb, hdrlen +
363 offsetof(struct ieee80211s_hdr, eaddr1),
364 dst, ETH_ALEN);
365 skb_copy_bits(skb, hdrlen +
366 offsetof(struct ieee80211s_hdr, eaddr2),
367 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800368 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800369 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800370 }
371 break;
372 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700373 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200374 iftype != NL80211_IFTYPE_P2P_CLIENT &&
375 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800376 (is_multicast_ether_addr(dst) &&
377 !compare_ether_addr(src, addr)))
378 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700379 if (iftype == NL80211_IFTYPE_MESH_POINT) {
380 struct ieee80211s_hdr *meshdr =
381 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800382 /* make sure meshdr->flags is on the linear part */
383 if (!pskb_may_pull(skb, hdrlen + 1))
384 return -1;
Johannes Berg2404ce32012-10-25 21:51:59 +0200385 if (meshdr->flags & MESH_FLAGS_AE_A5_A6)
386 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700387 if (meshdr->flags & MESH_FLAGS_AE_A4)
Zhu Yie3cf8b32010-03-29 17:35:07 +0800388 skb_copy_bits(skb, hdrlen +
389 offsetof(struct ieee80211s_hdr, eaddr1),
390 src, ETH_ALEN);
391 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Javier Cardona3c5772a2009-08-10 12:15:48 -0700392 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800393 break;
394 case cpu_to_le16(0):
Arik Nemtsov941c93c2011-09-28 14:12:54 +0300395 if (iftype != NL80211_IFTYPE_ADHOC &&
396 iftype != NL80211_IFTYPE_STATION)
397 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800398 break;
399 }
400
Zhu Yie3cf8b32010-03-29 17:35:07 +0800401 if (!pskb_may_pull(skb, hdrlen + 8))
Zhu Yie31a16d2009-05-21 21:47:03 +0800402 return -1;
403
404 payload = skb->data + hdrlen;
405 ethertype = (payload[6] << 8) | payload[7];
406
407 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
408 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
409 compare_ether_addr(payload, bridge_tunnel_header) == 0)) {
410 /* remove RFC1042 or Bridge-Tunnel encapsulation and
411 * replace EtherType */
412 skb_pull(skb, hdrlen + 6);
413 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
414 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
415 } else {
416 struct ethhdr *ehdr;
417 __be16 len;
418
419 skb_pull(skb, hdrlen);
420 len = htons(skb->len);
421 ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr));
422 memcpy(ehdr->h_dest, dst, ETH_ALEN);
423 memcpy(ehdr->h_source, src, ETH_ALEN);
424 ehdr->h_proto = len;
425 }
426 return 0;
427}
428EXPORT_SYMBOL(ieee80211_data_to_8023);
429
Zhu Yieaf85ca2009-12-01 10:18:37 +0800430int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800431 enum nl80211_iftype iftype, u8 *bssid, bool qos)
432{
433 struct ieee80211_hdr hdr;
434 u16 hdrlen, ethertype;
435 __le16 fc;
436 const u8 *encaps_data;
437 int encaps_len, skip_header_bytes;
438 int nh_pos, h_pos;
439 int head_need;
440
441 if (unlikely(skb->len < ETH_HLEN))
442 return -EINVAL;
443
444 nh_pos = skb_network_header(skb) - skb->data;
445 h_pos = skb_transport_header(skb) - skb->data;
446
447 /* convert Ethernet header to proper 802.11 header (based on
448 * operation mode) */
449 ethertype = (skb->data[12] << 8) | skb->data[13];
450 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
451
452 switch (iftype) {
453 case NL80211_IFTYPE_AP:
454 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200455 case NL80211_IFTYPE_P2P_GO:
Zhu Yie31a16d2009-05-21 21:47:03 +0800456 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
457 /* DA BSSID SA */
458 memcpy(hdr.addr1, skb->data, ETH_ALEN);
459 memcpy(hdr.addr2, addr, ETH_ALEN);
460 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
461 hdrlen = 24;
462 break;
463 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200464 case NL80211_IFTYPE_P2P_CLIENT:
Zhu Yie31a16d2009-05-21 21:47:03 +0800465 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
466 /* BSSID SA DA */
467 memcpy(hdr.addr1, bssid, ETH_ALEN);
468 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
469 memcpy(hdr.addr3, skb->data, ETH_ALEN);
470 hdrlen = 24;
471 break;
472 case NL80211_IFTYPE_ADHOC:
473 /* DA SA BSSID */
474 memcpy(hdr.addr1, skb->data, ETH_ALEN);
475 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
476 memcpy(hdr.addr3, bssid, ETH_ALEN);
477 hdrlen = 24;
478 break;
479 default:
480 return -EOPNOTSUPP;
481 }
482
483 if (qos) {
484 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
485 hdrlen += 2;
486 }
487
488 hdr.frame_control = fc;
489 hdr.duration_id = 0;
490 hdr.seq_ctrl = 0;
491
492 skip_header_bytes = ETH_HLEN;
493 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
494 encaps_data = bridge_tunnel_header;
495 encaps_len = sizeof(bridge_tunnel_header);
496 skip_header_bytes -= 2;
497 } else if (ethertype > 0x600) {
498 encaps_data = rfc1042_header;
499 encaps_len = sizeof(rfc1042_header);
500 skip_header_bytes -= 2;
501 } else {
502 encaps_data = NULL;
503 encaps_len = 0;
504 }
505
506 skb_pull(skb, skip_header_bytes);
507 nh_pos -= skip_header_bytes;
508 h_pos -= skip_header_bytes;
509
510 head_need = hdrlen + encaps_len - skb_headroom(skb);
511
512 if (head_need > 0 || skb_cloned(skb)) {
513 head_need = max(head_need, 0);
514 if (head_need)
515 skb_orphan(skb);
516
Joe Perches24616152011-08-29 14:17:41 -0700517 if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
Zhu Yie31a16d2009-05-21 21:47:03 +0800518 return -ENOMEM;
Joe Perches24616152011-08-29 14:17:41 -0700519
Zhu Yie31a16d2009-05-21 21:47:03 +0800520 skb->truesize += head_need;
521 }
522
523 if (encaps_data) {
524 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
525 nh_pos += encaps_len;
526 h_pos += encaps_len;
527 }
528
529 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
530
531 nh_pos += hdrlen;
532 h_pos += hdrlen;
533
534 /* Update skb pointers to various headers since this modified frame
535 * is going to go through Linux networking code that may potentially
536 * need things like pointer to IP header. */
537 skb_set_mac_header(skb, 0);
538 skb_set_network_header(skb, nh_pos);
539 skb_set_transport_header(skb, h_pos);
540
541 return 0;
542}
543EXPORT_SYMBOL(ieee80211_data_from_8023);
544
Zhu Yieaf85ca2009-12-01 10:18:37 +0800545
546void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
547 const u8 *addr, enum nl80211_iftype iftype,
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700548 const unsigned int extra_headroom,
549 bool has_80211_header)
Zhu Yieaf85ca2009-12-01 10:18:37 +0800550{
551 struct sk_buff *frame = NULL;
552 u16 ethertype;
553 u8 *payload;
554 const struct ethhdr *eth;
555 int remaining, err;
556 u8 dst[ETH_ALEN], src[ETH_ALEN];
557
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700558 if (has_80211_header) {
559 err = ieee80211_data_to_8023(skb, addr, iftype);
560 if (err)
561 goto out;
Zhu Yieaf85ca2009-12-01 10:18:37 +0800562
Yogesh Ashok Powar8b3beca2011-05-13 11:22:31 -0700563 /* skip the wrapping header */
564 eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr));
565 if (!eth)
566 goto out;
567 } else {
568 eth = (struct ethhdr *) skb->data;
569 }
Zhu Yieaf85ca2009-12-01 10:18:37 +0800570
571 while (skb != frame) {
572 u8 padding;
573 __be16 len = eth->h_proto;
574 unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len);
575
576 remaining = skb->len;
577 memcpy(dst, eth->h_dest, ETH_ALEN);
578 memcpy(src, eth->h_source, ETH_ALEN);
579
580 padding = (4 - subframe_len) & 0x3;
581 /* the last MSDU has no padding */
582 if (subframe_len > remaining)
583 goto purge;
584
585 skb_pull(skb, sizeof(struct ethhdr));
586 /* reuse skb for the last subframe */
587 if (remaining <= subframe_len + padding)
588 frame = skb;
589 else {
590 unsigned int hlen = ALIGN(extra_headroom, 4);
591 /*
592 * Allocate and reserve two bytes more for payload
593 * alignment since sizeof(struct ethhdr) is 14.
594 */
595 frame = dev_alloc_skb(hlen + subframe_len + 2);
596 if (!frame)
597 goto purge;
598
599 skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
600 memcpy(skb_put(frame, ntohs(len)), skb->data,
601 ntohs(len));
602
603 eth = (struct ethhdr *)skb_pull(skb, ntohs(len) +
604 padding);
605 if (!eth) {
606 dev_kfree_skb(frame);
607 goto purge;
608 }
609 }
610
611 skb_reset_network_header(frame);
612 frame->dev = skb->dev;
613 frame->priority = skb->priority;
614
615 payload = frame->data;
616 ethertype = (payload[6] << 8) | payload[7];
617
618 if (likely((compare_ether_addr(payload, rfc1042_header) == 0 &&
619 ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
620 compare_ether_addr(payload,
621 bridge_tunnel_header) == 0)) {
622 /* remove RFC1042 or Bridge-Tunnel
623 * encapsulation and replace EtherType */
624 skb_pull(frame, 6);
625 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
626 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
627 } else {
628 memcpy(skb_push(frame, sizeof(__be16)), &len,
629 sizeof(__be16));
630 memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN);
631 memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN);
632 }
633 __skb_queue_tail(list, frame);
634 }
635
636 return;
637
638 purge:
639 __skb_queue_purge(list);
640 out:
641 dev_kfree_skb(skb);
642}
643EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
644
Zhu Yie31a16d2009-05-21 21:47:03 +0800645/* Given a data frame determine the 802.1p/1d tag to use. */
646unsigned int cfg80211_classify8021d(struct sk_buff *skb)
647{
648 unsigned int dscp;
649
650 /* skb->priority values from 256->263 are magic values to
651 * directly indicate a specific 802.1d priority. This is used
652 * to allow 802.1d priority to be passed directly in from VLAN
653 * tags, etc.
654 */
655 if (skb->priority >= 256 && skb->priority <= 263)
656 return skb->priority - 256;
657
658 switch (skb->protocol) {
659 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800660 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
661 break;
662 case htons(ETH_P_IPV6):
663 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800664 break;
665 default:
666 return 0;
667 }
668
669 return dscp >> 5;
670}
671EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200672
673const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
674{
675 u8 *end, *pos;
676
677 pos = bss->information_elements;
678 if (pos == NULL)
679 return NULL;
680 end = pos + bss->len_information_elements;
681
682 while (pos + 1 < end) {
683 if (pos + 2 + pos[1] > end)
684 break;
685 if (pos[0] == ie)
686 return pos;
687 pos += 2 + pos[1];
688 }
689
690 return NULL;
691}
692EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200693
694void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
695{
696 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
697 struct net_device *dev = wdev->netdev;
698 int i;
699
700 if (!wdev->connect_keys)
701 return;
702
703 for (i = 0; i < 6; i++) {
704 if (!wdev->connect_keys->params[i].cipher)
705 continue;
Johannes Berge31b8212010-10-05 19:39:30 +0200706 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800707 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800708 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800709 continue;
710 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200711 if (wdev->connect_keys->def == i)
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100712 if (rdev->ops->set_default_key(wdev->wiphy, dev,
713 i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800714 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800715 continue;
716 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200717 if (wdev->connect_keys->defmgmt == i)
718 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800719 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200720 }
721
722 kfree(wdev->connect_keys);
723 wdev->connect_keys = NULL;
724}
Johannes Berg3d54d252009-08-21 14:51:05 +0200725
Daniel Drake25320e72012-08-02 18:41:48 +0100726void cfg80211_process_wdev_events(struct wireless_dev *wdev)
Johannes Berg3d54d252009-08-21 14:51:05 +0200727{
728 struct cfg80211_event *ev;
729 unsigned long flags;
730 const u8 *bssid = NULL;
731
732 spin_lock_irqsave(&wdev->event_lock, flags);
733 while (!list_empty(&wdev->event_list)) {
734 ev = list_first_entry(&wdev->event_list,
735 struct cfg80211_event, list);
736 list_del(&ev->list);
737 spin_unlock_irqrestore(&wdev->event_lock, flags);
738
739 wdev_lock(wdev);
740 switch (ev->type) {
741 case EVENT_CONNECT_RESULT:
742 if (!is_zero_ether_addr(ev->cr.bssid))
743 bssid = ev->cr.bssid;
744 __cfg80211_connect_result(
745 wdev->netdev, bssid,
746 ev->cr.req_ie, ev->cr.req_ie_len,
747 ev->cr.resp_ie, ev->cr.resp_ie_len,
748 ev->cr.status,
749 ev->cr.status == WLAN_STATUS_SUCCESS,
750 NULL);
751 break;
752 case EVENT_ROAMED:
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530753 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
754 ev->rm.req_ie_len, ev->rm.resp_ie,
755 ev->rm.resp_ie_len);
Johannes Berg3d54d252009-08-21 14:51:05 +0200756 break;
757 case EVENT_DISCONNECTED:
758 __cfg80211_disconnected(wdev->netdev,
759 ev->dc.ie, ev->dc.ie_len,
760 ev->dc.reason, true);
761 break;
762 case EVENT_IBSS_JOINED:
763 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
764 break;
765 }
766 wdev_unlock(wdev);
767
768 kfree(ev);
769
770 spin_lock_irqsave(&wdev->event_lock, flags);
771 }
772 spin_unlock_irqrestore(&wdev->event_lock, flags);
773}
774
775void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
776{
777 struct wireless_dev *wdev;
778
779 ASSERT_RTNL();
780 ASSERT_RDEV_LOCK(rdev);
781
782 mutex_lock(&rdev->devlist_mtx);
783
784 list_for_each_entry(wdev, &rdev->netdev_list, list)
785 cfg80211_process_wdev_events(wdev);
786
787 mutex_unlock(&rdev->devlist_mtx);
788}
789
790int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
791 struct net_device *dev, enum nl80211_iftype ntype,
792 u32 *flags, struct vif_params *params)
793{
794 int err;
795 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
796
797 ASSERT_RDEV_LOCK(rdev);
798
799 /* don't support changing VLANs, you just re-create them */
800 if (otype == NL80211_IFTYPE_AP_VLAN)
801 return -EOPNOTSUPP;
802
803 if (!rdev->ops->change_virtual_intf ||
804 !(rdev->wiphy.interface_modes & (1 << ntype)))
805 return -EOPNOTSUPP;
806
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100807 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a2010-06-15 06:50:45 +0000808 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200809 (ntype == NL80211_IFTYPE_ADHOC ||
810 ntype == NL80211_IFTYPE_STATION ||
811 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100812 return -EBUSY;
813
Michal Kazior738c88c2012-06-08 10:55:44 +0200814 if (ntype != otype && netif_running(dev)) {
Johannes Berg7527a782011-05-13 10:58:57 +0200815 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
816 ntype);
817 if (err)
818 return err;
819
Johannes Berg9bc383d2009-11-19 11:55:19 +0100820 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100821 dev->ieee80211_ptr->mesh_id_up_len = 0;
Johannes Berg9bc383d2009-11-19 11:55:19 +0100822
Johannes Berg3d54d252009-08-21 14:51:05 +0200823 switch (otype) {
824 case NL80211_IFTYPE_ADHOC:
825 cfg80211_leave_ibss(rdev, dev, false);
826 break;
827 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200828 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200829 cfg80211_disconnect(rdev, dev,
830 WLAN_REASON_DEAUTH_LEAVING, true);
831 break;
832 case NL80211_IFTYPE_MESH_POINT:
833 /* mesh should be handled? */
834 break;
835 default:
836 break;
837 }
838
839 cfg80211_process_rdev_events(rdev);
840 }
841
842 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
843 ntype, flags, params);
844
845 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
846
Johannes Berg9bc383d2009-11-19 11:55:19 +0100847 if (!err && params && params->use_4addr != -1)
848 dev->ieee80211_ptr->use_4addr = params->use_4addr;
849
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100850 if (!err) {
851 dev->priv_flags &= ~IFF_DONT_BRIDGE;
852 switch (ntype) {
853 case NL80211_IFTYPE_STATION:
854 if (dev->ieee80211_ptr->use_4addr)
855 break;
856 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200857 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100858 case NL80211_IFTYPE_ADHOC:
859 dev->priv_flags |= IFF_DONT_BRIDGE;
860 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200861 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100862 case NL80211_IFTYPE_AP:
863 case NL80211_IFTYPE_AP_VLAN:
864 case NL80211_IFTYPE_WDS:
865 case NL80211_IFTYPE_MESH_POINT:
866 /* bridging OK */
867 break;
868 case NL80211_IFTYPE_MONITOR:
869 /* monitor can't bridge anyway */
870 break;
871 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200872 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100873 /* not happening */
874 break;
875 }
876 }
877
Johannes Berg3d54d252009-08-21 14:51:05 +0200878 return err;
879}
John W. Linville254416a2009-12-09 16:43:52 -0500880
Sameer Thalappil92262232013-03-19 14:28:37 -0700881static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
882{
883 static const u32 base[4][10] = {
884 { 6500000,
885 13000000,
886 19500000,
887 26000000,
888 39000000,
889 52000000,
890 58500000,
891 65000000,
892 78000000,
893 0,
894 },
895 { 13500000,
896 27000000,
897 40500000,
898 54000000,
899 81000000,
900 108000000,
901 121500000,
902 135000000,
903 162000000,
904 180000000,
905 },
906 { 29300000,
907 58500000,
908 87800000,
909 117000000,
910 175500000,
911 234000000,
912 263300000,
913 292500000,
914 351000000,
915 390000000,
916 },
917 { 58500000,
918 117000000,
919 175500000,
920 234000000,
921 351000000,
922 468000000,
923 526500000,
924 585000000,
925 702000000,
926 780000000,
927 },
928 };
929 u32 bitrate;
930 int idx;
931
932 if (WARN_ON_ONCE(rate->mcs > 9))
933 return 0;
934
935 idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
936 RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
937 rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
938 rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
939
940 bitrate = base[idx][rate->mcs];
941 bitrate *= rate->nss;
942
943 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
944 bitrate = (bitrate / 9) * 10;
945
946 /* do NOT round down here */
947 return (bitrate + 50000) / 100000;
948}
949
John W. Linville254416a2009-12-09 16:43:52 -0500950u16 cfg80211_calculate_bitrate(struct rate_info *rate)
951{
952 int modulation, streams, bitrate;
953
Sameer Thalappil92262232013-03-19 14:28:37 -0700954 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
955 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
John W. Linville254416a2009-12-09 16:43:52 -0500956 return rate->legacy;
957
Sameer Thalappil92262232013-03-19 14:28:37 -0700958 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
959 return cfg80211_calculate_bitrate_vht(rate);
960
John W. Linville254416a2009-12-09 16:43:52 -0500961 /* the formula below does only work for MCS values smaller than 32 */
962 if (rate->mcs >= 32)
963 return 0;
964
965 modulation = rate->mcs & 7;
966 streams = (rate->mcs >> 3) + 1;
967
968 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
969 13500000 : 6500000;
970
971 if (modulation < 4)
972 bitrate *= (modulation + 1);
973 else if (modulation == 4)
974 bitrate *= (modulation + 2);
975 else
976 bitrate *= (modulation + 3);
977
978 bitrate *= streams;
979
980 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
981 bitrate = (bitrate / 9) * 10;
982
983 /* do NOT round down here */
984 return (bitrate + 50000) / 100000;
985}
Thomas Pedersen8097e142012-03-05 15:31:47 -0800986EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +0200987
988int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
989 u32 beacon_int)
990{
991 struct wireless_dev *wdev;
992 int res = 0;
993
994 if (!beacon_int)
995 return -EINVAL;
996
997 mutex_lock(&rdev->devlist_mtx);
998
999 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1000 if (!wdev->beacon_interval)
1001 continue;
1002 if (wdev->beacon_interval != beacon_int) {
1003 res = -EINVAL;
1004 break;
1005 }
1006 }
1007
1008 mutex_unlock(&rdev->devlist_mtx);
1009
1010 return res;
1011}
Johannes Berg7527a782011-05-13 10:58:57 +02001012
1013int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
1014 struct wireless_dev *wdev,
1015 enum nl80211_iftype iftype)
1016{
1017 struct wireless_dev *wdev_iter;
Johannes Berg6fa9c342012-06-05 12:16:50 +02001018 u32 used_iftypes = BIT(iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001019 int num[NUM_NL80211_IFTYPES];
1020 int total = 1;
1021 int i, j;
1022
1023 ASSERT_RTNL();
1024
1025 /* Always allow software iftypes */
1026 if (rdev->wiphy.software_iftypes & BIT(iftype))
1027 return 0;
1028
1029 /*
1030 * Drivers will gradually all set this flag, until all
1031 * have it we only enforce for those that set it.
1032 */
1033 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
1034 return 0;
1035
1036 memset(num, 0, sizeof(num));
1037
1038 num[iftype] = 1;
1039
1040 mutex_lock(&rdev->devlist_mtx);
1041 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
1042 if (wdev_iter == wdev)
1043 continue;
1044 if (!netif_running(wdev_iter->netdev))
1045 continue;
1046
1047 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1048 continue;
1049
1050 num[wdev_iter->iftype]++;
1051 total++;
Johannes Berg6fa9c342012-06-05 12:16:50 +02001052 used_iftypes |= BIT(wdev_iter->iftype);
Johannes Berg7527a782011-05-13 10:58:57 +02001053 }
1054 mutex_unlock(&rdev->devlist_mtx);
1055
Liang Li510f1d12012-08-02 18:55:41 -04001056 if (total == 1)
1057 return 0;
1058
Johannes Berg7527a782011-05-13 10:58:57 +02001059 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1060 const struct ieee80211_iface_combination *c;
1061 struct ieee80211_iface_limit *limits;
Johannes Berg6fa9c342012-06-05 12:16:50 +02001062 u32 all_iftypes = 0;
Johannes Berg7527a782011-05-13 10:58:57 +02001063
1064 c = &rdev->wiphy.iface_combinations[i];
1065
1066 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1067 GFP_KERNEL);
1068 if (!limits)
1069 return -ENOMEM;
1070 if (total > c->max_interfaces)
1071 goto cont;
1072
1073 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1074 if (rdev->wiphy.software_iftypes & BIT(iftype))
1075 continue;
1076 for (j = 0; j < c->n_limits; j++) {
Johannes Berg6fa9c342012-06-05 12:16:50 +02001077 all_iftypes |= limits[j].types;
Lukasz Kucharczyke55a4042012-04-11 14:55:10 +02001078 if (!(limits[j].types & BIT(iftype)))
Johannes Berg7527a782011-05-13 10:58:57 +02001079 continue;
1080 if (limits[j].max < num[iftype])
1081 goto cont;
1082 limits[j].max -= num[iftype];
1083 }
1084 }
Johannes Berg6fa9c342012-06-05 12:16:50 +02001085
1086 /*
1087 * Finally check that all iftypes that we're currently
1088 * using are actually part of this combination. If they
1089 * aren't then we can't use this combination and have
1090 * to continue to the next.
1091 */
1092 if ((all_iftypes & used_iftypes) != used_iftypes)
1093 goto cont;
1094
1095 /*
1096 * This combination covered all interface types and
1097 * supported the requested numbers, so we're good.
1098 */
Johannes Berg7527a782011-05-13 10:58:57 +02001099 kfree(limits);
1100 return 0;
1101 cont:
1102 kfree(limits);
1103 }
1104
1105 return -EBUSY;
1106}
Johannes Berg34850ab2011-07-18 18:08:35 +02001107
1108int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1109 const u8 *rates, unsigned int n_rates,
1110 u32 *mask)
1111{
1112 int i, j;
1113
Johannes Berga401d2b2011-07-20 00:52:16 +02001114 if (!sband)
1115 return -EINVAL;
1116
Johannes Berg34850ab2011-07-18 18:08:35 +02001117 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1118 return -EINVAL;
1119
1120 *mask = 0;
1121
1122 for (i = 0; i < n_rates; i++) {
1123 int rate = (rates[i] & 0x7f) * 5;
1124 bool found = false;
1125
1126 for (j = 0; j < sband->n_bitrates; j++) {
1127 if (sband->bitrates[j].bitrate == rate) {
1128 found = true;
1129 *mask |= BIT(j);
1130 break;
1131 }
1132 }
1133 if (!found)
1134 return -EINVAL;
1135 }
Johannes Berg34850ab2011-07-18 18:08:35 +02001136 /*
1137 * mask must have at least one bit set here since we
1138 * didn't accept a 0-length rates array nor allowed
1139 * entries in the array that didn't exist
1140 */
1141
1142 return 0;
1143}
Johannes Berg11a2a352011-11-21 11:09:22 +01001144
1145/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
1146/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
1147const unsigned char rfc1042_header[] __aligned(2) =
1148 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
1149EXPORT_SYMBOL(rfc1042_header);
1150
1151/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
1152const unsigned char bridge_tunnel_header[] __aligned(2) =
1153 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
1154EXPORT_SYMBOL(bridge_tunnel_header);