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