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