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 | */ |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 6 | #include <linux/export.h> |
Johannes Berg | d323655 | 2009-04-20 14:31:42 +0200 | [diff] [blame] | 7 | #include <linux/bitops.h> |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 8 | #include <linux/etherdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.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> |
Dave Täht | b156579 | 2011-12-22 12:55:08 -0800 | [diff] [blame] | 12 | #include <net/dsfield.h> |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 13 | #include "core.h" |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 14 | #include "rdev-ops.h" |
| 15 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 16 | |
Johannes Berg | bd81525 | 2008-10-29 20:00:45 +0100 | [diff] [blame] | 17 | struct ieee80211_rate * |
| 18 | ieee80211_get_response_rate(struct ieee80211_supported_band *sband, |
Johannes Berg | 881d948 | 2009-01-21 15:13:48 +0100 | [diff] [blame] | 19 | u32 basic_rates, int bitrate) |
Johannes Berg | bd81525 | 2008-10-29 20:00:45 +0100 | [diff] [blame] | 20 | { |
| 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 | } |
| 34 | EXPORT_SYMBOL(ieee80211_get_response_rate); |
| 35 | |
Ashok Nagarajan | b422c6c | 2013-05-10 17:50:51 -0700 | [diff] [blame] | 36 | u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband) |
| 37 | { |
| 38 | struct ieee80211_rate *bitrates; |
| 39 | u32 mandatory_rates = 0; |
| 40 | enum ieee80211_rate_flags mandatory_flag; |
| 41 | int i; |
| 42 | |
| 43 | if (WARN_ON(!sband)) |
| 44 | return 1; |
| 45 | |
| 46 | if (sband->band == IEEE80211_BAND_2GHZ) |
| 47 | mandatory_flag = IEEE80211_RATE_MANDATORY_B; |
| 48 | else |
| 49 | mandatory_flag = IEEE80211_RATE_MANDATORY_A; |
| 50 | |
| 51 | bitrates = sband->bitrates; |
| 52 | for (i = 0; i < sband->n_bitrates; i++) |
| 53 | if (bitrates[i].flags & mandatory_flag) |
| 54 | mandatory_rates |= BIT(i); |
| 55 | return mandatory_rates; |
| 56 | } |
| 57 | EXPORT_SYMBOL(ieee80211_mandatory_rates); |
| 58 | |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 59 | int ieee80211_channel_to_frequency(int chan, enum ieee80211_band band) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 60 | { |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 61 | /* see 802.11 17.3.8.3.2 and Annex J |
| 62 | * there are overlapping channel numbers in 5GHz and 2GHz bands */ |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 63 | if (chan <= 0) |
| 64 | return 0; /* not supported */ |
| 65 | switch (band) { |
| 66 | case IEEE80211_BAND_2GHZ: |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 67 | if (chan == 14) |
| 68 | return 2484; |
| 69 | else if (chan < 14) |
| 70 | return 2407 + chan * 5; |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 71 | break; |
| 72 | case IEEE80211_BAND_5GHZ: |
| 73 | if (chan >= 182 && chan <= 196) |
| 74 | return 4000 + chan * 5; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 75 | else |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 76 | return 5000 + chan * 5; |
| 77 | break; |
| 78 | case IEEE80211_BAND_60GHZ: |
| 79 | if (chan < 5) |
| 80 | return 56160 + chan * 2160; |
| 81 | break; |
| 82 | default: |
| 83 | ; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 84 | } |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 85 | return 0; /* not supported */ |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 86 | } |
| 87 | EXPORT_SYMBOL(ieee80211_channel_to_frequency); |
| 88 | |
| 89 | int ieee80211_frequency_to_channel(int freq) |
| 90 | { |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 91 | /* see 802.11 17.3.8.3.2 and Annex J */ |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 92 | if (freq == 2484) |
| 93 | return 14; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 94 | else if (freq < 2484) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 95 | return (freq - 2407) / 5; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 96 | else if (freq >= 4910 && freq <= 4980) |
| 97 | return (freq - 4000) / 5; |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 98 | else if (freq <= 45000) /* DMG band lower limit */ |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 99 | return (freq - 5000) / 5; |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 100 | else if (freq >= 58320 && freq <= 64800) |
| 101 | return (freq - 56160) / 2160; |
| 102 | else |
| 103 | return 0; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 104 | } |
| 105 | EXPORT_SYMBOL(ieee80211_frequency_to_channel); |
| 106 | |
Johannes Berg | 6c507cd | 2008-03-26 14:14:55 +0100 | [diff] [blame] | 107 | struct ieee80211_channel *__ieee80211_get_channel(struct wiphy *wiphy, |
| 108 | int freq) |
Johannes Berg | 906c730 | 2008-03-16 18:34:33 +0100 | [diff] [blame] | 109 | { |
| 110 | enum ieee80211_band band; |
| 111 | struct ieee80211_supported_band *sband; |
| 112 | int i; |
| 113 | |
| 114 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) { |
| 115 | sband = wiphy->bands[band]; |
| 116 | |
| 117 | if (!sband) |
| 118 | continue; |
| 119 | |
| 120 | for (i = 0; i < sband->n_channels; i++) { |
| 121 | if (sband->channels[i].center_freq == freq) |
| 122 | return &sband->channels[i]; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return NULL; |
| 127 | } |
Johannes Berg | 6c507cd | 2008-03-26 14:14:55 +0100 | [diff] [blame] | 128 | EXPORT_SYMBOL(__ieee80211_get_channel); |
Johannes Berg | 906c730 | 2008-03-16 18:34:33 +0100 | [diff] [blame] | 129 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 130 | static void set_mandatory_flags_band(struct ieee80211_supported_band *sband, |
| 131 | enum ieee80211_band band) |
| 132 | { |
| 133 | int i, want; |
| 134 | |
| 135 | switch (band) { |
| 136 | case IEEE80211_BAND_5GHZ: |
| 137 | want = 3; |
| 138 | for (i = 0; i < sband->n_bitrates; i++) { |
| 139 | if (sband->bitrates[i].bitrate == 60 || |
| 140 | sband->bitrates[i].bitrate == 120 || |
| 141 | sband->bitrates[i].bitrate == 240) { |
| 142 | sband->bitrates[i].flags |= |
| 143 | IEEE80211_RATE_MANDATORY_A; |
| 144 | want--; |
| 145 | } |
| 146 | } |
| 147 | WARN_ON(want); |
| 148 | break; |
| 149 | case IEEE80211_BAND_2GHZ: |
| 150 | want = 7; |
| 151 | for (i = 0; i < sband->n_bitrates; i++) { |
| 152 | if (sband->bitrates[i].bitrate == 10) { |
| 153 | sband->bitrates[i].flags |= |
| 154 | IEEE80211_RATE_MANDATORY_B | |
| 155 | IEEE80211_RATE_MANDATORY_G; |
| 156 | want--; |
| 157 | } |
| 158 | |
| 159 | if (sband->bitrates[i].bitrate == 20 || |
| 160 | sband->bitrates[i].bitrate == 55 || |
| 161 | sband->bitrates[i].bitrate == 110 || |
| 162 | sband->bitrates[i].bitrate == 60 || |
| 163 | sband->bitrates[i].bitrate == 120 || |
| 164 | sband->bitrates[i].bitrate == 240) { |
| 165 | sband->bitrates[i].flags |= |
| 166 | IEEE80211_RATE_MANDATORY_G; |
| 167 | want--; |
| 168 | } |
| 169 | |
Johannes Berg | aac09fb | 2008-01-30 17:36:10 +0100 | [diff] [blame] | 170 | if (sband->bitrates[i].bitrate != 10 && |
| 171 | sband->bitrates[i].bitrate != 20 && |
| 172 | sband->bitrates[i].bitrate != 55 && |
| 173 | sband->bitrates[i].bitrate != 110) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 174 | sband->bitrates[i].flags |= |
| 175 | IEEE80211_RATE_ERP_G; |
| 176 | } |
Ivo van Doorn | 406f238 | 2008-02-02 23:53:10 +0100 | [diff] [blame] | 177 | WARN_ON(want != 0 && want != 3 && want != 6); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 178 | break; |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 179 | case IEEE80211_BAND_60GHZ: |
| 180 | /* check for mandatory HT MCS 1..4 */ |
| 181 | WARN_ON(!sband->ht_cap.ht_supported); |
| 182 | WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e); |
| 183 | break; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 184 | case IEEE80211_NUM_BANDS: |
| 185 | WARN_ON(1); |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void ieee80211_set_bitrate_flags(struct wiphy *wiphy) |
| 191 | { |
| 192 | enum ieee80211_band band; |
| 193 | |
| 194 | for (band = 0; band < IEEE80211_NUM_BANDS; band++) |
| 195 | if (wiphy->bands[band]) |
| 196 | set_mandatory_flags_band(wiphy->bands[band], band); |
| 197 | } |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 198 | |
Jouni Malinen | 38ba3c5 | 2011-09-21 18:14:56 +0300 | [diff] [blame] | 199 | bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher) |
| 200 | { |
| 201 | int i; |
| 202 | for (i = 0; i < wiphy->n_cipher_suites; i++) |
| 203 | if (cipher == wiphy->cipher_suites[i]) |
| 204 | return true; |
| 205 | return false; |
| 206 | } |
| 207 | |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 208 | int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev, |
| 209 | struct key_params *params, int key_idx, |
Johannes Berg | e31b821 | 2010-10-05 19:39:30 +0200 | [diff] [blame] | 210 | bool pairwise, const u8 *mac_addr) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 211 | { |
| 212 | if (key_idx > 5) |
| 213 | return -EINVAL; |
| 214 | |
Johannes Berg | e31b821 | 2010-10-05 19:39:30 +0200 | [diff] [blame] | 215 | if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | if (pairwise && !mac_addr) |
| 219 | return -EINVAL; |
| 220 | |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 221 | /* |
| 222 | * Disallow pairwise keys with non-zero index unless it's WEP |
Juuso Oikarinen | 45cbad6 | 2011-01-25 12:21:22 +0200 | [diff] [blame] | 223 | * or a vendor specific cipher (because current deployments use |
| 224 | * pairwise WEP keys with non-zero indices and for vendor specific |
| 225 | * ciphers this should be validated in the driver or hardware level |
| 226 | * - but 802.11i clearly specifies to use zero) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 227 | */ |
Johannes Berg | e31b821 | 2010-10-05 19:39:30 +0200 | [diff] [blame] | 228 | if (pairwise && key_idx && |
Juuso Oikarinen | 45cbad6 | 2011-01-25 12:21:22 +0200 | [diff] [blame] | 229 | ((params->cipher == WLAN_CIPHER_SUITE_TKIP) || |
| 230 | (params->cipher == WLAN_CIPHER_SUITE_CCMP) || |
| 231 | (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC))) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 232 | return -EINVAL; |
| 233 | |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 234 | switch (params->cipher) { |
| 235 | case WLAN_CIPHER_SUITE_WEP40: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 236 | if (params->key_len != WLAN_KEY_LEN_WEP40) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 237 | return -EINVAL; |
| 238 | break; |
| 239 | case WLAN_CIPHER_SUITE_TKIP: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 240 | if (params->key_len != WLAN_KEY_LEN_TKIP) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 241 | return -EINVAL; |
| 242 | break; |
| 243 | case WLAN_CIPHER_SUITE_CCMP: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 244 | if (params->key_len != WLAN_KEY_LEN_CCMP) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 245 | return -EINVAL; |
| 246 | break; |
| 247 | case WLAN_CIPHER_SUITE_WEP104: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 248 | if (params->key_len != WLAN_KEY_LEN_WEP104) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | break; |
| 251 | case WLAN_CIPHER_SUITE_AES_CMAC: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 252 | if (params->key_len != WLAN_KEY_LEN_AES_CMAC) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 253 | return -EINVAL; |
| 254 | break; |
| 255 | default: |
Johannes Berg | 7d64b7c | 2010-08-27 14:26:51 +0300 | [diff] [blame] | 256 | /* |
| 257 | * We don't know anything about this algorithm, |
| 258 | * allow using it -- but the driver must check |
| 259 | * all parameters! We still check below whether |
| 260 | * or not the driver supports this algorithm, |
| 261 | * of course. |
| 262 | */ |
| 263 | break; |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Jouni Malinen | 9f26a95 | 2009-05-15 12:38:32 +0300 | [diff] [blame] | 266 | if (params->seq) { |
| 267 | switch (params->cipher) { |
| 268 | case WLAN_CIPHER_SUITE_WEP40: |
| 269 | case WLAN_CIPHER_SUITE_WEP104: |
| 270 | /* These ciphers do not use key sequence */ |
| 271 | return -EINVAL; |
| 272 | case WLAN_CIPHER_SUITE_TKIP: |
| 273 | case WLAN_CIPHER_SUITE_CCMP: |
| 274 | case WLAN_CIPHER_SUITE_AES_CMAC: |
| 275 | if (params->seq_len != 6) |
| 276 | return -EINVAL; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
Jouni Malinen | 38ba3c5 | 2011-09-21 18:14:56 +0300 | [diff] [blame] | 281 | if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher)) |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 282 | return -EINVAL; |
| 283 | |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 284 | return 0; |
| 285 | } |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 286 | |
Johannes Berg | 633adf1 | 2010-08-12 14:49:58 +0200 | [diff] [blame] | 287 | unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 288 | { |
| 289 | unsigned int hdrlen = 24; |
| 290 | |
| 291 | if (ieee80211_is_data(fc)) { |
| 292 | if (ieee80211_has_a4(fc)) |
| 293 | hdrlen = 30; |
Andriy Tkachuk | d0dd2de | 2010-01-20 13:55:06 +0200 | [diff] [blame] | 294 | if (ieee80211_is_data_qos(fc)) { |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 295 | hdrlen += IEEE80211_QOS_CTL_LEN; |
Andriy Tkachuk | d0dd2de | 2010-01-20 13:55:06 +0200 | [diff] [blame] | 296 | if (ieee80211_has_order(fc)) |
| 297 | hdrlen += IEEE80211_HT_CTL_LEN; |
| 298 | } |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 299 | goto out; |
| 300 | } |
| 301 | |
| 302 | if (ieee80211_is_ctl(fc)) { |
| 303 | /* |
| 304 | * ACK and CTS are 10 bytes, all others 16. To see how |
| 305 | * to get this condition consider |
| 306 | * subtype mask: 0b0000000011110000 (0x00F0) |
| 307 | * ACK subtype: 0b0000000011010000 (0x00D0) |
| 308 | * CTS subtype: 0b0000000011000000 (0x00C0) |
| 309 | * bits that matter: ^^^ (0x00E0) |
| 310 | * value of those: 0b0000000011000000 (0x00C0) |
| 311 | */ |
| 312 | if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0)) |
| 313 | hdrlen = 10; |
| 314 | else |
| 315 | hdrlen = 16; |
| 316 | } |
| 317 | out: |
| 318 | return hdrlen; |
| 319 | } |
| 320 | EXPORT_SYMBOL(ieee80211_hdrlen); |
| 321 | |
| 322 | unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb) |
| 323 | { |
| 324 | const struct ieee80211_hdr *hdr = |
| 325 | (const struct ieee80211_hdr *)skb->data; |
| 326 | unsigned int hdrlen; |
| 327 | |
| 328 | if (unlikely(skb->len < 10)) |
| 329 | return 0; |
| 330 | hdrlen = ieee80211_hdrlen(hdr->frame_control); |
| 331 | if (unlikely(hdrlen > skb->len)) |
| 332 | return 0; |
| 333 | return hdrlen; |
| 334 | } |
| 335 | EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb); |
| 336 | |
Johannes Berg | 9b395bc | 2012-10-26 00:36:40 +0200 | [diff] [blame] | 337 | unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 338 | { |
| 339 | int ae = meshhdr->flags & MESH_FLAGS_AE; |
Johannes Berg | 7dd111e | 2012-10-25 21:51:59 +0200 | [diff] [blame] | 340 | /* 802.11-2012, 8.2.4.7.3 */ |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 341 | switch (ae) { |
Johannes Berg | 7dd111e | 2012-10-25 21:51:59 +0200 | [diff] [blame] | 342 | default: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 343 | case 0: |
| 344 | return 6; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 345 | case MESH_FLAGS_AE_A4: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 346 | return 12; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 347 | case MESH_FLAGS_AE_A5_A6: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 348 | return 18; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 349 | } |
| 350 | } |
Johannes Berg | 9b395bc | 2012-10-26 00:36:40 +0200 | [diff] [blame] | 351 | EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 352 | |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 353 | int ieee80211_data_to_8023(struct sk_buff *skb, const u8 *addr, |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 354 | enum nl80211_iftype iftype) |
| 355 | { |
| 356 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
| 357 | u16 hdrlen, ethertype; |
| 358 | u8 *payload; |
| 359 | u8 dst[ETH_ALEN]; |
| 360 | u8 src[ETH_ALEN] __aligned(2); |
| 361 | |
| 362 | if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) |
| 363 | return -1; |
| 364 | |
| 365 | hdrlen = ieee80211_hdrlen(hdr->frame_control); |
| 366 | |
| 367 | /* convert IEEE 802.11 header + possible LLC headers into Ethernet |
| 368 | * header |
| 369 | * IEEE 802.11 address fields: |
| 370 | * ToDS FromDS Addr1 Addr2 Addr3 Addr4 |
| 371 | * 0 0 DA SA BSSID n/a |
| 372 | * 0 1 DA BSSID SA n/a |
| 373 | * 1 0 BSSID SA DA n/a |
| 374 | * 1 1 RA TA DA SA |
| 375 | */ |
| 376 | memcpy(dst, ieee80211_get_DA(hdr), ETH_ALEN); |
| 377 | memcpy(src, ieee80211_get_SA(hdr), ETH_ALEN); |
| 378 | |
| 379 | switch (hdr->frame_control & |
| 380 | cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) { |
| 381 | case cpu_to_le16(IEEE80211_FCTL_TODS): |
| 382 | if (unlikely(iftype != NL80211_IFTYPE_AP && |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 383 | iftype != NL80211_IFTYPE_AP_VLAN && |
| 384 | iftype != NL80211_IFTYPE_P2P_GO)) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 385 | return -1; |
| 386 | break; |
| 387 | case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS): |
| 388 | if (unlikely(iftype != NL80211_IFTYPE_WDS && |
Felix Fietkau | f14543ee | 2009-11-10 20:10:05 +0100 | [diff] [blame] | 389 | iftype != NL80211_IFTYPE_MESH_POINT && |
| 390 | iftype != NL80211_IFTYPE_AP_VLAN && |
| 391 | iftype != NL80211_IFTYPE_STATION)) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 392 | return -1; |
| 393 | if (iftype == NL80211_IFTYPE_MESH_POINT) { |
| 394 | struct ieee80211s_hdr *meshdr = |
| 395 | (struct ieee80211s_hdr *) (skb->data + hdrlen); |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 396 | /* make sure meshdr->flags is on the linear part */ |
| 397 | if (!pskb_may_pull(skb, hdrlen + 1)) |
| 398 | return -1; |
Johannes Berg | 7dd111e | 2012-10-25 21:51:59 +0200 | [diff] [blame] | 399 | if (meshdr->flags & MESH_FLAGS_AE_A4) |
| 400 | return -1; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 401 | if (meshdr->flags & MESH_FLAGS_AE_A5_A6) { |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 402 | skb_copy_bits(skb, hdrlen + |
| 403 | offsetof(struct ieee80211s_hdr, eaddr1), |
| 404 | dst, ETH_ALEN); |
| 405 | skb_copy_bits(skb, hdrlen + |
| 406 | offsetof(struct ieee80211s_hdr, eaddr2), |
| 407 | src, ETH_ALEN); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 408 | } |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 409 | hdrlen += ieee80211_get_mesh_hdrlen(meshdr); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 410 | } |
| 411 | break; |
| 412 | case cpu_to_le16(IEEE80211_FCTL_FROMDS): |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 413 | if ((iftype != NL80211_IFTYPE_STATION && |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 414 | iftype != NL80211_IFTYPE_P2P_CLIENT && |
| 415 | iftype != NL80211_IFTYPE_MESH_POINT) || |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 416 | (is_multicast_ether_addr(dst) && |
Joe Perches | 4c76472 | 2012-05-08 18:56:56 +0000 | [diff] [blame] | 417 | ether_addr_equal(src, addr))) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 418 | return -1; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 419 | if (iftype == NL80211_IFTYPE_MESH_POINT) { |
| 420 | struct ieee80211s_hdr *meshdr = |
| 421 | (struct ieee80211s_hdr *) (skb->data + hdrlen); |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 422 | /* make sure meshdr->flags is on the linear part */ |
| 423 | if (!pskb_may_pull(skb, hdrlen + 1)) |
| 424 | return -1; |
Johannes Berg | 7dd111e | 2012-10-25 21:51:59 +0200 | [diff] [blame] | 425 | if (meshdr->flags & MESH_FLAGS_AE_A5_A6) |
| 426 | return -1; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 427 | if (meshdr->flags & MESH_FLAGS_AE_A4) |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 428 | skb_copy_bits(skb, hdrlen + |
| 429 | offsetof(struct ieee80211s_hdr, eaddr1), |
| 430 | src, ETH_ALEN); |
| 431 | hdrlen += ieee80211_get_mesh_hdrlen(meshdr); |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 432 | } |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 433 | break; |
| 434 | case cpu_to_le16(0): |
Arik Nemtsov | 941c93c | 2011-09-28 14:12:54 +0300 | [diff] [blame] | 435 | if (iftype != NL80211_IFTYPE_ADHOC && |
| 436 | iftype != NL80211_IFTYPE_STATION) |
| 437 | return -1; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | |
Zhu Yi | e3cf8b3 | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 441 | if (!pskb_may_pull(skb, hdrlen + 8)) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 442 | return -1; |
| 443 | |
| 444 | payload = skb->data + hdrlen; |
| 445 | ethertype = (payload[6] << 8) | payload[7]; |
| 446 | |
Joe Perches | 4c76472 | 2012-05-08 18:56:56 +0000 | [diff] [blame] | 447 | if (likely((ether_addr_equal(payload, rfc1042_header) && |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 448 | ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) || |
Joe Perches | 4c76472 | 2012-05-08 18:56:56 +0000 | [diff] [blame] | 449 | ether_addr_equal(payload, bridge_tunnel_header))) { |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 450 | /* remove RFC1042 or Bridge-Tunnel encapsulation and |
| 451 | * replace EtherType */ |
| 452 | skb_pull(skb, hdrlen + 6); |
| 453 | memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN); |
| 454 | memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN); |
| 455 | } else { |
| 456 | struct ethhdr *ehdr; |
| 457 | __be16 len; |
| 458 | |
| 459 | skb_pull(skb, hdrlen); |
| 460 | len = htons(skb->len); |
| 461 | ehdr = (struct ethhdr *) skb_push(skb, sizeof(struct ethhdr)); |
| 462 | memcpy(ehdr->h_dest, dst, ETH_ALEN); |
| 463 | memcpy(ehdr->h_source, src, ETH_ALEN); |
| 464 | ehdr->h_proto = len; |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | EXPORT_SYMBOL(ieee80211_data_to_8023); |
| 469 | |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 470 | int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr, |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 471 | enum nl80211_iftype iftype, u8 *bssid, bool qos) |
| 472 | { |
| 473 | struct ieee80211_hdr hdr; |
| 474 | u16 hdrlen, ethertype; |
| 475 | __le16 fc; |
| 476 | const u8 *encaps_data; |
| 477 | int encaps_len, skip_header_bytes; |
| 478 | int nh_pos, h_pos; |
| 479 | int head_need; |
| 480 | |
| 481 | if (unlikely(skb->len < ETH_HLEN)) |
| 482 | return -EINVAL; |
| 483 | |
| 484 | nh_pos = skb_network_header(skb) - skb->data; |
| 485 | h_pos = skb_transport_header(skb) - skb->data; |
| 486 | |
| 487 | /* convert Ethernet header to proper 802.11 header (based on |
| 488 | * operation mode) */ |
| 489 | ethertype = (skb->data[12] << 8) | skb->data[13]; |
| 490 | fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA); |
| 491 | |
| 492 | switch (iftype) { |
| 493 | case NL80211_IFTYPE_AP: |
| 494 | case NL80211_IFTYPE_AP_VLAN: |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 495 | case NL80211_IFTYPE_P2P_GO: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 496 | fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS); |
| 497 | /* DA BSSID SA */ |
| 498 | memcpy(hdr.addr1, skb->data, ETH_ALEN); |
| 499 | memcpy(hdr.addr2, addr, ETH_ALEN); |
| 500 | memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN); |
| 501 | hdrlen = 24; |
| 502 | break; |
| 503 | case NL80211_IFTYPE_STATION: |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 504 | case NL80211_IFTYPE_P2P_CLIENT: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 505 | fc |= cpu_to_le16(IEEE80211_FCTL_TODS); |
| 506 | /* BSSID SA DA */ |
| 507 | memcpy(hdr.addr1, bssid, ETH_ALEN); |
| 508 | memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); |
| 509 | memcpy(hdr.addr3, skb->data, ETH_ALEN); |
| 510 | hdrlen = 24; |
| 511 | break; |
| 512 | case NL80211_IFTYPE_ADHOC: |
| 513 | /* DA SA BSSID */ |
| 514 | memcpy(hdr.addr1, skb->data, ETH_ALEN); |
| 515 | memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); |
| 516 | memcpy(hdr.addr3, bssid, ETH_ALEN); |
| 517 | hdrlen = 24; |
| 518 | break; |
| 519 | default: |
| 520 | return -EOPNOTSUPP; |
| 521 | } |
| 522 | |
| 523 | if (qos) { |
| 524 | fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA); |
| 525 | hdrlen += 2; |
| 526 | } |
| 527 | |
| 528 | hdr.frame_control = fc; |
| 529 | hdr.duration_id = 0; |
| 530 | hdr.seq_ctrl = 0; |
| 531 | |
| 532 | skip_header_bytes = ETH_HLEN; |
| 533 | if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) { |
| 534 | encaps_data = bridge_tunnel_header; |
| 535 | encaps_len = sizeof(bridge_tunnel_header); |
| 536 | skip_header_bytes -= 2; |
Simon Horman | e5c5d22 | 2013-03-28 13:38:25 +0900 | [diff] [blame] | 537 | } else if (ethertype >= ETH_P_802_3_MIN) { |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 538 | encaps_data = rfc1042_header; |
| 539 | encaps_len = sizeof(rfc1042_header); |
| 540 | skip_header_bytes -= 2; |
| 541 | } else { |
| 542 | encaps_data = NULL; |
| 543 | encaps_len = 0; |
| 544 | } |
| 545 | |
| 546 | skb_pull(skb, skip_header_bytes); |
| 547 | nh_pos -= skip_header_bytes; |
| 548 | h_pos -= skip_header_bytes; |
| 549 | |
| 550 | head_need = hdrlen + encaps_len - skb_headroom(skb); |
| 551 | |
| 552 | if (head_need > 0 || skb_cloned(skb)) { |
| 553 | head_need = max(head_need, 0); |
| 554 | if (head_need) |
| 555 | skb_orphan(skb); |
| 556 | |
Joe Perches | 2461615 | 2011-08-29 14:17:41 -0700 | [diff] [blame] | 557 | if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC)) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 558 | return -ENOMEM; |
Joe Perches | 2461615 | 2011-08-29 14:17:41 -0700 | [diff] [blame] | 559 | |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 560 | skb->truesize += head_need; |
| 561 | } |
| 562 | |
| 563 | if (encaps_data) { |
| 564 | memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len); |
| 565 | nh_pos += encaps_len; |
| 566 | h_pos += encaps_len; |
| 567 | } |
| 568 | |
| 569 | memcpy(skb_push(skb, hdrlen), &hdr, hdrlen); |
| 570 | |
| 571 | nh_pos += hdrlen; |
| 572 | h_pos += hdrlen; |
| 573 | |
| 574 | /* Update skb pointers to various headers since this modified frame |
| 575 | * is going to go through Linux networking code that may potentially |
| 576 | * need things like pointer to IP header. */ |
| 577 | skb_set_mac_header(skb, 0); |
| 578 | skb_set_network_header(skb, nh_pos); |
| 579 | skb_set_transport_header(skb, h_pos); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | EXPORT_SYMBOL(ieee80211_data_from_8023); |
| 584 | |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 585 | |
| 586 | void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list, |
| 587 | const u8 *addr, enum nl80211_iftype iftype, |
Yogesh Ashok Powar | 8b3beca | 2011-05-13 11:22:31 -0700 | [diff] [blame] | 588 | const unsigned int extra_headroom, |
| 589 | bool has_80211_header) |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 590 | { |
| 591 | struct sk_buff *frame = NULL; |
| 592 | u16 ethertype; |
| 593 | u8 *payload; |
| 594 | const struct ethhdr *eth; |
| 595 | int remaining, err; |
| 596 | u8 dst[ETH_ALEN], src[ETH_ALEN]; |
| 597 | |
Yogesh Ashok Powar | 8b3beca | 2011-05-13 11:22:31 -0700 | [diff] [blame] | 598 | if (has_80211_header) { |
| 599 | err = ieee80211_data_to_8023(skb, addr, iftype); |
| 600 | if (err) |
| 601 | goto out; |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 602 | |
Yogesh Ashok Powar | 8b3beca | 2011-05-13 11:22:31 -0700 | [diff] [blame] | 603 | /* skip the wrapping header */ |
| 604 | eth = (struct ethhdr *) skb_pull(skb, sizeof(struct ethhdr)); |
| 605 | if (!eth) |
| 606 | goto out; |
| 607 | } else { |
| 608 | eth = (struct ethhdr *) skb->data; |
| 609 | } |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 610 | |
| 611 | while (skb != frame) { |
| 612 | u8 padding; |
| 613 | __be16 len = eth->h_proto; |
| 614 | unsigned int subframe_len = sizeof(struct ethhdr) + ntohs(len); |
| 615 | |
| 616 | remaining = skb->len; |
| 617 | memcpy(dst, eth->h_dest, ETH_ALEN); |
| 618 | memcpy(src, eth->h_source, ETH_ALEN); |
| 619 | |
| 620 | padding = (4 - subframe_len) & 0x3; |
| 621 | /* the last MSDU has no padding */ |
| 622 | if (subframe_len > remaining) |
| 623 | goto purge; |
| 624 | |
| 625 | skb_pull(skb, sizeof(struct ethhdr)); |
| 626 | /* reuse skb for the last subframe */ |
| 627 | if (remaining <= subframe_len + padding) |
| 628 | frame = skb; |
| 629 | else { |
| 630 | unsigned int hlen = ALIGN(extra_headroom, 4); |
| 631 | /* |
| 632 | * Allocate and reserve two bytes more for payload |
| 633 | * alignment since sizeof(struct ethhdr) is 14. |
| 634 | */ |
| 635 | frame = dev_alloc_skb(hlen + subframe_len + 2); |
| 636 | if (!frame) |
| 637 | goto purge; |
| 638 | |
| 639 | skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2); |
| 640 | memcpy(skb_put(frame, ntohs(len)), skb->data, |
| 641 | ntohs(len)); |
| 642 | |
| 643 | eth = (struct ethhdr *)skb_pull(skb, ntohs(len) + |
| 644 | padding); |
| 645 | if (!eth) { |
| 646 | dev_kfree_skb(frame); |
| 647 | goto purge; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | skb_reset_network_header(frame); |
| 652 | frame->dev = skb->dev; |
| 653 | frame->priority = skb->priority; |
| 654 | |
| 655 | payload = frame->data; |
| 656 | ethertype = (payload[6] << 8) | payload[7]; |
| 657 | |
Joe Perches | ac422d3 | 2012-05-08 18:56:55 +0000 | [diff] [blame] | 658 | if (likely((ether_addr_equal(payload, rfc1042_header) && |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 659 | ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) || |
Joe Perches | ac422d3 | 2012-05-08 18:56:55 +0000 | [diff] [blame] | 660 | ether_addr_equal(payload, bridge_tunnel_header))) { |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 661 | /* remove RFC1042 or Bridge-Tunnel |
| 662 | * encapsulation and replace EtherType */ |
| 663 | skb_pull(frame, 6); |
| 664 | memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN); |
| 665 | memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN); |
| 666 | } else { |
| 667 | memcpy(skb_push(frame, sizeof(__be16)), &len, |
| 668 | sizeof(__be16)); |
| 669 | memcpy(skb_push(frame, ETH_ALEN), src, ETH_ALEN); |
| 670 | memcpy(skb_push(frame, ETH_ALEN), dst, ETH_ALEN); |
| 671 | } |
| 672 | __skb_queue_tail(list, frame); |
| 673 | } |
| 674 | |
| 675 | return; |
| 676 | |
| 677 | purge: |
| 678 | __skb_queue_purge(list); |
| 679 | out: |
| 680 | dev_kfree_skb(skb); |
| 681 | } |
| 682 | EXPORT_SYMBOL(ieee80211_amsdu_to_8023s); |
| 683 | |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 684 | /* Given a data frame determine the 802.1p/1d tag to use. */ |
| 685 | unsigned int cfg80211_classify8021d(struct sk_buff *skb) |
| 686 | { |
| 687 | unsigned int dscp; |
| 688 | |
| 689 | /* skb->priority values from 256->263 are magic values to |
| 690 | * directly indicate a specific 802.1d priority. This is used |
| 691 | * to allow 802.1d priority to be passed directly in from VLAN |
| 692 | * tags, etc. |
| 693 | */ |
| 694 | if (skb->priority >= 256 && skb->priority <= 263) |
| 695 | return skb->priority - 256; |
| 696 | |
| 697 | switch (skb->protocol) { |
| 698 | case htons(ETH_P_IP): |
Dave Täht | b156579 | 2011-12-22 12:55:08 -0800 | [diff] [blame] | 699 | dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc; |
| 700 | break; |
| 701 | case htons(ETH_P_IPV6): |
| 702 | dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 703 | break; |
| 704 | default: |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | return dscp >> 5; |
| 709 | } |
| 710 | EXPORT_SYMBOL(cfg80211_classify8021d); |
Johannes Berg | 517357c | 2009-07-02 17:18:40 +0200 | [diff] [blame] | 711 | |
| 712 | const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie) |
| 713 | { |
Johannes Berg | 9caf036 | 2012-11-29 01:25:20 +0100 | [diff] [blame] | 714 | const struct cfg80211_bss_ies *ies; |
| 715 | |
| 716 | ies = rcu_dereference(bss->ies); |
| 717 | if (!ies) |
Johannes Berg | 517357c | 2009-07-02 17:18:40 +0200 | [diff] [blame] | 718 | return NULL; |
Johannes Berg | 9caf036 | 2012-11-29 01:25:20 +0100 | [diff] [blame] | 719 | |
| 720 | return cfg80211_find_ie(ie, ies->data, ies->len); |
Johannes Berg | 517357c | 2009-07-02 17:18:40 +0200 | [diff] [blame] | 721 | } |
| 722 | EXPORT_SYMBOL(ieee80211_bss_get_ie); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 723 | |
| 724 | void cfg80211_upload_connect_keys(struct wireless_dev *wdev) |
| 725 | { |
| 726 | struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy); |
| 727 | struct net_device *dev = wdev->netdev; |
| 728 | int i; |
| 729 | |
| 730 | if (!wdev->connect_keys) |
| 731 | return; |
| 732 | |
| 733 | for (i = 0; i < 6; i++) { |
| 734 | if (!wdev->connect_keys->params[i].cipher) |
| 735 | continue; |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 736 | if (rdev_add_key(rdev, dev, i, false, NULL, |
| 737 | &wdev->connect_keys->params[i])) { |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 738 | netdev_err(dev, "failed to set key %d\n", i); |
Zhu Yi | 1e05666 | 2009-07-20 16:12:57 +0800 | [diff] [blame] | 739 | continue; |
| 740 | } |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 741 | if (wdev->connect_keys->def == i) |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 742 | if (rdev_set_default_key(rdev, dev, i, true, true)) { |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 743 | netdev_err(dev, "failed to set defkey %d\n", i); |
Zhu Yi | 1e05666 | 2009-07-20 16:12:57 +0800 | [diff] [blame] | 744 | continue; |
| 745 | } |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 746 | if (wdev->connect_keys->defmgmt == i) |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 747 | if (rdev_set_default_mgmt_key(rdev, dev, i)) |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 748 | netdev_err(dev, "failed to set mgtdef %d\n", i); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | kfree(wdev->connect_keys); |
| 752 | wdev->connect_keys = NULL; |
| 753 | } |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 754 | |
Daniel Drake | 1f6fc43 | 2012-08-02 18:41:48 +0100 | [diff] [blame] | 755 | void cfg80211_process_wdev_events(struct wireless_dev *wdev) |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 756 | { |
| 757 | struct cfg80211_event *ev; |
| 758 | unsigned long flags; |
| 759 | const u8 *bssid = NULL; |
| 760 | |
| 761 | spin_lock_irqsave(&wdev->event_lock, flags); |
| 762 | while (!list_empty(&wdev->event_list)) { |
| 763 | ev = list_first_entry(&wdev->event_list, |
| 764 | struct cfg80211_event, list); |
| 765 | list_del(&ev->list); |
| 766 | spin_unlock_irqrestore(&wdev->event_lock, flags); |
| 767 | |
| 768 | wdev_lock(wdev); |
| 769 | switch (ev->type) { |
| 770 | case EVENT_CONNECT_RESULT: |
| 771 | if (!is_zero_ether_addr(ev->cr.bssid)) |
| 772 | bssid = ev->cr.bssid; |
| 773 | __cfg80211_connect_result( |
| 774 | wdev->netdev, bssid, |
| 775 | ev->cr.req_ie, ev->cr.req_ie_len, |
| 776 | ev->cr.resp_ie, ev->cr.resp_ie_len, |
| 777 | ev->cr.status, |
| 778 | ev->cr.status == WLAN_STATUS_SUCCESS, |
| 779 | NULL); |
| 780 | break; |
| 781 | case EVENT_ROAMED: |
Vasanthakumar Thiagarajan | adbde34 | 2011-12-08 14:28:47 +0530 | [diff] [blame] | 782 | __cfg80211_roamed(wdev, ev->rm.bss, ev->rm.req_ie, |
| 783 | ev->rm.req_ie_len, ev->rm.resp_ie, |
| 784 | ev->rm.resp_ie_len); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 785 | break; |
| 786 | case EVENT_DISCONNECTED: |
| 787 | __cfg80211_disconnected(wdev->netdev, |
| 788 | ev->dc.ie, ev->dc.ie_len, |
| 789 | ev->dc.reason, true); |
| 790 | break; |
| 791 | case EVENT_IBSS_JOINED: |
| 792 | __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid); |
| 793 | break; |
| 794 | } |
| 795 | wdev_unlock(wdev); |
| 796 | |
| 797 | kfree(ev); |
| 798 | |
| 799 | spin_lock_irqsave(&wdev->event_lock, flags); |
| 800 | } |
| 801 | spin_unlock_irqrestore(&wdev->event_lock, flags); |
| 802 | } |
| 803 | |
| 804 | void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev) |
| 805 | { |
| 806 | struct wireless_dev *wdev; |
| 807 | |
| 808 | ASSERT_RTNL(); |
| 809 | ASSERT_RDEV_LOCK(rdev); |
| 810 | |
Johannes Berg | 89a54e4 | 2012-06-15 14:33:17 +0200 | [diff] [blame] | 811 | list_for_each_entry(wdev, &rdev->wdev_list, list) |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 812 | cfg80211_process_wdev_events(wdev); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | int cfg80211_change_iface(struct cfg80211_registered_device *rdev, |
| 816 | struct net_device *dev, enum nl80211_iftype ntype, |
| 817 | u32 *flags, struct vif_params *params) |
| 818 | { |
| 819 | int err; |
| 820 | enum nl80211_iftype otype = dev->ieee80211_ptr->iftype; |
| 821 | |
| 822 | ASSERT_RDEV_LOCK(rdev); |
| 823 | |
| 824 | /* don't support changing VLANs, you just re-create them */ |
| 825 | if (otype == NL80211_IFTYPE_AP_VLAN) |
| 826 | return -EOPNOTSUPP; |
| 827 | |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 828 | /* cannot change into P2P device type */ |
| 829 | if (ntype == NL80211_IFTYPE_P2P_DEVICE) |
| 830 | return -EOPNOTSUPP; |
| 831 | |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 832 | if (!rdev->ops->change_virtual_intf || |
| 833 | !(rdev->wiphy.interface_modes & (1 << ntype))) |
| 834 | return -EOPNOTSUPP; |
| 835 | |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 836 | /* 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] | 837 | if ((dev->priv_flags & IFF_BRIDGE_PORT) && |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 838 | (ntype == NL80211_IFTYPE_ADHOC || |
| 839 | ntype == NL80211_IFTYPE_STATION || |
| 840 | ntype == NL80211_IFTYPE_P2P_CLIENT)) |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 841 | return -EBUSY; |
| 842 | |
Michal Kazior | f8cdddb | 2012-06-08 10:55:44 +0200 | [diff] [blame] | 843 | if (ntype != otype && netif_running(dev)) { |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 844 | err = cfg80211_can_change_interface(rdev, dev->ieee80211_ptr, |
| 845 | ntype); |
| 846 | if (err) |
| 847 | return err; |
| 848 | |
Johannes Berg | 9bc383d | 2009-11-19 11:55:19 +0100 | [diff] [blame] | 849 | dev->ieee80211_ptr->use_4addr = false; |
Johannes Berg | 29cbe68 | 2010-12-03 09:20:44 +0100 | [diff] [blame] | 850 | dev->ieee80211_ptr->mesh_id_up_len = 0; |
Johannes Berg | 9bc383d | 2009-11-19 11:55:19 +0100 | [diff] [blame] | 851 | |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 852 | switch (otype) { |
Michal Kazior | ac80014 | 2012-06-29 12:46:57 +0200 | [diff] [blame] | 853 | case NL80211_IFTYPE_AP: |
| 854 | cfg80211_stop_ap(rdev, dev); |
| 855 | break; |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 856 | case NL80211_IFTYPE_ADHOC: |
| 857 | cfg80211_leave_ibss(rdev, dev, false); |
| 858 | break; |
| 859 | case NL80211_IFTYPE_STATION: |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 860 | case NL80211_IFTYPE_P2P_CLIENT: |
Johannes Berg | 83739b0 | 2013-05-15 17:44:01 +0200 | [diff] [blame] | 861 | wdev_lock(dev->ieee80211_ptr); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 862 | cfg80211_disconnect(rdev, dev, |
| 863 | WLAN_REASON_DEAUTH_LEAVING, true); |
Johannes Berg | 83739b0 | 2013-05-15 17:44:01 +0200 | [diff] [blame] | 864 | wdev_unlock(dev->ieee80211_ptr); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 865 | break; |
| 866 | case NL80211_IFTYPE_MESH_POINT: |
| 867 | /* mesh should be handled? */ |
| 868 | break; |
| 869 | default: |
| 870 | break; |
| 871 | } |
| 872 | |
| 873 | cfg80211_process_rdev_events(rdev); |
| 874 | } |
| 875 | |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 876 | err = rdev_change_virtual_intf(rdev, dev, ntype, flags, params); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 877 | |
| 878 | WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype); |
| 879 | |
Johannes Berg | 9bc383d | 2009-11-19 11:55:19 +0100 | [diff] [blame] | 880 | if (!err && params && params->use_4addr != -1) |
| 881 | dev->ieee80211_ptr->use_4addr = params->use_4addr; |
| 882 | |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 883 | if (!err) { |
| 884 | dev->priv_flags &= ~IFF_DONT_BRIDGE; |
| 885 | switch (ntype) { |
| 886 | case NL80211_IFTYPE_STATION: |
| 887 | if (dev->ieee80211_ptr->use_4addr) |
| 888 | break; |
| 889 | /* fall through */ |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 890 | case NL80211_IFTYPE_P2P_CLIENT: |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 891 | case NL80211_IFTYPE_ADHOC: |
| 892 | dev->priv_flags |= IFF_DONT_BRIDGE; |
| 893 | break; |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 894 | case NL80211_IFTYPE_P2P_GO: |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 895 | case NL80211_IFTYPE_AP: |
| 896 | case NL80211_IFTYPE_AP_VLAN: |
| 897 | case NL80211_IFTYPE_WDS: |
| 898 | case NL80211_IFTYPE_MESH_POINT: |
| 899 | /* bridging OK */ |
| 900 | break; |
| 901 | case NL80211_IFTYPE_MONITOR: |
| 902 | /* monitor can't bridge anyway */ |
| 903 | break; |
| 904 | case NL80211_IFTYPE_UNSPECIFIED: |
Johannes Berg | 2e161f7 | 2010-08-12 15:38:38 +0200 | [diff] [blame] | 905 | case NUM_NL80211_IFTYPES: |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 906 | /* not happening */ |
| 907 | break; |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 908 | case NL80211_IFTYPE_P2P_DEVICE: |
| 909 | WARN_ON(1); |
| 910 | break; |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
Michal Kazior | dbbae26 | 2012-06-29 12:47:01 +0200 | [diff] [blame] | 914 | if (!err && ntype != otype && netif_running(dev)) { |
| 915 | cfg80211_update_iface_num(rdev, ntype, 1); |
| 916 | cfg80211_update_iface_num(rdev, otype, -1); |
| 917 | } |
| 918 | |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 919 | return err; |
| 920 | } |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 921 | |
Vladimir Kondratiev | 95ddc1f | 2012-07-05 14:25:50 +0300 | [diff] [blame] | 922 | static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate) |
| 923 | { |
| 924 | static const u32 __mcs2bitrate[] = { |
| 925 | /* control PHY */ |
| 926 | [0] = 275, |
| 927 | /* SC PHY */ |
| 928 | [1] = 3850, |
| 929 | [2] = 7700, |
| 930 | [3] = 9625, |
| 931 | [4] = 11550, |
| 932 | [5] = 12512, /* 1251.25 mbps */ |
| 933 | [6] = 15400, |
| 934 | [7] = 19250, |
| 935 | [8] = 23100, |
| 936 | [9] = 25025, |
| 937 | [10] = 30800, |
| 938 | [11] = 38500, |
| 939 | [12] = 46200, |
| 940 | /* OFDM PHY */ |
| 941 | [13] = 6930, |
| 942 | [14] = 8662, /* 866.25 mbps */ |
| 943 | [15] = 13860, |
| 944 | [16] = 17325, |
| 945 | [17] = 20790, |
| 946 | [18] = 27720, |
| 947 | [19] = 34650, |
| 948 | [20] = 41580, |
| 949 | [21] = 45045, |
| 950 | [22] = 51975, |
| 951 | [23] = 62370, |
| 952 | [24] = 67568, /* 6756.75 mbps */ |
| 953 | /* LP-SC PHY */ |
| 954 | [25] = 6260, |
| 955 | [26] = 8340, |
| 956 | [27] = 11120, |
| 957 | [28] = 12510, |
| 958 | [29] = 16680, |
| 959 | [30] = 22240, |
| 960 | [31] = 25030, |
| 961 | }; |
| 962 | |
| 963 | if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate))) |
| 964 | return 0; |
| 965 | |
| 966 | return __mcs2bitrate[rate->mcs]; |
| 967 | } |
| 968 | |
Johannes Berg | db9c64c | 2012-11-09 14:56:41 +0100 | [diff] [blame] | 969 | static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate) |
| 970 | { |
| 971 | static const u32 base[4][10] = { |
| 972 | { 6500000, |
| 973 | 13000000, |
| 974 | 19500000, |
| 975 | 26000000, |
| 976 | 39000000, |
| 977 | 52000000, |
| 978 | 58500000, |
| 979 | 65000000, |
| 980 | 78000000, |
| 981 | 0, |
| 982 | }, |
| 983 | { 13500000, |
| 984 | 27000000, |
| 985 | 40500000, |
| 986 | 54000000, |
| 987 | 81000000, |
| 988 | 108000000, |
| 989 | 121500000, |
| 990 | 135000000, |
| 991 | 162000000, |
| 992 | 180000000, |
| 993 | }, |
| 994 | { 29300000, |
| 995 | 58500000, |
| 996 | 87800000, |
| 997 | 117000000, |
| 998 | 175500000, |
| 999 | 234000000, |
| 1000 | 263300000, |
| 1001 | 292500000, |
| 1002 | 351000000, |
| 1003 | 390000000, |
| 1004 | }, |
| 1005 | { 58500000, |
| 1006 | 117000000, |
| 1007 | 175500000, |
| 1008 | 234000000, |
| 1009 | 351000000, |
| 1010 | 468000000, |
| 1011 | 526500000, |
| 1012 | 585000000, |
| 1013 | 702000000, |
| 1014 | 780000000, |
| 1015 | }, |
| 1016 | }; |
| 1017 | u32 bitrate; |
| 1018 | int idx; |
| 1019 | |
| 1020 | if (WARN_ON_ONCE(rate->mcs > 9)) |
| 1021 | return 0; |
| 1022 | |
| 1023 | idx = rate->flags & (RATE_INFO_FLAGS_160_MHZ_WIDTH | |
| 1024 | RATE_INFO_FLAGS_80P80_MHZ_WIDTH) ? 3 : |
| 1025 | rate->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH ? 2 : |
| 1026 | rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH ? 1 : 0; |
| 1027 | |
| 1028 | bitrate = base[idx][rate->mcs]; |
| 1029 | bitrate *= rate->nss; |
| 1030 | |
| 1031 | if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) |
| 1032 | bitrate = (bitrate / 9) * 10; |
| 1033 | |
| 1034 | /* do NOT round down here */ |
| 1035 | return (bitrate + 50000) / 100000; |
| 1036 | } |
| 1037 | |
Vladimir Kondratiev | 8eb41c8 | 2012-07-05 14:25:49 +0300 | [diff] [blame] | 1038 | u32 cfg80211_calculate_bitrate(struct rate_info *rate) |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 1039 | { |
| 1040 | int modulation, streams, bitrate; |
| 1041 | |
Johannes Berg | db9c64c | 2012-11-09 14:56:41 +0100 | [diff] [blame] | 1042 | if (!(rate->flags & RATE_INFO_FLAGS_MCS) && |
| 1043 | !(rate->flags & RATE_INFO_FLAGS_VHT_MCS)) |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 1044 | return rate->legacy; |
Vladimir Kondratiev | 95ddc1f | 2012-07-05 14:25:50 +0300 | [diff] [blame] | 1045 | if (rate->flags & RATE_INFO_FLAGS_60G) |
| 1046 | return cfg80211_calculate_bitrate_60g(rate); |
Johannes Berg | db9c64c | 2012-11-09 14:56:41 +0100 | [diff] [blame] | 1047 | if (rate->flags & RATE_INFO_FLAGS_VHT_MCS) |
| 1048 | return cfg80211_calculate_bitrate_vht(rate); |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 1049 | |
| 1050 | /* the formula below does only work for MCS values smaller than 32 */ |
Johannes Berg | 2615f37 | 2012-05-08 21:36:20 +0200 | [diff] [blame] | 1051 | if (WARN_ON_ONCE(rate->mcs >= 32)) |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 1052 | return 0; |
| 1053 | |
| 1054 | modulation = rate->mcs & 7; |
| 1055 | streams = (rate->mcs >> 3) + 1; |
| 1056 | |
| 1057 | bitrate = (rate->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) ? |
| 1058 | 13500000 : 6500000; |
| 1059 | |
| 1060 | if (modulation < 4) |
| 1061 | bitrate *= (modulation + 1); |
| 1062 | else if (modulation == 4) |
| 1063 | bitrate *= (modulation + 2); |
| 1064 | else |
| 1065 | bitrate *= (modulation + 3); |
| 1066 | |
| 1067 | bitrate *= streams; |
| 1068 | |
| 1069 | if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) |
| 1070 | bitrate = (bitrate / 9) * 10; |
| 1071 | |
| 1072 | /* do NOT round down here */ |
| 1073 | return (bitrate + 50000) / 100000; |
| 1074 | } |
Thomas Pedersen | 8097e14 | 2012-03-05 15:31:47 -0800 | [diff] [blame] | 1075 | EXPORT_SYMBOL(cfg80211_calculate_bitrate); |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1076 | |
Arend van Spriel | c216e64 | 2012-11-25 19:13:28 +0100 | [diff] [blame] | 1077 | int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len, |
| 1078 | enum ieee80211_p2p_attr_id attr, |
| 1079 | u8 *buf, unsigned int bufsize) |
Johannes Berg | 0ee4535 | 2012-10-29 19:48:40 +0100 | [diff] [blame] | 1080 | { |
| 1081 | u8 *out = buf; |
| 1082 | u16 attr_remaining = 0; |
| 1083 | bool desired_attr = false; |
| 1084 | u16 desired_len = 0; |
| 1085 | |
| 1086 | while (len > 0) { |
| 1087 | unsigned int iedatalen; |
| 1088 | unsigned int copy; |
| 1089 | const u8 *iedata; |
| 1090 | |
| 1091 | if (len < 2) |
| 1092 | return -EILSEQ; |
| 1093 | iedatalen = ies[1]; |
| 1094 | if (iedatalen + 2 > len) |
| 1095 | return -EILSEQ; |
| 1096 | |
| 1097 | if (ies[0] != WLAN_EID_VENDOR_SPECIFIC) |
| 1098 | goto cont; |
| 1099 | |
| 1100 | if (iedatalen < 4) |
| 1101 | goto cont; |
| 1102 | |
| 1103 | iedata = ies + 2; |
| 1104 | |
| 1105 | /* check WFA OUI, P2P subtype */ |
| 1106 | if (iedata[0] != 0x50 || iedata[1] != 0x6f || |
| 1107 | iedata[2] != 0x9a || iedata[3] != 0x09) |
| 1108 | goto cont; |
| 1109 | |
| 1110 | iedatalen -= 4; |
| 1111 | iedata += 4; |
| 1112 | |
| 1113 | /* check attribute continuation into this IE */ |
| 1114 | copy = min_t(unsigned int, attr_remaining, iedatalen); |
| 1115 | if (copy && desired_attr) { |
| 1116 | desired_len += copy; |
| 1117 | if (out) { |
| 1118 | memcpy(out, iedata, min(bufsize, copy)); |
| 1119 | out += min(bufsize, copy); |
| 1120 | bufsize -= min(bufsize, copy); |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | if (copy == attr_remaining) |
| 1125 | return desired_len; |
| 1126 | } |
| 1127 | |
| 1128 | attr_remaining -= copy; |
| 1129 | if (attr_remaining) |
| 1130 | goto cont; |
| 1131 | |
| 1132 | iedatalen -= copy; |
| 1133 | iedata += copy; |
| 1134 | |
| 1135 | while (iedatalen > 0) { |
| 1136 | u16 attr_len; |
| 1137 | |
| 1138 | /* P2P attribute ID & size must fit */ |
| 1139 | if (iedatalen < 3) |
| 1140 | return -EILSEQ; |
| 1141 | desired_attr = iedata[0] == attr; |
| 1142 | attr_len = get_unaligned_le16(iedata + 1); |
| 1143 | iedatalen -= 3; |
| 1144 | iedata += 3; |
| 1145 | |
| 1146 | copy = min_t(unsigned int, attr_len, iedatalen); |
| 1147 | |
| 1148 | if (desired_attr) { |
| 1149 | desired_len += copy; |
| 1150 | if (out) { |
| 1151 | memcpy(out, iedata, min(bufsize, copy)); |
| 1152 | out += min(bufsize, copy); |
| 1153 | bufsize -= min(bufsize, copy); |
| 1154 | } |
| 1155 | |
| 1156 | if (copy == attr_len) |
| 1157 | return desired_len; |
| 1158 | } |
| 1159 | |
| 1160 | iedata += copy; |
| 1161 | iedatalen -= copy; |
| 1162 | attr_remaining = attr_len - copy; |
| 1163 | } |
| 1164 | |
| 1165 | cont: |
| 1166 | len -= ies[1] + 2; |
| 1167 | ies += ies[1] + 2; |
| 1168 | } |
| 1169 | |
| 1170 | if (attr_remaining && desired_attr) |
| 1171 | return -EILSEQ; |
| 1172 | |
| 1173 | return -ENOENT; |
| 1174 | } |
| 1175 | EXPORT_SYMBOL(cfg80211_get_p2p_attr); |
| 1176 | |
Johannes Berg | 1ce3e82 | 2012-08-01 17:00:55 +0200 | [diff] [blame] | 1177 | bool ieee80211_operating_class_to_band(u8 operating_class, |
| 1178 | enum ieee80211_band *band) |
| 1179 | { |
| 1180 | switch (operating_class) { |
| 1181 | case 112: |
| 1182 | case 115 ... 127: |
| 1183 | *band = IEEE80211_BAND_5GHZ; |
| 1184 | return true; |
| 1185 | case 81: |
| 1186 | case 82: |
| 1187 | case 83: |
| 1188 | case 84: |
| 1189 | *band = IEEE80211_BAND_2GHZ; |
| 1190 | return true; |
Vladimir Kondratiev | 55300a1 | 2013-04-23 09:54:21 +0300 | [diff] [blame] | 1191 | case 180: |
| 1192 | *band = IEEE80211_BAND_60GHZ; |
| 1193 | return true; |
Johannes Berg | 1ce3e82 | 2012-08-01 17:00:55 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | return false; |
| 1197 | } |
| 1198 | EXPORT_SYMBOL(ieee80211_operating_class_to_band); |
| 1199 | |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1200 | int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev, |
| 1201 | u32 beacon_int) |
| 1202 | { |
| 1203 | struct wireless_dev *wdev; |
| 1204 | int res = 0; |
| 1205 | |
| 1206 | if (!beacon_int) |
| 1207 | return -EINVAL; |
| 1208 | |
Johannes Berg | 89a54e4 | 2012-06-15 14:33:17 +0200 | [diff] [blame] | 1209 | list_for_each_entry(wdev, &rdev->wdev_list, list) { |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1210 | if (!wdev->beacon_interval) |
| 1211 | continue; |
| 1212 | if (wdev->beacon_interval != beacon_int) { |
| 1213 | res = -EINVAL; |
| 1214 | break; |
| 1215 | } |
| 1216 | } |
| 1217 | |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1218 | return res; |
| 1219 | } |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1220 | |
Michal Kazior | d4e50c5 | 2012-06-29 12:47:07 +0200 | [diff] [blame] | 1221 | int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev, |
| 1222 | struct wireless_dev *wdev, |
| 1223 | enum nl80211_iftype iftype, |
| 1224 | struct ieee80211_channel *chan, |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1225 | enum cfg80211_chan_mode chanmode, |
| 1226 | u8 radar_detect) |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1227 | { |
| 1228 | struct wireless_dev *wdev_iter; |
Johannes Berg | 463454b | 2012-06-05 12:16:50 +0200 | [diff] [blame] | 1229 | u32 used_iftypes = BIT(iftype); |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1230 | int num[NUM_NL80211_IFTYPES]; |
Michal Kazior | d4e50c5 | 2012-06-29 12:47:07 +0200 | [diff] [blame] | 1231 | struct ieee80211_channel |
| 1232 | *used_channels[CFG80211_MAX_NUM_DIFFERENT_CHANNELS]; |
| 1233 | struct ieee80211_channel *ch; |
| 1234 | enum cfg80211_chan_mode chmode; |
| 1235 | int num_different_channels = 0; |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1236 | int total = 1; |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1237 | bool radar_required; |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1238 | int i, j; |
| 1239 | |
| 1240 | ASSERT_RTNL(); |
| 1241 | |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1242 | if (WARN_ON(hweight32(radar_detect) > 1)) |
| 1243 | return -EINVAL; |
| 1244 | |
| 1245 | switch (iftype) { |
| 1246 | case NL80211_IFTYPE_ADHOC: |
| 1247 | case NL80211_IFTYPE_AP: |
| 1248 | case NL80211_IFTYPE_AP_VLAN: |
| 1249 | case NL80211_IFTYPE_MESH_POINT: |
| 1250 | case NL80211_IFTYPE_P2P_GO: |
| 1251 | case NL80211_IFTYPE_WDS: |
Simon Wunderlich | 683d41a | 2013-01-23 15:15:57 +0100 | [diff] [blame] | 1252 | radar_required = !!(chan && |
| 1253 | (chan->flags & IEEE80211_CHAN_RADAR)); |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1254 | break; |
| 1255 | case NL80211_IFTYPE_P2P_CLIENT: |
| 1256 | case NL80211_IFTYPE_STATION: |
Ilan Peer | bba87ff | 2013-02-03 08:28:27 +0200 | [diff] [blame] | 1257 | case NL80211_IFTYPE_P2P_DEVICE: |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1258 | case NL80211_IFTYPE_MONITOR: |
| 1259 | radar_required = false; |
| 1260 | break; |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1261 | case NUM_NL80211_IFTYPES: |
| 1262 | case NL80211_IFTYPE_UNSPECIFIED: |
| 1263 | default: |
| 1264 | return -EINVAL; |
| 1265 | } |
| 1266 | |
| 1267 | if (radar_required && !radar_detect) |
| 1268 | return -EINVAL; |
| 1269 | |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1270 | /* Always allow software iftypes */ |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1271 | if (rdev->wiphy.software_iftypes & BIT(iftype)) { |
| 1272 | if (radar_detect) |
| 1273 | return -EINVAL; |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1274 | return 0; |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1275 | } |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1276 | |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1277 | memset(num, 0, sizeof(num)); |
Michal Kazior | d4e50c5 | 2012-06-29 12:47:07 +0200 | [diff] [blame] | 1278 | memset(used_channels, 0, sizeof(used_channels)); |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1279 | |
| 1280 | num[iftype] = 1; |
| 1281 | |
Michal Kazior | d4e50c5 | 2012-06-29 12:47:07 +0200 | [diff] [blame] | 1282 | switch (chanmode) { |
| 1283 | case CHAN_MODE_UNDEFINED: |
| 1284 | break; |
| 1285 | case CHAN_MODE_SHARED: |
| 1286 | WARN_ON(!chan); |
| 1287 | used_channels[0] = chan; |
| 1288 | num_different_channels++; |
| 1289 | break; |
| 1290 | case CHAN_MODE_EXCLUSIVE: |
| 1291 | num_different_channels++; |
| 1292 | break; |
| 1293 | } |
| 1294 | |
Johannes Berg | 89a54e4 | 2012-06-15 14:33:17 +0200 | [diff] [blame] | 1295 | list_for_each_entry(wdev_iter, &rdev->wdev_list, list) { |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1296 | if (wdev_iter == wdev) |
| 1297 | continue; |
Johannes Berg | 6e3ab55 | 2013-04-19 12:19:39 +0200 | [diff] [blame] | 1298 | if (wdev_iter->iftype == NL80211_IFTYPE_P2P_DEVICE) { |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 1299 | if (!wdev_iter->p2p_started) |
| 1300 | continue; |
Johannes Berg | 6e3ab55 | 2013-04-19 12:19:39 +0200 | [diff] [blame] | 1301 | } else if (wdev_iter->netdev) { |
| 1302 | if (!netif_running(wdev_iter->netdev)) |
| 1303 | continue; |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 1304 | } else { |
| 1305 | WARN_ON(1); |
| 1306 | } |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1307 | |
| 1308 | if (rdev->wiphy.software_iftypes & BIT(wdev_iter->iftype)) |
| 1309 | continue; |
| 1310 | |
Johannes Berg | 8e95ea4 | 2012-07-10 19:39:02 +0200 | [diff] [blame] | 1311 | /* |
| 1312 | * We may be holding the "wdev" mutex, but now need to lock |
| 1313 | * wdev_iter. This is OK because once we get here wdev_iter |
| 1314 | * is not wdev (tested above), but we need to use the nested |
| 1315 | * locking for lockdep. |
| 1316 | */ |
| 1317 | mutex_lock_nested(&wdev_iter->mtx, 1); |
| 1318 | __acquire(wdev_iter->mtx); |
| 1319 | cfg80211_get_chan_state(wdev_iter, &ch, &chmode); |
| 1320 | wdev_unlock(wdev_iter); |
Michal Kazior | d4e50c5 | 2012-06-29 12:47:07 +0200 | [diff] [blame] | 1321 | |
| 1322 | switch (chmode) { |
| 1323 | case CHAN_MODE_UNDEFINED: |
| 1324 | break; |
| 1325 | case CHAN_MODE_SHARED: |
| 1326 | for (i = 0; i < CFG80211_MAX_NUM_DIFFERENT_CHANNELS; i++) |
| 1327 | if (!used_channels[i] || used_channels[i] == ch) |
| 1328 | break; |
| 1329 | |
Michal Kazior | e4e3245 | 2012-06-29 12:47:08 +0200 | [diff] [blame] | 1330 | if (i == CFG80211_MAX_NUM_DIFFERENT_CHANNELS) |
Michal Kazior | d4e50c5 | 2012-06-29 12:47:07 +0200 | [diff] [blame] | 1331 | return -EBUSY; |
Michal Kazior | d4e50c5 | 2012-06-29 12:47:07 +0200 | [diff] [blame] | 1332 | |
| 1333 | if (used_channels[i] == NULL) { |
| 1334 | used_channels[i] = ch; |
| 1335 | num_different_channels++; |
| 1336 | } |
| 1337 | break; |
| 1338 | case CHAN_MODE_EXCLUSIVE: |
| 1339 | num_different_channels++; |
| 1340 | break; |
| 1341 | } |
| 1342 | |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1343 | num[wdev_iter->iftype]++; |
| 1344 | total++; |
Johannes Berg | 463454b | 2012-06-05 12:16:50 +0200 | [diff] [blame] | 1345 | used_iftypes |= BIT(wdev_iter->iftype); |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1346 | } |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1347 | |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1348 | if (total == 1 && !radar_detect) |
Johannes Berg | 8e8b41f | 2012-03-15 10:16:16 +0100 | [diff] [blame] | 1349 | return 0; |
| 1350 | |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1351 | for (i = 0; i < rdev->wiphy.n_iface_combinations; i++) { |
| 1352 | const struct ieee80211_iface_combination *c; |
| 1353 | struct ieee80211_iface_limit *limits; |
Johannes Berg | 463454b | 2012-06-05 12:16:50 +0200 | [diff] [blame] | 1354 | u32 all_iftypes = 0; |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1355 | |
| 1356 | c = &rdev->wiphy.iface_combinations[i]; |
| 1357 | |
Michal Kazior | d4e50c5 | 2012-06-29 12:47:07 +0200 | [diff] [blame] | 1358 | if (total > c->max_interfaces) |
| 1359 | continue; |
| 1360 | if (num_different_channels > c->num_different_channels) |
| 1361 | continue; |
| 1362 | |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1363 | limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits, |
| 1364 | GFP_KERNEL); |
| 1365 | if (!limits) |
| 1366 | return -ENOMEM; |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1367 | |
| 1368 | for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { |
| 1369 | if (rdev->wiphy.software_iftypes & BIT(iftype)) |
| 1370 | continue; |
| 1371 | for (j = 0; j < c->n_limits; j++) { |
Johannes Berg | 463454b | 2012-06-05 12:16:50 +0200 | [diff] [blame] | 1372 | all_iftypes |= limits[j].types; |
Lukasz Kucharczyk | e55a404 | 2012-04-11 14:55:10 +0200 | [diff] [blame] | 1373 | if (!(limits[j].types & BIT(iftype))) |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1374 | continue; |
| 1375 | if (limits[j].max < num[iftype]) |
| 1376 | goto cont; |
| 1377 | limits[j].max -= num[iftype]; |
| 1378 | } |
| 1379 | } |
Johannes Berg | 463454b | 2012-06-05 12:16:50 +0200 | [diff] [blame] | 1380 | |
Simon Wunderlich | 11c4a07 | 2013-01-08 14:04:07 +0100 | [diff] [blame] | 1381 | if (radar_detect && !(c->radar_detect_widths & radar_detect)) |
| 1382 | goto cont; |
| 1383 | |
Johannes Berg | 463454b | 2012-06-05 12:16:50 +0200 | [diff] [blame] | 1384 | /* |
| 1385 | * Finally check that all iftypes that we're currently |
| 1386 | * using are actually part of this combination. If they |
| 1387 | * aren't then we can't use this combination and have |
| 1388 | * to continue to the next. |
| 1389 | */ |
| 1390 | if ((all_iftypes & used_iftypes) != used_iftypes) |
| 1391 | goto cont; |
| 1392 | |
| 1393 | /* |
| 1394 | * This combination covered all interface types and |
| 1395 | * supported the requested numbers, so we're good. |
| 1396 | */ |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1397 | kfree(limits); |
| 1398 | return 0; |
| 1399 | cont: |
| 1400 | kfree(limits); |
| 1401 | } |
| 1402 | |
| 1403 | return -EBUSY; |
| 1404 | } |
Johannes Berg | 34850ab | 2011-07-18 18:08:35 +0200 | [diff] [blame] | 1405 | |
| 1406 | int ieee80211_get_ratemask(struct ieee80211_supported_band *sband, |
| 1407 | const u8 *rates, unsigned int n_rates, |
| 1408 | u32 *mask) |
| 1409 | { |
| 1410 | int i, j; |
| 1411 | |
Johannes Berg | a401d2b | 2011-07-20 00:52:16 +0200 | [diff] [blame] | 1412 | if (!sband) |
| 1413 | return -EINVAL; |
| 1414 | |
Johannes Berg | 34850ab | 2011-07-18 18:08:35 +0200 | [diff] [blame] | 1415 | if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES) |
| 1416 | return -EINVAL; |
| 1417 | |
| 1418 | *mask = 0; |
| 1419 | |
| 1420 | for (i = 0; i < n_rates; i++) { |
| 1421 | int rate = (rates[i] & 0x7f) * 5; |
| 1422 | bool found = false; |
| 1423 | |
| 1424 | for (j = 0; j < sband->n_bitrates; j++) { |
| 1425 | if (sband->bitrates[j].bitrate == rate) { |
| 1426 | found = true; |
| 1427 | *mask |= BIT(j); |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
| 1431 | if (!found) |
| 1432 | return -EINVAL; |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * mask must have at least one bit set here since we |
| 1437 | * didn't accept a 0-length rates array nor allowed |
| 1438 | * entries in the array that didn't exist |
| 1439 | */ |
| 1440 | |
| 1441 | return 0; |
| 1442 | } |
Johannes Berg | 11a2a35 | 2011-11-21 11:09:22 +0100 | [diff] [blame] | 1443 | |
| 1444 | /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */ |
| 1445 | /* Ethernet-II snap header (RFC1042 for most EtherTypes) */ |
| 1446 | const unsigned char rfc1042_header[] __aligned(2) = |
| 1447 | { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; |
| 1448 | EXPORT_SYMBOL(rfc1042_header); |
| 1449 | |
| 1450 | /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */ |
| 1451 | const unsigned char bridge_tunnel_header[] __aligned(2) = |
| 1452 | { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 }; |
| 1453 | EXPORT_SYMBOL(bridge_tunnel_header); |