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