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