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