blob: b83c5b24e782ff8d6498220d4e29cdb55c11295e [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 Berg55ef7702014-02-25 15:04:46 -080013#include <net/ndisc.h>
14#include <linux/if_arp.h>
Johannes Berg8318d782008-01-24 19:38:38 +010015#include "core.h"
16
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 */
40 if (band == IEEE80211_BAND_5GHZ) {
41 if (chan >= 182 && chan <= 196)
42 return 4000 + chan * 5;
43 else
44 return 5000 + chan * 5;
45 } else { /* IEEE80211_BAND_2GHZ */
46 if (chan == 14)
47 return 2484;
48 else if (chan < 14)
49 return 2407 + chan * 5;
50 else
51 return 0; /* not supported */
52 }
Johannes Berg8318d782008-01-24 19:38:38 +010053}
54EXPORT_SYMBOL(ieee80211_channel_to_frequency);
55
56int ieee80211_frequency_to_channel(int freq)
57{
Bruno Randolf59eb21a2011-01-17 13:37:28 +090058 /* see 802.11 17.3.8.3.2 and Annex J */
Johannes Berg8318d782008-01-24 19:38:38 +010059 if (freq == 2484)
60 return 14;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090061 else if (freq < 2484)
Johannes Berg8318d782008-01-24 19:38:38 +010062 return (freq - 2407) / 5;
Bruno Randolf59eb21a2011-01-17 13:37:28 +090063 else if (freq >= 4910 && freq <= 4980)
64 return (freq - 4000) / 5;
65 else
66 return (freq - 5000) / 5;
Johannes Berg8318d782008-01-24 19:38:38 +010067}
68EXPORT_SYMBOL(ieee80211_frequency_to_channel);
69
Johannes Berg6c507cd2008-03-26 14:14:55 +010070struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy,
71 int freq)
Johannes Berg906c7302008-03-16 18:34:33 +010072{
73 enum ieee80211_band band;
74 struct ieee80211_supported_band *sband;
75 int i;
76
77 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
78 sband = wiphy->bands[band];
79
80 if (!sband)
81 continue;
82
83 for (i = 0; i < sband->n_channels; i++) {
84 if (sband->channels[i].center_freq == freq)
85 return &sband->channels[i];
86 }
87 }
88
89 return NULL;
90}
Johannes Berg6c507cd2008-03-26 14:14:55 +010091EXPORT_SYMBOL(__ieee80211_get_channel);
Johannes Berg906c7302008-03-16 18:34:33 +010092
Johannes Berg8318d782008-01-24 19:38:38 +010093static void set_mandatory_flags_band(struct ieee80211_supported_band *sband,
94 enum ieee80211_band band)
95{
96 int i, want;
97
98 switch (band) {
99 case IEEE80211_BAND_5GHZ:
100 want = 3;
101 for (i = 0; i < sband->n_bitrates; i++) {
102 if (sband->bitrates[i].bitrate == 60 ||
103 sband->bitrates[i].bitrate == 120 ||
104 sband->bitrates[i].bitrate == 240) {
105 sband->bitrates[i].flags |=
106 IEEE80211_RATE_MANDATORY_A;
107 want--;
108 }
109 }
110 WARN_ON(want);
111 break;
112 case IEEE80211_BAND_2GHZ:
113 want = 7;
114 for (i = 0; i < sband->n_bitrates; i++) {
115 if (sband->bitrates[i].bitrate == 10) {
116 sband->bitrates[i].flags |=
117 IEEE80211_RATE_MANDATORY_B |
118 IEEE80211_RATE_MANDATORY_G;
119 want--;
120 }
121
122 if (sband->bitrates[i].bitrate == 20 ||
123 sband->bitrates[i].bitrate == 55 ||
124 sband->bitrates[i].bitrate == 110 ||
125 sband->bitrates[i].bitrate == 60 ||
126 sband->bitrates[i].bitrate == 120 ||
127 sband->bitrates[i].bitrate == 240) {
128 sband->bitrates[i].flags |=
129 IEEE80211_RATE_MANDATORY_G;
130 want--;
131 }
132
Johannes Bergaac09fb2008-01-30 17:36:10 +0100133 if (sband->bitrates[i].bitrate != 10 &&
134 sband->bitrates[i].bitrate != 20 &&
135 sband->bitrates[i].bitrate != 55 &&
136 sband->bitrates[i].bitrate != 110)
Johannes Berg8318d782008-01-24 19:38:38 +0100137 sband->bitrates[i].flags |=
138 IEEE80211_RATE_ERP_G;
139 }
Ivo van Doorn406f2382008-02-02 23:53:10 +0100140 WARN_ON(want != 0 && want != 3 && want != 6);
Johannes Berg8318d782008-01-24 19:38:38 +0100141 break;
142 case IEEE80211_NUM_BANDS:
143 WARN_ON(1);
144 break;
145 }
146}
147
148void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
149{
150 enum ieee80211_band band;
151
152 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
153 if (wiphy->bands[band])
154 set_mandatory_flags_band(wiphy->bands[band], band);
155}
Johannes Berg08645122009-05-11 13:54:58 +0200156
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300157bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
158{
159 int i;
160 for (i = 0; i < wiphy->n_cipher_suites; i++)
161 if (cipher == wiphy->cipher_suites[i])
162 return true;
163 return false;
164}
165
Johannes Bergfffd0932009-07-08 14:22:54 +0200166int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
167 struct key_params *params, int key_idx,
Johannes Berge31b8212010-10-05 19:39:30 +0200168 bool pairwise, const u8 *mac_addr)
Johannes Berg08645122009-05-11 13:54:58 +0200169{
170 if (key_idx > 5)
171 return -EINVAL;
172
Johannes Berge31b8212010-10-05 19:39:30 +0200173 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
174 return -EINVAL;
175
176 if (pairwise && !mac_addr)
177 return -EINVAL;
178
Johannes Berg08645122009-05-11 13:54:58 +0200179 /*
180 * Disallow pairwise keys with non-zero index unless it's WEP
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200181 * or a vendor specific cipher (because current deployments use
182 * pairwise WEP keys with non-zero indices and for vendor specific
183 * ciphers this should be validated in the driver or hardware level
184 * - but 802.11i clearly specifies to use zero)
Johannes Berg08645122009-05-11 13:54:58 +0200185 */
Johannes Berge31b8212010-10-05 19:39:30 +0200186 if (pairwise && key_idx &&
Juuso Oikarinen45cbad62011-01-25 12:21:22 +0200187 ((params->cipher == WLAN_CIPHER_SUITE_TKIP) ||
188 (params->cipher == WLAN_CIPHER_SUITE_CCMP) ||
189 (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC)))
Johannes Berg08645122009-05-11 13:54:58 +0200190 return -EINVAL;
191
Johannes Berg08645122009-05-11 13:54:58 +0200192 switch (params->cipher) {
193 case WLAN_CIPHER_SUITE_WEP40:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200194 if (params->key_len != WLAN_KEY_LEN_WEP40)
Johannes Berg08645122009-05-11 13:54:58 +0200195 return -EINVAL;
196 break;
197 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200198 if (params->key_len != WLAN_KEY_LEN_TKIP)
Johannes Berg08645122009-05-11 13:54:58 +0200199 return -EINVAL;
200 break;
201 case WLAN_CIPHER_SUITE_CCMP:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200202 if (params->key_len != WLAN_KEY_LEN_CCMP)
Johannes Berg08645122009-05-11 13:54:58 +0200203 return -EINVAL;
204 break;
205 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200206 if (params->key_len != WLAN_KEY_LEN_WEP104)
Johannes Berg08645122009-05-11 13:54:58 +0200207 return -EINVAL;
208 break;
209 case WLAN_CIPHER_SUITE_AES_CMAC:
Johannes Berg8fc0fee2009-05-24 16:57:19 +0200210 if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
Johannes Berg08645122009-05-11 13:54:58 +0200211 return -EINVAL;
212 break;
Deepthi Gowri5b26a952011-11-29 11:22:42 +0530213 case WLAN_CIPHER_SUITE_SMS4:
214 if (params->key_len != WLAN_KEY_LEN_WAPI_SMS4)
215 return -EINVAL;
216 break;
Johannes Berg08645122009-05-11 13:54:58 +0200217 default:
Johannes Berg7d64b7c2010-08-27 14:26:51 +0300218 /*
219 * We don't know anything about this algorithm,
220 * allow using it -- but the driver must check
221 * all parameters! We still check below whether
222 * or not the driver supports this algorithm,
223 * of course.
224 */
225 break;
Johannes Berg08645122009-05-11 13:54:58 +0200226 }
227
Jouni Malinen9f26a952009-05-15 12:38:32 +0300228 if (params->seq) {
229 switch (params->cipher) {
230 case WLAN_CIPHER_SUITE_WEP40:
231 case WLAN_CIPHER_SUITE_WEP104:
232 /* These ciphers do not use key sequence */
233 return -EINVAL;
234 case WLAN_CIPHER_SUITE_TKIP:
235 case WLAN_CIPHER_SUITE_CCMP:
236 case WLAN_CIPHER_SUITE_AES_CMAC:
237 if (params->seq_len != 6)
238 return -EINVAL;
239 break;
240 }
241 }
242
Jouni Malinen38ba3c52011-09-21 18:14:56 +0300243 if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
Johannes Bergfffd0932009-07-08 14:22:54 +0200244 return -EINVAL;
245
Johannes Berg08645122009-05-11 13:54:58 +0200246 return 0;
247}
Zhu Yie31a16d2009-05-21 21:47:03 +0800248
Johannes Berg633adf12010-08-12 14:49:58 +0200249unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
Zhu Yie31a16d2009-05-21 21:47:03 +0800250{
251 unsigned int hdrlen = 24;
252
253 if (ieee80211_is_data(fc)) {
254 if (ieee80211_has_a4(fc))
255 hdrlen = 30;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200256 if (ieee80211_is_data_qos(fc)) {
Zhu Yie31a16d2009-05-21 21:47:03 +0800257 hdrlen += IEEE80211_QOS_CTL_LEN;
Andriy Tkachukd0dd2de2010-01-20 13:55:06 +0200258 if (ieee80211_has_order(fc))
259 hdrlen += IEEE80211_HT_CTL_LEN;
260 }
Zhu Yie31a16d2009-05-21 21:47:03 +0800261 goto out;
262 }
263
264 if (ieee80211_is_ctl(fc)) {
265 /*
266 * ACK and CTS are 10 bytes, all others 16. To see how
267 * to get this condition consider
268 * subtype mask: 0b0000000011110000 (0x00F0)
269 * ACK subtype: 0b0000000011010000 (0x00D0)
270 * CTS subtype: 0b0000000011000000 (0x00C0)
271 * bits that matter: ^^^ (0x00E0)
272 * value of those: 0b0000000011000000 (0x00C0)
273 */
274 if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
275 hdrlen = 10;
276 else
277 hdrlen = 16;
278 }
279out:
280 return hdrlen;
281}
282EXPORT_SYMBOL(ieee80211_hdrlen);
283
284unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
285{
286 const struct ieee80211_hdr *hdr =
287 (const struct ieee80211_hdr *)skb->data;
288 unsigned int hdrlen;
289
290 if (unlikely(skb->len < 10))
291 return 0;
292 hdrlen = ieee80211_hdrlen(hdr->frame_control);
293 if (unlikely(hdrlen > skb->len))
294 return 0;
295 return hdrlen;
296}
297EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
298
Luis R. Rodriguez60fd2b62009-06-02 16:31:10 -0400299static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
Zhu Yie31a16d2009-05-21 21:47:03 +0800300{
301 int ae = meshhdr->flags & MESH_FLAGS_AE;
302 /* 7.1.3.5a.2 */
303 switch (ae) {
304 case 0:
305 return 6;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700306 case MESH_FLAGS_AE_A4:
Zhu Yie31a16d2009-05-21 21:47:03 +0800307 return 12;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700308 case MESH_FLAGS_AE_A5_A6:
Zhu Yie31a16d2009-05-21 21:47:03 +0800309 return 18;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700310 case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6):
Zhu Yie31a16d2009-05-21 21:47:03 +0800311 return 24;
312 default:
313 return 6;
314 }
315}
316
Zhu Yieaf85ca2009-12-01 10:18:37 +0800317int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr,
Zhu Yie31a16d2009-05-21 21:47:03 +0800318 enum nl80211_iftype iftype)
319{
320 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
321 u16 hdrlen, ethertype;
322 u8 *payload;
323 u8 dst[ETH_ALEN];
324 u8 src[ETH_ALEN] __aligned(2);
325
326 if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
327 return -1;
328
329 hdrlen = ieee80211_hdrlen(hdr->frame_control);
330
331 /* convert IEEE 802.11 header + possible LLC headers into Ethernet
332 * header
333 * IEEE 802.11 address fields:
334 * ToDS FromDS Addr1 Addr2 Addr3 Addr4
335 * 0 0 DA SA BSSID n/a
336 * 0 1 DA BSSID SA n/a
337 * 1 0 BSSID SA DA n/a
338 * 1 1 RA TA DA SA
339 */
340 memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN);
341 memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN);
342
343 switch (hdr->frame_control &
344 cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
345 case cpu_to_le16(IEEE80211_FCTL_TODS):
346 if (unlikely(iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200347 iftype != NL80211_IFTYPE_AP_VLAN &&
348 iftype != NL80211_IFTYPE_P2P_GO))
Zhu Yie31a16d2009-05-21 21:47:03 +0800349 return -1;
350 break;
351 case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
352 if (unlikely(iftype != NL80211_IFTYPE_WDS &&
Felix Fietkauf14543e2009-11-10 20:10:05 +0100353 iftype != NL80211_IFTYPE_MESH_POINT &&
354 iftype != NL80211_IFTYPE_AP_VLAN &&
355 iftype != NL80211_IFTYPE_STATION))
Zhu Yie31a16d2009-05-21 21:47:03 +0800356 return -1;
357 if (iftype == NL80211_IFTYPE_MESH_POINT) {
358 struct ieee80211s_hdr *meshdr =
359 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800360 /* make sure meshdr->flags is on the linear part */
361 if (!pskb_may_pull(skb, hdrlen + 1))
362 return -1;
Zhu Yie31a16d2009-05-21 21:47:03 +0800363 if (meshdr->flags & MESH_FLAGS_AE_A5_A6) {
Zhu Yie3cf8b32010-03-29 17:35:07 +0800364 skb_copy_bits(skb, hdrlen +
365 offsetof(struct ieee80211s_hdr, eaddr1),
366 dst, ETH_ALEN);
367 skb_copy_bits(skb, hdrlen +
368 offsetof(struct ieee80211s_hdr, eaddr2),
369 src, ETH_ALEN);
Zhu Yie31a16d2009-05-21 21:47:03 +0800370 }
Zhu Yie3cf8b32010-03-29 17:35:07 +0800371 hdrlen += ieee80211_get_mesh_hdrlen(meshdr);
Zhu Yie31a16d2009-05-21 21:47:03 +0800372 }
373 break;
374 case cpu_to_le16(IEEE80211_FCTL_FROMDS):
Javier Cardona3c5772a2009-08-10 12:15:48 -0700375 if ((iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200376 iftype != NL80211_IFTYPE_P2P_CLIENT &&
377 iftype != NL80211_IFTYPE_MESH_POINT) ||
Zhu Yie31a16d2009-05-21 21:47:03 +0800378 (is_multicast_ether_addr(dst) &&
379 !compare_ether_addr(src, addr)))
380 return -1;
Javier Cardona3c5772a2009-08-10 12:15:48 -0700381 if (iftype == NL80211_IFTYPE_MESH_POINT) {
382 struct ieee80211s_hdr *meshdr =
383 (struct ieee80211s_hdr *) (skb->data + hdrlen);
Zhu Yie3cf8b32010-03-29 17:35:07 +0800384 /* make sure meshdr->flags is on the linear part */
385 if (!pskb_may_pull(skb, hdrlen + 1))
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. */
Kyeyoon Park1368a382014-04-17 19:57:34 +0530646unsigned int cfg80211_classify8021d(struct sk_buff *skb,
647 struct cfg80211_qos_map *qos_map)
Zhu Yie31a16d2009-05-21 21:47:03 +0800648{
649 unsigned int dscp;
650
651 /* skb->priority values from 256->263 are magic values to
652 * directly indicate a specific 802.1d priority. This is used
653 * to allow 802.1d priority to be passed directly in from VLAN
654 * tags, etc.
655 */
656 if (skb->priority >= 256 && skb->priority <= 263)
657 return skb->priority - 256;
658
659 switch (skb->protocol) {
660 case htons(ETH_P_IP):
Dave Tähtb1565792011-12-22 12:55:08 -0800661 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
662 break;
663 case htons(ETH_P_IPV6):
664 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
Zhu Yie31a16d2009-05-21 21:47:03 +0800665 break;
666 default:
667 return 0;
668 }
669
Kyeyoon Park1368a382014-04-17 19:57:34 +0530670 if (qos_map) {
671 unsigned int i, tmp_dscp = dscp >> 2;
672
673 for (i = 0; i < qos_map->num_des; i++) {
674 if (tmp_dscp == qos_map->dscp_exception[i].dscp)
675 return qos_map->dscp_exception[i].up;
676 }
677
678 for (i = 0; i < 8; i++) {
679 if (tmp_dscp >= qos_map->up[i].low &&
680 tmp_dscp <= qos_map->up[i].high)
681 return i;
682 }
683 }
684
Zhu Yie31a16d2009-05-21 21:47:03 +0800685 return dscp >> 5;
686}
687EXPORT_SYMBOL(cfg80211_classify8021d);
Johannes Berg517357c2009-07-02 17:18:40 +0200688
689const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
690{
691 u8 *end, *pos;
692
693 pos = bss->information_elements;
694 if (pos == NULL)
695 return NULL;
696 end = pos + bss->len_information_elements;
697
698 while (pos + 1 < end) {
699 if (pos + 2 + pos[1] > end)
700 break;
701 if (pos[0] == ie)
702 return pos;
703 pos += 2 + pos[1];
704 }
705
706 return NULL;
707}
708EXPORT_SYMBOL(ieee80211_bss_get_ie);
Johannes Bergfffd0932009-07-08 14:22:54 +0200709
710void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
711{
712 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
713 struct net_device *dev = wdev->netdev;
714 int i;
715
716 if (!wdev->connect_keys)
717 return;
718
719 for (i = 0; i < 6; i++) {
720 if (!wdev->connect_keys->params[i].cipher)
721 continue;
Johannes Berge31b8212010-10-05 19:39:30 +0200722 if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL,
Zhu Yi1e056662009-07-20 16:12:57 +0800723 &wdev->connect_keys->params[i])) {
Joe Perchese9c02682010-11-16 19:56:49 -0800724 netdev_err(dev, "failed to set key %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800725 continue;
726 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200727 if (wdev->connect_keys->def == i)
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100728 if (rdev->ops->set_default_key(wdev->wiphy, dev,
729 i, true, true)) {
Joe Perchese9c02682010-11-16 19:56:49 -0800730 netdev_err(dev, "failed to set defkey %d\n", i);
Zhu Yi1e056662009-07-20 16:12:57 +0800731 continue;
732 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200733 if (wdev->connect_keys->defmgmt == i)
734 if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i))
Joe Perchese9c02682010-11-16 19:56:49 -0800735 netdev_err(dev, "failed to set mgtdef %d\n", i);
Johannes Bergfffd0932009-07-08 14:22:54 +0200736 }
737
738 kfree(wdev->connect_keys);
739 wdev->connect_keys = NULL;
740}
Johannes Berg3d54d252009-08-21 14:51:05 +0200741
742static void cfg80211_process_wdev_events(struct wireless_dev *wdev)
743{
744 struct cfg80211_event *ev;
745 unsigned long flags;
746 const u8 *bssid = NULL;
747
748 spin_lock_irqsave(&wdev->event_lock, flags);
749 while (!list_empty(&wdev->event_list)) {
750 ev = list_first_entry(&wdev->event_list,
751 struct cfg80211_event, list);
752 list_del(&ev->list);
753 spin_unlock_irqrestore(&wdev->event_lock, flags);
754
755 wdev_lock(wdev);
756 switch (ev->type) {
757 case EVENT_CONNECT_RESULT:
758 if (!is_zero_ether_addr(ev->cr.bssid))
759 bssid = ev->cr.bssid;
760 __cfg80211_connect_result(
761 wdev->netdev, bssid,
762 ev->cr.req_ie, ev->cr.req_ie_len,
763 ev->cr.resp_ie, ev->cr.resp_ie_len,
764 ev->cr.status,
765 ev->cr.status == WLAN_STATUS_SUCCESS,
766 NULL);
767 break;
768 case EVENT_ROAMED:
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530769 __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie,
770 ev->rm.req_ie_len, ev->rm.resp_ie,
771 ev->rm.resp_ie_len);
Johannes Berg3d54d252009-08-21 14:51:05 +0200772 break;
773 case EVENT_DISCONNECTED:
774 __cfg80211_disconnected(wdev->netdev,
775 ev->dc.ie, ev->dc.ie_len,
776 ev->dc.reason, true);
777 break;
778 case EVENT_IBSS_JOINED:
779 __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid);
780 break;
781 }
782 wdev_unlock(wdev);
783
784 kfree(ev);
785
786 spin_lock_irqsave(&wdev->event_lock, flags);
787 }
788 spin_unlock_irqrestore(&wdev->event_lock, flags);
789}
790
791void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
792{
793 struct wireless_dev *wdev;
794
795 ASSERT_RTNL();
796 ASSERT_RDEV_LOCK(rdev);
797
798 mutex_lock(&rdev->devlist_mtx);
799
800 list_for_each_entry(wdev, &rdev->netdev_list, list)
801 cfg80211_process_wdev_events(wdev);
802
803 mutex_unlock(&rdev->devlist_mtx);
804}
805
806int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
807 struct net_device *dev, enum nl80211_iftype ntype,
808 u32 *flags, struct vif_params *params)
809{
810 int err;
811 enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
812
813 ASSERT_RDEV_LOCK(rdev);
814
815 /* don't support changing VLANs, you just re-create them */
816 if (otype == NL80211_IFTYPE_AP_VLAN)
817 return -EOPNOTSUPP;
818
819 if (!rdev->ops->change_virtual_intf ||
820 !(rdev->wiphy.interface_modes & (1 << ntype)))
821 return -EOPNOTSUPP;
822
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100823 /* if it's part of a bridge, reject changing type to station/ibss */
Jiri Pirkof350a0a2010-06-15 06:50:45 +0000824 if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
Johannes Berg074ac8d2010-09-16 14:58:22 +0200825 (ntype == NL80211_IFTYPE_ADHOC ||
826 ntype == NL80211_IFTYPE_STATION ||
827 ntype == NL80211_IFTYPE_P2P_CLIENT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100828 return -EBUSY;
829
Johannes Berg3d54d252009-08-21 14:51:05 +0200830 if (ntype != otype) {
Johannes Berg7527a782011-05-13 10:58:57 +0200831 err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr,
832 ntype);
833 if (err)
834 return err;
835
Johannes Berg9bc383d2009-11-19 11:55:19 +0100836 dev->ieee80211_ptr->use_4addr = false;
Johannes Berg29cbe682010-12-03 09:20:44 +0100837 dev->ieee80211_ptr->mesh_id_up_len = 0;
Kyeyoon Park1368a382014-04-17 19:57:34 +0530838 if (rdev->ops->set_qos_map) {
839 rdev->ops->set_qos_map(&rdev->wiphy, dev, NULL);
840 }
Johannes Berg9bc383d2009-11-19 11:55:19 +0100841
Johannes Berg3d54d252009-08-21 14:51:05 +0200842 switch (otype) {
843 case NL80211_IFTYPE_ADHOC:
844 cfg80211_leave_ibss(rdev, dev, false);
845 break;
846 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200847 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg3d54d252009-08-21 14:51:05 +0200848 cfg80211_disconnect(rdev, dev,
849 WLAN_REASON_DEAUTH_LEAVING, true);
850 break;
851 case NL80211_IFTYPE_MESH_POINT:
852 /* mesh should be handled? */
853 break;
854 default:
855 break;
856 }
857
858 cfg80211_process_rdev_events(rdev);
859 }
860
861 err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev,
862 ntype, flags, params);
863
864 WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
865
Johannes Berg9bc383d2009-11-19 11:55:19 +0100866 if (!err && params && params->use_4addr != -1)
867 dev->ieee80211_ptr->use_4addr = params->use_4addr;
868
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100869 if (!err) {
870 dev->priv_flags &= ~IFF_DONT_BRIDGE;
871 switch (ntype) {
872 case NL80211_IFTYPE_STATION:
873 if (dev->ieee80211_ptr->use_4addr)
874 break;
875 /* fall through */
Johannes Berg074ac8d2010-09-16 14:58:22 +0200876 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100877 case NL80211_IFTYPE_ADHOC:
878 dev->priv_flags |= IFF_DONT_BRIDGE;
879 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +0200880 case NL80211_IFTYPE_P2P_GO:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100881 case NL80211_IFTYPE_AP:
882 case NL80211_IFTYPE_AP_VLAN:
883 case NL80211_IFTYPE_WDS:
884 case NL80211_IFTYPE_MESH_POINT:
885 /* bridging OK */
886 break;
887 case NL80211_IFTYPE_MONITOR:
888 /* monitor can't bridge anyway */
889 break;
890 case NL80211_IFTYPE_UNSPECIFIED:
Johannes Berg2e161f72010-08-12 15:38:38 +0200891 case NUM_NL80211_IFTYPES:
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100892 /* not happening */
893 break;
894 }
895 }
896
Johannes Berg3d54d252009-08-21 14:51:05 +0200897 return err;
898}
John W. Linville254416a2009-12-09 16:43:52 -0500899
Johannes Berg99958b22013-03-19 14:28:37 -0700900static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
901{
902 static const u32 base[4][10] = {
903 { 6500000,
904 13000000,
905 19500000,
906 26000000,
907 39000000,
908 52000000,
909 58500000,
910 65000000,
911 78000000,
912 0,
913 },
914 { 13500000,
915 27000000,
916 40500000,
917 54000000,
918 81000000,
919 108000000,
920 121500000,
921 135000000,
922 162000000,
923 180000000,
924 },
925 { 29300000,
926 58500000,
927 87800000,
928 117000000,
929 175500000,
930 234000000,
931 263300000,
932 292500000,
933 351000000,
934 390000000,
935 },
936 { 58500000,
937 117000000,
938 175500000,
939 234000000,
940 351000000,
941 468000000,
942 526500000,
943 585000000,
944 702000000,
945 780000000,
946 },
947 };
948 u32 bitrate;
949 int idx;
950
951 if (WARN_ON_ONCE(rate->mcs > 9))
952 return 0;
953
954 idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH |
955 RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 :
956 rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 :
957 rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0;
958
959 bitrate = base[idx][rate->mcs];
960 bitrate *= rate->nss;
961
962 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
963 bitrate = (bitrate / 9) * 10;
964
965 /* do NOT round down here */
966 return (bitrate + 50000) / 100000;
967}
968
John W. Linville254416a2009-12-09 16:43:52 -0500969u16 cfg80211_calculate_bitrate(struct rate_info *rate)
970{
971 int modulation, streams, bitrate;
972
Johannes Berg99958b22013-03-19 14:28:37 -0700973 if (!(rate->flags & RATE_INFO_FLAGS_MCS) &&
974 !(rate->flags & RATE_INFO_FLAGS_VHT_MCS))
John W. Linville254416a2009-12-09 16:43:52 -0500975 return rate->legacy;
976
Johannes Berg99958b22013-03-19 14:28:37 -0700977 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
978 return cfg80211_calculate_bitrate_vht(rate);
979
John W. Linville254416a2009-12-09 16:43:52 -0500980 /* the formula below does only work for MCS values smaller than 32 */
981 if (rate->mcs >= 32)
982 return 0;
983
984 modulation = rate->mcs & 7;
985 streams = (rate->mcs >> 3) + 1;
986
987 bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ?
988 13500000 : 6500000;
989
990 if (modulation < 4)
991 bitrate *= (modulation + 1);
992 else if (modulation == 4)
993 bitrate *= (modulation + 2);
994 else
995 bitrate *= (modulation + 3);
996
997 bitrate *= streams;
998
999 if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
1000 bitrate = (bitrate / 9) * 10;
1001
1002 /* do NOT round down here */
1003 return (bitrate + 50000) / 100000;
1004}
Thomas Pedersen8097e142012-03-05 15:31:47 -08001005EXPORT_SYMBOL(cfg80211_calculate_bitrate);
Johannes Berg56d18932011-05-09 18:41:15 +02001006
1007int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1008 u32 beacon_int)
1009{
1010 struct wireless_dev *wdev;
1011 int res = 0;
1012
1013 if (!beacon_int)
1014 return -EINVAL;
1015
1016 mutex_lock(&rdev->devlist_mtx);
1017
1018 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1019 if (!wdev->beacon_interval)
1020 continue;
1021 if (wdev->beacon_interval != beacon_int) {
1022 res = -EINVAL;
1023 break;
1024 }
1025 }
1026
1027 mutex_unlock(&rdev->devlist_mtx);
1028
1029 return res;
1030}
Johannes Berg7527a782011-05-13 10:58:57 +02001031
1032int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev,
1033 struct wireless_dev *wdev,
1034 enum nl80211_iftype iftype)
1035{
1036 struct wireless_dev *wdev_iter;
1037 int num[NUM_NL80211_IFTYPES];
1038 int total = 1;
1039 int i, j;
1040
1041 ASSERT_RTNL();
1042
1043 /* Always allow software iftypes */
1044 if (rdev->wiphy.software_iftypes & BIT(iftype))
1045 return 0;
1046
1047 /*
1048 * Drivers will gradually all set this flag, until all
1049 * have it we only enforce for those that set it.
1050 */
1051 if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS))
1052 return 0;
1053
1054 memset(num, 0, sizeof(num));
1055
1056 num[iftype] = 1;
1057
1058 mutex_lock(&rdev->devlist_mtx);
1059 list_for_each_entry(wdev_iter, &rdev->netdev_list, list) {
1060 if (wdev_iter == wdev)
1061 continue;
1062 if (!netif_running(wdev_iter->netdev))
1063 continue;
1064
1065 if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype))
1066 continue;
1067
1068 num[wdev_iter->iftype]++;
1069 total++;
1070 }
1071 mutex_unlock(&rdev->devlist_mtx);
1072
1073 for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) {
1074 const struct ieee80211_iface_combination *c;
1075 struct ieee80211_iface_limit *limits;
1076
1077 c = &rdev->wiphy.iface_combinations[i];
1078
1079 limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
1080 GFP_KERNEL);
1081 if (!limits)
1082 return -ENOMEM;
1083 if (total > c->max_interfaces)
1084 goto cont;
1085
1086 for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
1087 if (rdev->wiphy.software_iftypes & BIT(iftype))
1088 continue;
1089 for (j = 0; j < c->n_limits; j++) {
Lukasz Kucharczyke55a4042012-04-11 14:55:10 +02001090 if (!(limits[j].types & BIT(iftype)))
Johannes Berg7527a782011-05-13 10:58:57 +02001091 continue;
1092 if (limits[j].max < num[iftype])
1093 goto cont;
1094 limits[j].max -= num[iftype];
1095 }
1096 }
1097 /* yay, it fits */
1098 kfree(limits);
1099 return 0;
1100 cont:
1101 kfree(limits);
1102 }
1103
1104 return -EBUSY;
1105}
Johannes Berg34850ab2011-07-18 18:08:35 +02001106
1107int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
1108 const u8 *rates, unsigned int n_rates,
1109 u32 *mask)
1110{
1111 int i, j;
1112
Johannes Berga401d2b2011-07-20 00:52:16 +02001113 if (!sband)
1114 return -EINVAL;
1115
Johannes Berg34850ab2011-07-18 18:08:35 +02001116 if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
1117 return -EINVAL;
1118
1119 *mask = 0;
1120
1121 for (i = 0; i < n_rates; i++) {
1122 int rate = (rates[i] & 0x7f) * 5;
1123 bool found = false;
1124
1125 for (j = 0; j < sband->n_bitrates; j++) {
1126 if (sband->bitrates[j].bitrate == rate) {
1127 found = true;
1128 *mask |= BIT(j);
1129 break;
1130 }
1131 }
1132 if (!found)
1133 return -EINVAL;
1134 }
Stephen Boyd42517402013-01-14 16:41:42 -08001135
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);
Johannes Berg55ef7702014-02-25 15:04:46 -08001155
1156bool cfg80211_is_gratuitous_arp_unsolicited_na(struct sk_buff *skb)
1157{
1158 const struct ethhdr *eth = (void *)skb->data;
1159 const struct {
1160 struct arphdr hdr;
1161 u8 ar_sha[ETH_ALEN];
1162 u8 ar_sip[4];
1163 u8 ar_tha[ETH_ALEN];
1164 u8 ar_tip[4];
1165 } __packed *arp;
1166 const struct ipv6hdr *ipv6;
1167 const struct icmp6hdr *icmpv6;
1168
1169 switch (eth->h_proto) {
1170 case cpu_to_be16(ETH_P_ARP):
1171 /* can't say - but will probably be dropped later anyway */
1172 if (!pskb_may_pull(skb, sizeof(*eth) + sizeof(*arp)))
1173 return false;
1174
1175 arp = (void *)(eth + 1);
1176
1177 if ((arp->hdr.ar_op == cpu_to_be16(ARPOP_REPLY) ||
1178 arp->hdr.ar_op == cpu_to_be16(ARPOP_REQUEST)) &&
1179 !memcmp(arp->ar_sip, arp->ar_tip, sizeof(arp->ar_sip)))
1180 return true;
1181 break;
1182 case cpu_to_be16(ETH_P_IPV6):
1183 /* can't say - but will probably be dropped later anyway */
1184 if (!pskb_may_pull(skb, sizeof(*eth) + sizeof(*ipv6) +
1185 sizeof(*icmpv6)))
1186 return false;
1187
1188 ipv6 = (void *)(eth + 1);
1189 icmpv6 = (void *)(ipv6 + 1);
1190
1191 if (icmpv6->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT &&
1192 !memcmp(&ipv6->saddr, &ipv6->daddr, sizeof(ipv6->saddr)))
1193 return true;
1194 break;
1195 default:
1196 /*
1197 * no need to support other protocols, proxy service isn't
1198 * specified for any others
1199 */
1200 break;
1201 }
1202
1203 return false;
1204}
1205EXPORT_SYMBOL(cfg80211_is_gratuitous_arp_unsolicited_na);