Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Wireless utility functions |
| 3 | * |
Johannes Berg | d323655 | 2009-04-20 14:31:42 +0200 | [diff] [blame] | 4 | * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net> |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 5 | */ |
Johannes Berg | d323655 | 2009-04-20 14:31:42 +0200 | [diff] [blame] | 6 | #include <linux/bitops.h> |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 7 | #include <linux/etherdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Yogesh Ashok Powar | 0879fa4 | 2011-08-09 18:02:26 -0700 | [diff] [blame^] | 9 | #include <linux/crc32.h> |
Johannes Berg | d323655 | 2009-04-20 14:31:42 +0200 | [diff] [blame] | 10 | #include <net/cfg80211.h> |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 11 | #include <net/ip.h> |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 12 | #include "core.h" |
| 13 | |
Johannes Berg | bd81525 | 2008-10-29 20:00:45 +0100 | [diff] [blame] | 14 | struct ieee80211_rate * |
| 15 | ieee80211_get_response_rate(struct ieee80211_supported_band *sband, |
Johannes Berg | 881d948 | 2009-01-21 15:13:48 +0100 | [diff] [blame] | 16 | u32 basic_rates, int bitrate) |
Johannes Berg | bd81525 | 2008-10-29 20:00:45 +0100 | [diff] [blame] | 17 | { |
| 18 | struct ieee80211_rate *result = &sband->bitrates[0]; |
| 19 | int i; |
| 20 | |
| 21 | for (i = 0; i < sband->n_bitrates; i++) { |
| 22 | if (!(basic_rates & BIT(i))) |
| 23 | continue; |
| 24 | if (sband->bitrates[i].bitrate > bitrate) |
| 25 | continue; |
| 26 | result = &sband->bitrates[i]; |
| 27 | } |
| 28 | |
| 29 | return result; |
| 30 | } |
| 31 | EXPORT_SYMBOL(ieee80211_get_response_rate); |
| 32 | |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 33 | int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 34 | { |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 35 | /* see 802.11 17.3.8.3.2 and Annex J |
| 36 | * there are overlapping channel numbers in 5GHz and 2GHz bands */ |
| 37 | if (band == IEEE80211_BAND_5GHZ) { |
| 38 | if (chan >= 182 && chan <= 196) |
| 39 | return 4000 + chan * 5; |
| 40 | else |
| 41 | return 5000 + chan * 5; |
| 42 | } else { /* IEEE80211_BAND_2GHZ */ |
| 43 | if (chan == 14) |
| 44 | return 2484; |
| 45 | else if (chan < 14) |
| 46 | return 2407 + chan * 5; |
| 47 | else |
| 48 | return 0; /* not supported */ |
| 49 | } |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 50 | } |
| 51 | EXPORT_SYMBOL(ieee80211_channel_to_frequency); |
| 52 | |
| 53 | int ieee80211_frequency_to_channel(int freq) |
| 54 | { |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 55 | /* see 802.11 17.3.8.3.2 and Annex J */ |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 56 | if (freq == 2484) |
| 57 | return 14; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 58 | else if (freq < 2484) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 59 | return (freq - 2407) / 5; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 60 | else if (freq >= 4910 && freq <= 4980) |
| 61 | return (freq - 4000) / 5; |
| 62 | else |
| 63 | return (freq - 5000) / 5; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 64 | } |
| 65 | EXPORT_SYMBOL(ieee80211_frequency_to_channel); |
| 66 | |
Johannes Berg | 6c507cd | 2008-03-26 14:14:55 +0100 | [diff] [blame] | 67 | struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy, |
| 68 | int freq) |
Johannes Berg | 906c730 | 2008-03-16 18:34:33 +0100 | [diff] [blame] | 69 | { |
| 70 | enum ieee80211_band band; |
| 71 | struct ieee80211_supported_band *sband; |
| 72 | int i; |
| 73 | |
| 74 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 75 | sband = wiphy->bands[band]; |
| 76 | |
| 77 | if (!sband) |
| 78 | continue; |
| 79 | |
| 80 | for (i = 0; i < sband->n_channels; i++) { |
| 81 | if (sband->channels[i].center_freq == freq) |
| 82 | return &sband->channels[i]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return NULL; |
| 87 | } |
Johannes Berg | 6c507cd | 2008-03-26 14:14:55 +0100 | [diff] [blame] | 88 | EXPORT_SYMBOL(__ieee80211_get_channel); |
Johannes Berg | 906c730 | 2008-03-16 18:34:33 +0100 | [diff] [blame] | 89 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 90 | static void set_mandatory_flags_band(struct ieee80211_supported_band *sband, |
| 91 | enum ieee80211_band band) |
| 92 | { |
| 93 | int i, want; |
| 94 | |
| 95 | switch (band) { |
| 96 | case IEEE80211_BAND_5GHZ: |
| 97 | want = 3; |
| 98 | for (i = 0; i < sband->n_bitrates; i++) { |
| 99 | if (sband->bitrates[i].bitrate == 60 || |
| 100 | sband->bitrates[i].bitrate == 120 || |
| 101 | sband->bitrates[i].bitrate == 240) { |
| 102 | sband->bitrates[i].flags |= |
| 103 | IEEE80211_RATE_MANDATORY_A; |
| 104 | want--; |
| 105 | } |
| 106 | } |
| 107 | WARN_ON(want); |
| 108 | break; |
| 109 | case IEEE80211_BAND_2GHZ: |
| 110 | want = 7; |
| 111 | for (i = 0; i < sband->n_bitrates; i++) { |
| 112 | if (sband->bitrates[i].bitrate == 10) { |
| 113 | sband->bitrates[i].flags |= |
| 114 | IEEE80211_RATE_MANDATORY_B | |
| 115 | IEEE80211_RATE_MANDATORY_G; |
| 116 | want--; |
| 117 | } |
| 118 | |
| 119 | if (sband->bitrates[i].bitrate == 20 || |
| 120 | sband->bitrates[i].bitrate == 55 || |
| 121 | sband->bitrates[i].bitrate == 110 || |
| 122 | sband->bitrates[i].bitrate == 60 || |
| 123 | sband->bitrates[i].bitrate == 120 || |
| 124 | sband->bitrates[i].bitrate == 240) { |
| 125 | sband->bitrates[i].flags |= |
| 126 | IEEE80211_RATE_MANDATORY_G; |
| 127 | want--; |
| 128 | } |
| 129 | |
Johannes Berg | aac09fb | 2008-01-30 17:36:10 +0100 | [diff] [blame] | 130 | if (sband->bitrates[i].bitrate != 10 && |
| 131 | sband->bitrates[i].bitrate != 20 && |
| 132 | sband->bitrates[i].bitrate != 55 && |
| 133 | sband->bitrates[i].bitrate != 110) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 134 | sband->bitrates[i].flags |= |
| 135 | IEEE80211_RATE_ERP_G; |
| 136 | } |
Ivo van Doorn | 406f238 | 2008-02-02 23:53:10 +0100 | [diff] [blame] | 137 | WARN_ON(want != 0 && want != 3 && want != 6); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 138 | break; |
| 139 | case IEEE80211_NUM_BANDS: |
| 140 | WARN_ON(1); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void ieee80211_set_bitrate_flags(struct wiphy *wiphy) |
| 146 | { |
| 147 | enum ieee80211_band band; |
| 148 | |
| 149 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) |
| 150 | if (wiphy->bands[band]) |
| 151 | set_mandatory_flags_band(wiphy->bands[band], band); |
| 152 | } |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 153 | |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 154 | int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev, |
| 155 | struct key_params *params, int key_idx, |
Johannes Berg | e31b821 | 2010-10-05 19:39:30 +0200 | [diff] [blame] | 156 | bool pairwise, const u8 *mac_addr) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 157 | { |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 158 | int i; |
| 159 | |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 160 | if (key_idx > 5) |
| 161 | return -EINVAL; |
| 162 | |
Johannes Berg | e31b821 | 2010-10-05 19:39:30 +0200 | [diff] [blame] | 163 | if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | if (pairwise && !mac_addr) |
| 167 | return -EINVAL; |
| 168 | |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 169 | /* |
| 170 | * Disallow pairwise keys with non-zero index unless it's WEP |
Juuso Oikarinen | 45cbad6 | 2011-01-25 12:21:22 +0200 | [diff] [blame] | 171 | * or a vendor specific cipher (because current deployments use |
| 172 | * pairwise WEP keys with non-zero indices and for vendor specific |
| 173 | * ciphers this should be validated in the driver or hardware level |
| 174 | * - but 802.11i clearly specifies to use zero) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 175 | */ |
Johannes Berg | e31b821 | 2010-10-05 19:39:30 +0200 | [diff] [blame] | 176 | if (pairwise && key_idx && |
Juuso Oikarinen | 45cbad6 | 2011-01-25 12:21:22 +0200 | [diff] [blame] | 177 | ((params->cipher == WLAN_CIPHER_SUITE_TKIP) || |
| 178 | (params->cipher == WLAN_CIPHER_SUITE_CCMP) || |
| 179 | (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC))) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 180 | return -EINVAL; |
| 181 | |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 182 | switch (params->cipher) { |
| 183 | case WLAN_CIPHER_SUITE_WEP40: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 184 | if (params->key_len != WLAN_KEY_LEN_WEP40) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 185 | return -EINVAL; |
| 186 | break; |
| 187 | case WLAN_CIPHER_SUITE_TKIP: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 188 | if (params->key_len != WLAN_KEY_LEN_TKIP) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 189 | return -EINVAL; |
| 190 | break; |
| 191 | case WLAN_CIPHER_SUITE_CCMP: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 192 | if (params->key_len != WLAN_KEY_LEN_CCMP) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 193 | return -EINVAL; |
| 194 | break; |
| 195 | case WLAN_CIPHER_SUITE_WEP104: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 196 | if (params->key_len != WLAN_KEY_LEN_WEP104) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 197 | return -EINVAL; |
| 198 | break; |
| 199 | case WLAN_CIPHER_SUITE_AES_CMAC: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 200 | if (params->key_len != WLAN_KEY_LEN_AES_CMAC) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 201 | return -EINVAL; |
| 202 | break; |
| 203 | default: |
Johannes Berg | 7d64b7c | 2010-08-27 14:26:51 +0300 | [diff] [blame] | 204 | /* |
| 205 | * We don't know anything about this algorithm, |
| 206 | * allow using it -- but the driver must check |
| 207 | * all parameters! We still check below whether |
| 208 | * or not the driver supports this algorithm, |
| 209 | * of course. |
| 210 | */ |
| 211 | break; |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Jouni Malinen | 9f26a95 | 2009-05-15 12:38:32 +0300 | [diff] [blame] | 214 | if (params->seq) { |
| 215 | switch (params->cipher) { |
| 216 | case WLAN_CIPHER_SUITE_WEP40: |
| 217 | case WLAN_CIPHER_SUITE_WEP104: |
| 218 | /* These ciphers do not use key sequence */ |
| 219 | return -EINVAL; |
| 220 | case WLAN_CIPHER_SUITE_TKIP: |
| 221 | case WLAN_CIPHER_SUITE_CCMP: |
| 222 | case WLAN_CIPHER_SUITE_AES_CMAC: |
| 223 | if (params->seq_len != 6) |
| 224 | return -EINVAL; |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 229 | for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) |
| 230 | if (params->cipher == rdev->wiphy.cipher_suites[i]) |
| 231 | break; |
| 232 | if (i == rdev->wiphy.n_cipher_suites) |
| 233 | return -EINVAL; |
| 234 | |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 235 | return 0; |
| 236 | } |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 237 | |
| 238 | /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */ |
| 239 | /* Ethernet-II snap header (RFC1042 for most EtherTypes) */ |
| 240 | const unsigned char rfc1042_header[] __aligned(2) = |
| 241 | { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; |
| 242 | EXPORT_SYMBOL(rfc1042_header); |
| 243 | |
| 244 | /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */ |
| 245 | const unsigned char bridge_tunnel_header[] __aligned(2) = |
| 246 | { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 }; |
| 247 | EXPORT_SYMBOL(bridge_tunnel_header); |
| 248 | |
Johannes Berg | 633adf1 | 2010-08-12 14:49:58 +0200 | [diff] [blame] | 249 | unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 250 | { |
| 251 | unsigned int hdrlen = 24; |
| 252 | |
| 253 | if (ieee80211_is_data(fc)) { |
| 254 | if (ieee80211_has_a4(fc)) |
| 255 | hdrlen = 30; |
Andriy Tkachuk | d0dd2de | 2010-01-20 13:55:06 +0200 | [diff] [blame] | 256 | if (ieee80211_is_data_qos(fc)) { |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 257 | hdrlen += IEEE80211_QOS_CTL_LEN; |
Andriy Tkachuk | d0dd2de | 2010-01-20 13:55:06 +0200 | [diff] [blame] | 258 | if (ieee80211_has_order(fc)) |
| 259 | hdrlen += IEEE80211_HT_CTL_LEN; |
| 260 | } |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 261 | 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 | } |
| 279 | out: |
| 280 | return hdrlen; |
| 281 | } |
| 282 | EXPORT_SYMBOL(ieee80211_hdrlen); |
| 283 | |
| 284 | unsigned 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 | } |
| 297 | EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb); |
| 298 | |
Luis R. Rodriguez | 60fd2b6 | 2009-06-02 16:31:10 -0400 | [diff] [blame] | 299 | static int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 300 | { |
| 301 | int ae = meshhdr->flags & MESH_FLAGS_AE; |
| 302 | /* 7.1.3.5a.2 */ |
| 303 | switch (ae) { |
| 304 | case 0: |
| 305 | return 6; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 306 | case MESH_FLAGS_AE_A4: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 307 | return 12; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 308 | case MESH_FLAGS_AE_A5_A6: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 309 | return 18; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 310 | case (MESH_FLAGS_AE_A4 | MESH_FLAGS_AE_A5_A6): |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 311 | return 24; |
| 312 | default: |
| 313 | return 6; |
| 314 | } |
| 315 | } |
| 316 | |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 317 | int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr, |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 318 | 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 Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 347 | iftype != NL80211_IFTYPE_AP_VLAN && |
| 348 | iftype != NL80211_IFTYPE_P2P_GO)) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 349 | return -1; |
| 350 | break; |
| 351 | case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS): |
| 352 | if (unlikely(iftype != NL80211_IFTYPE_WDS && |
Felix Fietkau | f14543ee | 2009-11-10 20:10:05 +0100 | [diff] [blame] | 353 | iftype != NL80211_IFTYPE_MESH_POINT && |
| 354 | iftype != NL80211_IFTYPE_AP_VLAN && |
| 355 | iftype != NL80211_IFTYPE_STATION)) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 356 | return -1; |
| 357 | if (iftype == NL80211_IFTYPE_MESH_POINT) { |
| 358 | struct ieee80211s_hdr *meshdr = |
| 359 | (struct ieee80211s_hdr *) (skb->data + hdrlen); |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 360 | /* make sure meshdr->flags is on the linear part */ |
| 361 | if (!pskb_may_pull(skb, hdrlen + 1)) |
| 362 | return -1; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 363 | if (meshdr->flags & MESH_FLAGS_AE_A5_A6) { |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 364 | 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 Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 370 | } |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 371 | hdrlen += ieee80211_get_mesh_hdrlen(meshdr); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 372 | } |
| 373 | break; |
| 374 | case cpu_to_le16(IEEE80211_FCTL_FROMDS): |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 375 | if ((iftype != NL80211_IFTYPE_STATION && |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 376 | iftype != NL80211_IFTYPE_P2P_CLIENT && |
| 377 | iftype != NL80211_IFTYPE_MESH_POINT) || |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 378 | (is_multicast_ether_addr(dst) && |
| 379 | !compare_ether_addr(src, addr))) |
| 380 | return -1; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 381 | if (iftype == NL80211_IFTYPE_MESH_POINT) { |
| 382 | struct ieee80211s_hdr *meshdr = |
| 383 | (struct ieee80211s_hdr *) (skb->data + hdrlen); |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 384 | /* make sure meshdr->flags is on the linear part */ |
| 385 | if (!pskb_may_pull(skb, hdrlen + 1)) |
| 386 | return -1; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 387 | if (meshdr->flags & MESH_FLAGS_AE_A4) |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 388 | skb_copy_bits(skb, hdrlen + |
| 389 | offsetof(struct ieee80211s_hdr, eaddr1), |
| 390 | src, ETH_ALEN); |
| 391 | hdrlen += ieee80211_get_mesh_hdrlen(meshdr); |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 392 | } |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 393 | break; |
| 394 | case cpu_to_le16(0): |
| 395 | if (iftype != NL80211_IFTYPE_ADHOC) |
| 396 | return -1; |
| 397 | break; |
| 398 | } |
| 399 | |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 400 | if (!pskb_may_pull(skb, hdrlen + 8)) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 401 | return -1; |
| 402 | |
| 403 | payload = skb->data + hdrlen; |
| 404 | ethertype = (payload[6] << 8) | payload[7]; |
| 405 | |
| 406 | if (likely((compare_ether_addr(payload, rfc1042_header) == 0 && |
| 407 | ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) || |
| 408 | compare_ether_addr(payload, bridge_tunnel_header) == 0)) { |
| 409 | /* remove RFC1042 or Bridge-Tunnel encapsulation and |
| 410 | * replace EtherType */ |
| 411 | skb_pull(skb, hdrlen + 6); |
| 412 | memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN); |
| 413 | memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN); |
| 414 | } else { |
| 415 | struct ethhdr *ehdr; |
| 416 | __be16 len; |
| 417 | |
| 418 | skb_pull(skb, hdrlen); |
| 419 | len = htons(skb->len); |
| 420 | ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr)); |
| 421 | memcpy(ehdr->h_dest, dst, ETH_ALEN); |
| 422 | memcpy(ehdr->h_source, src, ETH_ALEN); |
| 423 | ehdr->h_proto = len; |
| 424 | } |
| 425 | return 0; |
| 426 | } |
| 427 | EXPORT_SYMBOL(ieee80211_data_to_8023); |
| 428 | |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 429 | int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr, |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 430 | enum nl80211_iftype iftype, u8 *bssid, bool qos) |
| 431 | { |
| 432 | struct ieee80211_hdr hdr; |
| 433 | u16 hdrlen, ethertype; |
| 434 | __le16 fc; |
| 435 | const u8 *encaps_data; |
| 436 | int encaps_len, skip_header_bytes; |
| 437 | int nh_pos, h_pos; |
| 438 | int head_need; |
| 439 | |
| 440 | if (unlikely(skb->len < ETH_HLEN)) |
| 441 | return -EINVAL; |
| 442 | |
| 443 | nh_pos = skb_network_header(skb) - skb->data; |
| 444 | h_pos = skb_transport_header(skb) - skb->data; |
| 445 | |
| 446 | /* convert Ethernet header to proper 802.11 header (based on |
| 447 | * operation mode) */ |
| 448 | ethertype = (skb->data[12] << 8) | skb->data[13]; |
| 449 | fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA); |
| 450 | |
| 451 | switch (iftype) { |
| 452 | case NL80211_IFTYPE_AP: |
| 453 | case NL80211_IFTYPE_AP_VLAN: |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 454 | case NL80211_IFTYPE_P2P_GO: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 455 | fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS); |
| 456 | /* DA BSSID SA */ |
| 457 | memcpy(hdr.addr1, skb->data, ETH_ALEN); |
| 458 | memcpy(hdr.addr2, addr, ETH_ALEN); |
| 459 | memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN); |
| 460 | hdrlen = 24; |
| 461 | break; |
| 462 | case NL80211_IFTYPE_STATION: |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 463 | case NL80211_IFTYPE_P2P_CLIENT: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 464 | fc |= cpu_to_le16(IEEE80211_FCTL_TODS); |
| 465 | /* BSSID SA DA */ |
| 466 | memcpy(hdr.addr1, bssid, ETH_ALEN); |
| 467 | memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); |
| 468 | memcpy(hdr.addr3, skb->data, ETH_ALEN); |
| 469 | hdrlen = 24; |
| 470 | break; |
| 471 | case NL80211_IFTYPE_ADHOC: |
| 472 | /* DA SA BSSID */ |
| 473 | memcpy(hdr.addr1, skb->data, ETH_ALEN); |
| 474 | memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); |
| 475 | memcpy(hdr.addr3, bssid, ETH_ALEN); |
| 476 | hdrlen = 24; |
| 477 | break; |
| 478 | default: |
| 479 | return -EOPNOTSUPP; |
| 480 | } |
| 481 | |
| 482 | if (qos) { |
| 483 | fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA); |
| 484 | hdrlen += 2; |
| 485 | } |
| 486 | |
| 487 | hdr.frame_control = fc; |
| 488 | hdr.duration_id = 0; |
| 489 | hdr.seq_ctrl = 0; |
| 490 | |
| 491 | skip_header_bytes = ETH_HLEN; |
| 492 | if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) { |
| 493 | encaps_data = bridge_tunnel_header; |
| 494 | encaps_len = sizeof(bridge_tunnel_header); |
| 495 | skip_header_bytes -= 2; |
| 496 | } else if (ethertype > 0x600) { |
| 497 | encaps_data = rfc1042_header; |
| 498 | encaps_len = sizeof(rfc1042_header); |
| 499 | skip_header_bytes -= 2; |
| 500 | } else { |
| 501 | encaps_data = NULL; |
| 502 | encaps_len = 0; |
| 503 | } |
| 504 | |
| 505 | skb_pull(skb, skip_header_bytes); |
| 506 | nh_pos -= skip_header_bytes; |
| 507 | h_pos -= skip_header_bytes; |
| 508 | |
| 509 | head_need = hdrlen + encaps_len - skb_headroom(skb); |
| 510 | |
| 511 | if (head_need > 0 || skb_cloned(skb)) { |
| 512 | head_need = max(head_need, 0); |
| 513 | if (head_need) |
| 514 | skb_orphan(skb); |
| 515 | |
| 516 | if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) { |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 517 | pr_err("failed to reallocate Tx buffer\n"); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 518 | return -ENOMEM; |
| 519 | } |
| 520 | 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 | } |
| 543 | EXPORT_SYMBOL(ieee80211_data_from_8023); |
| 544 | |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 545 | |
| 546 | void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list, |
| 547 | const u8 *addr, enum nl80211_iftype iftype, |
Yogesh Ashok Powar | 8b3beca | 2011-05-13 11:22:31 -0700 | [diff] [blame] | 548 | const unsigned int extra_headroom, |
| 549 | bool has_80211_header) |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 550 | { |
| 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 Powar | 8b3beca | 2011-05-13 11:22:31 -0700 | [diff] [blame] | 558 | if (has_80211_header) { |
| 559 | err = ieee80211_data_to_8023(skb, addr, iftype); |
| 560 | if (err) |
| 561 | goto out; |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 562 | |
Yogesh Ashok Powar | 8b3beca | 2011-05-13 11:22:31 -0700 | [diff] [blame] | 563 | /* 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 Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 570 | |
| 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 | } |
| 643 | EXPORT_SYMBOL(ieee80211_amsdu_to_8023s); |
| 644 | |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 645 | /* Given a data frame determine the 802.1p/1d tag to use. */ |
| 646 | unsigned 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): |
| 660 | dscp = ip_hdr(skb)->tos & 0xfc; |
| 661 | break; |
| 662 | default: |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | return dscp >> 5; |
| 667 | } |
| 668 | EXPORT_SYMBOL(cfg80211_classify8021d); |
Johannes Berg | 517357c | 2009-07-02 17:18:40 +0200 | [diff] [blame] | 669 | |
| 670 | const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie) |
| 671 | { |
| 672 | u8 *end, *pos; |
| 673 | |
| 674 | pos = bss->information_elements; |
| 675 | if (pos == NULL) |
| 676 | return NULL; |
| 677 | end = pos + bss->len_information_elements; |
| 678 | |
| 679 | while (pos + 1 < end) { |
| 680 | if (pos + 2 + pos[1] > end) |
| 681 | break; |
| 682 | if (pos[0] == ie) |
| 683 | return pos; |
| 684 | pos += 2 + pos[1]; |
| 685 | } |
| 686 | |
| 687 | return NULL; |
| 688 | } |
| 689 | EXPORT_SYMBOL(ieee80211_bss_get_ie); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 690 | |
| 691 | void cfg80211_upload_connect_keys(struct wireless_dev *wdev) |
| 692 | { |
| 693 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy); |
| 694 | struct net_device *dev = wdev->netdev; |
| 695 | int i; |
| 696 | |
| 697 | if (!wdev->connect_keys) |
| 698 | return; |
| 699 | |
| 700 | for (i = 0; i < 6; i++) { |
| 701 | if (!wdev->connect_keys->params[i].cipher) |
| 702 | continue; |
Johannes Berg | e31b821 | 2010-10-05 19:39:30 +0200 | [diff] [blame] | 703 | if (rdev->ops->add_key(wdev->wiphy, dev, i, false, NULL, |
Zhu Yi | 1e05666 | 2009-07-20 16:12:57 +0800 | [diff] [blame] | 704 | &wdev->connect_keys->params[i])) { |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 705 | netdev_err(dev, "failed to set key %d\n", i); |
Zhu Yi | 1e05666 | 2009-07-20 16:12:57 +0800 | [diff] [blame] | 706 | continue; |
| 707 | } |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 708 | if (wdev->connect_keys->def == i) |
Johannes Berg | dbd2fd6 | 2010-12-09 19:58:59 +0100 | [diff] [blame] | 709 | if (rdev->ops->set_default_key(wdev->wiphy, dev, |
| 710 | i, true, true)) { |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 711 | netdev_err(dev, "failed to set defkey %d\n", i); |
Zhu Yi | 1e05666 | 2009-07-20 16:12:57 +0800 | [diff] [blame] | 712 | continue; |
| 713 | } |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 714 | if (wdev->connect_keys->defmgmt == i) |
| 715 | if (rdev->ops->set_default_mgmt_key(wdev->wiphy, dev, i)) |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 716 | netdev_err(dev, "failed to set mgtdef %d\n", i); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | kfree(wdev->connect_keys); |
| 720 | wdev->connect_keys = NULL; |
| 721 | } |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 722 | |
| 723 | static void cfg80211_process_wdev_events(struct wireless_dev *wdev) |
| 724 | { |
| 725 | struct cfg80211_event *ev; |
| 726 | unsigned long flags; |
| 727 | const u8 *bssid = NULL; |
| 728 | |
| 729 | spin_lock_irqsave(&wdev->event_lock, flags); |
| 730 | while (!list_empty(&wdev->event_list)) { |
| 731 | ev = list_first_entry(&wdev->event_list, |
| 732 | struct cfg80211_event, list); |
| 733 | list_del(&ev->list); |
| 734 | spin_unlock_irqrestore(&wdev->event_lock, flags); |
| 735 | |
| 736 | wdev_lock(wdev); |
| 737 | switch (ev->type) { |
| 738 | case EVENT_CONNECT_RESULT: |
| 739 | if (!is_zero_ether_addr(ev->cr.bssid)) |
| 740 | bssid = ev->cr.bssid; |
| 741 | __cfg80211_connect_result( |
| 742 | wdev->netdev, bssid, |
| 743 | ev->cr.req_ie, ev->cr.req_ie_len, |
| 744 | ev->cr.resp_ie, ev->cr.resp_ie_len, |
| 745 | ev->cr.status, |
| 746 | ev->cr.status == WLAN_STATUS_SUCCESS, |
| 747 | NULL); |
| 748 | break; |
| 749 | case EVENT_ROAMED: |
Jouni Malinen | ed9d010 | 2011-05-16 19:40:15 +0300 | [diff] [blame] | 750 | __cfg80211_roamed(wdev, ev->rm.channel, ev->rm.bssid, |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 751 | ev->rm.req_ie, ev->rm.req_ie_len, |
| 752 | ev->rm.resp_ie, ev->rm.resp_ie_len); |
| 753 | break; |
| 754 | case EVENT_DISCONNECTED: |
| 755 | __cfg80211_disconnected(wdev->netdev, |
| 756 | ev->dc.ie, ev->dc.ie_len, |
| 757 | ev->dc.reason, true); |
| 758 | break; |
| 759 | case EVENT_IBSS_JOINED: |
| 760 | __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid); |
| 761 | break; |
| 762 | } |
| 763 | wdev_unlock(wdev); |
| 764 | |
| 765 | kfree(ev); |
| 766 | |
| 767 | spin_lock_irqsave(&wdev->event_lock, flags); |
| 768 | } |
| 769 | spin_unlock_irqrestore(&wdev->event_lock, flags); |
| 770 | } |
| 771 | |
| 772 | void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev) |
| 773 | { |
| 774 | struct wireless_dev *wdev; |
| 775 | |
| 776 | ASSERT_RTNL(); |
| 777 | ASSERT_RDEV_LOCK(rdev); |
| 778 | |
| 779 | mutex_lock(&rdev->devlist_mtx); |
| 780 | |
| 781 | list_for_each_entry(wdev, &rdev->netdev_list, list) |
| 782 | cfg80211_process_wdev_events(wdev); |
| 783 | |
| 784 | mutex_unlock(&rdev->devlist_mtx); |
| 785 | } |
| 786 | |
| 787 | int cfg80211_change_iface(struct cfg80211_registered_device *rdev, |
| 788 | struct net_device *dev, enum nl80211_iftype ntype, |
| 789 | u32 *flags, struct vif_params *params) |
| 790 | { |
| 791 | int err; |
| 792 | enum nl80211_iftype otype = dev->ieee80211_ptr->iftype; |
| 793 | |
| 794 | ASSERT_RDEV_LOCK(rdev); |
| 795 | |
| 796 | /* don't support changing VLANs, you just re-create them */ |
| 797 | if (otype == NL80211_IFTYPE_AP_VLAN) |
| 798 | return -EOPNOTSUPP; |
| 799 | |
| 800 | if (!rdev->ops->change_virtual_intf || |
| 801 | !(rdev->wiphy.interface_modes & (1 << ntype))) |
| 802 | return -EOPNOTSUPP; |
| 803 | |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 804 | /* if it's part of a bridge, reject changing type to station/ibss */ |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 805 | if ((dev->priv_flags & IFF_BRIDGE_PORT) && |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 806 | (ntype == NL80211_IFTYPE_ADHOC || |
| 807 | ntype == NL80211_IFTYPE_STATION || |
| 808 | ntype == NL80211_IFTYPE_P2P_CLIENT)) |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 809 | return -EBUSY; |
| 810 | |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 811 | if (ntype != otype) { |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 812 | err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr, |
| 813 | ntype); |
| 814 | if (err) |
| 815 | return err; |
| 816 | |
Johannes Berg | 9bc383d | 2009-11-19 11:55:19 +0100 | [diff] [blame] | 817 | dev->ieee80211_ptr->use_4addr = false; |
Johannes Berg | 29cbe68 | 2010-12-03 09:20:44 +0100 | [diff] [blame] | 818 | dev->ieee80211_ptr->mesh_id_up_len = 0; |
Johannes Berg | 9bc383d | 2009-11-19 11:55:19 +0100 | [diff] [blame] | 819 | |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 820 | switch (otype) { |
| 821 | case NL80211_IFTYPE_ADHOC: |
| 822 | cfg80211_leave_ibss(rdev, dev, false); |
| 823 | break; |
| 824 | case NL80211_IFTYPE_STATION: |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 825 | case NL80211_IFTYPE_P2P_CLIENT: |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 826 | cfg80211_disconnect(rdev, dev, |
| 827 | WLAN_REASON_DEAUTH_LEAVING, true); |
| 828 | break; |
| 829 | case NL80211_IFTYPE_MESH_POINT: |
| 830 | /* mesh should be handled? */ |
| 831 | break; |
| 832 | default: |
| 833 | break; |
| 834 | } |
| 835 | |
| 836 | cfg80211_process_rdev_events(rdev); |
| 837 | } |
| 838 | |
| 839 | err = rdev->ops->change_virtual_intf(&rdev->wiphy, dev, |
| 840 | ntype, flags, params); |
| 841 | |
| 842 | WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype); |
| 843 | |
Johannes Berg | 9bc383d | 2009-11-19 11:55:19 +0100 | [diff] [blame] | 844 | if (!err && params && params->use_4addr != -1) |
| 845 | dev->ieee80211_ptr->use_4addr = params->use_4addr; |
| 846 | |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 847 | if (!err) { |
| 848 | dev->priv_flags &= ~IFF_DONT_BRIDGE; |
| 849 | switch (ntype) { |
| 850 | case NL80211_IFTYPE_STATION: |
| 851 | if (dev->ieee80211_ptr->use_4addr) |
| 852 | break; |
| 853 | /* fall through */ |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 854 | case NL80211_IFTYPE_P2P_CLIENT: |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 855 | case NL80211_IFTYPE_ADHOC: |
| 856 | dev->priv_flags |= IFF_DONT_BRIDGE; |
| 857 | break; |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 858 | case NL80211_IFTYPE_P2P_GO: |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 859 | case NL80211_IFTYPE_AP: |
| 860 | case NL80211_IFTYPE_AP_VLAN: |
| 861 | case NL80211_IFTYPE_WDS: |
| 862 | case NL80211_IFTYPE_MESH_POINT: |
| 863 | /* bridging OK */ |
| 864 | break; |
| 865 | case NL80211_IFTYPE_MONITOR: |
| 866 | /* monitor can't bridge anyway */ |
| 867 | break; |
| 868 | case NL80211_IFTYPE_UNSPECIFIED: |
Johannes Berg | 2e161f7 | 2010-08-12 15:38:38 +0200 | [diff] [blame] | 869 | case NUM_NL80211_IFTYPES: |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 870 | /* not happening */ |
| 871 | break; |
| 872 | } |
| 873 | } |
| 874 | |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 875 | return err; |
| 876 | } |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 877 | |
| 878 | u16 cfg80211_calculate_bitrate(struct rate_info *rate) |
| 879 | { |
| 880 | int modulation, streams, bitrate; |
| 881 | |
| 882 | if (!(rate->flags & RATE_INFO_FLAGS_MCS)) |
| 883 | return rate->legacy; |
| 884 | |
| 885 | /* the formula below does only work for MCS values smaller than 32 */ |
| 886 | if (rate->mcs >= 32) |
| 887 | return 0; |
| 888 | |
| 889 | modulation = rate->mcs & 7; |
| 890 | streams = (rate->mcs >> 3) + 1; |
| 891 | |
| 892 | bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ? |
| 893 | 13500000 : 6500000; |
| 894 | |
| 895 | if (modulation < 4) |
| 896 | bitrate *= (modulation + 1); |
| 897 | else if (modulation == 4) |
| 898 | bitrate *= (modulation + 2); |
| 899 | else |
| 900 | bitrate *= (modulation + 3); |
| 901 | |
| 902 | bitrate *= streams; |
| 903 | |
| 904 | if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) |
| 905 | bitrate = (bitrate / 9) * 10; |
| 906 | |
| 907 | /* do NOT round down here */ |
| 908 | return (bitrate + 50000) / 100000; |
| 909 | } |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 910 | |
| 911 | int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev, |
| 912 | u32 beacon_int) |
| 913 | { |
| 914 | struct wireless_dev *wdev; |
| 915 | int res = 0; |
| 916 | |
| 917 | if (!beacon_int) |
| 918 | return -EINVAL; |
| 919 | |
| 920 | mutex_lock(&rdev->devlist_mtx); |
| 921 | |
| 922 | list_for_each_entry(wdev, &rdev->netdev_list, list) { |
| 923 | if (!wdev->beacon_interval) |
| 924 | continue; |
| 925 | if (wdev->beacon_interval != beacon_int) { |
| 926 | res = -EINVAL; |
| 927 | break; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | mutex_unlock(&rdev->devlist_mtx); |
| 932 | |
| 933 | return res; |
| 934 | } |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 935 | |
| 936 | int cfg80211_can_change_interface(struct cfg80211_registered_device *rdev, |
| 937 | struct wireless_dev *wdev, |
| 938 | enum nl80211_iftype iftype) |
| 939 | { |
| 940 | struct wireless_dev *wdev_iter; |
| 941 | int num[NUM_NL80211_IFTYPES]; |
| 942 | int total = 1; |
| 943 | int i, j; |
| 944 | |
| 945 | ASSERT_RTNL(); |
| 946 | |
| 947 | /* Always allow software iftypes */ |
| 948 | if (rdev->wiphy.software_iftypes & BIT(iftype)) |
| 949 | return 0; |
| 950 | |
| 951 | /* |
| 952 | * Drivers will gradually all set this flag, until all |
| 953 | * have it we only enforce for those that set it. |
| 954 | */ |
| 955 | if (!(rdev->wiphy.flags & WIPHY_FLAG_ENFORCE_COMBINATIONS)) |
| 956 | return 0; |
| 957 | |
| 958 | memset(num, 0, sizeof(num)); |
| 959 | |
| 960 | num[iftype] = 1; |
| 961 | |
| 962 | mutex_lock(&rdev->devlist_mtx); |
| 963 | list_for_each_entry(wdev_iter, &rdev->netdev_list, list) { |
| 964 | if (wdev_iter == wdev) |
| 965 | continue; |
| 966 | if (!netif_running(wdev_iter->netdev)) |
| 967 | continue; |
| 968 | |
| 969 | if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype)) |
| 970 | continue; |
| 971 | |
| 972 | num[wdev_iter->iftype]++; |
| 973 | total++; |
| 974 | } |
| 975 | mutex_unlock(&rdev->devlist_mtx); |
| 976 | |
| 977 | for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) { |
| 978 | const struct ieee80211_iface_combination *c; |
| 979 | struct ieee80211_iface_limit *limits; |
| 980 | |
| 981 | c = &rdev->wiphy.iface_combinations[i]; |
| 982 | |
| 983 | limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits, |
| 984 | GFP_KERNEL); |
| 985 | if (!limits) |
| 986 | return -ENOMEM; |
| 987 | if (total > c->max_interfaces) |
| 988 | goto cont; |
| 989 | |
| 990 | for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { |
| 991 | if (rdev->wiphy.software_iftypes & BIT(iftype)) |
| 992 | continue; |
| 993 | for (j = 0; j < c->n_limits; j++) { |
| 994 | if (!(limits[j].types & iftype)) |
| 995 | continue; |
| 996 | if (limits[j].max < num[iftype]) |
| 997 | goto cont; |
| 998 | limits[j].max -= num[iftype]; |
| 999 | } |
| 1000 | } |
| 1001 | /* yay, it fits */ |
| 1002 | kfree(limits); |
| 1003 | return 0; |
| 1004 | cont: |
| 1005 | kfree(limits); |
| 1006 | } |
| 1007 | |
| 1008 | return -EBUSY; |
| 1009 | } |
Johannes Berg | 34850ab | 2011-07-18 18:08:35 +0200 | [diff] [blame] | 1010 | |
| 1011 | int ieee80211_get_ratemask(struct ieee80211_supported_band *sband, |
| 1012 | const u8 *rates, unsigned int n_rates, |
| 1013 | u32 *mask) |
| 1014 | { |
| 1015 | int i, j; |
| 1016 | |
Johannes Berg | a401d2b | 2011-07-20 00:52:16 +0200 | [diff] [blame] | 1017 | if (!sband) |
| 1018 | return -EINVAL; |
| 1019 | |
Johannes Berg | 34850ab | 2011-07-18 18:08:35 +0200 | [diff] [blame] | 1020 | if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES) |
| 1021 | return -EINVAL; |
| 1022 | |
| 1023 | *mask = 0; |
| 1024 | |
| 1025 | for (i = 0; i < n_rates; i++) { |
| 1026 | int rate = (rates[i] & 0x7f) * 5; |
| 1027 | bool found = false; |
| 1028 | |
| 1029 | for (j = 0; j < sband->n_bitrates; j++) { |
| 1030 | if (sband->bitrates[j].bitrate == rate) { |
| 1031 | found = true; |
| 1032 | *mask |= BIT(j); |
| 1033 | break; |
| 1034 | } |
| 1035 | } |
| 1036 | if (!found) |
| 1037 | return -EINVAL; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * mask must have at least one bit set here since we |
| 1042 | * didn't accept a 0-length rates array nor allowed |
| 1043 | * entries in the array that didn't exist |
| 1044 | */ |
| 1045 | |
| 1046 | return 0; |
| 1047 | } |
Yogesh Ashok Powar | 0879fa4 | 2011-08-09 18:02:26 -0700 | [diff] [blame^] | 1048 | |
| 1049 | u32 ieee802_11_parse_elems_crc(u8 *start, size_t len, |
| 1050 | struct ieee802_11_elems *elems, |
| 1051 | u64 filter, u32 crc) |
| 1052 | { |
| 1053 | size_t left = len; |
| 1054 | u8 *pos = start; |
| 1055 | bool calc_crc = filter != 0; |
| 1056 | |
| 1057 | memset(elems, 0, sizeof(*elems)); |
| 1058 | elems->ie_start = start; |
| 1059 | elems->total_len = len; |
| 1060 | |
| 1061 | while (left >= 2) { |
| 1062 | u8 id, elen; |
| 1063 | |
| 1064 | id = *pos++; |
| 1065 | elen = *pos++; |
| 1066 | left -= 2; |
| 1067 | |
| 1068 | if (elen > left) |
| 1069 | break; |
| 1070 | |
| 1071 | if (calc_crc && id < 64 && (filter & (1ULL << id))) |
| 1072 | crc = crc32_be(crc, pos - 2, elen + 2); |
| 1073 | |
| 1074 | switch (id) { |
| 1075 | case WLAN_EID_SSID: |
| 1076 | elems->ssid = pos; |
| 1077 | elems->ssid_len = elen; |
| 1078 | break; |
| 1079 | case WLAN_EID_SUPP_RATES: |
| 1080 | elems->supp_rates = pos; |
| 1081 | elems->supp_rates_len = elen; |
| 1082 | break; |
| 1083 | case WLAN_EID_FH_PARAMS: |
| 1084 | elems->fh_params = pos; |
| 1085 | elems->fh_params_len = elen; |
| 1086 | break; |
| 1087 | case WLAN_EID_DS_PARAMS: |
| 1088 | elems->ds_params = pos; |
| 1089 | elems->ds_params_len = elen; |
| 1090 | break; |
| 1091 | case WLAN_EID_CF_PARAMS: |
| 1092 | elems->cf_params = pos; |
| 1093 | elems->cf_params_len = elen; |
| 1094 | break; |
| 1095 | case WLAN_EID_TIM: |
| 1096 | if (elen >= sizeof(struct ieee80211_tim_ie)) { |
| 1097 | elems->tim = (void *)pos; |
| 1098 | elems->tim_len = elen; |
| 1099 | } |
| 1100 | break; |
| 1101 | case WLAN_EID_IBSS_PARAMS: |
| 1102 | elems->ibss_params = pos; |
| 1103 | elems->ibss_params_len = elen; |
| 1104 | break; |
| 1105 | case WLAN_EID_CHALLENGE: |
| 1106 | elems->challenge = pos; |
| 1107 | elems->challenge_len = elen; |
| 1108 | break; |
| 1109 | case WLAN_EID_VENDOR_SPECIFIC: |
| 1110 | if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 && |
| 1111 | pos[2] == 0xf2) { |
| 1112 | /* Microsoft OUI (00:50:F2) */ |
| 1113 | |
| 1114 | if (calc_crc) |
| 1115 | crc = crc32_be(crc, pos - 2, elen + 2); |
| 1116 | |
| 1117 | if (pos[3] == 1) { |
| 1118 | /* OUI Type 1 - WPA IE */ |
| 1119 | elems->wpa = pos; |
| 1120 | elems->wpa_len = elen; |
| 1121 | } else if (elen >= 5 && pos[3] == 2) { |
| 1122 | /* OUI Type 2 - WMM IE */ |
| 1123 | if (pos[4] == 0) { |
| 1124 | elems->wmm_info = pos; |
| 1125 | elems->wmm_info_len = elen; |
| 1126 | } else if (pos[4] == 1) { |
| 1127 | elems->wmm_param = pos; |
| 1128 | elems->wmm_param_len = elen; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | break; |
| 1133 | case WLAN_EID_RSN: |
| 1134 | elems->rsn = pos; |
| 1135 | elems->rsn_len = elen; |
| 1136 | break; |
| 1137 | case WLAN_EID_ERP_INFO: |
| 1138 | elems->erp_info = pos; |
| 1139 | elems->erp_info_len = elen; |
| 1140 | break; |
| 1141 | case WLAN_EID_EXT_SUPP_RATES: |
| 1142 | elems->ext_supp_rates = pos; |
| 1143 | elems->ext_supp_rates_len = elen; |
| 1144 | break; |
| 1145 | case WLAN_EID_HT_CAPABILITY: |
| 1146 | if (elen >= sizeof(struct ieee80211_ht_cap)) |
| 1147 | elems->ht_cap_elem = (void *)pos; |
| 1148 | break; |
| 1149 | case WLAN_EID_HT_INFORMATION: |
| 1150 | if (elen >= sizeof(struct ieee80211_ht_info)) |
| 1151 | elems->ht_info_elem = (void *)pos; |
| 1152 | break; |
| 1153 | case WLAN_EID_MESH_ID: |
| 1154 | elems->mesh_id = pos; |
| 1155 | elems->mesh_id_len = elen; |
| 1156 | break; |
| 1157 | case WLAN_EID_MESH_CONFIG: |
| 1158 | if (elen >= sizeof(struct ieee80211_meshconf_ie)) |
| 1159 | elems->mesh_config = (void *)pos; |
| 1160 | break; |
| 1161 | case WLAN_EID_PEER_LINK: |
| 1162 | elems->peer_link = pos; |
| 1163 | elems->peer_link_len = elen; |
| 1164 | break; |
| 1165 | case WLAN_EID_PREQ: |
| 1166 | elems->preq = pos; |
| 1167 | elems->preq_len = elen; |
| 1168 | break; |
| 1169 | case WLAN_EID_PREP: |
| 1170 | elems->prep = pos; |
| 1171 | elems->prep_len = elen; |
| 1172 | break; |
| 1173 | case WLAN_EID_PERR: |
| 1174 | elems->perr = pos; |
| 1175 | elems->perr_len = elen; |
| 1176 | break; |
| 1177 | case WLAN_EID_RANN: |
| 1178 | if (elen >= sizeof(struct ieee80211_rann_ie)) |
| 1179 | elems->rann = (void *)pos; |
| 1180 | break; |
| 1181 | case WLAN_EID_CHANNEL_SWITCH: |
| 1182 | elems->ch_switch_elem = pos; |
| 1183 | elems->ch_switch_elem_len = elen; |
| 1184 | break; |
| 1185 | case WLAN_EID_QUIET: |
| 1186 | if (!elems->quiet_elem) { |
| 1187 | elems->quiet_elem = pos; |
| 1188 | elems->quiet_elem_len = elen; |
| 1189 | } |
| 1190 | elems->num_of_quiet_elem++; |
| 1191 | break; |
| 1192 | case WLAN_EID_COUNTRY: |
| 1193 | elems->country_elem = pos; |
| 1194 | elems->country_elem_len = elen; |
| 1195 | break; |
| 1196 | case WLAN_EID_PWR_CONSTRAINT: |
| 1197 | elems->pwr_constr_elem = pos; |
| 1198 | elems->pwr_constr_elem_len = elen; |
| 1199 | break; |
| 1200 | case WLAN_EID_TIMEOUT_INTERVAL: |
| 1201 | elems->timeout_int = pos; |
| 1202 | elems->timeout_int_len = elen; |
| 1203 | break; |
| 1204 | default: |
| 1205 | break; |
| 1206 | } |
| 1207 | |
| 1208 | left -= elen; |
| 1209 | pos += elen; |
| 1210 | } |
| 1211 | |
| 1212 | return crc; |
| 1213 | } |
| 1214 | EXPORT_SYMBOL(ieee802_11_parse_elems_crc); |