Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Wireless utility functions |
| 4 | * |
Johannes Berg | d323655 | 2009-04-20 14:31:42 +0200 | [diff] [blame] | 5 | * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net> |
Johannes Berg | 2740f0c | 2014-09-03 15:24:58 +0300 | [diff] [blame] | 6 | * Copyright 2013-2014 Intel Mobile Communications GmbH |
Luca Coelho | c4cbaf7 | 2018-06-09 09:14:42 +0300 | [diff] [blame] | 7 | * Copyright 2017 Intel Deutschland GmbH |
Johannes Berg | b0aa75f | 2018-08-31 11:31:16 +0300 | [diff] [blame^] | 8 | * Copyright (C) 2018 Intel Corporation |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 9 | */ |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 10 | #include <linux/export.h> |
Johannes Berg | d323655 | 2009-04-20 14:31:42 +0200 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 12 | #include <linux/etherdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Johannes Berg | b0aa75f | 2018-08-31 11:31:16 +0300 | [diff] [blame^] | 14 | #include <linux/ieee80211.h> |
Johannes Berg | d323655 | 2009-04-20 14:31:42 +0200 | [diff] [blame] | 15 | #include <net/cfg80211.h> |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 16 | #include <net/ip.h> |
Dave Täht | b156579 | 2011-12-22 12:55:08 -0800 | [diff] [blame] | 17 | #include <net/dsfield.h> |
cedric Voncken | c6ca5e2 | 2013-08-26 14:04:52 +0200 | [diff] [blame] | 18 | #include <linux/if_vlan.h> |
Simon Wunderlich | 960d97f | 2014-03-03 17:23:12 +0100 | [diff] [blame] | 19 | #include <linux/mpls.h> |
Johannes Berg | 4c8dea6 | 2016-10-21 14:25:13 +0200 | [diff] [blame] | 20 | #include <linux/gcd.h> |
Johannes Berg | b0aa75f | 2018-08-31 11:31:16 +0300 | [diff] [blame^] | 21 | #include <linux/bitfield.h> |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 22 | #include "core.h" |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 23 | #include "rdev-ops.h" |
| 24 | |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 25 | |
Johannes Berg | bd81525 | 2008-10-29 20:00:45 +0100 | [diff] [blame] | 26 | struct ieee80211_rate * |
| 27 | ieee80211_get_response_rate(struct ieee80211_supported_band *sband, |
Johannes Berg | 881d948 | 2009-01-21 15:13:48 +0100 | [diff] [blame] | 28 | u32 basic_rates, int bitrate) |
Johannes Berg | bd81525 | 2008-10-29 20:00:45 +0100 | [diff] [blame] | 29 | { |
| 30 | struct ieee80211_rate *result = &sband->bitrates[0]; |
| 31 | int i; |
| 32 | |
| 33 | for (i = 0; i < sband->n_bitrates; i++) { |
| 34 | if (!(basic_rates & BIT(i))) |
| 35 | continue; |
| 36 | if (sband->bitrates[i].bitrate > bitrate) |
| 37 | continue; |
| 38 | result = &sband->bitrates[i]; |
| 39 | } |
| 40 | |
| 41 | return result; |
| 42 | } |
| 43 | EXPORT_SYMBOL(ieee80211_get_response_rate); |
| 44 | |
Simon Wunderlich | 74608ac | 2013-07-08 16:55:54 +0200 | [diff] [blame] | 45 | u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband, |
| 46 | enum nl80211_bss_scan_width scan_width) |
Ashok Nagarajan | b422c6c | 2013-05-10 17:50:51 -0700 | [diff] [blame] | 47 | { |
| 48 | struct ieee80211_rate *bitrates; |
| 49 | u32 mandatory_rates = 0; |
| 50 | enum ieee80211_rate_flags mandatory_flag; |
| 51 | int i; |
| 52 | |
| 53 | if (WARN_ON(!sband)) |
| 54 | return 1; |
| 55 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 56 | if (sband->band == NL80211_BAND_2GHZ) { |
Simon Wunderlich | 74608ac | 2013-07-08 16:55:54 +0200 | [diff] [blame] | 57 | if (scan_width == NL80211_BSS_CHAN_WIDTH_5 || |
| 58 | scan_width == NL80211_BSS_CHAN_WIDTH_10) |
| 59 | mandatory_flag = IEEE80211_RATE_MANDATORY_G; |
| 60 | else |
| 61 | mandatory_flag = IEEE80211_RATE_MANDATORY_B; |
| 62 | } else { |
Ashok Nagarajan | b422c6c | 2013-05-10 17:50:51 -0700 | [diff] [blame] | 63 | mandatory_flag = IEEE80211_RATE_MANDATORY_A; |
Simon Wunderlich | 74608ac | 2013-07-08 16:55:54 +0200 | [diff] [blame] | 64 | } |
Ashok Nagarajan | b422c6c | 2013-05-10 17:50:51 -0700 | [diff] [blame] | 65 | |
| 66 | bitrates = sband->bitrates; |
| 67 | for (i = 0; i < sband->n_bitrates; i++) |
| 68 | if (bitrates[i].flags & mandatory_flag) |
| 69 | mandatory_rates |= BIT(i); |
| 70 | return mandatory_rates; |
| 71 | } |
| 72 | EXPORT_SYMBOL(ieee80211_mandatory_rates); |
| 73 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 74 | int ieee80211_channel_to_frequency(int chan, enum nl80211_band band) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 75 | { |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 76 | /* see 802.11 17.3.8.3.2 and Annex J |
| 77 | * there are overlapping channel numbers in 5GHz and 2GHz bands */ |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 78 | if (chan <= 0) |
| 79 | return 0; /* not supported */ |
| 80 | switch (band) { |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 81 | case NL80211_BAND_2GHZ: |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 82 | if (chan == 14) |
| 83 | return 2484; |
| 84 | else if (chan < 14) |
| 85 | return 2407 + chan * 5; |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 86 | break; |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 87 | case NL80211_BAND_5GHZ: |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 88 | if (chan >= 182 && chan <= 196) |
| 89 | return 4000 + chan * 5; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 90 | else |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 91 | return 5000 + chan * 5; |
| 92 | break; |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 93 | case NL80211_BAND_60GHZ: |
Alexei Avshalom Lazar | 9cf0a0b | 2018-08-13 15:33:00 +0300 | [diff] [blame] | 94 | if (chan < 7) |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 95 | return 56160 + chan * 2160; |
| 96 | break; |
| 97 | default: |
| 98 | ; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 99 | } |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 100 | return 0; /* not supported */ |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 101 | } |
| 102 | EXPORT_SYMBOL(ieee80211_channel_to_frequency); |
| 103 | |
| 104 | int ieee80211_frequency_to_channel(int freq) |
| 105 | { |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 106 | /* see 802.11 17.3.8.3.2 and Annex J */ |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 107 | if (freq == 2484) |
| 108 | return 14; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 109 | else if (freq < 2484) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 110 | return (freq - 2407) / 5; |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 111 | else if (freq >= 4910 && freq <= 4980) |
| 112 | return (freq - 4000) / 5; |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 113 | else if (freq <= 45000) /* DMG band lower limit */ |
Bruno Randolf | 59eb21a | 2011-01-17 13:37:28 +0900 | [diff] [blame] | 114 | return (freq - 5000) / 5; |
Alexei Avshalom Lazar | 9cf0a0b | 2018-08-13 15:33:00 +0300 | [diff] [blame] | 115 | else if (freq >= 58320 && freq <= 70200) |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 116 | return (freq - 56160) / 2160; |
| 117 | else |
| 118 | return 0; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 119 | } |
| 120 | EXPORT_SYMBOL(ieee80211_frequency_to_channel); |
| 121 | |
Arend Van Spriel | 543b921 | 2016-11-17 12:48:53 +0000 | [diff] [blame] | 122 | struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq) |
Johannes Berg | 906c730 | 2008-03-16 18:34:33 +0100 | [diff] [blame] | 123 | { |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 124 | enum nl80211_band band; |
Johannes Berg | 906c730 | 2008-03-16 18:34:33 +0100 | [diff] [blame] | 125 | struct ieee80211_supported_band *sband; |
| 126 | int i; |
| 127 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 128 | for (band = 0; band < NUM_NL80211_BANDS; band++) { |
Johannes Berg | 906c730 | 2008-03-16 18:34:33 +0100 | [diff] [blame] | 129 | sband = wiphy->bands[band]; |
| 130 | |
| 131 | if (!sband) |
| 132 | continue; |
| 133 | |
| 134 | for (i = 0; i < sband->n_channels; i++) { |
| 135 | if (sband->channels[i].center_freq == freq) |
| 136 | return &sband->channels[i]; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return NULL; |
| 141 | } |
Arend Van Spriel | 543b921 | 2016-11-17 12:48:53 +0000 | [diff] [blame] | 142 | EXPORT_SYMBOL(ieee80211_get_channel); |
Johannes Berg | 906c730 | 2008-03-16 18:34:33 +0100 | [diff] [blame] | 143 | |
Arend Van Spriel | 343884c | 2017-01-04 10:53:37 +0000 | [diff] [blame] | 144 | static void set_mandatory_flags_band(struct ieee80211_supported_band *sband) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 145 | { |
| 146 | int i, want; |
| 147 | |
Arend Van Spriel | 343884c | 2017-01-04 10:53:37 +0000 | [diff] [blame] | 148 | switch (sband->band) { |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 149 | case NL80211_BAND_5GHZ: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 150 | want = 3; |
| 151 | for (i = 0; i < sband->n_bitrates; i++) { |
| 152 | if (sband->bitrates[i].bitrate == 60 || |
| 153 | sband->bitrates[i].bitrate == 120 || |
| 154 | sband->bitrates[i].bitrate == 240) { |
| 155 | sband->bitrates[i].flags |= |
| 156 | IEEE80211_RATE_MANDATORY_A; |
| 157 | want--; |
| 158 | } |
| 159 | } |
| 160 | WARN_ON(want); |
| 161 | break; |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 162 | case NL80211_BAND_2GHZ: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 163 | want = 7; |
| 164 | for (i = 0; i < sband->n_bitrates; i++) { |
Richard Schütz | 1bd773c | 2017-09-07 17:47:43 +0200 | [diff] [blame] | 165 | switch (sband->bitrates[i].bitrate) { |
| 166 | case 10: |
| 167 | case 20: |
| 168 | case 55: |
| 169 | case 110: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 170 | sband->bitrates[i].flags |= |
| 171 | IEEE80211_RATE_MANDATORY_B | |
| 172 | IEEE80211_RATE_MANDATORY_G; |
| 173 | want--; |
Richard Schütz | 1bd773c | 2017-09-07 17:47:43 +0200 | [diff] [blame] | 174 | break; |
| 175 | case 60: |
| 176 | case 120: |
| 177 | case 240: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 178 | sband->bitrates[i].flags |= |
| 179 | IEEE80211_RATE_MANDATORY_G; |
| 180 | want--; |
Richard Schütz | 1bd773c | 2017-09-07 17:47:43 +0200 | [diff] [blame] | 181 | /* fall through */ |
| 182 | default: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 183 | sband->bitrates[i].flags |= |
| 184 | IEEE80211_RATE_ERP_G; |
Richard Schütz | 1bd773c | 2017-09-07 17:47:43 +0200 | [diff] [blame] | 185 | break; |
| 186 | } |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 187 | } |
Richard Schütz | 1bd773c | 2017-09-07 17:47:43 +0200 | [diff] [blame] | 188 | WARN_ON(want != 0 && want != 3); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 189 | break; |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 190 | case NL80211_BAND_60GHZ: |
Vladimir Kondratiev | 3a0c52a | 2012-07-02 09:32:32 +0300 | [diff] [blame] | 191 | /* check for mandatory HT MCS 1..4 */ |
| 192 | WARN_ON(!sband->ht_cap.ht_supported); |
| 193 | WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e); |
| 194 | break; |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 195 | case NUM_NL80211_BANDS: |
Arend Van Spriel | 343884c | 2017-01-04 10:53:37 +0000 | [diff] [blame] | 196 | default: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 197 | WARN_ON(1); |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void ieee80211_set_bitrate_flags(struct wiphy *wiphy) |
| 203 | { |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 204 | enum nl80211_band band; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 205 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 206 | for (band = 0; band < NUM_NL80211_BANDS; band++) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 207 | if (wiphy->bands[band]) |
Arend Van Spriel | 343884c | 2017-01-04 10:53:37 +0000 | [diff] [blame] | 208 | set_mandatory_flags_band(wiphy->bands[band]); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 209 | } |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 210 | |
Jouni Malinen | 38ba3c5 | 2011-09-21 18:14:56 +0300 | [diff] [blame] | 211 | bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher) |
| 212 | { |
| 213 | int i; |
| 214 | for (i = 0; i < wiphy->n_cipher_suites; i++) |
| 215 | if (cipher == wiphy->cipher_suites[i]) |
| 216 | return true; |
| 217 | return false; |
| 218 | } |
| 219 | |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 220 | int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev, |
| 221 | struct key_params *params, int key_idx, |
Johannes Berg | e31b821 | 2010-10-05 19:39:30 +0200 | [diff] [blame] | 222 | bool pairwise, const u8 *mac_addr) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 223 | { |
Johannes Berg | e9c8f8d | 2016-09-13 16:37:40 +0200 | [diff] [blame] | 224 | if (key_idx < 0 || key_idx > 5) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 225 | return -EINVAL; |
| 226 | |
Johannes Berg | e31b821 | 2010-10-05 19:39:30 +0200 | [diff] [blame] | 227 | if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)) |
| 228 | return -EINVAL; |
| 229 | |
| 230 | if (pairwise && !mac_addr) |
| 231 | return -EINVAL; |
| 232 | |
Jouni Malinen | 3772056 | 2015-01-24 19:52:04 +0200 | [diff] [blame] | 233 | switch (params->cipher) { |
| 234 | case WLAN_CIPHER_SUITE_TKIP: |
| 235 | case WLAN_CIPHER_SUITE_CCMP: |
Jouni Malinen | cfcf168 | 2015-01-24 19:52:05 +0200 | [diff] [blame] | 236 | case WLAN_CIPHER_SUITE_CCMP_256: |
| 237 | case WLAN_CIPHER_SUITE_GCMP: |
| 238 | case WLAN_CIPHER_SUITE_GCMP_256: |
Jouni Malinen | 3772056 | 2015-01-24 19:52:04 +0200 | [diff] [blame] | 239 | /* Disallow pairwise keys with non-zero index unless it's WEP |
| 240 | * or a vendor specific cipher (because current deployments use |
| 241 | * pairwise WEP keys with non-zero indices and for vendor |
| 242 | * specific ciphers this should be validated in the driver or |
| 243 | * hardware level - but 802.11i clearly specifies to use zero) |
| 244 | */ |
| 245 | if (pairwise && key_idx) |
| 246 | return -EINVAL; |
| 247 | break; |
| 248 | case WLAN_CIPHER_SUITE_AES_CMAC: |
Jouni Malinen | cfcf168 | 2015-01-24 19:52:05 +0200 | [diff] [blame] | 249 | case WLAN_CIPHER_SUITE_BIP_CMAC_256: |
| 250 | case WLAN_CIPHER_SUITE_BIP_GMAC_128: |
| 251 | case WLAN_CIPHER_SUITE_BIP_GMAC_256: |
Jouni Malinen | 3772056 | 2015-01-24 19:52:04 +0200 | [diff] [blame] | 252 | /* Disallow BIP (group-only) cipher as pairwise cipher */ |
| 253 | if (pairwise) |
| 254 | return -EINVAL; |
Johannes Berg | e9c8f8d | 2016-09-13 16:37:40 +0200 | [diff] [blame] | 255 | if (key_idx < 4) |
| 256 | return -EINVAL; |
Jouni Malinen | 3772056 | 2015-01-24 19:52:04 +0200 | [diff] [blame] | 257 | break; |
Johannes Berg | e9c8f8d | 2016-09-13 16:37:40 +0200 | [diff] [blame] | 258 | case WLAN_CIPHER_SUITE_WEP40: |
| 259 | case WLAN_CIPHER_SUITE_WEP104: |
| 260 | if (key_idx > 3) |
| 261 | return -EINVAL; |
Jouni Malinen | 3772056 | 2015-01-24 19:52:04 +0200 | [diff] [blame] | 262 | default: |
| 263 | break; |
| 264 | } |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 265 | |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 266 | switch (params->cipher) { |
| 267 | case WLAN_CIPHER_SUITE_WEP40: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 268 | if (params->key_len != WLAN_KEY_LEN_WEP40) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 269 | return -EINVAL; |
| 270 | break; |
| 271 | case WLAN_CIPHER_SUITE_TKIP: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 272 | if (params->key_len != WLAN_KEY_LEN_TKIP) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 273 | return -EINVAL; |
| 274 | break; |
| 275 | case WLAN_CIPHER_SUITE_CCMP: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 276 | if (params->key_len != WLAN_KEY_LEN_CCMP) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 277 | return -EINVAL; |
| 278 | break; |
Jouni Malinen | cfcf168 | 2015-01-24 19:52:05 +0200 | [diff] [blame] | 279 | case WLAN_CIPHER_SUITE_CCMP_256: |
| 280 | if (params->key_len != WLAN_KEY_LEN_CCMP_256) |
| 281 | return -EINVAL; |
| 282 | break; |
| 283 | case WLAN_CIPHER_SUITE_GCMP: |
| 284 | if (params->key_len != WLAN_KEY_LEN_GCMP) |
| 285 | return -EINVAL; |
| 286 | break; |
| 287 | case WLAN_CIPHER_SUITE_GCMP_256: |
| 288 | if (params->key_len != WLAN_KEY_LEN_GCMP_256) |
| 289 | return -EINVAL; |
| 290 | break; |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 291 | case WLAN_CIPHER_SUITE_WEP104: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 292 | if (params->key_len != WLAN_KEY_LEN_WEP104) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | break; |
| 295 | case WLAN_CIPHER_SUITE_AES_CMAC: |
Johannes Berg | 8fc0fee | 2009-05-24 16:57:19 +0200 | [diff] [blame] | 296 | if (params->key_len != WLAN_KEY_LEN_AES_CMAC) |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 297 | return -EINVAL; |
| 298 | break; |
Jouni Malinen | cfcf168 | 2015-01-24 19:52:05 +0200 | [diff] [blame] | 299 | case WLAN_CIPHER_SUITE_BIP_CMAC_256: |
| 300 | if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256) |
| 301 | return -EINVAL; |
| 302 | break; |
| 303 | case WLAN_CIPHER_SUITE_BIP_GMAC_128: |
| 304 | if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128) |
| 305 | return -EINVAL; |
| 306 | break; |
| 307 | case WLAN_CIPHER_SUITE_BIP_GMAC_256: |
| 308 | if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256) |
| 309 | return -EINVAL; |
| 310 | break; |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 311 | default: |
Johannes Berg | 7d64b7c | 2010-08-27 14:26:51 +0300 | [diff] [blame] | 312 | /* |
| 313 | * We don't know anything about this algorithm, |
| 314 | * allow using it -- but the driver must check |
| 315 | * all parameters! We still check below whether |
| 316 | * or not the driver supports this algorithm, |
| 317 | * of course. |
| 318 | */ |
| 319 | break; |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Jouni Malinen | 9f26a95 | 2009-05-15 12:38:32 +0300 | [diff] [blame] | 322 | if (params->seq) { |
| 323 | switch (params->cipher) { |
| 324 | case WLAN_CIPHER_SUITE_WEP40: |
| 325 | case WLAN_CIPHER_SUITE_WEP104: |
| 326 | /* These ciphers do not use key sequence */ |
| 327 | return -EINVAL; |
| 328 | case WLAN_CIPHER_SUITE_TKIP: |
| 329 | case WLAN_CIPHER_SUITE_CCMP: |
Jouni Malinen | cfcf168 | 2015-01-24 19:52:05 +0200 | [diff] [blame] | 330 | case WLAN_CIPHER_SUITE_CCMP_256: |
| 331 | case WLAN_CIPHER_SUITE_GCMP: |
| 332 | case WLAN_CIPHER_SUITE_GCMP_256: |
Jouni Malinen | 9f26a95 | 2009-05-15 12:38:32 +0300 | [diff] [blame] | 333 | case WLAN_CIPHER_SUITE_AES_CMAC: |
Jouni Malinen | cfcf168 | 2015-01-24 19:52:05 +0200 | [diff] [blame] | 334 | case WLAN_CIPHER_SUITE_BIP_CMAC_256: |
| 335 | case WLAN_CIPHER_SUITE_BIP_GMAC_128: |
| 336 | case WLAN_CIPHER_SUITE_BIP_GMAC_256: |
Jouni Malinen | 9f26a95 | 2009-05-15 12:38:32 +0300 | [diff] [blame] | 337 | if (params->seq_len != 6) |
| 338 | return -EINVAL; |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | |
Jouni Malinen | 38ba3c5 | 2011-09-21 18:14:56 +0300 | [diff] [blame] | 343 | if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher)) |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 344 | return -EINVAL; |
| 345 | |
Johannes Berg | 0864512 | 2009-05-11 13:54:58 +0200 | [diff] [blame] | 346 | return 0; |
| 347 | } |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 348 | |
Johannes Berg | 633adf1 | 2010-08-12 14:49:58 +0200 | [diff] [blame] | 349 | unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 350 | { |
| 351 | unsigned int hdrlen = 24; |
| 352 | |
| 353 | if (ieee80211_is_data(fc)) { |
| 354 | if (ieee80211_has_a4(fc)) |
| 355 | hdrlen = 30; |
Andriy Tkachuk | d0dd2de | 2010-01-20 13:55:06 +0200 | [diff] [blame] | 356 | if (ieee80211_is_data_qos(fc)) { |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 357 | hdrlen += IEEE80211_QOS_CTL_LEN; |
Andriy Tkachuk | d0dd2de | 2010-01-20 13:55:06 +0200 | [diff] [blame] | 358 | if (ieee80211_has_order(fc)) |
| 359 | hdrlen += IEEE80211_HT_CTL_LEN; |
| 360 | } |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 361 | goto out; |
| 362 | } |
| 363 | |
Fred Chou | fb142f4 | 2015-01-20 10:17:27 +0800 | [diff] [blame] | 364 | if (ieee80211_is_mgmt(fc)) { |
| 365 | if (ieee80211_has_order(fc)) |
| 366 | hdrlen += IEEE80211_HT_CTL_LEN; |
| 367 | goto out; |
| 368 | } |
| 369 | |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 370 | if (ieee80211_is_ctl(fc)) { |
| 371 | /* |
| 372 | * ACK and CTS are 10 bytes, all others 16. To see how |
| 373 | * to get this condition consider |
| 374 | * subtype mask: 0b0000000011110000 (0x00F0) |
| 375 | * ACK subtype: 0b0000000011010000 (0x00D0) |
| 376 | * CTS subtype: 0b0000000011000000 (0x00C0) |
| 377 | * bits that matter: ^^^ (0x00E0) |
| 378 | * value of those: 0b0000000011000000 (0x00C0) |
| 379 | */ |
| 380 | if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0)) |
| 381 | hdrlen = 10; |
| 382 | else |
| 383 | hdrlen = 16; |
| 384 | } |
| 385 | out: |
| 386 | return hdrlen; |
| 387 | } |
| 388 | EXPORT_SYMBOL(ieee80211_hdrlen); |
| 389 | |
| 390 | unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb) |
| 391 | { |
| 392 | const struct ieee80211_hdr *hdr = |
| 393 | (const struct ieee80211_hdr *)skb->data; |
| 394 | unsigned int hdrlen; |
| 395 | |
| 396 | if (unlikely(skb->len < 10)) |
| 397 | return 0; |
| 398 | hdrlen = ieee80211_hdrlen(hdr->frame_control); |
| 399 | if (unlikely(hdrlen > skb->len)) |
| 400 | return 0; |
| 401 | return hdrlen; |
| 402 | } |
| 403 | EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb); |
| 404 | |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 405 | static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 406 | { |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 407 | int ae = flags & MESH_FLAGS_AE; |
Johannes Berg | 7dd111e | 2012-10-25 21:51:59 +0200 | [diff] [blame] | 408 | /* 802.11-2012, 8.2.4.7.3 */ |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 409 | switch (ae) { |
Johannes Berg | 7dd111e | 2012-10-25 21:51:59 +0200 | [diff] [blame] | 410 | default: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 411 | case 0: |
| 412 | return 6; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 413 | case MESH_FLAGS_AE_A4: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 414 | return 12; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 415 | case MESH_FLAGS_AE_A5_A6: |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 416 | return 18; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 417 | } |
| 418 | } |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 419 | |
| 420 | unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr) |
| 421 | { |
| 422 | return __ieee80211_get_mesh_hdrlen(meshhdr->flags); |
| 423 | } |
Johannes Berg | 9b395bc | 2012-10-26 00:36:40 +0200 | [diff] [blame] | 424 | EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 425 | |
Johannes Berg | 7f6990c | 2016-10-05 15:29:49 +0200 | [diff] [blame] | 426 | int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr, |
Felix Fietkau | 24bba07 | 2018-02-27 13:03:07 +0100 | [diff] [blame] | 427 | const u8 *addr, enum nl80211_iftype iftype, |
| 428 | u8 data_offset) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 429 | { |
| 430 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 431 | struct { |
| 432 | u8 hdr[ETH_ALEN] __aligned(2); |
| 433 | __be16 proto; |
| 434 | } payload; |
| 435 | struct ethhdr tmp; |
| 436 | u16 hdrlen; |
| 437 | u8 mesh_flags = 0; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 438 | |
| 439 | if (unlikely(!ieee80211_is_data_present(hdr->frame_control))) |
| 440 | return -1; |
| 441 | |
Felix Fietkau | 24bba07 | 2018-02-27 13:03:07 +0100 | [diff] [blame] | 442 | hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset; |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 443 | if (skb->len < hdrlen + 8) |
| 444 | return -1; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 445 | |
| 446 | /* convert IEEE 802.11 header + possible LLC headers into Ethernet |
| 447 | * header |
| 448 | * IEEE 802.11 address fields: |
| 449 | * ToDS FromDS Addr1 Addr2 Addr3 Addr4 |
| 450 | * 0 0 DA SA BSSID n/a |
| 451 | * 0 1 DA BSSID SA n/a |
| 452 | * 1 0 BSSID SA DA n/a |
| 453 | * 1 1 RA TA DA SA |
| 454 | */ |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 455 | memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN); |
| 456 | memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN); |
| 457 | |
| 458 | if (iftype == NL80211_IFTYPE_MESH_POINT) |
| 459 | skb_copy_bits(skb, hdrlen, &mesh_flags, 1); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 460 | |
Rajkumar Manoharan | 5667c86 | 2017-05-14 21:41:55 -0700 | [diff] [blame] | 461 | mesh_flags &= MESH_FLAGS_AE; |
| 462 | |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 463 | switch (hdr->frame_control & |
| 464 | cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) { |
| 465 | case cpu_to_le16(IEEE80211_FCTL_TODS): |
| 466 | if (unlikely(iftype != NL80211_IFTYPE_AP && |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 467 | iftype != NL80211_IFTYPE_AP_VLAN && |
| 468 | iftype != NL80211_IFTYPE_P2P_GO)) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 469 | return -1; |
| 470 | break; |
| 471 | case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS): |
| 472 | if (unlikely(iftype != NL80211_IFTYPE_WDS && |
Felix Fietkau | f14543ee | 2009-11-10 20:10:05 +0100 | [diff] [blame] | 473 | iftype != NL80211_IFTYPE_MESH_POINT && |
| 474 | iftype != NL80211_IFTYPE_AP_VLAN && |
| 475 | iftype != NL80211_IFTYPE_STATION)) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 476 | return -1; |
| 477 | if (iftype == NL80211_IFTYPE_MESH_POINT) { |
Rajkumar Manoharan | 5667c86 | 2017-05-14 21:41:55 -0700 | [diff] [blame] | 478 | if (mesh_flags == MESH_FLAGS_AE_A4) |
Zhu Yi | e3cf8b3f | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 479 | return -1; |
Rajkumar Manoharan | 5667c86 | 2017-05-14 21:41:55 -0700 | [diff] [blame] | 480 | if (mesh_flags == MESH_FLAGS_AE_A5_A6) { |
Zhu Yi | e3cf8b3f | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 481 | skb_copy_bits(skb, hdrlen + |
| 482 | offsetof(struct ieee80211s_hdr, eaddr1), |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 483 | tmp.h_dest, 2 * ETH_ALEN); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 484 | } |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 485 | hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 486 | } |
| 487 | break; |
| 488 | case cpu_to_le16(IEEE80211_FCTL_FROMDS): |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 489 | if ((iftype != NL80211_IFTYPE_STATION && |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 490 | iftype != NL80211_IFTYPE_P2P_CLIENT && |
| 491 | iftype != NL80211_IFTYPE_MESH_POINT) || |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 492 | (is_multicast_ether_addr(tmp.h_dest) && |
| 493 | ether_addr_equal(tmp.h_source, addr))) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 494 | return -1; |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 495 | if (iftype == NL80211_IFTYPE_MESH_POINT) { |
Rajkumar Manoharan | 5667c86 | 2017-05-14 21:41:55 -0700 | [diff] [blame] | 496 | if (mesh_flags == MESH_FLAGS_AE_A5_A6) |
Zhu Yi | e3cf8b3f | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 497 | return -1; |
Rajkumar Manoharan | 5667c86 | 2017-05-14 21:41:55 -0700 | [diff] [blame] | 498 | if (mesh_flags == MESH_FLAGS_AE_A4) |
Zhu Yi | e3cf8b3f | 2010-03-29 17:35:07 +0800 | [diff] [blame] | 499 | skb_copy_bits(skb, hdrlen + |
| 500 | offsetof(struct ieee80211s_hdr, eaddr1), |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 501 | tmp.h_source, ETH_ALEN); |
| 502 | hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags); |
Javier Cardona | 3c5772a | 2009-08-10 12:15:48 -0700 | [diff] [blame] | 503 | } |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 504 | break; |
| 505 | case cpu_to_le16(0): |
Arik Nemtsov | 941c93c | 2011-09-28 14:12:54 +0300 | [diff] [blame] | 506 | if (iftype != NL80211_IFTYPE_ADHOC && |
Rostislav Lisovy | 6e0bd6c | 2014-11-03 10:33:18 +0100 | [diff] [blame] | 507 | iftype != NL80211_IFTYPE_STATION && |
| 508 | iftype != NL80211_IFTYPE_OCB) |
Arik Nemtsov | 941c93c | 2011-09-28 14:12:54 +0300 | [diff] [blame] | 509 | return -1; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 510 | break; |
| 511 | } |
| 512 | |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 513 | skb_copy_bits(skb, hdrlen, &payload, sizeof(payload)); |
| 514 | tmp.h_proto = payload.proto; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 515 | |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 516 | if (likely((ether_addr_equal(payload.hdr, rfc1042_header) && |
| 517 | tmp.h_proto != htons(ETH_P_AARP) && |
| 518 | tmp.h_proto != htons(ETH_P_IPX)) || |
| 519 | ether_addr_equal(payload.hdr, bridge_tunnel_header))) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 520 | /* remove RFC1042 or Bridge-Tunnel encapsulation and |
| 521 | * replace EtherType */ |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 522 | hdrlen += ETH_ALEN + 2; |
| 523 | else |
Felix Fietkau | c041778 | 2016-06-29 10:36:39 +0200 | [diff] [blame] | 524 | tmp.h_proto = htons(skb->len - hdrlen); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 525 | |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 526 | pskb_pull(skb, hdrlen); |
| 527 | |
| 528 | if (!ehdr) |
Johannes Berg | d58ff35 | 2017-06-16 14:29:23 +0200 | [diff] [blame] | 529 | ehdr = skb_push(skb, sizeof(struct ethhdr)); |
Felix Fietkau | 2d1c304 | 2016-02-02 14:39:09 +0100 | [diff] [blame] | 530 | memcpy(ehdr, &tmp, sizeof(tmp)); |
| 531 | |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 532 | return 0; |
| 533 | } |
Johannes Berg | 7f6990c | 2016-10-05 15:29:49 +0200 | [diff] [blame] | 534 | EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr); |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 535 | |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 536 | static void |
| 537 | __frame_add_frag(struct sk_buff *skb, struct page *page, |
| 538 | void *ptr, int len, int size) |
| 539 | { |
| 540 | struct skb_shared_info *sh = skb_shinfo(skb); |
| 541 | int page_offset; |
| 542 | |
Joonsoo Kim | 6d061f9 | 2016-05-19 17:10:46 -0700 | [diff] [blame] | 543 | page_ref_inc(page); |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 544 | page_offset = ptr - page_address(page); |
| 545 | skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size); |
| 546 | } |
| 547 | |
| 548 | static void |
| 549 | __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame, |
| 550 | int offset, int len) |
| 551 | { |
| 552 | struct skb_shared_info *sh = skb_shinfo(skb); |
Matthias Kaehlcke | aa1702d | 2017-04-13 10:05:04 -0700 | [diff] [blame] | 553 | const skb_frag_t *frag = &sh->frags[0]; |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 554 | struct page *frag_page; |
| 555 | void *frag_ptr; |
| 556 | int frag_len, frag_size; |
| 557 | int head_size = skb->len - skb->data_len; |
| 558 | int cur_len; |
| 559 | |
| 560 | frag_page = virt_to_head_page(skb->head); |
| 561 | frag_ptr = skb->data; |
| 562 | frag_size = head_size; |
| 563 | |
| 564 | while (offset >= frag_size) { |
| 565 | offset -= frag_size; |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 566 | frag_page = skb_frag_page(frag); |
| 567 | frag_ptr = skb_frag_address(frag); |
| 568 | frag_size = skb_frag_size(frag); |
Matthias Kaehlcke | aa1702d | 2017-04-13 10:05:04 -0700 | [diff] [blame] | 569 | frag++; |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | frag_ptr += offset; |
| 573 | frag_len = frag_size - offset; |
| 574 | |
| 575 | cur_len = min(len, frag_len); |
| 576 | |
| 577 | __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size); |
| 578 | len -= cur_len; |
| 579 | |
| 580 | while (len > 0) { |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 581 | frag_len = skb_frag_size(frag); |
| 582 | cur_len = min(len, frag_len); |
| 583 | __frame_add_frag(frame, skb_frag_page(frag), |
| 584 | skb_frag_address(frag), cur_len, frag_len); |
| 585 | len -= cur_len; |
Matthias Kaehlcke | aa1702d | 2017-04-13 10:05:04 -0700 | [diff] [blame] | 586 | frag++; |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 590 | static struct sk_buff * |
| 591 | __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen, |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 592 | int offset, int len, bool reuse_frag) |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 593 | { |
| 594 | struct sk_buff *frame; |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 595 | int cur_len = len; |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 596 | |
| 597 | if (skb->len - offset < len) |
| 598 | return NULL; |
| 599 | |
| 600 | /* |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 601 | * When reusing framents, copy some data to the head to simplify |
| 602 | * ethernet header handling and speed up protocol header processing |
| 603 | * in the stack later. |
| 604 | */ |
| 605 | if (reuse_frag) |
| 606 | cur_len = min_t(int, len, 32); |
| 607 | |
| 608 | /* |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 609 | * Allocate and reserve two bytes more for payload |
| 610 | * alignment since sizeof(struct ethhdr) is 14. |
| 611 | */ |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 612 | frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len); |
Gregory Greenman | 16a910a | 2016-07-05 15:23:10 +0300 | [diff] [blame] | 613 | if (!frame) |
| 614 | return NULL; |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 615 | |
| 616 | skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2); |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 617 | skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len); |
| 618 | |
| 619 | len -= cur_len; |
| 620 | if (!len) |
| 621 | return frame; |
| 622 | |
| 623 | offset += cur_len; |
| 624 | __ieee80211_amsdu_copy_frag(skb, frame, offset, len); |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 625 | |
| 626 | return frame; |
| 627 | } |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 628 | |
| 629 | void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list, |
| 630 | const u8 *addr, enum nl80211_iftype iftype, |
Yogesh Ashok Powar | 8b3beca | 2011-05-13 11:22:31 -0700 | [diff] [blame] | 631 | const unsigned int extra_headroom, |
Johannes Berg | 8b935ee | 2016-10-05 16:17:01 +0200 | [diff] [blame] | 632 | const u8 *check_da, const u8 *check_sa) |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 633 | { |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 634 | unsigned int hlen = ALIGN(extra_headroom, 4); |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 635 | struct sk_buff *frame = NULL; |
| 636 | u16 ethertype; |
| 637 | u8 *payload; |
Johannes Berg | 7f6990c | 2016-10-05 15:29:49 +0200 | [diff] [blame] | 638 | int offset = 0, remaining; |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 639 | struct ethhdr eth; |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 640 | bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb); |
Felix Fietkau | 2bf0ccc | 2016-02-08 14:25:26 +0100 | [diff] [blame] | 641 | bool reuse_skb = false; |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 642 | bool last = false; |
Felix Fietkau | 88665f5 | 2016-02-02 14:39:08 +0100 | [diff] [blame] | 643 | |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 644 | while (!last) { |
| 645 | unsigned int subframe_len; |
| 646 | int len; |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 647 | u8 padding; |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 648 | |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 649 | skb_copy_bits(skb, offset, ð, sizeof(eth)); |
| 650 | len = ntohs(eth.h_proto); |
| 651 | subframe_len = sizeof(struct ethhdr) + len; |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 652 | padding = (4 - subframe_len) & 0x3; |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 653 | |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 654 | /* the last MSDU has no padding */ |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 655 | remaining = skb->len - offset; |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 656 | if (subframe_len > remaining) |
| 657 | goto purge; |
| 658 | |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 659 | offset += sizeof(struct ethhdr); |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 660 | last = remaining <= subframe_len + padding; |
Johannes Berg | 8b935ee | 2016-10-05 16:17:01 +0200 | [diff] [blame] | 661 | |
| 662 | /* FIXME: should we really accept multicast DA? */ |
| 663 | if ((check_da && !is_multicast_ether_addr(eth.h_dest) && |
| 664 | !ether_addr_equal(check_da, eth.h_dest)) || |
| 665 | (check_sa && !ether_addr_equal(check_sa, eth.h_source))) { |
| 666 | offset += len + padding; |
| 667 | continue; |
| 668 | } |
| 669 | |
| 670 | /* reuse skb for the last subframe */ |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 671 | if (!skb_is_nonlinear(skb) && !reuse_frag && last) { |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 672 | skb_pull(skb, offset); |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 673 | frame = skb; |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 674 | reuse_skb = true; |
| 675 | } else { |
Felix Fietkau | 2b67f94 | 2016-02-08 14:34:42 +0100 | [diff] [blame] | 676 | frame = __ieee80211_amsdu_copy(skb, hlen, offset, len, |
| 677 | reuse_frag); |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 678 | if (!frame) |
| 679 | goto purge; |
| 680 | |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 681 | offset += len + padding; |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | skb_reset_network_header(frame); |
| 685 | frame->dev = skb->dev; |
| 686 | frame->priority = skb->priority; |
| 687 | |
| 688 | payload = frame->data; |
| 689 | ethertype = (payload[6] << 8) | payload[7]; |
Joe Perches | ac422d3 | 2012-05-08 18:56:55 +0000 | [diff] [blame] | 690 | if (likely((ether_addr_equal(payload, rfc1042_header) && |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 691 | ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) || |
Joe Perches | ac422d3 | 2012-05-08 18:56:55 +0000 | [diff] [blame] | 692 | ether_addr_equal(payload, bridge_tunnel_header))) { |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 693 | eth.h_proto = htons(ethertype); |
| 694 | skb_pull(frame, ETH_ALEN + 2); |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 695 | } |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 696 | |
| 697 | memcpy(skb_push(frame, sizeof(eth)), ð, sizeof(eth)); |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 698 | __skb_queue_tail(list, frame); |
| 699 | } |
| 700 | |
Felix Fietkau | 230fd28 | 2016-02-02 14:39:10 +0100 | [diff] [blame] | 701 | if (!reuse_skb) |
| 702 | dev_kfree_skb(skb); |
| 703 | |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 704 | return; |
| 705 | |
| 706 | purge: |
| 707 | __skb_queue_purge(list); |
Zhu Yi | eaf85ca | 2009-12-01 10:18:37 +0800 | [diff] [blame] | 708 | dev_kfree_skb(skb); |
| 709 | } |
| 710 | EXPORT_SYMBOL(ieee80211_amsdu_to_8023s); |
| 711 | |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 712 | /* Given a data frame determine the 802.1p/1d tag to use. */ |
Kyeyoon Park | fa9ffc7 | 2013-12-16 23:01:30 -0800 | [diff] [blame] | 713 | unsigned int cfg80211_classify8021d(struct sk_buff *skb, |
| 714 | struct cfg80211_qos_map *qos_map) |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 715 | { |
| 716 | unsigned int dscp; |
cedric Voncken | c6ca5e2 | 2013-08-26 14:04:52 +0200 | [diff] [blame] | 717 | unsigned char vlan_priority; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 718 | |
| 719 | /* skb->priority values from 256->263 are magic values to |
| 720 | * directly indicate a specific 802.1d priority. This is used |
| 721 | * to allow 802.1d priority to be passed directly in from VLAN |
| 722 | * tags, etc. |
| 723 | */ |
| 724 | if (skb->priority >= 256 && skb->priority <= 263) |
| 725 | return skb->priority - 256; |
| 726 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 727 | if (skb_vlan_tag_present(skb)) { |
| 728 | vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK) |
cedric Voncken | c6ca5e2 | 2013-08-26 14:04:52 +0200 | [diff] [blame] | 729 | >> VLAN_PRIO_SHIFT; |
| 730 | if (vlan_priority > 0) |
| 731 | return vlan_priority; |
| 732 | } |
| 733 | |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 734 | switch (skb->protocol) { |
| 735 | case htons(ETH_P_IP): |
Dave Täht | b156579 | 2011-12-22 12:55:08 -0800 | [diff] [blame] | 736 | dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc; |
| 737 | break; |
| 738 | case htons(ETH_P_IPV6): |
| 739 | dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 740 | break; |
Simon Wunderlich | 960d97f | 2014-03-03 17:23:12 +0100 | [diff] [blame] | 741 | case htons(ETH_P_MPLS_UC): |
| 742 | case htons(ETH_P_MPLS_MC): { |
| 743 | struct mpls_label mpls_tmp, *mpls; |
| 744 | |
| 745 | mpls = skb_header_pointer(skb, sizeof(struct ethhdr), |
| 746 | sizeof(*mpls), &mpls_tmp); |
| 747 | if (!mpls) |
| 748 | return 0; |
| 749 | |
| 750 | return (ntohl(mpls->entry) & MPLS_LS_TC_MASK) |
| 751 | >> MPLS_LS_TC_SHIFT; |
| 752 | } |
| 753 | case htons(ETH_P_80221): |
| 754 | /* 802.21 is always network control traffic */ |
| 755 | return 7; |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 756 | default: |
| 757 | return 0; |
| 758 | } |
| 759 | |
Kyeyoon Park | fa9ffc7 | 2013-12-16 23:01:30 -0800 | [diff] [blame] | 760 | if (qos_map) { |
| 761 | unsigned int i, tmp_dscp = dscp >> 2; |
| 762 | |
| 763 | for (i = 0; i < qos_map->num_des; i++) { |
| 764 | if (tmp_dscp == qos_map->dscp_exception[i].dscp) |
| 765 | return qos_map->dscp_exception[i].up; |
| 766 | } |
| 767 | |
| 768 | for (i = 0; i < 8; i++) { |
| 769 | if (tmp_dscp >= qos_map->up[i].low && |
| 770 | tmp_dscp <= qos_map->up[i].high) |
| 771 | return i; |
| 772 | } |
| 773 | } |
| 774 | |
Zhu Yi | e31a16d | 2009-05-21 21:47:03 +0800 | [diff] [blame] | 775 | return dscp >> 5; |
| 776 | } |
| 777 | EXPORT_SYMBOL(cfg80211_classify8021d); |
Johannes Berg | 517357c | 2009-07-02 17:18:40 +0200 | [diff] [blame] | 778 | |
| 779 | const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie) |
| 780 | { |
Johannes Berg | 9caf036 | 2012-11-29 01:25:20 +0100 | [diff] [blame] | 781 | const struct cfg80211_bss_ies *ies; |
| 782 | |
| 783 | ies = rcu_dereference(bss->ies); |
| 784 | if (!ies) |
Johannes Berg | 517357c | 2009-07-02 17:18:40 +0200 | [diff] [blame] | 785 | return NULL; |
Johannes Berg | 9caf036 | 2012-11-29 01:25:20 +0100 | [diff] [blame] | 786 | |
| 787 | return cfg80211_find_ie(ie, ies->data, ies->len); |
Johannes Berg | 517357c | 2009-07-02 17:18:40 +0200 | [diff] [blame] | 788 | } |
| 789 | EXPORT_SYMBOL(ieee80211_bss_get_ie); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 790 | |
| 791 | void cfg80211_upload_connect_keys(struct wireless_dev *wdev) |
| 792 | { |
Zhao, Gang | f26cbf4 | 2014-04-21 12:53:03 +0800 | [diff] [blame] | 793 | struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 794 | struct net_device *dev = wdev->netdev; |
| 795 | int i; |
| 796 | |
| 797 | if (!wdev->connect_keys) |
| 798 | return; |
| 799 | |
David Spinadel | b867622 | 2016-09-22 23:16:50 +0300 | [diff] [blame] | 800 | for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) { |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 801 | if (!wdev->connect_keys->params[i].cipher) |
| 802 | continue; |
Hila Gonen | e35e4d2 | 2012-06-27 17:19:42 +0300 | [diff] [blame] | 803 | if (rdev_add_key(rdev, dev, i, false, NULL, |
| 804 | &wdev->connect_keys->params[i])) { |
Joe Perches | e9c0268 | 2010-11-16 19:56:49 -0800 | [diff] [blame] | 805 | netdev_err(dev, "failed to set key %d\n", i); |
Zhu Yi | 1e05666 | 2009-07-20 16:12:57 +0800 | [diff] [blame] | 806 | continue; |
| 807 | } |
Johannes Berg | d4f2997 | 2017-02-13 20:53:38 +0100 | [diff] [blame] | 808 | if (wdev->connect_keys->def == i && |
| 809 | rdev_set_default_key(rdev, dev, i, true, true)) { |
| 810 | netdev_err(dev, "failed to set defkey %d\n", i); |
| 811 | continue; |
| 812 | } |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 813 | } |
| 814 | |
Johannes Berg | b47f610 | 2014-09-10 13:39:54 +0300 | [diff] [blame] | 815 | kzfree(wdev->connect_keys); |
Johannes Berg | fffd093 | 2009-07-08 14:22:54 +0200 | [diff] [blame] | 816 | wdev->connect_keys = NULL; |
| 817 | } |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 818 | |
Daniel Drake | 1f6fc43 | 2012-08-02 18:41:48 +0100 | [diff] [blame] | 819 | void cfg80211_process_wdev_events(struct wireless_dev *wdev) |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 820 | { |
| 821 | struct cfg80211_event *ev; |
| 822 | unsigned long flags; |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 823 | |
| 824 | spin_lock_irqsave(&wdev->event_lock, flags); |
| 825 | while (!list_empty(&wdev->event_list)) { |
| 826 | ev = list_first_entry(&wdev->event_list, |
| 827 | struct cfg80211_event, list); |
| 828 | list_del(&ev->list); |
| 829 | spin_unlock_irqrestore(&wdev->event_lock, flags); |
| 830 | |
| 831 | wdev_lock(wdev); |
| 832 | switch (ev->type) { |
| 833 | case EVENT_CONNECT_RESULT: |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 834 | __cfg80211_connect_result( |
Vidyullatha Kanchanapally | 5349a0f | 2017-03-31 00:22:33 +0300 | [diff] [blame] | 835 | wdev->netdev, |
| 836 | &ev->cr, |
| 837 | ev->cr.status == WLAN_STATUS_SUCCESS); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 838 | break; |
| 839 | case EVENT_ROAMED: |
Avraham Stern | 29ce6ec | 2017-04-26 10:58:49 +0300 | [diff] [blame] | 840 | __cfg80211_roamed(wdev, &ev->rm); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 841 | break; |
| 842 | case EVENT_DISCONNECTED: |
| 843 | __cfg80211_disconnected(wdev->netdev, |
| 844 | ev->dc.ie, ev->dc.ie_len, |
Johannes Berg | 80279fb | 2015-05-22 16:22:20 +0200 | [diff] [blame] | 845 | ev->dc.reason, |
| 846 | !ev->dc.locally_generated); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 847 | break; |
| 848 | case EVENT_IBSS_JOINED: |
Antonio Quartulli | fe94f3a | 2014-01-29 17:53:43 +0100 | [diff] [blame] | 849 | __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid, |
| 850 | ev->ij.channel); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 851 | break; |
Michal Kazior | f04c220 | 2014-04-09 15:11:01 +0200 | [diff] [blame] | 852 | case EVENT_STOPPED: |
| 853 | __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev); |
| 854 | break; |
Avraham Stern | 503c1fb | 2017-09-29 14:21:49 +0200 | [diff] [blame] | 855 | case EVENT_PORT_AUTHORIZED: |
| 856 | __cfg80211_port_authorized(wdev, ev->pa.bssid); |
| 857 | break; |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 858 | } |
| 859 | wdev_unlock(wdev); |
| 860 | |
| 861 | kfree(ev); |
| 862 | |
| 863 | spin_lock_irqsave(&wdev->event_lock, flags); |
| 864 | } |
| 865 | spin_unlock_irqrestore(&wdev->event_lock, flags); |
| 866 | } |
| 867 | |
| 868 | void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev) |
| 869 | { |
| 870 | struct wireless_dev *wdev; |
| 871 | |
| 872 | ASSERT_RTNL(); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 873 | |
Johannes Berg | 53873f1 | 2016-05-03 16:52:04 +0300 | [diff] [blame] | 874 | list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 875 | cfg80211_process_wdev_events(wdev); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | int cfg80211_change_iface(struct cfg80211_registered_device *rdev, |
| 879 | struct net_device *dev, enum nl80211_iftype ntype, |
Johannes Berg | 818a986 | 2017-04-12 11:23:28 +0200 | [diff] [blame] | 880 | struct vif_params *params) |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 881 | { |
| 882 | int err; |
| 883 | enum nl80211_iftype otype = dev->ieee80211_ptr->iftype; |
| 884 | |
Zhao, Gang | 73fb08e | 2014-03-19 17:04:36 +0800 | [diff] [blame] | 885 | ASSERT_RTNL(); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 886 | |
| 887 | /* don't support changing VLANs, you just re-create them */ |
| 888 | if (otype == NL80211_IFTYPE_AP_VLAN) |
| 889 | return -EOPNOTSUPP; |
| 890 | |
Ayala Beker | cb3b7d8 | 2016-09-20 17:31:13 +0300 | [diff] [blame] | 891 | /* cannot change into P2P device or NAN */ |
| 892 | if (ntype == NL80211_IFTYPE_P2P_DEVICE || |
| 893 | ntype == NL80211_IFTYPE_NAN) |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 894 | return -EOPNOTSUPP; |
| 895 | |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 896 | if (!rdev->ops->change_virtual_intf || |
| 897 | !(rdev->wiphy.interface_modes & (1 << ntype))) |
| 898 | return -EOPNOTSUPP; |
| 899 | |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 900 | /* 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] | 901 | if ((dev->priv_flags & IFF_BRIDGE_PORT) && |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 902 | (ntype == NL80211_IFTYPE_ADHOC || |
| 903 | ntype == NL80211_IFTYPE_STATION || |
| 904 | ntype == NL80211_IFTYPE_P2P_CLIENT)) |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 905 | return -EBUSY; |
| 906 | |
Michal Kazior | 6cbfb1b | 2015-05-22 10:57:22 +0200 | [diff] [blame] | 907 | if (ntype != otype) { |
Johannes Berg | 9bc383d | 2009-11-19 11:55:19 +0100 | [diff] [blame] | 908 | dev->ieee80211_ptr->use_4addr = false; |
Johannes Berg | 29cbe68 | 2010-12-03 09:20:44 +0100 | [diff] [blame] | 909 | dev->ieee80211_ptr->mesh_id_up_len = 0; |
Johannes Berg | 194ff52 | 2013-12-30 23:12:37 +0100 | [diff] [blame] | 910 | wdev_lock(dev->ieee80211_ptr); |
Kyeyoon Park | fa9ffc7 | 2013-12-16 23:01:30 -0800 | [diff] [blame] | 911 | rdev_set_qos_map(rdev, dev, NULL); |
Johannes Berg | 194ff52 | 2013-12-30 23:12:37 +0100 | [diff] [blame] | 912 | wdev_unlock(dev->ieee80211_ptr); |
Johannes Berg | 9bc383d | 2009-11-19 11:55:19 +0100 | [diff] [blame] | 913 | |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 914 | switch (otype) { |
Michal Kazior | ac80014 | 2012-06-29 12:46:57 +0200 | [diff] [blame] | 915 | case NL80211_IFTYPE_AP: |
Ilan Peer | 7c8d5e0 | 2014-02-25 15:33:38 +0200 | [diff] [blame] | 916 | cfg80211_stop_ap(rdev, dev, true); |
Michal Kazior | ac80014 | 2012-06-29 12:46:57 +0200 | [diff] [blame] | 917 | break; |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 918 | case NL80211_IFTYPE_ADHOC: |
| 919 | cfg80211_leave_ibss(rdev, dev, false); |
| 920 | break; |
| 921 | case NL80211_IFTYPE_STATION: |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 922 | case NL80211_IFTYPE_P2P_CLIENT: |
Johannes Berg | 83739b0 | 2013-05-15 17:44:01 +0200 | [diff] [blame] | 923 | wdev_lock(dev->ieee80211_ptr); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 924 | cfg80211_disconnect(rdev, dev, |
| 925 | WLAN_REASON_DEAUTH_LEAVING, true); |
Johannes Berg | 83739b0 | 2013-05-15 17:44:01 +0200 | [diff] [blame] | 926 | wdev_unlock(dev->ieee80211_ptr); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 927 | break; |
| 928 | case NL80211_IFTYPE_MESH_POINT: |
| 929 | /* mesh should be handled? */ |
| 930 | break; |
| 931 | default: |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | cfg80211_process_rdev_events(rdev); |
| 936 | } |
| 937 | |
Johannes Berg | 818a986 | 2017-04-12 11:23:28 +0200 | [diff] [blame] | 938 | err = rdev_change_virtual_intf(rdev, dev, ntype, params); |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 939 | |
| 940 | WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype); |
| 941 | |
Johannes Berg | 9bc383d | 2009-11-19 11:55:19 +0100 | [diff] [blame] | 942 | if (!err && params && params->use_4addr != -1) |
| 943 | dev->ieee80211_ptr->use_4addr = params->use_4addr; |
| 944 | |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 945 | if (!err) { |
| 946 | dev->priv_flags &= ~IFF_DONT_BRIDGE; |
| 947 | switch (ntype) { |
| 948 | case NL80211_IFTYPE_STATION: |
| 949 | if (dev->ieee80211_ptr->use_4addr) |
| 950 | break; |
| 951 | /* fall through */ |
Rostislav Lisovy | 6e0bd6c | 2014-11-03 10:33:18 +0100 | [diff] [blame] | 952 | case NL80211_IFTYPE_OCB: |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 953 | case NL80211_IFTYPE_P2P_CLIENT: |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 954 | case NL80211_IFTYPE_ADHOC: |
| 955 | dev->priv_flags |= IFF_DONT_BRIDGE; |
| 956 | break; |
Johannes Berg | 074ac8d | 2010-09-16 14:58:22 +0200 | [diff] [blame] | 957 | case NL80211_IFTYPE_P2P_GO: |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 958 | case NL80211_IFTYPE_AP: |
| 959 | case NL80211_IFTYPE_AP_VLAN: |
| 960 | case NL80211_IFTYPE_WDS: |
| 961 | case NL80211_IFTYPE_MESH_POINT: |
| 962 | /* bridging OK */ |
| 963 | break; |
| 964 | case NL80211_IFTYPE_MONITOR: |
| 965 | /* monitor can't bridge anyway */ |
| 966 | break; |
| 967 | case NL80211_IFTYPE_UNSPECIFIED: |
Johannes Berg | 2e161f78 | 2010-08-12 15:38:38 +0200 | [diff] [blame] | 968 | case NUM_NL80211_IFTYPES: |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 969 | /* not happening */ |
| 970 | break; |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 971 | case NL80211_IFTYPE_P2P_DEVICE: |
Ayala Beker | cb3b7d8 | 2016-09-20 17:31:13 +0300 | [diff] [blame] | 972 | case NL80211_IFTYPE_NAN: |
Johannes Berg | 98104fde | 2012-06-16 00:19:54 +0200 | [diff] [blame] | 973 | WARN_ON(1); |
| 974 | break; |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | |
Michal Kazior | dbbae26 | 2012-06-29 12:47:01 +0200 | [diff] [blame] | 978 | if (!err && ntype != otype && netif_running(dev)) { |
| 979 | cfg80211_update_iface_num(rdev, ntype, 1); |
| 980 | cfg80211_update_iface_num(rdev, otype, -1); |
| 981 | } |
| 982 | |
Johannes Berg | 3d54d25 | 2009-08-21 14:51:05 +0200 | [diff] [blame] | 983 | return err; |
| 984 | } |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 985 | |
Johannes Berg | 0c1eca4 | 2017-02-15 15:02:08 +0100 | [diff] [blame] | 986 | static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate) |
| 987 | { |
| 988 | int modulation, streams, bitrate; |
| 989 | |
| 990 | /* the formula below does only work for MCS values smaller than 32 */ |
| 991 | if (WARN_ON_ONCE(rate->mcs >= 32)) |
| 992 | return 0; |
| 993 | |
| 994 | modulation = rate->mcs & 7; |
| 995 | streams = (rate->mcs >> 3) + 1; |
| 996 | |
| 997 | bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000; |
| 998 | |
| 999 | if (modulation < 4) |
| 1000 | bitrate *= (modulation + 1); |
| 1001 | else if (modulation == 4) |
| 1002 | bitrate *= (modulation + 2); |
| 1003 | else |
| 1004 | bitrate *= (modulation + 3); |
| 1005 | |
| 1006 | bitrate *= streams; |
| 1007 | |
| 1008 | if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) |
| 1009 | bitrate = (bitrate / 9) * 10; |
| 1010 | |
| 1011 | /* do NOT round down here */ |
| 1012 | return (bitrate + 50000) / 100000; |
| 1013 | } |
| 1014 | |
Vladimir Kondratiev | 95ddc1f | 2012-07-05 14:25:50 +0300 | [diff] [blame] | 1015 | static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate) |
| 1016 | { |
| 1017 | static const u32 __mcs2bitrate[] = { |
| 1018 | /* control PHY */ |
| 1019 | [0] = 275, |
| 1020 | /* SC PHY */ |
| 1021 | [1] = 3850, |
| 1022 | [2] = 7700, |
| 1023 | [3] = 9625, |
| 1024 | [4] = 11550, |
| 1025 | [5] = 12512, /* 1251.25 mbps */ |
| 1026 | [6] = 15400, |
| 1027 | [7] = 19250, |
| 1028 | [8] = 23100, |
| 1029 | [9] = 25025, |
| 1030 | [10] = 30800, |
| 1031 | [11] = 38500, |
| 1032 | [12] = 46200, |
| 1033 | /* OFDM PHY */ |
| 1034 | [13] = 6930, |
| 1035 | [14] = 8662, /* 866.25 mbps */ |
| 1036 | [15] = 13860, |
| 1037 | [16] = 17325, |
| 1038 | [17] = 20790, |
| 1039 | [18] = 27720, |
| 1040 | [19] = 34650, |
| 1041 | [20] = 41580, |
| 1042 | [21] = 45045, |
| 1043 | [22] = 51975, |
| 1044 | [23] = 62370, |
| 1045 | [24] = 67568, /* 6756.75 mbps */ |
| 1046 | /* LP-SC PHY */ |
| 1047 | [25] = 6260, |
| 1048 | [26] = 8340, |
| 1049 | [27] = 11120, |
| 1050 | [28] = 12510, |
| 1051 | [29] = 16680, |
| 1052 | [30] = 22240, |
| 1053 | [31] = 25030, |
| 1054 | }; |
| 1055 | |
| 1056 | if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate))) |
| 1057 | return 0; |
| 1058 | |
| 1059 | return __mcs2bitrate[rate->mcs]; |
| 1060 | } |
| 1061 | |
Johannes Berg | db9c64c | 2012-11-09 14:56:41 +0100 | [diff] [blame] | 1062 | static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate) |
| 1063 | { |
| 1064 | static const u32 base[4][10] = { |
| 1065 | { 6500000, |
| 1066 | 13000000, |
| 1067 | 19500000, |
| 1068 | 26000000, |
| 1069 | 39000000, |
| 1070 | 52000000, |
| 1071 | 58500000, |
| 1072 | 65000000, |
| 1073 | 78000000, |
Pedersen, Thomas | 8fdd136 | 2016-10-31 11:28:40 -0700 | [diff] [blame] | 1074 | /* not in the spec, but some devices use this: */ |
| 1075 | 86500000, |
Johannes Berg | db9c64c | 2012-11-09 14:56:41 +0100 | [diff] [blame] | 1076 | }, |
| 1077 | { 13500000, |
| 1078 | 27000000, |
| 1079 | 40500000, |
| 1080 | 54000000, |
| 1081 | 81000000, |
| 1082 | 108000000, |
| 1083 | 121500000, |
| 1084 | 135000000, |
| 1085 | 162000000, |
| 1086 | 180000000, |
| 1087 | }, |
| 1088 | { 29300000, |
| 1089 | 58500000, |
| 1090 | 87800000, |
| 1091 | 117000000, |
| 1092 | 175500000, |
| 1093 | 234000000, |
| 1094 | 263300000, |
| 1095 | 292500000, |
| 1096 | 351000000, |
| 1097 | 390000000, |
| 1098 | }, |
| 1099 | { 58500000, |
| 1100 | 117000000, |
| 1101 | 175500000, |
| 1102 | 234000000, |
| 1103 | 351000000, |
| 1104 | 468000000, |
| 1105 | 526500000, |
| 1106 | 585000000, |
| 1107 | 702000000, |
| 1108 | 780000000, |
| 1109 | }, |
| 1110 | }; |
| 1111 | u32 bitrate; |
| 1112 | int idx; |
| 1113 | |
Johannes Berg | ca8fe25 | 2017-05-04 07:52:10 +0200 | [diff] [blame] | 1114 | if (rate->mcs > 9) |
| 1115 | goto warn; |
Johannes Berg | db9c64c | 2012-11-09 14:56:41 +0100 | [diff] [blame] | 1116 | |
Johannes Berg | b51f3be | 2015-01-15 16:14:02 +0100 | [diff] [blame] | 1117 | switch (rate->bw) { |
| 1118 | case RATE_INFO_BW_160: |
| 1119 | idx = 3; |
| 1120 | break; |
| 1121 | case RATE_INFO_BW_80: |
| 1122 | idx = 2; |
| 1123 | break; |
| 1124 | case RATE_INFO_BW_40: |
| 1125 | idx = 1; |
| 1126 | break; |
| 1127 | case RATE_INFO_BW_5: |
| 1128 | case RATE_INFO_BW_10: |
| 1129 | default: |
Johannes Berg | ca8fe25 | 2017-05-04 07:52:10 +0200 | [diff] [blame] | 1130 | goto warn; |
Johannes Berg | b51f3be | 2015-01-15 16:14:02 +0100 | [diff] [blame] | 1131 | case RATE_INFO_BW_20: |
| 1132 | idx = 0; |
| 1133 | } |
Johannes Berg | db9c64c | 2012-11-09 14:56:41 +0100 | [diff] [blame] | 1134 | |
| 1135 | bitrate = base[idx][rate->mcs]; |
| 1136 | bitrate *= rate->nss; |
| 1137 | |
| 1138 | if (rate->flags & RATE_INFO_FLAGS_SHORT_GI) |
| 1139 | bitrate = (bitrate / 9) * 10; |
| 1140 | |
| 1141 | /* do NOT round down here */ |
| 1142 | return (bitrate + 50000) / 100000; |
Johannes Berg | ca8fe25 | 2017-05-04 07:52:10 +0200 | [diff] [blame] | 1143 | warn: |
| 1144 | WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n", |
| 1145 | rate->bw, rate->mcs, rate->nss); |
| 1146 | return 0; |
Johannes Berg | db9c64c | 2012-11-09 14:56:41 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
Luca Coelho | c4cbaf7 | 2018-06-09 09:14:42 +0300 | [diff] [blame] | 1149 | static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate) |
| 1150 | { |
| 1151 | #define SCALE 2048 |
| 1152 | u16 mcs_divisors[12] = { |
| 1153 | 34133, /* 16.666666... */ |
| 1154 | 17067, /* 8.333333... */ |
| 1155 | 11378, /* 5.555555... */ |
| 1156 | 8533, /* 4.166666... */ |
| 1157 | 5689, /* 2.777777... */ |
| 1158 | 4267, /* 2.083333... */ |
| 1159 | 3923, /* 1.851851... */ |
| 1160 | 3413, /* 1.666666... */ |
| 1161 | 2844, /* 1.388888... */ |
| 1162 | 2560, /* 1.250000... */ |
| 1163 | 2276, /* 1.111111... */ |
| 1164 | 2048, /* 1.000000... */ |
| 1165 | }; |
| 1166 | u32 rates_160M[3] = { 960777777, 907400000, 816666666 }; |
| 1167 | u32 rates_969[3] = { 480388888, 453700000, 408333333 }; |
| 1168 | u32 rates_484[3] = { 229411111, 216666666, 195000000 }; |
| 1169 | u32 rates_242[3] = { 114711111, 108333333, 97500000 }; |
| 1170 | u32 rates_106[3] = { 40000000, 37777777, 34000000 }; |
| 1171 | u32 rates_52[3] = { 18820000, 17777777, 16000000 }; |
| 1172 | u32 rates_26[3] = { 9411111, 8888888, 8000000 }; |
| 1173 | u64 tmp; |
| 1174 | u32 result; |
| 1175 | |
| 1176 | if (WARN_ON_ONCE(rate->mcs > 11)) |
| 1177 | return 0; |
| 1178 | |
| 1179 | if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2)) |
| 1180 | return 0; |
| 1181 | if (WARN_ON_ONCE(rate->he_ru_alloc > |
| 1182 | NL80211_RATE_INFO_HE_RU_ALLOC_2x996)) |
| 1183 | return 0; |
| 1184 | if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8)) |
| 1185 | return 0; |
| 1186 | |
| 1187 | if (rate->bw == RATE_INFO_BW_160) |
| 1188 | result = rates_160M[rate->he_gi]; |
| 1189 | else if (rate->bw == RATE_INFO_BW_80 || |
| 1190 | (rate->bw == RATE_INFO_BW_HE_RU && |
| 1191 | rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996)) |
| 1192 | result = rates_969[rate->he_gi]; |
| 1193 | else if (rate->bw == RATE_INFO_BW_40 || |
| 1194 | (rate->bw == RATE_INFO_BW_HE_RU && |
| 1195 | rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484)) |
| 1196 | result = rates_484[rate->he_gi]; |
| 1197 | else if (rate->bw == RATE_INFO_BW_20 || |
| 1198 | (rate->bw == RATE_INFO_BW_HE_RU && |
| 1199 | rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242)) |
| 1200 | result = rates_242[rate->he_gi]; |
| 1201 | else if (rate->bw == RATE_INFO_BW_HE_RU && |
| 1202 | rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106) |
| 1203 | result = rates_106[rate->he_gi]; |
| 1204 | else if (rate->bw == RATE_INFO_BW_HE_RU && |
| 1205 | rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52) |
| 1206 | result = rates_52[rate->he_gi]; |
| 1207 | else if (rate->bw == RATE_INFO_BW_HE_RU && |
| 1208 | rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26) |
| 1209 | result = rates_26[rate->he_gi]; |
| 1210 | else if (WARN(1, "invalid HE MCS: bw:%d, ru:%d\n", |
| 1211 | rate->bw, rate->he_ru_alloc)) |
| 1212 | return 0; |
| 1213 | |
| 1214 | /* now scale to the appropriate MCS */ |
| 1215 | tmp = result; |
| 1216 | tmp *= SCALE; |
| 1217 | do_div(tmp, mcs_divisors[rate->mcs]); |
| 1218 | result = tmp; |
| 1219 | |
| 1220 | /* and take NSS, DCM into account */ |
| 1221 | result = (result * rate->nss) / 8; |
| 1222 | if (rate->he_dcm) |
| 1223 | result /= 2; |
| 1224 | |
| 1225 | return result; |
| 1226 | } |
| 1227 | |
Vladimir Kondratiev | 8eb41c8 | 2012-07-05 14:25:49 +0300 | [diff] [blame] | 1228 | u32 cfg80211_calculate_bitrate(struct rate_info *rate) |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 1229 | { |
Johannes Berg | 0c1eca4 | 2017-02-15 15:02:08 +0100 | [diff] [blame] | 1230 | if (rate->flags & RATE_INFO_FLAGS_MCS) |
| 1231 | return cfg80211_calculate_bitrate_ht(rate); |
Vladimir Kondratiev | 95ddc1f | 2012-07-05 14:25:50 +0300 | [diff] [blame] | 1232 | if (rate->flags & RATE_INFO_FLAGS_60G) |
| 1233 | return cfg80211_calculate_bitrate_60g(rate); |
Johannes Berg | db9c64c | 2012-11-09 14:56:41 +0100 | [diff] [blame] | 1234 | if (rate->flags & RATE_INFO_FLAGS_VHT_MCS) |
| 1235 | return cfg80211_calculate_bitrate_vht(rate); |
Luca Coelho | c4cbaf7 | 2018-06-09 09:14:42 +0300 | [diff] [blame] | 1236 | if (rate->flags & RATE_INFO_FLAGS_HE_MCS) |
| 1237 | return cfg80211_calculate_bitrate_he(rate); |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 1238 | |
Johannes Berg | 0c1eca4 | 2017-02-15 15:02:08 +0100 | [diff] [blame] | 1239 | return rate->legacy; |
John W. Linville | 254416a | 2009-12-09 16:43:52 -0500 | [diff] [blame] | 1240 | } |
Thomas Pedersen | 8097e14 | 2012-03-05 15:31:47 -0800 | [diff] [blame] | 1241 | EXPORT_SYMBOL(cfg80211_calculate_bitrate); |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1242 | |
Arend van Spriel | c216e64 | 2012-11-25 19:13:28 +0100 | [diff] [blame] | 1243 | int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len, |
| 1244 | enum ieee80211_p2p_attr_id attr, |
| 1245 | u8 *buf, unsigned int bufsize) |
Johannes Berg | 0ee4535 | 2012-10-29 19:48:40 +0100 | [diff] [blame] | 1246 | { |
| 1247 | u8 *out = buf; |
| 1248 | u16 attr_remaining = 0; |
| 1249 | bool desired_attr = false; |
| 1250 | u16 desired_len = 0; |
| 1251 | |
| 1252 | while (len > 0) { |
| 1253 | unsigned int iedatalen; |
| 1254 | unsigned int copy; |
| 1255 | const u8 *iedata; |
| 1256 | |
| 1257 | if (len < 2) |
| 1258 | return -EILSEQ; |
| 1259 | iedatalen = ies[1]; |
| 1260 | if (iedatalen + 2 > len) |
| 1261 | return -EILSEQ; |
| 1262 | |
| 1263 | if (ies[0] != WLAN_EID_VENDOR_SPECIFIC) |
| 1264 | goto cont; |
| 1265 | |
| 1266 | if (iedatalen < 4) |
| 1267 | goto cont; |
| 1268 | |
| 1269 | iedata = ies + 2; |
| 1270 | |
| 1271 | /* check WFA OUI, P2P subtype */ |
| 1272 | if (iedata[0] != 0x50 || iedata[1] != 0x6f || |
| 1273 | iedata[2] != 0x9a || iedata[3] != 0x09) |
| 1274 | goto cont; |
| 1275 | |
| 1276 | iedatalen -= 4; |
| 1277 | iedata += 4; |
| 1278 | |
| 1279 | /* check attribute continuation into this IE */ |
| 1280 | copy = min_t(unsigned int, attr_remaining, iedatalen); |
| 1281 | if (copy && desired_attr) { |
| 1282 | desired_len += copy; |
| 1283 | if (out) { |
| 1284 | memcpy(out, iedata, min(bufsize, copy)); |
| 1285 | out += min(bufsize, copy); |
| 1286 | bufsize -= min(bufsize, copy); |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | if (copy == attr_remaining) |
| 1291 | return desired_len; |
| 1292 | } |
| 1293 | |
| 1294 | attr_remaining -= copy; |
| 1295 | if (attr_remaining) |
| 1296 | goto cont; |
| 1297 | |
| 1298 | iedatalen -= copy; |
| 1299 | iedata += copy; |
| 1300 | |
| 1301 | while (iedatalen > 0) { |
| 1302 | u16 attr_len; |
| 1303 | |
| 1304 | /* P2P attribute ID & size must fit */ |
| 1305 | if (iedatalen < 3) |
| 1306 | return -EILSEQ; |
| 1307 | desired_attr = iedata[0] == attr; |
| 1308 | attr_len = get_unaligned_le16(iedata + 1); |
| 1309 | iedatalen -= 3; |
| 1310 | iedata += 3; |
| 1311 | |
| 1312 | copy = min_t(unsigned int, attr_len, iedatalen); |
| 1313 | |
| 1314 | if (desired_attr) { |
| 1315 | desired_len += copy; |
| 1316 | if (out) { |
| 1317 | memcpy(out, iedata, min(bufsize, copy)); |
| 1318 | out += min(bufsize, copy); |
| 1319 | bufsize -= min(bufsize, copy); |
| 1320 | } |
| 1321 | |
| 1322 | if (copy == attr_len) |
| 1323 | return desired_len; |
| 1324 | } |
| 1325 | |
| 1326 | iedata += copy; |
| 1327 | iedatalen -= copy; |
| 1328 | attr_remaining = attr_len - copy; |
| 1329 | } |
| 1330 | |
| 1331 | cont: |
| 1332 | len -= ies[1] + 2; |
| 1333 | ies += ies[1] + 2; |
| 1334 | } |
| 1335 | |
| 1336 | if (attr_remaining && desired_attr) |
| 1337 | return -EILSEQ; |
| 1338 | |
| 1339 | return -ENOENT; |
| 1340 | } |
| 1341 | EXPORT_SYMBOL(cfg80211_get_p2p_attr); |
| 1342 | |
Liad Kaufman | 2512b1b | 2017-08-05 11:44:31 +0300 | [diff] [blame] | 1343 | static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext) |
Johannes Berg | 29464cc | 2015-03-31 15:36:22 +0200 | [diff] [blame] | 1344 | { |
| 1345 | int i; |
| 1346 | |
Liad Kaufman | 2512b1b | 2017-08-05 11:44:31 +0300 | [diff] [blame] | 1347 | /* Make sure array values are legal */ |
| 1348 | if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION)) |
| 1349 | return false; |
| 1350 | |
| 1351 | i = 0; |
| 1352 | while (i < n_ids) { |
| 1353 | if (ids[i] == WLAN_EID_EXTENSION) { |
| 1354 | if (id_ext && (ids[i + 1] == id)) |
| 1355 | return true; |
| 1356 | |
| 1357 | i += 2; |
| 1358 | continue; |
| 1359 | } |
| 1360 | |
| 1361 | if (ids[i] == id && !id_ext) |
Johannes Berg | 29464cc | 2015-03-31 15:36:22 +0200 | [diff] [blame] | 1362 | return true; |
Liad Kaufman | 2512b1b | 2017-08-05 11:44:31 +0300 | [diff] [blame] | 1363 | |
| 1364 | i++; |
| 1365 | } |
Johannes Berg | 29464cc | 2015-03-31 15:36:22 +0200 | [diff] [blame] | 1366 | return false; |
| 1367 | } |
| 1368 | |
Johannes Berg | 8ac6344 | 2015-09-04 20:03:23 +0200 | [diff] [blame] | 1369 | static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos) |
| 1370 | { |
| 1371 | /* we assume a validly formed IEs buffer */ |
| 1372 | u8 len = ies[pos + 1]; |
| 1373 | |
| 1374 | pos += 2 + len; |
| 1375 | |
| 1376 | /* the IE itself must have 255 bytes for fragments to follow */ |
| 1377 | if (len < 255) |
| 1378 | return pos; |
| 1379 | |
| 1380 | while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) { |
| 1381 | len = ies[pos + 1]; |
| 1382 | pos += 2 + len; |
| 1383 | } |
| 1384 | |
| 1385 | return pos; |
| 1386 | } |
| 1387 | |
Johannes Berg | 29464cc | 2015-03-31 15:36:22 +0200 | [diff] [blame] | 1388 | size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen, |
| 1389 | const u8 *ids, int n_ids, |
| 1390 | const u8 *after_ric, int n_after_ric, |
| 1391 | size_t offset) |
| 1392 | { |
| 1393 | size_t pos = offset; |
| 1394 | |
Liad Kaufman | 2512b1b | 2017-08-05 11:44:31 +0300 | [diff] [blame] | 1395 | while (pos < ielen) { |
| 1396 | u8 ext = 0; |
| 1397 | |
| 1398 | if (ies[pos] == WLAN_EID_EXTENSION) |
| 1399 | ext = 2; |
| 1400 | if ((pos + ext) >= ielen) |
| 1401 | break; |
| 1402 | |
| 1403 | if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext], |
| 1404 | ies[pos] == WLAN_EID_EXTENSION)) |
| 1405 | break; |
| 1406 | |
Johannes Berg | 29464cc | 2015-03-31 15:36:22 +0200 | [diff] [blame] | 1407 | if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) { |
Johannes Berg | 8ac6344 | 2015-09-04 20:03:23 +0200 | [diff] [blame] | 1408 | pos = skip_ie(ies, ielen, pos); |
Johannes Berg | 29464cc | 2015-03-31 15:36:22 +0200 | [diff] [blame] | 1409 | |
Liad Kaufman | 2512b1b | 2017-08-05 11:44:31 +0300 | [diff] [blame] | 1410 | while (pos < ielen) { |
| 1411 | if (ies[pos] == WLAN_EID_EXTENSION) |
| 1412 | ext = 2; |
| 1413 | else |
| 1414 | ext = 0; |
| 1415 | |
| 1416 | if ((pos + ext) >= ielen) |
| 1417 | break; |
| 1418 | |
| 1419 | if (!ieee80211_id_in_list(after_ric, |
| 1420 | n_after_ric, |
| 1421 | ies[pos + ext], |
| 1422 | ext == 2)) |
| 1423 | pos = skip_ie(ies, ielen, pos); |
| 1424 | } |
Johannes Berg | 29464cc | 2015-03-31 15:36:22 +0200 | [diff] [blame] | 1425 | } else { |
Johannes Berg | 8ac6344 | 2015-09-04 20:03:23 +0200 | [diff] [blame] | 1426 | pos = skip_ie(ies, ielen, pos); |
Johannes Berg | 29464cc | 2015-03-31 15:36:22 +0200 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | return pos; |
| 1431 | } |
| 1432 | EXPORT_SYMBOL(ieee80211_ie_split_ric); |
| 1433 | |
Johannes Berg | 1ce3e82 | 2012-08-01 17:00:55 +0200 | [diff] [blame] | 1434 | bool ieee80211_operating_class_to_band(u8 operating_class, |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 1435 | enum nl80211_band *band) |
Johannes Berg | 1ce3e82 | 2012-08-01 17:00:55 +0200 | [diff] [blame] | 1436 | { |
| 1437 | switch (operating_class) { |
| 1438 | case 112: |
| 1439 | case 115 ... 127: |
Eliad Peller | 954a86e | 2015-03-01 09:10:01 +0200 | [diff] [blame] | 1440 | case 128 ... 130: |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 1441 | *band = NL80211_BAND_5GHZ; |
Johannes Berg | 1ce3e82 | 2012-08-01 17:00:55 +0200 | [diff] [blame] | 1442 | return true; |
| 1443 | case 81: |
| 1444 | case 82: |
| 1445 | case 83: |
| 1446 | case 84: |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 1447 | *band = NL80211_BAND_2GHZ; |
Johannes Berg | 1ce3e82 | 2012-08-01 17:00:55 +0200 | [diff] [blame] | 1448 | return true; |
Vladimir Kondratiev | 55300a1 | 2013-04-23 09:54:21 +0300 | [diff] [blame] | 1449 | case 180: |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 1450 | *band = NL80211_BAND_60GHZ; |
Vladimir Kondratiev | 55300a1 | 2013-04-23 09:54:21 +0300 | [diff] [blame] | 1451 | return true; |
Johannes Berg | 1ce3e82 | 2012-08-01 17:00:55 +0200 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | return false; |
| 1455 | } |
| 1456 | EXPORT_SYMBOL(ieee80211_operating_class_to_band); |
| 1457 | |
Arik Nemtsov | a38700d | 2015-03-18 08:46:08 +0200 | [diff] [blame] | 1458 | bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef, |
| 1459 | u8 *op_class) |
| 1460 | { |
| 1461 | u8 vht_opclass; |
Dan Carpenter | 8442938 | 2018-08-31 11:10:55 +0300 | [diff] [blame] | 1462 | u32 freq = chandef->center_freq1; |
Arik Nemtsov | a38700d | 2015-03-18 08:46:08 +0200 | [diff] [blame] | 1463 | |
| 1464 | if (freq >= 2412 && freq <= 2472) { |
| 1465 | if (chandef->width > NL80211_CHAN_WIDTH_40) |
| 1466 | return false; |
| 1467 | |
| 1468 | /* 2.407 GHz, channels 1..13 */ |
| 1469 | if (chandef->width == NL80211_CHAN_WIDTH_40) { |
| 1470 | if (freq > chandef->chan->center_freq) |
| 1471 | *op_class = 83; /* HT40+ */ |
| 1472 | else |
| 1473 | *op_class = 84; /* HT40- */ |
| 1474 | } else { |
| 1475 | *op_class = 81; |
| 1476 | } |
| 1477 | |
| 1478 | return true; |
| 1479 | } |
| 1480 | |
| 1481 | if (freq == 2484) { |
| 1482 | if (chandef->width > NL80211_CHAN_WIDTH_40) |
| 1483 | return false; |
| 1484 | |
| 1485 | *op_class = 82; /* channel 14 */ |
| 1486 | return true; |
| 1487 | } |
| 1488 | |
| 1489 | switch (chandef->width) { |
| 1490 | case NL80211_CHAN_WIDTH_80: |
| 1491 | vht_opclass = 128; |
| 1492 | break; |
| 1493 | case NL80211_CHAN_WIDTH_160: |
| 1494 | vht_opclass = 129; |
| 1495 | break; |
| 1496 | case NL80211_CHAN_WIDTH_80P80: |
| 1497 | vht_opclass = 130; |
| 1498 | break; |
| 1499 | case NL80211_CHAN_WIDTH_10: |
| 1500 | case NL80211_CHAN_WIDTH_5: |
| 1501 | return false; /* unsupported for now */ |
| 1502 | default: |
| 1503 | vht_opclass = 0; |
| 1504 | break; |
| 1505 | } |
| 1506 | |
| 1507 | /* 5 GHz, channels 36..48 */ |
| 1508 | if (freq >= 5180 && freq <= 5240) { |
| 1509 | if (vht_opclass) { |
| 1510 | *op_class = vht_opclass; |
| 1511 | } else if (chandef->width == NL80211_CHAN_WIDTH_40) { |
| 1512 | if (freq > chandef->chan->center_freq) |
| 1513 | *op_class = 116; |
| 1514 | else |
| 1515 | *op_class = 117; |
| 1516 | } else { |
| 1517 | *op_class = 115; |
| 1518 | } |
| 1519 | |
| 1520 | return true; |
| 1521 | } |
| 1522 | |
| 1523 | /* 5 GHz, channels 52..64 */ |
| 1524 | if (freq >= 5260 && freq <= 5320) { |
| 1525 | if (vht_opclass) { |
| 1526 | *op_class = vht_opclass; |
| 1527 | } else if (chandef->width == NL80211_CHAN_WIDTH_40) { |
| 1528 | if (freq > chandef->chan->center_freq) |
| 1529 | *op_class = 119; |
| 1530 | else |
| 1531 | *op_class = 120; |
| 1532 | } else { |
| 1533 | *op_class = 118; |
| 1534 | } |
| 1535 | |
| 1536 | return true; |
| 1537 | } |
| 1538 | |
| 1539 | /* 5 GHz, channels 100..144 */ |
| 1540 | if (freq >= 5500 && freq <= 5720) { |
| 1541 | if (vht_opclass) { |
| 1542 | *op_class = vht_opclass; |
| 1543 | } else if (chandef->width == NL80211_CHAN_WIDTH_40) { |
| 1544 | if (freq > chandef->chan->center_freq) |
| 1545 | *op_class = 122; |
| 1546 | else |
| 1547 | *op_class = 123; |
| 1548 | } else { |
| 1549 | *op_class = 121; |
| 1550 | } |
| 1551 | |
| 1552 | return true; |
| 1553 | } |
| 1554 | |
| 1555 | /* 5 GHz, channels 149..169 */ |
| 1556 | if (freq >= 5745 && freq <= 5845) { |
| 1557 | if (vht_opclass) { |
| 1558 | *op_class = vht_opclass; |
| 1559 | } else if (chandef->width == NL80211_CHAN_WIDTH_40) { |
| 1560 | if (freq > chandef->chan->center_freq) |
| 1561 | *op_class = 126; |
| 1562 | else |
| 1563 | *op_class = 127; |
| 1564 | } else if (freq <= 5805) { |
| 1565 | *op_class = 124; |
| 1566 | } else { |
| 1567 | *op_class = 125; |
| 1568 | } |
| 1569 | |
| 1570 | return true; |
| 1571 | } |
| 1572 | |
| 1573 | /* 56.16 GHz, channel 1..4 */ |
Alexei Avshalom Lazar | 9cf0a0b | 2018-08-13 15:33:00 +0300 | [diff] [blame] | 1574 | if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) { |
Arik Nemtsov | a38700d | 2015-03-18 08:46:08 +0200 | [diff] [blame] | 1575 | if (chandef->width >= NL80211_CHAN_WIDTH_40) |
| 1576 | return false; |
| 1577 | |
| 1578 | *op_class = 180; |
| 1579 | return true; |
| 1580 | } |
| 1581 | |
| 1582 | /* not supported yet */ |
| 1583 | return false; |
| 1584 | } |
| 1585 | EXPORT_SYMBOL(ieee80211_chandef_to_operating_class); |
| 1586 | |
Johannes Berg | 4c8dea6 | 2016-10-21 14:25:13 +0200 | [diff] [blame] | 1587 | static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int, |
| 1588 | u32 *beacon_int_gcd, |
| 1589 | bool *beacon_int_different) |
| 1590 | { |
| 1591 | struct wireless_dev *wdev; |
| 1592 | |
| 1593 | *beacon_int_gcd = 0; |
| 1594 | *beacon_int_different = false; |
| 1595 | |
| 1596 | list_for_each_entry(wdev, &wiphy->wdev_list, list) { |
| 1597 | if (!wdev->beacon_interval) |
| 1598 | continue; |
| 1599 | |
| 1600 | if (!*beacon_int_gcd) { |
| 1601 | *beacon_int_gcd = wdev->beacon_interval; |
| 1602 | continue; |
| 1603 | } |
| 1604 | |
| 1605 | if (wdev->beacon_interval == *beacon_int_gcd) |
| 1606 | continue; |
| 1607 | |
| 1608 | *beacon_int_different = true; |
| 1609 | *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval); |
| 1610 | } |
| 1611 | |
| 1612 | if (new_beacon_int && *beacon_int_gcd != new_beacon_int) { |
| 1613 | if (*beacon_int_gcd) |
| 1614 | *beacon_int_different = true; |
| 1615 | *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int); |
| 1616 | } |
| 1617 | } |
| 1618 | |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1619 | int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev, |
Purushottam Kushwaha | 0c317a0 | 2016-10-12 18:26:51 +0530 | [diff] [blame] | 1620 | enum nl80211_iftype iftype, u32 beacon_int) |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1621 | { |
Johannes Berg | 4c8dea6 | 2016-10-21 14:25:13 +0200 | [diff] [blame] | 1622 | /* |
| 1623 | * This is just a basic pre-condition check; if interface combinations |
| 1624 | * are possible the driver must already be checking those with a call |
| 1625 | * to cfg80211_check_combinations(), in which case we'll validate more |
| 1626 | * through the cfg80211_calculate_bi_data() call and code in |
| 1627 | * cfg80211_iter_combinations(). |
| 1628 | */ |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1629 | |
Purushottam Kushwaha | 12d20fc9 | 2016-08-11 15:14:02 +0530 | [diff] [blame] | 1630 | if (beacon_int < 10 || beacon_int > 10000) |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1631 | return -EINVAL; |
| 1632 | |
Johannes Berg | 4c8dea6 | 2016-10-21 14:25:13 +0200 | [diff] [blame] | 1633 | return 0; |
Johannes Berg | 56d1893 | 2011-05-09 18:41:15 +0200 | [diff] [blame] | 1634 | } |
Johannes Berg | 7527a78 | 2011-05-13 10:58:57 +0200 | [diff] [blame] | 1635 | |
Michal Kazior | 65a124d | 2014-04-09 15:29:22 +0200 | [diff] [blame] | 1636 | int cfg80211_iter_combinations(struct wiphy *wiphy, |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1637 | struct iface_combination_params *params, |
Michal Kazior | 65a124d | 2014-04-09 15:29:22 +0200 | [diff] [blame] | 1638 | void (*iter)(const struct ieee80211_iface_combination *c, |
| 1639 | void *data), |
| 1640 | void *data) |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1641 | { |
Felix Fietkau | 8c48b50 | 2014-05-05 11:48:40 +0200 | [diff] [blame] | 1642 | const struct ieee80211_regdomain *regdom; |
| 1643 | enum nl80211_dfs_regions region = 0; |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1644 | int i, j, iftype; |
| 1645 | int num_interfaces = 0; |
| 1646 | u32 used_iftypes = 0; |
Johannes Berg | 4c8dea6 | 2016-10-21 14:25:13 +0200 | [diff] [blame] | 1647 | u32 beacon_int_gcd; |
| 1648 | bool beacon_int_different; |
| 1649 | |
| 1650 | /* |
| 1651 | * This is a bit strange, since the iteration used to rely only on |
| 1652 | * the data given by the driver, but here it now relies on context, |
| 1653 | * in form of the currently operating interfaces. |
| 1654 | * This is OK for all current users, and saves us from having to |
| 1655 | * push the GCD calculations into all the drivers. |
| 1656 | * In the future, this should probably rely more on data that's in |
| 1657 | * cfg80211 already - the only thing not would appear to be any new |
| 1658 | * interfaces (while being brought up) and channel/radar data. |
| 1659 | */ |
| 1660 | cfg80211_calculate_bi_data(wiphy, params->new_beacon_int, |
| 1661 | &beacon_int_gcd, &beacon_int_different); |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1662 | |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1663 | if (params->radar_detect) { |
Felix Fietkau | 8c48b50 | 2014-05-05 11:48:40 +0200 | [diff] [blame] | 1664 | rcu_read_lock(); |
| 1665 | regdom = rcu_dereference(cfg80211_regdomain); |
| 1666 | if (regdom) |
| 1667 | region = regdom->dfs_region; |
| 1668 | rcu_read_unlock(); |
| 1669 | } |
| 1670 | |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1671 | for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1672 | num_interfaces += params->iftype_num[iftype]; |
| 1673 | if (params->iftype_num[iftype] > 0 && |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1674 | !(wiphy->software_iftypes & BIT(iftype))) |
| 1675 | used_iftypes |= BIT(iftype); |
| 1676 | } |
| 1677 | |
| 1678 | for (i = 0; i < wiphy->n_iface_combinations; i++) { |
| 1679 | const struct ieee80211_iface_combination *c; |
| 1680 | struct ieee80211_iface_limit *limits; |
| 1681 | u32 all_iftypes = 0; |
| 1682 | |
| 1683 | c = &wiphy->iface_combinations[i]; |
| 1684 | |
| 1685 | if (num_interfaces > c->max_interfaces) |
| 1686 | continue; |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1687 | if (params->num_different_channels > c->num_different_channels) |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1688 | continue; |
| 1689 | |
| 1690 | limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits, |
| 1691 | GFP_KERNEL); |
| 1692 | if (!limits) |
| 1693 | return -ENOMEM; |
| 1694 | |
| 1695 | for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) { |
| 1696 | if (wiphy->software_iftypes & BIT(iftype)) |
| 1697 | continue; |
| 1698 | for (j = 0; j < c->n_limits; j++) { |
| 1699 | all_iftypes |= limits[j].types; |
| 1700 | if (!(limits[j].types & BIT(iftype))) |
| 1701 | continue; |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1702 | if (limits[j].max < params->iftype_num[iftype]) |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1703 | goto cont; |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1704 | limits[j].max -= params->iftype_num[iftype]; |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1705 | } |
| 1706 | } |
| 1707 | |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1708 | if (params->radar_detect != |
| 1709 | (c->radar_detect_widths & params->radar_detect)) |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1710 | goto cont; |
| 1711 | |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1712 | if (params->radar_detect && c->radar_detect_regions && |
Felix Fietkau | 8c48b50 | 2014-05-05 11:48:40 +0200 | [diff] [blame] | 1713 | !(c->radar_detect_regions & BIT(region))) |
| 1714 | goto cont; |
| 1715 | |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1716 | /* Finally check that all iftypes that we're currently |
| 1717 | * using are actually part of this combination. If they |
| 1718 | * aren't then we can't use this combination and have |
| 1719 | * to continue to the next. |
| 1720 | */ |
| 1721 | if ((all_iftypes & used_iftypes) != used_iftypes) |
| 1722 | goto cont; |
| 1723 | |
Johannes Berg | 4c8dea6 | 2016-10-21 14:25:13 +0200 | [diff] [blame] | 1724 | if (beacon_int_gcd) { |
Purushottam Kushwaha | 0c317a0 | 2016-10-12 18:26:51 +0530 | [diff] [blame] | 1725 | if (c->beacon_int_min_gcd && |
Johannes Berg | 4c8dea6 | 2016-10-21 14:25:13 +0200 | [diff] [blame] | 1726 | beacon_int_gcd < c->beacon_int_min_gcd) |
Johannes Berg | 0507a3a | 2016-10-21 12:15:00 +0200 | [diff] [blame] | 1727 | goto cont; |
Johannes Berg | 4c8dea6 | 2016-10-21 14:25:13 +0200 | [diff] [blame] | 1728 | if (!c->beacon_int_min_gcd && beacon_int_different) |
Purushottam Kushwaha | 0c317a0 | 2016-10-12 18:26:51 +0530 | [diff] [blame] | 1729 | goto cont; |
| 1730 | } |
| 1731 | |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1732 | /* This combination covered all interface types and |
| 1733 | * supported the requested numbers, so we're good. |
| 1734 | */ |
Michal Kazior | 65a124d | 2014-04-09 15:29:22 +0200 | [diff] [blame] | 1735 | |
| 1736 | (*iter)(c, data); |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1737 | cont: |
| 1738 | kfree(limits); |
| 1739 | } |
| 1740 | |
Michal Kazior | 65a124d | 2014-04-09 15:29:22 +0200 | [diff] [blame] | 1741 | return 0; |
| 1742 | } |
| 1743 | EXPORT_SYMBOL(cfg80211_iter_combinations); |
| 1744 | |
| 1745 | static void |
| 1746 | cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c, |
| 1747 | void *data) |
| 1748 | { |
| 1749 | int *num = data; |
| 1750 | (*num)++; |
| 1751 | } |
| 1752 | |
| 1753 | int cfg80211_check_combinations(struct wiphy *wiphy, |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1754 | struct iface_combination_params *params) |
Michal Kazior | 65a124d | 2014-04-09 15:29:22 +0200 | [diff] [blame] | 1755 | { |
| 1756 | int err, num = 0; |
| 1757 | |
Purushottam Kushwaha | e227300 | 2016-10-12 18:25:35 +0530 | [diff] [blame] | 1758 | err = cfg80211_iter_combinations(wiphy, params, |
Michal Kazior | 65a124d | 2014-04-09 15:29:22 +0200 | [diff] [blame] | 1759 | cfg80211_iter_sum_ifcombs, &num); |
| 1760 | if (err) |
| 1761 | return err; |
| 1762 | if (num == 0) |
| 1763 | return -EBUSY; |
| 1764 | |
| 1765 | return 0; |
Luciano Coelho | cb2d956 | 2014-02-17 16:52:35 +0200 | [diff] [blame] | 1766 | } |
| 1767 | EXPORT_SYMBOL(cfg80211_check_combinations); |
| 1768 | |
Johannes Berg | 34850ab | 2011-07-18 18:08:35 +0200 | [diff] [blame] | 1769 | int ieee80211_get_ratemask(struct ieee80211_supported_band *sband, |
| 1770 | const u8 *rates, unsigned int n_rates, |
| 1771 | u32 *mask) |
| 1772 | { |
| 1773 | int i, j; |
| 1774 | |
Johannes Berg | a401d2b | 2011-07-20 00:52:16 +0200 | [diff] [blame] | 1775 | if (!sband) |
| 1776 | return -EINVAL; |
| 1777 | |
Johannes Berg | 34850ab | 2011-07-18 18:08:35 +0200 | [diff] [blame] | 1778 | if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES) |
| 1779 | return -EINVAL; |
| 1780 | |
| 1781 | *mask = 0; |
| 1782 | |
| 1783 | for (i = 0; i < n_rates; i++) { |
| 1784 | int rate = (rates[i] & 0x7f) * 5; |
| 1785 | bool found = false; |
| 1786 | |
| 1787 | for (j = 0; j < sband->n_bitrates; j++) { |
| 1788 | if (sband->bitrates[j].bitrate == rate) { |
| 1789 | found = true; |
| 1790 | *mask |= BIT(j); |
| 1791 | break; |
| 1792 | } |
| 1793 | } |
| 1794 | if (!found) |
| 1795 | return -EINVAL; |
| 1796 | } |
| 1797 | |
| 1798 | /* |
| 1799 | * mask must have at least one bit set here since we |
| 1800 | * didn't accept a 0-length rates array nor allowed |
| 1801 | * entries in the array that didn't exist |
| 1802 | */ |
| 1803 | |
| 1804 | return 0; |
| 1805 | } |
Johannes Berg | 11a2a35 | 2011-11-21 11:09:22 +0100 | [diff] [blame] | 1806 | |
Ilan Peer | bdfbec2 | 2014-01-09 11:37:23 +0200 | [diff] [blame] | 1807 | unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy) |
| 1808 | { |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 1809 | enum nl80211_band band; |
Ilan Peer | bdfbec2 | 2014-01-09 11:37:23 +0200 | [diff] [blame] | 1810 | unsigned int n_channels = 0; |
| 1811 | |
Johannes Berg | 57fbcce | 2016-04-12 15:56:15 +0200 | [diff] [blame] | 1812 | for (band = 0; band < NUM_NL80211_BANDS; band++) |
Ilan Peer | bdfbec2 | 2014-01-09 11:37:23 +0200 | [diff] [blame] | 1813 | if (wiphy->bands[band]) |
| 1814 | n_channels += wiphy->bands[band]->n_channels; |
| 1815 | |
| 1816 | return n_channels; |
| 1817 | } |
| 1818 | EXPORT_SYMBOL(ieee80211_get_num_supported_channels); |
| 1819 | |
Antonio Quartulli | 7406353 | 2014-05-19 21:53:21 +0200 | [diff] [blame] | 1820 | int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr, |
| 1821 | struct station_info *sinfo) |
| 1822 | { |
| 1823 | struct cfg80211_registered_device *rdev; |
| 1824 | struct wireless_dev *wdev; |
| 1825 | |
| 1826 | wdev = dev->ieee80211_ptr; |
| 1827 | if (!wdev) |
| 1828 | return -EOPNOTSUPP; |
| 1829 | |
| 1830 | rdev = wiphy_to_rdev(wdev->wiphy); |
| 1831 | if (!rdev->ops->get_station) |
| 1832 | return -EOPNOTSUPP; |
| 1833 | |
Sven Eckelmann | 3c12d04 | 2018-06-06 10:53:55 +0200 | [diff] [blame] | 1834 | memset(sinfo, 0, sizeof(*sinfo)); |
| 1835 | |
Antonio Quartulli | 7406353 | 2014-05-19 21:53:21 +0200 | [diff] [blame] | 1836 | return rdev_get_station(rdev, dev, mac_addr, sinfo); |
| 1837 | } |
| 1838 | EXPORT_SYMBOL(cfg80211_get_station); |
| 1839 | |
Ayala Beker | a442b76 | 2016-09-20 17:31:15 +0300 | [diff] [blame] | 1840 | void cfg80211_free_nan_func(struct cfg80211_nan_func *f) |
| 1841 | { |
| 1842 | int i; |
| 1843 | |
| 1844 | if (!f) |
| 1845 | return; |
| 1846 | |
| 1847 | kfree(f->serv_spec_info); |
| 1848 | kfree(f->srf_bf); |
| 1849 | kfree(f->srf_macs); |
| 1850 | for (i = 0; i < f->num_rx_filters; i++) |
| 1851 | kfree(f->rx_filters[i].filter); |
| 1852 | |
| 1853 | for (i = 0; i < f->num_tx_filters; i++) |
| 1854 | kfree(f->tx_filters[i].filter); |
| 1855 | |
| 1856 | kfree(f->rx_filters); |
| 1857 | kfree(f->tx_filters); |
| 1858 | kfree(f); |
| 1859 | } |
| 1860 | EXPORT_SYMBOL(cfg80211_free_nan_func); |
| 1861 | |
Rafał Miłecki | 4787cfa | 2017-01-04 18:58:30 +0100 | [diff] [blame] | 1862 | bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range, |
| 1863 | u32 center_freq_khz, u32 bw_khz) |
| 1864 | { |
| 1865 | u32 start_freq_khz, end_freq_khz; |
| 1866 | |
| 1867 | start_freq_khz = center_freq_khz - (bw_khz / 2); |
| 1868 | end_freq_khz = center_freq_khz + (bw_khz / 2); |
| 1869 | |
| 1870 | if (start_freq_khz >= freq_range->start_freq_khz && |
| 1871 | end_freq_khz <= freq_range->end_freq_khz) |
| 1872 | return true; |
| 1873 | |
| 1874 | return false; |
| 1875 | } |
| 1876 | |
Arend van Spriel | 8689c05 | 2018-05-10 13:50:12 +0200 | [diff] [blame] | 1877 | int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp) |
| 1878 | { |
Johannes Berg | 1d211d4 | 2018-05-23 14:53:41 +0200 | [diff] [blame] | 1879 | sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1, |
| 1880 | sizeof(*(sinfo->pertid)), |
| 1881 | gfp); |
Arend van Spriel | 8689c05 | 2018-05-10 13:50:12 +0200 | [diff] [blame] | 1882 | if (!sinfo->pertid) |
| 1883 | return -ENOMEM; |
| 1884 | |
| 1885 | return 0; |
| 1886 | } |
| 1887 | EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats); |
| 1888 | |
Johannes Berg | 11a2a35 | 2011-11-21 11:09:22 +0100 | [diff] [blame] | 1889 | /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */ |
| 1890 | /* Ethernet-II snap header (RFC1042 for most EtherTypes) */ |
| 1891 | const unsigned char rfc1042_header[] __aligned(2) = |
| 1892 | { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; |
| 1893 | EXPORT_SYMBOL(rfc1042_header); |
| 1894 | |
| 1895 | /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */ |
| 1896 | const unsigned char bridge_tunnel_header[] __aligned(2) = |
| 1897 | { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 }; |
| 1898 | EXPORT_SYMBOL(bridge_tunnel_header); |
Dedy Lansky | 30ca1aa | 2018-07-29 14:59:16 +0300 | [diff] [blame] | 1899 | |
| 1900 | /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */ |
| 1901 | struct iapp_layer2_update { |
| 1902 | u8 da[ETH_ALEN]; /* broadcast */ |
| 1903 | u8 sa[ETH_ALEN]; /* STA addr */ |
| 1904 | __be16 len; /* 6 */ |
| 1905 | u8 dsap; /* 0 */ |
| 1906 | u8 ssap; /* 0 */ |
| 1907 | u8 control; |
| 1908 | u8 xid_info[3]; |
| 1909 | } __packed; |
| 1910 | |
| 1911 | void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr) |
| 1912 | { |
| 1913 | struct iapp_layer2_update *msg; |
| 1914 | struct sk_buff *skb; |
| 1915 | |
| 1916 | /* Send Level 2 Update Frame to update forwarding tables in layer 2 |
| 1917 | * bridge devices */ |
| 1918 | |
| 1919 | skb = dev_alloc_skb(sizeof(*msg)); |
| 1920 | if (!skb) |
| 1921 | return; |
| 1922 | msg = skb_put(skb, sizeof(*msg)); |
| 1923 | |
| 1924 | /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID) |
| 1925 | * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */ |
| 1926 | |
| 1927 | eth_broadcast_addr(msg->da); |
| 1928 | ether_addr_copy(msg->sa, addr); |
| 1929 | msg->len = htons(6); |
| 1930 | msg->dsap = 0; |
| 1931 | msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */ |
| 1932 | msg->control = 0xaf; /* XID response lsb.1111F101. |
| 1933 | * F=0 (no poll command; unsolicited frame) */ |
| 1934 | msg->xid_info[0] = 0x81; /* XID format identifier */ |
| 1935 | msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */ |
| 1936 | msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */ |
| 1937 | |
| 1938 | skb->dev = dev; |
| 1939 | skb->protocol = eth_type_trans(skb, dev); |
| 1940 | memset(skb->cb, 0, sizeof(skb->cb)); |
| 1941 | netif_rx_ni(skb); |
| 1942 | } |
| 1943 | EXPORT_SYMBOL(cfg80211_send_layer2_update); |
Johannes Berg | b0aa75f | 2018-08-31 11:31:16 +0300 | [diff] [blame^] | 1944 | |
| 1945 | int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap, |
| 1946 | enum ieee80211_vht_chanwidth bw, |
| 1947 | int mcs, bool ext_nss_bw_capable) |
| 1948 | { |
| 1949 | u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map); |
| 1950 | int max_vht_nss = 0; |
| 1951 | int ext_nss_bw; |
| 1952 | int supp_width; |
| 1953 | int i, mcs_encoding; |
| 1954 | |
| 1955 | if (map == 0xffff) |
| 1956 | return 0; |
| 1957 | |
| 1958 | if (WARN_ON(mcs > 9)) |
| 1959 | return 0; |
| 1960 | if (mcs <= 7) |
| 1961 | mcs_encoding = 0; |
| 1962 | else if (mcs == 8) |
| 1963 | mcs_encoding = 1; |
| 1964 | else |
| 1965 | mcs_encoding = 2; |
| 1966 | |
| 1967 | /* find max_vht_nss for the given MCS */ |
| 1968 | for (i = 7; i >= 0; i--) { |
| 1969 | int supp = (map >> (2 * i)) & 3; |
| 1970 | |
| 1971 | if (supp == 3) |
| 1972 | continue; |
| 1973 | |
| 1974 | if (supp >= mcs_encoding) { |
| 1975 | max_vht_nss = i; |
| 1976 | break; |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | if (!(cap->supp_mcs.tx_mcs_map & |
| 1981 | cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE))) |
| 1982 | return max_vht_nss; |
| 1983 | |
| 1984 | ext_nss_bw = le32_get_bits(cap->vht_cap_info, |
| 1985 | IEEE80211_VHT_CAP_EXT_NSS_BW_MASK); |
| 1986 | supp_width = le32_get_bits(cap->vht_cap_info, |
| 1987 | IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK); |
| 1988 | |
| 1989 | /* if not capable, treat ext_nss_bw as 0 */ |
| 1990 | if (!ext_nss_bw_capable) |
| 1991 | ext_nss_bw = 0; |
| 1992 | |
| 1993 | /* This is invalid */ |
| 1994 | if (supp_width == 3) |
| 1995 | return 0; |
| 1996 | |
| 1997 | /* This is an invalid combination so pretend nothing is supported */ |
| 1998 | if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2)) |
| 1999 | return 0; |
| 2000 | |
| 2001 | /* |
| 2002 | * Cover all the special cases according to IEEE 802.11-2016 |
| 2003 | * Table 9-250. All other cases are either factor of 1 or not |
| 2004 | * valid/supported. |
| 2005 | */ |
| 2006 | switch (bw) { |
| 2007 | case IEEE80211_VHT_CHANWIDTH_USE_HT: |
| 2008 | case IEEE80211_VHT_CHANWIDTH_80MHZ: |
| 2009 | if ((supp_width == 1 || supp_width == 2) && |
| 2010 | ext_nss_bw == 3) |
| 2011 | return 2 * max_vht_nss; |
| 2012 | break; |
| 2013 | case IEEE80211_VHT_CHANWIDTH_160MHZ: |
| 2014 | if (supp_width == 0 && |
| 2015 | (ext_nss_bw == 1 || ext_nss_bw == 2)) |
| 2016 | return DIV_ROUND_UP(max_vht_nss, 2); |
| 2017 | if (supp_width == 0 && |
| 2018 | ext_nss_bw == 3) |
| 2019 | return DIV_ROUND_UP(3 * max_vht_nss, 4); |
| 2020 | if (supp_width == 1 && |
| 2021 | ext_nss_bw == 3) |
| 2022 | return 2 * max_vht_nss; |
| 2023 | break; |
| 2024 | case IEEE80211_VHT_CHANWIDTH_80P80MHZ: |
| 2025 | if (supp_width == 0 && |
| 2026 | (ext_nss_bw == 1 || ext_nss_bw == 2)) |
| 2027 | return 0; /* not possible */ |
| 2028 | if (supp_width == 0 && |
| 2029 | ext_nss_bw == 2) |
| 2030 | return DIV_ROUND_UP(max_vht_nss, 2); |
| 2031 | if (supp_width == 0 && |
| 2032 | ext_nss_bw == 3) |
| 2033 | return DIV_ROUND_UP(3 * max_vht_nss, 4); |
| 2034 | if (supp_width == 1 && |
| 2035 | ext_nss_bw == 0) |
| 2036 | return 0; /* not possible */ |
| 2037 | if (supp_width == 1 && |
| 2038 | ext_nss_bw == 1) |
| 2039 | return DIV_ROUND_UP(max_vht_nss, 2); |
| 2040 | if (supp_width == 1 && |
| 2041 | ext_nss_bw == 2) |
| 2042 | return DIV_ROUND_UP(3 * max_vht_nss, 4); |
| 2043 | break; |
| 2044 | } |
| 2045 | |
| 2046 | /* not covered or invalid combination received */ |
| 2047 | return max_vht_nss; |
| 2048 | } |
| 2049 | EXPORT_SYMBOL(ieee80211_get_vht_max_nss); |