util.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Wireless utility functions
  4. *
  5. * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright 2017 Intel Deutschland GmbH
  8. * Copyright (C) 2018-2023 Intel Corporation
  9. */
  10. #include <linux/export.h>
  11. #include <linux/bitops.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/slab.h>
  14. #include <linux/ieee80211.h>
  15. #include <net/cfg80211.h>
  16. #include <net/ip.h>
  17. #include <net/dsfield.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/mpls.h>
  20. #include <linux/gcd.h>
  21. #include <linux/bitfield.h>
  22. #include <linux/nospec.h>
  23. #include "core.h"
  24. #include "rdev-ops.h"
  25. const struct ieee80211_rate *
  26. ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
  27. u32 basic_rates, int bitrate)
  28. {
  29. struct ieee80211_rate *result = &sband->bitrates[0];
  30. int i;
  31. for (i = 0; i < sband->n_bitrates; i++) {
  32. if (!(basic_rates & BIT(i)))
  33. continue;
  34. if (sband->bitrates[i].bitrate > bitrate)
  35. continue;
  36. result = &sband->bitrates[i];
  37. }
  38. return result;
  39. }
  40. EXPORT_SYMBOL(ieee80211_get_response_rate);
  41. u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband)
  42. {
  43. struct ieee80211_rate *bitrates;
  44. u32 mandatory_rates = 0;
  45. enum ieee80211_rate_flags mandatory_flag;
  46. int i;
  47. if (WARN_ON(!sband))
  48. return 1;
  49. if (sband->band == NL80211_BAND_2GHZ)
  50. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  51. else
  52. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  53. bitrates = sband->bitrates;
  54. for (i = 0; i < sband->n_bitrates; i++)
  55. if (bitrates[i].flags & mandatory_flag)
  56. mandatory_rates |= BIT(i);
  57. return mandatory_rates;
  58. }
  59. EXPORT_SYMBOL(ieee80211_mandatory_rates);
  60. u32 ieee80211_channel_to_freq_khz(int chan, enum nl80211_band band)
  61. {
  62. /* see 802.11 17.3.8.3.2 and Annex J
  63. * there are overlapping channel numbers in 5GHz and 2GHz bands */
  64. if (chan <= 0)
  65. return 0; /* not supported */
  66. switch (band) {
  67. case NL80211_BAND_2GHZ:
  68. case NL80211_BAND_LC:
  69. if (chan == 14)
  70. return MHZ_TO_KHZ(2484);
  71. else if (chan < 14)
  72. return MHZ_TO_KHZ(2407 + chan * 5);
  73. break;
  74. case NL80211_BAND_5GHZ:
  75. if (chan >= 182 && chan <= 196)
  76. return MHZ_TO_KHZ(4000 + chan * 5);
  77. else
  78. return MHZ_TO_KHZ(5000 + chan * 5);
  79. break;
  80. case NL80211_BAND_6GHZ:
  81. /* see 802.11ax D6.1 27.3.23.2 */
  82. if (chan == 2)
  83. return MHZ_TO_KHZ(5935);
  84. if (chan <= 233)
  85. return MHZ_TO_KHZ(5950 + chan * 5);
  86. break;
  87. case NL80211_BAND_60GHZ:
  88. if (chan < 7)
  89. return MHZ_TO_KHZ(56160 + chan * 2160);
  90. break;
  91. case NL80211_BAND_S1GHZ:
  92. return 902000 + chan * 500;
  93. default:
  94. ;
  95. }
  96. return 0; /* not supported */
  97. }
  98. EXPORT_SYMBOL(ieee80211_channel_to_freq_khz);
  99. enum nl80211_chan_width
  100. ieee80211_s1g_channel_width(const struct ieee80211_channel *chan)
  101. {
  102. if (WARN_ON(!chan || chan->band != NL80211_BAND_S1GHZ))
  103. return NL80211_CHAN_WIDTH_20_NOHT;
  104. /*S1G defines a single allowed channel width per channel.
  105. * Extract that width here.
  106. */
  107. if (chan->flags & IEEE80211_CHAN_1MHZ)
  108. return NL80211_CHAN_WIDTH_1;
  109. else if (chan->flags & IEEE80211_CHAN_2MHZ)
  110. return NL80211_CHAN_WIDTH_2;
  111. else if (chan->flags & IEEE80211_CHAN_4MHZ)
  112. return NL80211_CHAN_WIDTH_4;
  113. else if (chan->flags & IEEE80211_CHAN_8MHZ)
  114. return NL80211_CHAN_WIDTH_8;
  115. else if (chan->flags & IEEE80211_CHAN_16MHZ)
  116. return NL80211_CHAN_WIDTH_16;
  117. pr_err("unknown channel width for channel at %dKHz?\n",
  118. ieee80211_channel_to_khz(chan));
  119. return NL80211_CHAN_WIDTH_1;
  120. }
  121. EXPORT_SYMBOL(ieee80211_s1g_channel_width);
  122. int ieee80211_freq_khz_to_channel(u32 freq)
  123. {
  124. /* TODO: just handle MHz for now */
  125. freq = KHZ_TO_MHZ(freq);
  126. /* see 802.11 17.3.8.3.2 and Annex J */
  127. if (freq == 2484)
  128. return 14;
  129. else if (freq < 2484)
  130. return (freq - 2407) / 5;
  131. else if (freq >= 4910 && freq <= 4980)
  132. return (freq - 4000) / 5;
  133. else if (freq < 5925)
  134. return (freq - 5000) / 5;
  135. else if (freq == 5935)
  136. return 2;
  137. else if (freq <= 45000) /* DMG band lower limit */
  138. /* see 802.11ax D6.1 27.3.22.2 */
  139. return (freq - 5950) / 5;
  140. else if (freq >= 58320 && freq <= 70200)
  141. return (freq - 56160) / 2160;
  142. else
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(ieee80211_freq_khz_to_channel);
  146. struct ieee80211_channel *ieee80211_get_channel_khz(struct wiphy *wiphy,
  147. u32 freq)
  148. {
  149. enum nl80211_band band;
  150. struct ieee80211_supported_band *sband;
  151. int i;
  152. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  153. sband = wiphy->bands[band];
  154. if (!sband)
  155. continue;
  156. for (i = 0; i < sband->n_channels; i++) {
  157. struct ieee80211_channel *chan = &sband->channels[i];
  158. if (ieee80211_channel_to_khz(chan) == freq)
  159. return chan;
  160. }
  161. }
  162. return NULL;
  163. }
  164. EXPORT_SYMBOL(ieee80211_get_channel_khz);
  165. static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
  166. {
  167. int i, want;
  168. switch (sband->band) {
  169. case NL80211_BAND_5GHZ:
  170. case NL80211_BAND_6GHZ:
  171. want = 3;
  172. for (i = 0; i < sband->n_bitrates; i++) {
  173. if (sband->bitrates[i].bitrate == 60 ||
  174. sband->bitrates[i].bitrate == 120 ||
  175. sband->bitrates[i].bitrate == 240) {
  176. sband->bitrates[i].flags |=
  177. IEEE80211_RATE_MANDATORY_A;
  178. want--;
  179. }
  180. }
  181. WARN_ON(want);
  182. break;
  183. case NL80211_BAND_2GHZ:
  184. case NL80211_BAND_LC:
  185. want = 7;
  186. for (i = 0; i < sband->n_bitrates; i++) {
  187. switch (sband->bitrates[i].bitrate) {
  188. case 10:
  189. case 20:
  190. case 55:
  191. case 110:
  192. sband->bitrates[i].flags |=
  193. IEEE80211_RATE_MANDATORY_B |
  194. IEEE80211_RATE_MANDATORY_G;
  195. want--;
  196. break;
  197. case 60:
  198. case 120:
  199. case 240:
  200. sband->bitrates[i].flags |=
  201. IEEE80211_RATE_MANDATORY_G;
  202. want--;
  203. fallthrough;
  204. default:
  205. sband->bitrates[i].flags |=
  206. IEEE80211_RATE_ERP_G;
  207. break;
  208. }
  209. }
  210. WARN_ON(want != 0 && want != 3);
  211. break;
  212. case NL80211_BAND_60GHZ:
  213. /* check for mandatory HT MCS 1..4 */
  214. WARN_ON(!sband->ht_cap.ht_supported);
  215. WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
  216. break;
  217. case NL80211_BAND_S1GHZ:
  218. /* Figure 9-589bd: 3 means unsupported, so != 3 means at least
  219. * mandatory is ok.
  220. */
  221. WARN_ON((sband->s1g_cap.nss_mcs[0] & 0x3) == 0x3);
  222. break;
  223. case NUM_NL80211_BANDS:
  224. default:
  225. WARN_ON(1);
  226. break;
  227. }
  228. }
  229. void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
  230. {
  231. enum nl80211_band band;
  232. for (band = 0; band < NUM_NL80211_BANDS; band++)
  233. if (wiphy->bands[band])
  234. set_mandatory_flags_band(wiphy->bands[band]);
  235. }
  236. bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
  237. {
  238. int i;
  239. for (i = 0; i < wiphy->n_cipher_suites; i++)
  240. if (cipher == wiphy->cipher_suites[i])
  241. return true;
  242. return false;
  243. }
  244. static bool
  245. cfg80211_igtk_cipher_supported(struct cfg80211_registered_device *rdev)
  246. {
  247. struct wiphy *wiphy = &rdev->wiphy;
  248. int i;
  249. for (i = 0; i < wiphy->n_cipher_suites; i++) {
  250. switch (wiphy->cipher_suites[i]) {
  251. case WLAN_CIPHER_SUITE_AES_CMAC:
  252. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  253. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  254. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  255. return true;
  256. }
  257. }
  258. return false;
  259. }
  260. bool cfg80211_valid_key_idx(struct cfg80211_registered_device *rdev,
  261. int key_idx, bool pairwise)
  262. {
  263. int max_key_idx;
  264. if (pairwise)
  265. max_key_idx = 3;
  266. else if (wiphy_ext_feature_isset(&rdev->wiphy,
  267. NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
  268. wiphy_ext_feature_isset(&rdev->wiphy,
  269. NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
  270. max_key_idx = 7;
  271. else if (cfg80211_igtk_cipher_supported(rdev))
  272. max_key_idx = 5;
  273. else
  274. max_key_idx = 3;
  275. if (key_idx < 0 || key_idx > max_key_idx)
  276. return false;
  277. return true;
  278. }
  279. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  280. struct key_params *params, int key_idx,
  281. bool pairwise, const u8 *mac_addr)
  282. {
  283. if (!cfg80211_valid_key_idx(rdev, key_idx, pairwise))
  284. return -EINVAL;
  285. if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
  286. return -EINVAL;
  287. if (pairwise && !mac_addr)
  288. return -EINVAL;
  289. switch (params->cipher) {
  290. case WLAN_CIPHER_SUITE_TKIP:
  291. /* Extended Key ID can only be used with CCMP/GCMP ciphers */
  292. if ((pairwise && key_idx) ||
  293. params->mode != NL80211_KEY_RX_TX)
  294. return -EINVAL;
  295. break;
  296. case WLAN_CIPHER_SUITE_CCMP:
  297. case WLAN_CIPHER_SUITE_CCMP_256:
  298. case WLAN_CIPHER_SUITE_GCMP:
  299. case WLAN_CIPHER_SUITE_GCMP_256:
  300. /* IEEE802.11-2016 allows only 0 and - when supporting
  301. * Extended Key ID - 1 as index for pairwise keys.
  302. * @NL80211_KEY_NO_TX is only allowed for pairwise keys when
  303. * the driver supports Extended Key ID.
  304. * @NL80211_KEY_SET_TX can't be set when installing and
  305. * validating a key.
  306. */
  307. if ((params->mode == NL80211_KEY_NO_TX && !pairwise) ||
  308. params->mode == NL80211_KEY_SET_TX)
  309. return -EINVAL;
  310. if (wiphy_ext_feature_isset(&rdev->wiphy,
  311. NL80211_EXT_FEATURE_EXT_KEY_ID)) {
  312. if (pairwise && (key_idx < 0 || key_idx > 1))
  313. return -EINVAL;
  314. } else if (pairwise && key_idx) {
  315. return -EINVAL;
  316. }
  317. break;
  318. case WLAN_CIPHER_SUITE_AES_CMAC:
  319. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  320. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  321. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  322. /* Disallow BIP (group-only) cipher as pairwise cipher */
  323. if (pairwise)
  324. return -EINVAL;
  325. if (key_idx < 4)
  326. return -EINVAL;
  327. break;
  328. case WLAN_CIPHER_SUITE_WEP40:
  329. case WLAN_CIPHER_SUITE_WEP104:
  330. if (key_idx > 3)
  331. return -EINVAL;
  332. break;
  333. default:
  334. break;
  335. }
  336. switch (params->cipher) {
  337. case WLAN_CIPHER_SUITE_WEP40:
  338. if (params->key_len != WLAN_KEY_LEN_WEP40)
  339. return -EINVAL;
  340. break;
  341. case WLAN_CIPHER_SUITE_TKIP:
  342. if (params->key_len != WLAN_KEY_LEN_TKIP)
  343. return -EINVAL;
  344. break;
  345. case WLAN_CIPHER_SUITE_CCMP:
  346. if (params->key_len != WLAN_KEY_LEN_CCMP)
  347. return -EINVAL;
  348. break;
  349. case WLAN_CIPHER_SUITE_CCMP_256:
  350. if (params->key_len != WLAN_KEY_LEN_CCMP_256)
  351. return -EINVAL;
  352. break;
  353. case WLAN_CIPHER_SUITE_GCMP:
  354. if (params->key_len != WLAN_KEY_LEN_GCMP)
  355. return -EINVAL;
  356. break;
  357. case WLAN_CIPHER_SUITE_GCMP_256:
  358. if (params->key_len != WLAN_KEY_LEN_GCMP_256)
  359. return -EINVAL;
  360. break;
  361. case WLAN_CIPHER_SUITE_WEP104:
  362. if (params->key_len != WLAN_KEY_LEN_WEP104)
  363. return -EINVAL;
  364. break;
  365. case WLAN_CIPHER_SUITE_AES_CMAC:
  366. if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
  367. return -EINVAL;
  368. break;
  369. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  370. if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
  371. return -EINVAL;
  372. break;
  373. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  374. if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
  375. return -EINVAL;
  376. break;
  377. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  378. if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
  379. return -EINVAL;
  380. break;
  381. default:
  382. /*
  383. * We don't know anything about this algorithm,
  384. * allow using it -- but the driver must check
  385. * all parameters! We still check below whether
  386. * or not the driver supports this algorithm,
  387. * of course.
  388. */
  389. break;
  390. }
  391. if (params->seq) {
  392. switch (params->cipher) {
  393. case WLAN_CIPHER_SUITE_WEP40:
  394. case WLAN_CIPHER_SUITE_WEP104:
  395. /* These ciphers do not use key sequence */
  396. return -EINVAL;
  397. case WLAN_CIPHER_SUITE_TKIP:
  398. case WLAN_CIPHER_SUITE_CCMP:
  399. case WLAN_CIPHER_SUITE_CCMP_256:
  400. case WLAN_CIPHER_SUITE_GCMP:
  401. case WLAN_CIPHER_SUITE_GCMP_256:
  402. case WLAN_CIPHER_SUITE_AES_CMAC:
  403. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  404. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  405. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  406. if (params->seq_len != 6)
  407. return -EINVAL;
  408. break;
  409. }
  410. }
  411. if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
  412. return -EINVAL;
  413. return 0;
  414. }
  415. unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
  416. {
  417. unsigned int hdrlen = 24;
  418. if (ieee80211_is_ext(fc)) {
  419. hdrlen = 4;
  420. goto out;
  421. }
  422. if (ieee80211_is_data(fc)) {
  423. if (ieee80211_has_a4(fc))
  424. hdrlen = 30;
  425. if (ieee80211_is_data_qos(fc)) {
  426. hdrlen += IEEE80211_QOS_CTL_LEN;
  427. if (ieee80211_has_order(fc))
  428. hdrlen += IEEE80211_HT_CTL_LEN;
  429. }
  430. goto out;
  431. }
  432. if (ieee80211_is_mgmt(fc)) {
  433. if (ieee80211_has_order(fc))
  434. hdrlen += IEEE80211_HT_CTL_LEN;
  435. goto out;
  436. }
  437. if (ieee80211_is_ctl(fc)) {
  438. /*
  439. * ACK and CTS are 10 bytes, all others 16. To see how
  440. * to get this condition consider
  441. * subtype mask: 0b0000000011110000 (0x00F0)
  442. * ACK subtype: 0b0000000011010000 (0x00D0)
  443. * CTS subtype: 0b0000000011000000 (0x00C0)
  444. * bits that matter: ^^^ (0x00E0)
  445. * value of those: 0b0000000011000000 (0x00C0)
  446. */
  447. if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
  448. hdrlen = 10;
  449. else
  450. hdrlen = 16;
  451. }
  452. out:
  453. return hdrlen;
  454. }
  455. EXPORT_SYMBOL(ieee80211_hdrlen);
  456. unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  457. {
  458. const struct ieee80211_hdr *hdr =
  459. (const struct ieee80211_hdr *)skb->data;
  460. unsigned int hdrlen;
  461. if (unlikely(skb->len < 10))
  462. return 0;
  463. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  464. if (unlikely(hdrlen > skb->len))
  465. return 0;
  466. return hdrlen;
  467. }
  468. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  469. static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
  470. {
  471. int ae = flags & MESH_FLAGS_AE;
  472. /* 802.11-2012, 8.2.4.7.3 */
  473. switch (ae) {
  474. default:
  475. case 0:
  476. return 6;
  477. case MESH_FLAGS_AE_A4:
  478. return 12;
  479. case MESH_FLAGS_AE_A5_A6:
  480. return 18;
  481. }
  482. }
  483. unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
  484. {
  485. return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
  486. }
  487. EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
  488. bool ieee80211_get_8023_tunnel_proto(const void *hdr, __be16 *proto)
  489. {
  490. const __be16 *hdr_proto = hdr + ETH_ALEN;
  491. if (!(ether_addr_equal(hdr, rfc1042_header) &&
  492. *hdr_proto != htons(ETH_P_AARP) &&
  493. *hdr_proto != htons(ETH_P_IPX)) &&
  494. !ether_addr_equal(hdr, bridge_tunnel_header))
  495. return false;
  496. *proto = *hdr_proto;
  497. return true;
  498. }
  499. EXPORT_SYMBOL(ieee80211_get_8023_tunnel_proto);
  500. int ieee80211_strip_8023_mesh_hdr(struct sk_buff *skb)
  501. {
  502. const void *mesh_addr;
  503. struct {
  504. struct ethhdr eth;
  505. u8 flags;
  506. } payload;
  507. int hdrlen;
  508. int ret;
  509. ret = skb_copy_bits(skb, 0, &payload, sizeof(payload));
  510. if (ret)
  511. return ret;
  512. hdrlen = sizeof(payload.eth) + __ieee80211_get_mesh_hdrlen(payload.flags);
  513. if (likely(pskb_may_pull(skb, hdrlen + 8) &&
  514. ieee80211_get_8023_tunnel_proto(skb->data + hdrlen,
  515. &payload.eth.h_proto)))
  516. hdrlen += ETH_ALEN + 2;
  517. else if (!pskb_may_pull(skb, hdrlen))
  518. return -EINVAL;
  519. else
  520. payload.eth.h_proto = htons(skb->len - hdrlen);
  521. mesh_addr = skb->data + sizeof(payload.eth) + ETH_ALEN;
  522. switch (payload.flags & MESH_FLAGS_AE) {
  523. case MESH_FLAGS_AE_A4:
  524. memcpy(&payload.eth.h_source, mesh_addr, ETH_ALEN);
  525. break;
  526. case MESH_FLAGS_AE_A5_A6:
  527. memcpy(&payload.eth, mesh_addr, 2 * ETH_ALEN);
  528. break;
  529. default:
  530. break;
  531. }
  532. pskb_pull(skb, hdrlen - sizeof(payload.eth));
  533. memcpy(skb->data, &payload.eth, sizeof(payload.eth));
  534. return 0;
  535. }
  536. EXPORT_SYMBOL(ieee80211_strip_8023_mesh_hdr);
  537. int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
  538. const u8 *addr, enum nl80211_iftype iftype,
  539. u8 data_offset, bool is_amsdu)
  540. {
  541. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  542. struct {
  543. u8 hdr[ETH_ALEN] __aligned(2);
  544. __be16 proto;
  545. } payload;
  546. struct ethhdr tmp;
  547. u16 hdrlen;
  548. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  549. return -1;
  550. hdrlen = ieee80211_hdrlen(hdr->frame_control) + data_offset;
  551. if (skb->len < hdrlen)
  552. return -1;
  553. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  554. * header
  555. * IEEE 802.11 address fields:
  556. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  557. * 0 0 DA SA BSSID n/a
  558. * 0 1 DA BSSID SA n/a
  559. * 1 0 BSSID SA DA n/a
  560. * 1 1 RA TA DA SA
  561. */
  562. memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
  563. memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
  564. switch (hdr->frame_control &
  565. cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  566. case cpu_to_le16(IEEE80211_FCTL_TODS):
  567. if (unlikely(iftype != NL80211_IFTYPE_AP &&
  568. iftype != NL80211_IFTYPE_AP_VLAN &&
  569. iftype != NL80211_IFTYPE_P2P_GO))
  570. return -1;
  571. break;
  572. case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  573. if (unlikely(iftype != NL80211_IFTYPE_MESH_POINT &&
  574. iftype != NL80211_IFTYPE_AP_VLAN &&
  575. iftype != NL80211_IFTYPE_STATION))
  576. return -1;
  577. break;
  578. case cpu_to_le16(IEEE80211_FCTL_FROMDS):
  579. if ((iftype != NL80211_IFTYPE_STATION &&
  580. iftype != NL80211_IFTYPE_P2P_CLIENT &&
  581. iftype != NL80211_IFTYPE_MESH_POINT) ||
  582. (is_multicast_ether_addr(tmp.h_dest) &&
  583. ether_addr_equal(tmp.h_source, addr)))
  584. return -1;
  585. break;
  586. case cpu_to_le16(0):
  587. if (iftype != NL80211_IFTYPE_ADHOC &&
  588. iftype != NL80211_IFTYPE_STATION &&
  589. iftype != NL80211_IFTYPE_OCB)
  590. return -1;
  591. break;
  592. }
  593. if (likely(!is_amsdu && iftype != NL80211_IFTYPE_MESH_POINT &&
  594. skb_copy_bits(skb, hdrlen, &payload, sizeof(payload)) == 0 &&
  595. ieee80211_get_8023_tunnel_proto(&payload, &tmp.h_proto))) {
  596. /* remove RFC1042 or Bridge-Tunnel encapsulation */
  597. hdrlen += ETH_ALEN + 2;
  598. skb_postpull_rcsum(skb, &payload, ETH_ALEN + 2);
  599. } else {
  600. tmp.h_proto = htons(skb->len - hdrlen);
  601. }
  602. pskb_pull(skb, hdrlen);
  603. if (!ehdr)
  604. ehdr = skb_push(skb, sizeof(struct ethhdr));
  605. memcpy(ehdr, &tmp, sizeof(tmp));
  606. return 0;
  607. }
  608. EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
  609. static void
  610. __frame_add_frag(struct sk_buff *skb, struct page *page,
  611. void *ptr, int len, int size)
  612. {
  613. struct skb_shared_info *sh = skb_shinfo(skb);
  614. int page_offset;
  615. get_page(page);
  616. page_offset = ptr - page_address(page);
  617. skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
  618. }
  619. static void
  620. __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
  621. int offset, int len)
  622. {
  623. struct skb_shared_info *sh = skb_shinfo(skb);
  624. const skb_frag_t *frag = &sh->frags[0];
  625. struct page *frag_page;
  626. void *frag_ptr;
  627. int frag_len, frag_size;
  628. int head_size = skb->len - skb->data_len;
  629. int cur_len;
  630. frag_page = virt_to_head_page(skb->head);
  631. frag_ptr = skb->data;
  632. frag_size = head_size;
  633. while (offset >= frag_size) {
  634. offset -= frag_size;
  635. frag_page = skb_frag_page(frag);
  636. frag_ptr = skb_frag_address(frag);
  637. frag_size = skb_frag_size(frag);
  638. frag++;
  639. }
  640. frag_ptr += offset;
  641. frag_len = frag_size - offset;
  642. cur_len = min(len, frag_len);
  643. __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
  644. len -= cur_len;
  645. while (len > 0) {
  646. frag_len = skb_frag_size(frag);
  647. cur_len = min(len, frag_len);
  648. __frame_add_frag(frame, skb_frag_page(frag),
  649. skb_frag_address(frag), cur_len, frag_len);
  650. len -= cur_len;
  651. frag++;
  652. }
  653. }
  654. static struct sk_buff *
  655. __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
  656. int offset, int len, bool reuse_frag,
  657. int min_len)
  658. {
  659. struct sk_buff *frame;
  660. int cur_len = len;
  661. if (skb->len - offset < len)
  662. return NULL;
  663. /*
  664. * When reusing framents, copy some data to the head to simplify
  665. * ethernet header handling and speed up protocol header processing
  666. * in the stack later.
  667. */
  668. if (reuse_frag)
  669. cur_len = min_t(int, len, min_len);
  670. /*
  671. * Allocate and reserve two bytes more for payload
  672. * alignment since sizeof(struct ethhdr) is 14.
  673. */
  674. frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
  675. if (!frame)
  676. return NULL;
  677. frame->priority = skb->priority;
  678. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  679. skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
  680. len -= cur_len;
  681. if (!len)
  682. return frame;
  683. offset += cur_len;
  684. __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
  685. return frame;
  686. }
  687. static u16
  688. ieee80211_amsdu_subframe_length(void *field, u8 mesh_flags, u8 hdr_type)
  689. {
  690. __le16 *field_le = field;
  691. __be16 *field_be = field;
  692. u16 len;
  693. if (hdr_type >= 2)
  694. len = le16_to_cpu(*field_le);
  695. else
  696. len = be16_to_cpu(*field_be);
  697. if (hdr_type)
  698. len += __ieee80211_get_mesh_hdrlen(mesh_flags);
  699. return len;
  700. }
  701. bool ieee80211_is_valid_amsdu(struct sk_buff *skb, u8 mesh_hdr)
  702. {
  703. int offset = 0, subframe_len, padding;
  704. for (offset = 0; offset < skb->len; offset += subframe_len + padding) {
  705. int remaining = skb->len - offset;
  706. struct {
  707. __be16 len;
  708. u8 mesh_flags;
  709. } hdr;
  710. u16 len;
  711. if (sizeof(hdr) > remaining)
  712. return false;
  713. if (skb_copy_bits(skb, offset + 2 * ETH_ALEN, &hdr, sizeof(hdr)) < 0)
  714. return false;
  715. len = ieee80211_amsdu_subframe_length(&hdr.len, hdr.mesh_flags,
  716. mesh_hdr);
  717. subframe_len = sizeof(struct ethhdr) + len;
  718. padding = (4 - subframe_len) & 0x3;
  719. if (subframe_len > remaining)
  720. return false;
  721. }
  722. return true;
  723. }
  724. EXPORT_SYMBOL(ieee80211_is_valid_amsdu);
  725. /*
  726. * Detects if an MSDU frame was maliciously converted into an A-MSDU
  727. * frame by an adversary. This is done by parsing the received frame
  728. * as if it were a regular MSDU, even though the A-MSDU flag is set.
  729. *
  730. * For non-mesh interfaces, detection involves checking whether the
  731. * payload, when interpreted as an MSDU, begins with a valid RFC1042
  732. * header. This is done by comparing the A-MSDU subheader's destination
  733. * address to the start of the RFC1042 header.
  734. *
  735. * For mesh interfaces, the MSDU includes a 6-byte Mesh Control field
  736. * and an optional variable-length Mesh Address Extension field before
  737. * the RFC1042 header. The position of the RFC1042 header must therefore
  738. * be calculated based on the mesh header length.
  739. *
  740. * Since this function intentionally parses an A-MSDU frame as an MSDU,
  741. * it only assumes that the A-MSDU subframe header is present, and
  742. * beyond this it performs its own bounds checks under the assumption
  743. * that the frame is instead parsed as a non-aggregated MSDU.
  744. */
  745. static bool
  746. is_amsdu_aggregation_attack(struct ethhdr *eth, struct sk_buff *skb,
  747. enum nl80211_iftype iftype)
  748. {
  749. int offset;
  750. /* Non-mesh case can be directly compared */
  751. if (iftype != NL80211_IFTYPE_MESH_POINT)
  752. return ether_addr_equal(eth->h_dest, rfc1042_header);
  753. offset = __ieee80211_get_mesh_hdrlen(eth->h_dest[0]);
  754. if (offset == 6) {
  755. /* Mesh case with empty address extension field */
  756. return ether_addr_equal(eth->h_source, rfc1042_header);
  757. } else if (offset + ETH_ALEN <= skb->len) {
  758. /* Mesh case with non-empty address extension field */
  759. u8 temp[ETH_ALEN];
  760. skb_copy_bits(skb, offset, temp, ETH_ALEN);
  761. return ether_addr_equal(temp, rfc1042_header);
  762. }
  763. return false;
  764. }
  765. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  766. const u8 *addr, enum nl80211_iftype iftype,
  767. const unsigned int extra_headroom,
  768. const u8 *check_da, const u8 *check_sa,
  769. u8 mesh_control)
  770. {
  771. unsigned int hlen = ALIGN(extra_headroom, 4);
  772. struct sk_buff *frame = NULL;
  773. int offset = 0;
  774. struct {
  775. struct ethhdr eth;
  776. uint8_t flags;
  777. } hdr;
  778. bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
  779. bool reuse_skb = false;
  780. bool last = false;
  781. int copy_len = sizeof(hdr.eth);
  782. if (iftype == NL80211_IFTYPE_MESH_POINT)
  783. copy_len = sizeof(hdr);
  784. while (!last) {
  785. int remaining = skb->len - offset;
  786. unsigned int subframe_len;
  787. int len, mesh_len = 0;
  788. u8 padding;
  789. if (copy_len > remaining)
  790. goto purge;
  791. skb_copy_bits(skb, offset, &hdr, copy_len);
  792. if (iftype == NL80211_IFTYPE_MESH_POINT)
  793. mesh_len = __ieee80211_get_mesh_hdrlen(hdr.flags);
  794. len = ieee80211_amsdu_subframe_length(&hdr.eth.h_proto, hdr.flags,
  795. mesh_control);
  796. subframe_len = sizeof(struct ethhdr) + len;
  797. padding = (4 - subframe_len) & 0x3;
  798. /* the last MSDU has no padding */
  799. if (subframe_len > remaining)
  800. goto purge;
  801. /* mitigate A-MSDU aggregation injection attacks, to be
  802. * checked when processing first subframe (offset == 0).
  803. */
  804. if (offset == 0 && is_amsdu_aggregation_attack(&hdr.eth, skb, iftype))
  805. goto purge;
  806. offset += sizeof(struct ethhdr);
  807. last = remaining <= subframe_len + padding;
  808. /* FIXME: should we really accept multicast DA? */
  809. if ((check_da && !is_multicast_ether_addr(hdr.eth.h_dest) &&
  810. !ether_addr_equal(check_da, hdr.eth.h_dest)) ||
  811. (check_sa && !ether_addr_equal(check_sa, hdr.eth.h_source))) {
  812. offset += len + padding;
  813. continue;
  814. }
  815. /* reuse skb for the last subframe */
  816. if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
  817. skb_pull(skb, offset);
  818. frame = skb;
  819. reuse_skb = true;
  820. } else {
  821. frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
  822. reuse_frag, 32 + mesh_len);
  823. if (!frame)
  824. goto purge;
  825. offset += len + padding;
  826. }
  827. skb_reset_network_header(frame);
  828. frame->dev = skb->dev;
  829. frame->priority = skb->priority;
  830. if (likely(iftype != NL80211_IFTYPE_MESH_POINT &&
  831. ieee80211_get_8023_tunnel_proto(frame->data, &hdr.eth.h_proto)))
  832. skb_pull(frame, ETH_ALEN + 2);
  833. memcpy(skb_push(frame, sizeof(hdr.eth)), &hdr.eth, sizeof(hdr.eth));
  834. __skb_queue_tail(list, frame);
  835. }
  836. if (!reuse_skb)
  837. dev_kfree_skb(skb);
  838. return;
  839. purge:
  840. __skb_queue_purge(list);
  841. dev_kfree_skb(skb);
  842. }
  843. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  844. /* Given a data frame determine the 802.1p/1d tag to use. */
  845. unsigned int cfg80211_classify8021d(struct sk_buff *skb,
  846. struct cfg80211_qos_map *qos_map)
  847. {
  848. unsigned int dscp;
  849. unsigned char vlan_priority;
  850. unsigned int ret;
  851. /* skb->priority values from 256->263 are magic values to
  852. * directly indicate a specific 802.1d priority. This is used
  853. * to allow 802.1d priority to be passed directly in from VLAN
  854. * tags, etc.
  855. */
  856. if (skb->priority >= 256 && skb->priority <= 263) {
  857. ret = skb->priority - 256;
  858. goto out;
  859. }
  860. if (skb_vlan_tag_present(skb)) {
  861. vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
  862. >> VLAN_PRIO_SHIFT;
  863. if (vlan_priority > 0) {
  864. ret = vlan_priority;
  865. goto out;
  866. }
  867. }
  868. switch (skb->protocol) {
  869. case htons(ETH_P_IP):
  870. dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
  871. break;
  872. case htons(ETH_P_IPV6):
  873. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
  874. break;
  875. case htons(ETH_P_MPLS_UC):
  876. case htons(ETH_P_MPLS_MC): {
  877. struct mpls_label mpls_tmp, *mpls;
  878. mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
  879. sizeof(*mpls), &mpls_tmp);
  880. if (!mpls)
  881. return 0;
  882. ret = (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
  883. >> MPLS_LS_TC_SHIFT;
  884. goto out;
  885. }
  886. case htons(ETH_P_80221):
  887. /* 802.21 is always network control traffic */
  888. return 7;
  889. default:
  890. return 0;
  891. }
  892. if (qos_map) {
  893. unsigned int i, tmp_dscp = dscp >> 2;
  894. for (i = 0; i < qos_map->num_des; i++) {
  895. if (tmp_dscp == qos_map->dscp_exception[i].dscp) {
  896. ret = qos_map->dscp_exception[i].up;
  897. goto out;
  898. }
  899. }
  900. for (i = 0; i < 8; i++) {
  901. if (tmp_dscp >= qos_map->up[i].low &&
  902. tmp_dscp <= qos_map->up[i].high) {
  903. ret = i;
  904. goto out;
  905. }
  906. }
  907. }
  908. /* The default mapping as defined Section 2.3 in RFC8325: The three
  909. * Most Significant Bits (MSBs) of the DSCP are used as the
  910. * corresponding L2 markings.
  911. */
  912. ret = dscp >> 5;
  913. /* Handle specific DSCP values for which the default mapping (as
  914. * described above) doesn't adhere to the intended usage of the DSCP
  915. * value. See section 4 in RFC8325. Specifically, for the following
  916. * Diffserv Service Classes no update is needed:
  917. * - Standard: DF
  918. * - Low Priority Data: CS1
  919. * - Multimedia Conferencing: AF41, AF42, AF43
  920. * - Network Control Traffic: CS7
  921. * - Real-Time Interactive: CS4
  922. * - Signaling: CS5
  923. */
  924. switch (dscp >> 2) {
  925. case 10:
  926. case 12:
  927. case 14:
  928. /* High throughput data: AF11, AF12, AF13 */
  929. ret = 0;
  930. break;
  931. case 16:
  932. /* Operations, Administration, and Maintenance and Provisioning:
  933. * CS2
  934. */
  935. ret = 0;
  936. break;
  937. case 18:
  938. case 20:
  939. case 22:
  940. /* Low latency data: AF21, AF22, AF23 */
  941. ret = 3;
  942. break;
  943. case 24:
  944. /* Broadcasting video: CS3 */
  945. ret = 4;
  946. break;
  947. case 26:
  948. case 28:
  949. case 30:
  950. /* Multimedia Streaming: AF31, AF32, AF33 */
  951. ret = 4;
  952. break;
  953. case 44:
  954. /* Voice Admit: VA */
  955. ret = 6;
  956. break;
  957. case 46:
  958. /* Telephony traffic: EF */
  959. ret = 6;
  960. break;
  961. case 48:
  962. /* Network Control Traffic: CS6 */
  963. ret = 7;
  964. break;
  965. }
  966. out:
  967. return array_index_nospec(ret, IEEE80211_NUM_TIDS);
  968. }
  969. EXPORT_SYMBOL(cfg80211_classify8021d);
  970. const struct element *ieee80211_bss_get_elem(struct cfg80211_bss *bss, u8 id)
  971. {
  972. const struct cfg80211_bss_ies *ies;
  973. ies = rcu_dereference(bss->ies);
  974. if (!ies)
  975. return NULL;
  976. return cfg80211_find_elem(id, ies->data, ies->len);
  977. }
  978. EXPORT_SYMBOL(ieee80211_bss_get_elem);
  979. void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
  980. {
  981. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  982. struct net_device *dev = wdev->netdev;
  983. int i;
  984. if (!wdev->connect_keys)
  985. return;
  986. for (i = 0; i < 4; i++) {
  987. if (!wdev->connect_keys->params[i].cipher)
  988. continue;
  989. if (rdev_add_key(rdev, dev, -1, i, false, NULL,
  990. &wdev->connect_keys->params[i])) {
  991. netdev_err(dev, "failed to set key %d\n", i);
  992. continue;
  993. }
  994. if (wdev->connect_keys->def == i &&
  995. rdev_set_default_key(rdev, dev, -1, i, true, true)) {
  996. netdev_err(dev, "failed to set defkey %d\n", i);
  997. continue;
  998. }
  999. }
  1000. kfree_sensitive(wdev->connect_keys);
  1001. wdev->connect_keys = NULL;
  1002. }
  1003. void cfg80211_process_wdev_events(struct wireless_dev *wdev)
  1004. {
  1005. struct cfg80211_event *ev;
  1006. unsigned long flags;
  1007. spin_lock_irqsave(&wdev->event_lock, flags);
  1008. while (!list_empty(&wdev->event_list)) {
  1009. ev = list_first_entry(&wdev->event_list,
  1010. struct cfg80211_event, list);
  1011. list_del(&ev->list);
  1012. spin_unlock_irqrestore(&wdev->event_lock, flags);
  1013. switch (ev->type) {
  1014. case EVENT_CONNECT_RESULT:
  1015. __cfg80211_connect_result(
  1016. wdev->netdev,
  1017. &ev->cr,
  1018. ev->cr.status == WLAN_STATUS_SUCCESS);
  1019. break;
  1020. case EVENT_ROAMED:
  1021. __cfg80211_roamed(wdev, &ev->rm);
  1022. break;
  1023. case EVENT_DISCONNECTED:
  1024. __cfg80211_disconnected(wdev->netdev,
  1025. ev->dc.ie, ev->dc.ie_len,
  1026. ev->dc.reason,
  1027. !ev->dc.locally_generated);
  1028. break;
  1029. case EVENT_IBSS_JOINED:
  1030. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
  1031. ev->ij.channel);
  1032. break;
  1033. case EVENT_STOPPED:
  1034. cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
  1035. break;
  1036. case EVENT_PORT_AUTHORIZED:
  1037. __cfg80211_port_authorized(wdev, ev->pa.peer_addr,
  1038. ev->pa.td_bitmap,
  1039. ev->pa.td_bitmap_len);
  1040. break;
  1041. }
  1042. kfree(ev);
  1043. spin_lock_irqsave(&wdev->event_lock, flags);
  1044. }
  1045. spin_unlock_irqrestore(&wdev->event_lock, flags);
  1046. }
  1047. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
  1048. {
  1049. struct wireless_dev *wdev;
  1050. lockdep_assert_held(&rdev->wiphy.mtx);
  1051. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
  1052. cfg80211_process_wdev_events(wdev);
  1053. }
  1054. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  1055. struct net_device *dev, enum nl80211_iftype ntype,
  1056. struct vif_params *params)
  1057. {
  1058. int err;
  1059. enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
  1060. lockdep_assert_held(&rdev->wiphy.mtx);
  1061. /* don't support changing VLANs, you just re-create them */
  1062. if (otype == NL80211_IFTYPE_AP_VLAN)
  1063. return -EOPNOTSUPP;
  1064. /* cannot change into P2P device or NAN */
  1065. if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
  1066. ntype == NL80211_IFTYPE_NAN)
  1067. return -EOPNOTSUPP;
  1068. if (!rdev->ops->change_virtual_intf ||
  1069. !(rdev->wiphy.interface_modes & (1 << ntype)))
  1070. return -EOPNOTSUPP;
  1071. if (ntype != otype) {
  1072. /* if it's part of a bridge, reject changing type to station/ibss */
  1073. if (netif_is_bridge_port(dev) &&
  1074. (ntype == NL80211_IFTYPE_ADHOC ||
  1075. ntype == NL80211_IFTYPE_STATION ||
  1076. ntype == NL80211_IFTYPE_P2P_CLIENT))
  1077. return -EBUSY;
  1078. dev->ieee80211_ptr->use_4addr = false;
  1079. rdev_set_qos_map(rdev, dev, NULL);
  1080. switch (otype) {
  1081. case NL80211_IFTYPE_AP:
  1082. case NL80211_IFTYPE_P2P_GO:
  1083. cfg80211_stop_ap(rdev, dev, -1, true);
  1084. break;
  1085. case NL80211_IFTYPE_ADHOC:
  1086. cfg80211_leave_ibss(rdev, dev, false);
  1087. break;
  1088. case NL80211_IFTYPE_STATION:
  1089. case NL80211_IFTYPE_P2P_CLIENT:
  1090. cfg80211_disconnect(rdev, dev,
  1091. WLAN_REASON_DEAUTH_LEAVING, true);
  1092. break;
  1093. case NL80211_IFTYPE_MESH_POINT:
  1094. /* mesh should be handled? */
  1095. break;
  1096. case NL80211_IFTYPE_OCB:
  1097. cfg80211_leave_ocb(rdev, dev);
  1098. break;
  1099. default:
  1100. break;
  1101. }
  1102. cfg80211_process_rdev_events(rdev);
  1103. cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
  1104. memset(&dev->ieee80211_ptr->u, 0,
  1105. sizeof(dev->ieee80211_ptr->u));
  1106. memset(&dev->ieee80211_ptr->links, 0,
  1107. sizeof(dev->ieee80211_ptr->links));
  1108. }
  1109. err = rdev_change_virtual_intf(rdev, dev, ntype, params);
  1110. WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
  1111. if (!err && params && params->use_4addr != -1)
  1112. dev->ieee80211_ptr->use_4addr = params->use_4addr;
  1113. if (!err) {
  1114. dev->priv_flags &= ~IFF_DONT_BRIDGE;
  1115. switch (ntype) {
  1116. case NL80211_IFTYPE_STATION:
  1117. if (dev->ieee80211_ptr->use_4addr)
  1118. break;
  1119. fallthrough;
  1120. case NL80211_IFTYPE_OCB:
  1121. case NL80211_IFTYPE_P2P_CLIENT:
  1122. case NL80211_IFTYPE_ADHOC:
  1123. dev->priv_flags |= IFF_DONT_BRIDGE;
  1124. break;
  1125. case NL80211_IFTYPE_P2P_GO:
  1126. case NL80211_IFTYPE_AP:
  1127. case NL80211_IFTYPE_AP_VLAN:
  1128. case NL80211_IFTYPE_MESH_POINT:
  1129. /* bridging OK */
  1130. break;
  1131. case NL80211_IFTYPE_MONITOR:
  1132. /* monitor can't bridge anyway */
  1133. break;
  1134. case NL80211_IFTYPE_UNSPECIFIED:
  1135. case NUM_NL80211_IFTYPES:
  1136. /* not happening */
  1137. break;
  1138. case NL80211_IFTYPE_P2P_DEVICE:
  1139. case NL80211_IFTYPE_WDS:
  1140. case NL80211_IFTYPE_NAN:
  1141. WARN_ON(1);
  1142. break;
  1143. }
  1144. }
  1145. if (!err && ntype != otype && netif_running(dev)) {
  1146. cfg80211_update_iface_num(rdev, ntype, 1);
  1147. cfg80211_update_iface_num(rdev, otype, -1);
  1148. }
  1149. return err;
  1150. }
  1151. static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
  1152. {
  1153. int modulation, streams, bitrate;
  1154. /* the formula below does only work for MCS values smaller than 32 */
  1155. if (WARN_ON_ONCE(rate->mcs >= 32))
  1156. return 0;
  1157. modulation = rate->mcs & 7;
  1158. streams = (rate->mcs >> 3) + 1;
  1159. bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
  1160. if (modulation < 4)
  1161. bitrate *= (modulation + 1);
  1162. else if (modulation == 4)
  1163. bitrate *= (modulation + 2);
  1164. else
  1165. bitrate *= (modulation + 3);
  1166. bitrate *= streams;
  1167. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1168. bitrate = (bitrate / 9) * 10;
  1169. /* do NOT round down here */
  1170. return (bitrate + 50000) / 100000;
  1171. }
  1172. static u32 cfg80211_calculate_bitrate_dmg(struct rate_info *rate)
  1173. {
  1174. static const u32 __mcs2bitrate[] = {
  1175. /* control PHY */
  1176. [0] = 275,
  1177. /* SC PHY */
  1178. [1] = 3850,
  1179. [2] = 7700,
  1180. [3] = 9625,
  1181. [4] = 11550,
  1182. [5] = 12512, /* 1251.25 mbps */
  1183. [6] = 15400,
  1184. [7] = 19250,
  1185. [8] = 23100,
  1186. [9] = 25025,
  1187. [10] = 30800,
  1188. [11] = 38500,
  1189. [12] = 46200,
  1190. /* OFDM PHY */
  1191. [13] = 6930,
  1192. [14] = 8662, /* 866.25 mbps */
  1193. [15] = 13860,
  1194. [16] = 17325,
  1195. [17] = 20790,
  1196. [18] = 27720,
  1197. [19] = 34650,
  1198. [20] = 41580,
  1199. [21] = 45045,
  1200. [22] = 51975,
  1201. [23] = 62370,
  1202. [24] = 67568, /* 6756.75 mbps */
  1203. /* LP-SC PHY */
  1204. [25] = 6260,
  1205. [26] = 8340,
  1206. [27] = 11120,
  1207. [28] = 12510,
  1208. [29] = 16680,
  1209. [30] = 22240,
  1210. [31] = 25030,
  1211. };
  1212. if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
  1213. return 0;
  1214. return __mcs2bitrate[rate->mcs];
  1215. }
  1216. static u32 cfg80211_calculate_bitrate_extended_sc_dmg(struct rate_info *rate)
  1217. {
  1218. static const u32 __mcs2bitrate[] = {
  1219. [6 - 6] = 26950, /* MCS 9.1 : 2695.0 mbps */
  1220. [7 - 6] = 50050, /* MCS 12.1 */
  1221. [8 - 6] = 53900,
  1222. [9 - 6] = 57750,
  1223. [10 - 6] = 63900,
  1224. [11 - 6] = 75075,
  1225. [12 - 6] = 80850,
  1226. };
  1227. /* Extended SC MCS not defined for base MCS below 6 or above 12 */
  1228. if (WARN_ON_ONCE(rate->mcs < 6 || rate->mcs > 12))
  1229. return 0;
  1230. return __mcs2bitrate[rate->mcs - 6];
  1231. }
  1232. static u32 cfg80211_calculate_bitrate_edmg(struct rate_info *rate)
  1233. {
  1234. static const u32 __mcs2bitrate[] = {
  1235. /* control PHY */
  1236. [0] = 275,
  1237. /* SC PHY */
  1238. [1] = 3850,
  1239. [2] = 7700,
  1240. [3] = 9625,
  1241. [4] = 11550,
  1242. [5] = 12512, /* 1251.25 mbps */
  1243. [6] = 13475,
  1244. [7] = 15400,
  1245. [8] = 19250,
  1246. [9] = 23100,
  1247. [10] = 25025,
  1248. [11] = 26950,
  1249. [12] = 30800,
  1250. [13] = 38500,
  1251. [14] = 46200,
  1252. [15] = 50050,
  1253. [16] = 53900,
  1254. [17] = 57750,
  1255. [18] = 69300,
  1256. [19] = 75075,
  1257. [20] = 80850,
  1258. };
  1259. if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
  1260. return 0;
  1261. return __mcs2bitrate[rate->mcs] * rate->n_bonded_ch;
  1262. }
  1263. static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
  1264. {
  1265. static const u32 base[4][12] = {
  1266. { 6500000,
  1267. 13000000,
  1268. 19500000,
  1269. 26000000,
  1270. 39000000,
  1271. 52000000,
  1272. 58500000,
  1273. 65000000,
  1274. 78000000,
  1275. /* not in the spec, but some devices use this: */
  1276. 86700000,
  1277. 97500000,
  1278. 108300000,
  1279. },
  1280. { 13500000,
  1281. 27000000,
  1282. 40500000,
  1283. 54000000,
  1284. 81000000,
  1285. 108000000,
  1286. 121500000,
  1287. 135000000,
  1288. 162000000,
  1289. 180000000,
  1290. 202500000,
  1291. 225000000,
  1292. },
  1293. { 29300000,
  1294. 58500000,
  1295. 87800000,
  1296. 117000000,
  1297. 175500000,
  1298. 234000000,
  1299. 263300000,
  1300. 292500000,
  1301. 351000000,
  1302. 390000000,
  1303. 438800000,
  1304. 487500000,
  1305. },
  1306. { 58500000,
  1307. 117000000,
  1308. 175500000,
  1309. 234000000,
  1310. 351000000,
  1311. 468000000,
  1312. 526500000,
  1313. 585000000,
  1314. 702000000,
  1315. 780000000,
  1316. 877500000,
  1317. 975000000,
  1318. },
  1319. };
  1320. u32 bitrate;
  1321. int idx;
  1322. if (rate->mcs > 11)
  1323. goto warn;
  1324. switch (rate->bw) {
  1325. case RATE_INFO_BW_160:
  1326. idx = 3;
  1327. break;
  1328. case RATE_INFO_BW_80:
  1329. idx = 2;
  1330. break;
  1331. case RATE_INFO_BW_40:
  1332. idx = 1;
  1333. break;
  1334. case RATE_INFO_BW_5:
  1335. case RATE_INFO_BW_10:
  1336. default:
  1337. goto warn;
  1338. case RATE_INFO_BW_20:
  1339. idx = 0;
  1340. }
  1341. bitrate = base[idx][rate->mcs];
  1342. bitrate *= rate->nss;
  1343. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1344. bitrate = (bitrate / 9) * 10;
  1345. /* do NOT round down here */
  1346. return (bitrate + 50000) / 100000;
  1347. warn:
  1348. WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
  1349. rate->bw, rate->mcs, rate->nss);
  1350. return 0;
  1351. }
  1352. static u32 cfg80211_calculate_bitrate_he(struct rate_info *rate)
  1353. {
  1354. #define SCALE 6144
  1355. u32 mcs_divisors[14] = {
  1356. 102399, /* 16.666666... */
  1357. 51201, /* 8.333333... */
  1358. 34134, /* 5.555555... */
  1359. 25599, /* 4.166666... */
  1360. 17067, /* 2.777777... */
  1361. 12801, /* 2.083333... */
  1362. 11377, /* 1.851725... */
  1363. 10239, /* 1.666666... */
  1364. 8532, /* 1.388888... */
  1365. 7680, /* 1.250000... */
  1366. 6828, /* 1.111111... */
  1367. 6144, /* 1.000000... */
  1368. 5690, /* 0.926106... */
  1369. 5120, /* 0.833333... */
  1370. };
  1371. u32 rates_160M[3] = { 960777777, 907400000, 816666666 };
  1372. u32 rates_996[3] = { 480388888, 453700000, 408333333 };
  1373. u32 rates_484[3] = { 229411111, 216666666, 195000000 };
  1374. u32 rates_242[3] = { 114711111, 108333333, 97500000 };
  1375. u32 rates_106[3] = { 40000000, 37777777, 34000000 };
  1376. u32 rates_52[3] = { 18820000, 17777777, 16000000 };
  1377. u32 rates_26[3] = { 9411111, 8888888, 8000000 };
  1378. u64 tmp;
  1379. u32 result;
  1380. if (WARN_ON_ONCE(rate->mcs > 13))
  1381. return 0;
  1382. if (WARN_ON_ONCE(rate->he_gi > NL80211_RATE_INFO_HE_GI_3_2))
  1383. return 0;
  1384. if (WARN_ON_ONCE(rate->he_ru_alloc >
  1385. NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
  1386. return 0;
  1387. if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
  1388. return 0;
  1389. if (rate->bw == RATE_INFO_BW_160 ||
  1390. (rate->bw == RATE_INFO_BW_HE_RU &&
  1391. rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_2x996))
  1392. result = rates_160M[rate->he_gi];
  1393. else if (rate->bw == RATE_INFO_BW_80 ||
  1394. (rate->bw == RATE_INFO_BW_HE_RU &&
  1395. rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_996))
  1396. result = rates_996[rate->he_gi];
  1397. else if (rate->bw == RATE_INFO_BW_40 ||
  1398. (rate->bw == RATE_INFO_BW_HE_RU &&
  1399. rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_484))
  1400. result = rates_484[rate->he_gi];
  1401. else if (rate->bw == RATE_INFO_BW_20 ||
  1402. (rate->bw == RATE_INFO_BW_HE_RU &&
  1403. rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_242))
  1404. result = rates_242[rate->he_gi];
  1405. else if (rate->bw == RATE_INFO_BW_HE_RU &&
  1406. rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_106)
  1407. result = rates_106[rate->he_gi];
  1408. else if (rate->bw == RATE_INFO_BW_HE_RU &&
  1409. rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_52)
  1410. result = rates_52[rate->he_gi];
  1411. else if (rate->bw == RATE_INFO_BW_HE_RU &&
  1412. rate->he_ru_alloc == NL80211_RATE_INFO_HE_RU_ALLOC_26)
  1413. result = rates_26[rate->he_gi];
  1414. else {
  1415. WARN(1, "invalid HE MCS: bw:%d, ru:%d\n",
  1416. rate->bw, rate->he_ru_alloc);
  1417. return 0;
  1418. }
  1419. /* now scale to the appropriate MCS */
  1420. tmp = result;
  1421. tmp *= SCALE;
  1422. do_div(tmp, mcs_divisors[rate->mcs]);
  1423. result = tmp;
  1424. /* and take NSS, DCM into account */
  1425. result = (result * rate->nss) / 8;
  1426. if (rate->he_dcm)
  1427. result /= 2;
  1428. return result / 10000;
  1429. }
  1430. static u32 cfg80211_calculate_bitrate_eht(struct rate_info *rate)
  1431. {
  1432. #define SCALE 6144
  1433. static const u32 mcs_divisors[16] = {
  1434. 102399, /* 16.666666... */
  1435. 51201, /* 8.333333... */
  1436. 34134, /* 5.555555... */
  1437. 25599, /* 4.166666... */
  1438. 17067, /* 2.777777... */
  1439. 12801, /* 2.083333... */
  1440. 11377, /* 1.851725... */
  1441. 10239, /* 1.666666... */
  1442. 8532, /* 1.388888... */
  1443. 7680, /* 1.250000... */
  1444. 6828, /* 1.111111... */
  1445. 6144, /* 1.000000... */
  1446. 5690, /* 0.926106... */
  1447. 5120, /* 0.833333... */
  1448. 409600, /* 66.666666... */
  1449. 204800, /* 33.333333... */
  1450. };
  1451. static const u32 rates_996[3] = { 480388888, 453700000, 408333333 };
  1452. static const u32 rates_484[3] = { 229411111, 216666666, 195000000 };
  1453. static const u32 rates_242[3] = { 114711111, 108333333, 97500000 };
  1454. static const u32 rates_106[3] = { 40000000, 37777777, 34000000 };
  1455. static const u32 rates_52[3] = { 18820000, 17777777, 16000000 };
  1456. static const u32 rates_26[3] = { 9411111, 8888888, 8000000 };
  1457. u64 tmp;
  1458. u32 result;
  1459. if (WARN_ON_ONCE(rate->mcs > 15))
  1460. return 0;
  1461. if (WARN_ON_ONCE(rate->eht_gi > NL80211_RATE_INFO_EHT_GI_3_2))
  1462. return 0;
  1463. if (WARN_ON_ONCE(rate->eht_ru_alloc >
  1464. NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
  1465. return 0;
  1466. if (WARN_ON_ONCE(rate->nss < 1 || rate->nss > 8))
  1467. return 0;
  1468. /* Bandwidth checks for MCS 14 */
  1469. if (rate->mcs == 14) {
  1470. if ((rate->bw != RATE_INFO_BW_EHT_RU &&
  1471. rate->bw != RATE_INFO_BW_80 &&
  1472. rate->bw != RATE_INFO_BW_160 &&
  1473. rate->bw != RATE_INFO_BW_320) ||
  1474. (rate->bw == RATE_INFO_BW_EHT_RU &&
  1475. rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_996 &&
  1476. rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_2x996 &&
  1477. rate->eht_ru_alloc != NL80211_RATE_INFO_EHT_RU_ALLOC_4x996)) {
  1478. WARN(1, "invalid EHT BW for MCS 14: bw:%d, ru:%d\n",
  1479. rate->bw, rate->eht_ru_alloc);
  1480. return 0;
  1481. }
  1482. }
  1483. if (rate->bw == RATE_INFO_BW_320 ||
  1484. (rate->bw == RATE_INFO_BW_EHT_RU &&
  1485. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_4x996))
  1486. result = 4 * rates_996[rate->eht_gi];
  1487. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1488. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484)
  1489. result = 3 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
  1490. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1491. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_3x996)
  1492. result = 3 * rates_996[rate->eht_gi];
  1493. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1494. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484)
  1495. result = 2 * rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
  1496. else if (rate->bw == RATE_INFO_BW_160 ||
  1497. (rate->bw == RATE_INFO_BW_EHT_RU &&
  1498. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_2x996))
  1499. result = 2 * rates_996[rate->eht_gi];
  1500. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1501. rate->eht_ru_alloc ==
  1502. NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242)
  1503. result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi]
  1504. + rates_242[rate->eht_gi];
  1505. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1506. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996P484)
  1507. result = rates_996[rate->eht_gi] + rates_484[rate->eht_gi];
  1508. else if (rate->bw == RATE_INFO_BW_80 ||
  1509. (rate->bw == RATE_INFO_BW_EHT_RU &&
  1510. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_996))
  1511. result = rates_996[rate->eht_gi];
  1512. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1513. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484P242)
  1514. result = rates_484[rate->eht_gi] + rates_242[rate->eht_gi];
  1515. else if (rate->bw == RATE_INFO_BW_40 ||
  1516. (rate->bw == RATE_INFO_BW_EHT_RU &&
  1517. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_484))
  1518. result = rates_484[rate->eht_gi];
  1519. else if (rate->bw == RATE_INFO_BW_20 ||
  1520. (rate->bw == RATE_INFO_BW_EHT_RU &&
  1521. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_242))
  1522. result = rates_242[rate->eht_gi];
  1523. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1524. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106P26)
  1525. result = rates_106[rate->eht_gi] + rates_26[rate->eht_gi];
  1526. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1527. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_106)
  1528. result = rates_106[rate->eht_gi];
  1529. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1530. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52P26)
  1531. result = rates_52[rate->eht_gi] + rates_26[rate->eht_gi];
  1532. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1533. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_52)
  1534. result = rates_52[rate->eht_gi];
  1535. else if (rate->bw == RATE_INFO_BW_EHT_RU &&
  1536. rate->eht_ru_alloc == NL80211_RATE_INFO_EHT_RU_ALLOC_26)
  1537. result = rates_26[rate->eht_gi];
  1538. else {
  1539. WARN(1, "invalid EHT MCS: bw:%d, ru:%d\n",
  1540. rate->bw, rate->eht_ru_alloc);
  1541. return 0;
  1542. }
  1543. /* now scale to the appropriate MCS */
  1544. tmp = result;
  1545. tmp *= SCALE;
  1546. do_div(tmp, mcs_divisors[rate->mcs]);
  1547. /* and take NSS */
  1548. tmp *= rate->nss;
  1549. do_div(tmp, 8);
  1550. result = tmp;
  1551. return result / 10000;
  1552. }
  1553. static u32 cfg80211_calculate_bitrate_s1g(struct rate_info *rate)
  1554. {
  1555. /* For 1, 2, 4, 8 and 16 MHz channels */
  1556. static const u32 base[5][11] = {
  1557. { 300000,
  1558. 600000,
  1559. 900000,
  1560. 1200000,
  1561. 1800000,
  1562. 2400000,
  1563. 2700000,
  1564. 3000000,
  1565. 3600000,
  1566. 4000000,
  1567. /* MCS 10 supported in 1 MHz only */
  1568. 150000,
  1569. },
  1570. { 650000,
  1571. 1300000,
  1572. 1950000,
  1573. 2600000,
  1574. 3900000,
  1575. 5200000,
  1576. 5850000,
  1577. 6500000,
  1578. 7800000,
  1579. /* MCS 9 not valid */
  1580. },
  1581. { 1350000,
  1582. 2700000,
  1583. 4050000,
  1584. 5400000,
  1585. 8100000,
  1586. 10800000,
  1587. 12150000,
  1588. 13500000,
  1589. 16200000,
  1590. 18000000,
  1591. },
  1592. { 2925000,
  1593. 5850000,
  1594. 8775000,
  1595. 11700000,
  1596. 17550000,
  1597. 23400000,
  1598. 26325000,
  1599. 29250000,
  1600. 35100000,
  1601. 39000000,
  1602. },
  1603. { 8580000,
  1604. 11700000,
  1605. 17550000,
  1606. 23400000,
  1607. 35100000,
  1608. 46800000,
  1609. 52650000,
  1610. 58500000,
  1611. 70200000,
  1612. 78000000,
  1613. },
  1614. };
  1615. u32 bitrate;
  1616. /* default is 1 MHz index */
  1617. int idx = 0;
  1618. if (rate->mcs >= 11)
  1619. goto warn;
  1620. switch (rate->bw) {
  1621. case RATE_INFO_BW_16:
  1622. idx = 4;
  1623. break;
  1624. case RATE_INFO_BW_8:
  1625. idx = 3;
  1626. break;
  1627. case RATE_INFO_BW_4:
  1628. idx = 2;
  1629. break;
  1630. case RATE_INFO_BW_2:
  1631. idx = 1;
  1632. break;
  1633. case RATE_INFO_BW_1:
  1634. idx = 0;
  1635. break;
  1636. case RATE_INFO_BW_5:
  1637. case RATE_INFO_BW_10:
  1638. case RATE_INFO_BW_20:
  1639. case RATE_INFO_BW_40:
  1640. case RATE_INFO_BW_80:
  1641. case RATE_INFO_BW_160:
  1642. default:
  1643. goto warn;
  1644. }
  1645. bitrate = base[idx][rate->mcs];
  1646. bitrate *= rate->nss;
  1647. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1648. bitrate = (bitrate / 9) * 10;
  1649. /* do NOT round down here */
  1650. return (bitrate + 50000) / 100000;
  1651. warn:
  1652. WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
  1653. rate->bw, rate->mcs, rate->nss);
  1654. return 0;
  1655. }
  1656. u32 cfg80211_calculate_bitrate(struct rate_info *rate)
  1657. {
  1658. if (rate->flags & RATE_INFO_FLAGS_MCS)
  1659. return cfg80211_calculate_bitrate_ht(rate);
  1660. if (rate->flags & RATE_INFO_FLAGS_DMG)
  1661. return cfg80211_calculate_bitrate_dmg(rate);
  1662. if (rate->flags & RATE_INFO_FLAGS_EXTENDED_SC_DMG)
  1663. return cfg80211_calculate_bitrate_extended_sc_dmg(rate);
  1664. if (rate->flags & RATE_INFO_FLAGS_EDMG)
  1665. return cfg80211_calculate_bitrate_edmg(rate);
  1666. if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
  1667. return cfg80211_calculate_bitrate_vht(rate);
  1668. if (rate->flags & RATE_INFO_FLAGS_HE_MCS)
  1669. return cfg80211_calculate_bitrate_he(rate);
  1670. if (rate->flags & RATE_INFO_FLAGS_EHT_MCS)
  1671. return cfg80211_calculate_bitrate_eht(rate);
  1672. if (rate->flags & RATE_INFO_FLAGS_S1G_MCS)
  1673. return cfg80211_calculate_bitrate_s1g(rate);
  1674. return rate->legacy;
  1675. }
  1676. EXPORT_SYMBOL(cfg80211_calculate_bitrate);
  1677. int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
  1678. enum ieee80211_p2p_attr_id attr,
  1679. u8 *buf, unsigned int bufsize)
  1680. {
  1681. u8 *out = buf;
  1682. u16 attr_remaining = 0;
  1683. bool desired_attr = false;
  1684. u16 desired_len = 0;
  1685. while (len > 0) {
  1686. unsigned int iedatalen;
  1687. unsigned int copy;
  1688. const u8 *iedata;
  1689. if (len < 2)
  1690. return -EILSEQ;
  1691. iedatalen = ies[1];
  1692. if (iedatalen + 2 > len)
  1693. return -EILSEQ;
  1694. if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
  1695. goto cont;
  1696. if (iedatalen < 4)
  1697. goto cont;
  1698. iedata = ies + 2;
  1699. /* check WFA OUI, P2P subtype */
  1700. if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
  1701. iedata[2] != 0x9a || iedata[3] != 0x09)
  1702. goto cont;
  1703. iedatalen -= 4;
  1704. iedata += 4;
  1705. /* check attribute continuation into this IE */
  1706. copy = min_t(unsigned int, attr_remaining, iedatalen);
  1707. if (copy && desired_attr) {
  1708. desired_len += copy;
  1709. if (out) {
  1710. memcpy(out, iedata, min(bufsize, copy));
  1711. out += min(bufsize, copy);
  1712. bufsize -= min(bufsize, copy);
  1713. }
  1714. if (copy == attr_remaining)
  1715. return desired_len;
  1716. }
  1717. attr_remaining -= copy;
  1718. if (attr_remaining)
  1719. goto cont;
  1720. iedatalen -= copy;
  1721. iedata += copy;
  1722. while (iedatalen > 0) {
  1723. u16 attr_len;
  1724. /* P2P attribute ID & size must fit */
  1725. if (iedatalen < 3)
  1726. return -EILSEQ;
  1727. desired_attr = iedata[0] == attr;
  1728. attr_len = get_unaligned_le16(iedata + 1);
  1729. iedatalen -= 3;
  1730. iedata += 3;
  1731. copy = min_t(unsigned int, attr_len, iedatalen);
  1732. if (desired_attr) {
  1733. desired_len += copy;
  1734. if (out) {
  1735. memcpy(out, iedata, min(bufsize, copy));
  1736. out += min(bufsize, copy);
  1737. bufsize -= min(bufsize, copy);
  1738. }
  1739. if (copy == attr_len)
  1740. return desired_len;
  1741. }
  1742. iedata += copy;
  1743. iedatalen -= copy;
  1744. attr_remaining = attr_len - copy;
  1745. }
  1746. cont:
  1747. len -= ies[1] + 2;
  1748. ies += ies[1] + 2;
  1749. }
  1750. if (attr_remaining && desired_attr)
  1751. return -EILSEQ;
  1752. return -ENOENT;
  1753. }
  1754. EXPORT_SYMBOL(cfg80211_get_p2p_attr);
  1755. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id, bool id_ext)
  1756. {
  1757. int i;
  1758. /* Make sure array values are legal */
  1759. if (WARN_ON(ids[n_ids - 1] == WLAN_EID_EXTENSION))
  1760. return false;
  1761. i = 0;
  1762. while (i < n_ids) {
  1763. if (ids[i] == WLAN_EID_EXTENSION) {
  1764. if (id_ext && (ids[i + 1] == id))
  1765. return true;
  1766. i += 2;
  1767. continue;
  1768. }
  1769. if (ids[i] == id && !id_ext)
  1770. return true;
  1771. i++;
  1772. }
  1773. return false;
  1774. }
  1775. static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
  1776. {
  1777. /* we assume a validly formed IEs buffer */
  1778. u8 len = ies[pos + 1];
  1779. pos += 2 + len;
  1780. /* the IE itself must have 255 bytes for fragments to follow */
  1781. if (len < 255)
  1782. return pos;
  1783. while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
  1784. len = ies[pos + 1];
  1785. pos += 2 + len;
  1786. }
  1787. return pos;
  1788. }
  1789. size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
  1790. const u8 *ids, int n_ids,
  1791. const u8 *after_ric, int n_after_ric,
  1792. size_t offset)
  1793. {
  1794. size_t pos = offset;
  1795. while (pos < ielen) {
  1796. u8 ext = 0;
  1797. if (ies[pos] == WLAN_EID_EXTENSION)
  1798. ext = 2;
  1799. if ((pos + ext) >= ielen)
  1800. break;
  1801. if (!ieee80211_id_in_list(ids, n_ids, ies[pos + ext],
  1802. ies[pos] == WLAN_EID_EXTENSION))
  1803. break;
  1804. if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
  1805. pos = skip_ie(ies, ielen, pos);
  1806. while (pos < ielen) {
  1807. if (ies[pos] == WLAN_EID_EXTENSION)
  1808. ext = 2;
  1809. else
  1810. ext = 0;
  1811. if ((pos + ext) >= ielen)
  1812. break;
  1813. if (!ieee80211_id_in_list(after_ric,
  1814. n_after_ric,
  1815. ies[pos + ext],
  1816. ext == 2))
  1817. pos = skip_ie(ies, ielen, pos);
  1818. else
  1819. break;
  1820. }
  1821. } else {
  1822. pos = skip_ie(ies, ielen, pos);
  1823. }
  1824. }
  1825. return pos;
  1826. }
  1827. EXPORT_SYMBOL(ieee80211_ie_split_ric);
  1828. void ieee80211_fragment_element(struct sk_buff *skb, u8 *len_pos, u8 frag_id)
  1829. {
  1830. unsigned int elem_len;
  1831. if (!len_pos)
  1832. return;
  1833. elem_len = skb->data + skb->len - len_pos - 1;
  1834. while (elem_len > 255) {
  1835. /* this one is 255 */
  1836. *len_pos = 255;
  1837. /* remaining data gets smaller */
  1838. elem_len -= 255;
  1839. /* make space for the fragment ID/len in SKB */
  1840. skb_put(skb, 2);
  1841. /* shift back the remaining data to place fragment ID/len */
  1842. memmove(len_pos + 255 + 3, len_pos + 255 + 1, elem_len);
  1843. /* place the fragment ID */
  1844. len_pos += 255 + 1;
  1845. *len_pos = frag_id;
  1846. /* and point to fragment length to update later */
  1847. len_pos++;
  1848. }
  1849. *len_pos = elem_len;
  1850. }
  1851. EXPORT_SYMBOL(ieee80211_fragment_element);
  1852. bool ieee80211_operating_class_to_band(u8 operating_class,
  1853. enum nl80211_band *band)
  1854. {
  1855. switch (operating_class) {
  1856. case 112:
  1857. case 115 ... 127:
  1858. case 128 ... 130:
  1859. *band = NL80211_BAND_5GHZ;
  1860. return true;
  1861. case 131 ... 135:
  1862. case 137:
  1863. *band = NL80211_BAND_6GHZ;
  1864. return true;
  1865. case 81:
  1866. case 82:
  1867. case 83:
  1868. case 84:
  1869. *band = NL80211_BAND_2GHZ;
  1870. return true;
  1871. case 180:
  1872. *band = NL80211_BAND_60GHZ;
  1873. return true;
  1874. }
  1875. return false;
  1876. }
  1877. EXPORT_SYMBOL(ieee80211_operating_class_to_band);
  1878. bool ieee80211_operating_class_to_chandef(u8 operating_class,
  1879. struct ieee80211_channel *chan,
  1880. struct cfg80211_chan_def *chandef)
  1881. {
  1882. u32 control_freq, offset = 0;
  1883. enum nl80211_band band;
  1884. if (!ieee80211_operating_class_to_band(operating_class, &band) ||
  1885. !chan || band != chan->band)
  1886. return false;
  1887. control_freq = chan->center_freq;
  1888. chandef->chan = chan;
  1889. if (control_freq >= 5955)
  1890. offset = control_freq - 5955;
  1891. else if (control_freq >= 5745)
  1892. offset = control_freq - 5745;
  1893. else if (control_freq >= 5180)
  1894. offset = control_freq - 5180;
  1895. offset /= 20;
  1896. switch (operating_class) {
  1897. case 81: /* 2 GHz band; 20 MHz; channels 1..13 */
  1898. case 82: /* 2 GHz band; 20 MHz; channel 14 */
  1899. case 115: /* 5 GHz band; 20 MHz; channels 36,40,44,48 */
  1900. case 118: /* 5 GHz band; 20 MHz; channels 52,56,60,64 */
  1901. case 121: /* 5 GHz band; 20 MHz; channels 100..144 */
  1902. case 124: /* 5 GHz band; 20 MHz; channels 149,153,157,161 */
  1903. case 125: /* 5 GHz band; 20 MHz; channels 149..177 */
  1904. case 131: /* 6 GHz band; 20 MHz; channels 1..233*/
  1905. case 136: /* 6 GHz band; 20 MHz; channel 2 */
  1906. chandef->center_freq1 = control_freq;
  1907. chandef->width = NL80211_CHAN_WIDTH_20;
  1908. return true;
  1909. case 83: /* 2 GHz band; 40 MHz; channels 1..9 */
  1910. case 116: /* 5 GHz band; 40 MHz; channels 36,44 */
  1911. case 119: /* 5 GHz band; 40 MHz; channels 52,60 */
  1912. case 122: /* 5 GHz band; 40 MHz; channels 100,108,116,124,132,140 */
  1913. case 126: /* 5 GHz band; 40 MHz; channels 149,157,165,173 */
  1914. chandef->center_freq1 = control_freq + 10;
  1915. chandef->width = NL80211_CHAN_WIDTH_40;
  1916. return true;
  1917. case 84: /* 2 GHz band; 40 MHz; channels 5..13 */
  1918. case 117: /* 5 GHz band; 40 MHz; channels 40,48 */
  1919. case 120: /* 5 GHz band; 40 MHz; channels 56,64 */
  1920. case 123: /* 5 GHz band; 40 MHz; channels 104,112,120,128,136,144 */
  1921. case 127: /* 5 GHz band; 40 MHz; channels 153,161,169,177 */
  1922. chandef->center_freq1 = control_freq - 10;
  1923. chandef->width = NL80211_CHAN_WIDTH_40;
  1924. return true;
  1925. case 132: /* 6 GHz band; 40 MHz; channels 1,5,..,229*/
  1926. chandef->center_freq1 = control_freq + 10 - (offset & 1) * 20;
  1927. chandef->width = NL80211_CHAN_WIDTH_40;
  1928. return true;
  1929. case 128: /* 5 GHz band; 80 MHz; channels 36..64,100..144,149..177 */
  1930. case 133: /* 6 GHz band; 80 MHz; channels 1,5,..,229 */
  1931. chandef->center_freq1 = control_freq + 30 - (offset & 3) * 20;
  1932. chandef->width = NL80211_CHAN_WIDTH_80;
  1933. return true;
  1934. case 129: /* 5 GHz band; 160 MHz; channels 36..64,100..144,149..177 */
  1935. case 134: /* 6 GHz band; 160 MHz; channels 1,5,..,229 */
  1936. chandef->center_freq1 = control_freq + 70 - (offset & 7) * 20;
  1937. chandef->width = NL80211_CHAN_WIDTH_160;
  1938. return true;
  1939. case 130: /* 5 GHz band; 80+80 MHz; channels 36..64,100..144,149..177 */
  1940. case 135: /* 6 GHz band; 80+80 MHz; channels 1,5,..,229 */
  1941. /* The center_freq2 of 80+80 MHz is unknown */
  1942. case 137: /* 6 GHz band; 320 MHz; channels 1,5,..,229 */
  1943. /* 320-1 or 320-2 channelization is unknown */
  1944. default:
  1945. return false;
  1946. }
  1947. }
  1948. EXPORT_SYMBOL(ieee80211_operating_class_to_chandef);
  1949. bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
  1950. u8 *op_class)
  1951. {
  1952. u8 vht_opclass;
  1953. u32 freq = chandef->center_freq1;
  1954. if (freq >= 2412 && freq <= 2472) {
  1955. if (chandef->width > NL80211_CHAN_WIDTH_40)
  1956. return false;
  1957. /* 2.407 GHz, channels 1..13 */
  1958. if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1959. if (freq > chandef->chan->center_freq)
  1960. *op_class = 83; /* HT40+ */
  1961. else
  1962. *op_class = 84; /* HT40- */
  1963. } else {
  1964. *op_class = 81;
  1965. }
  1966. return true;
  1967. }
  1968. if (freq == 2484) {
  1969. /* channel 14 is only for IEEE 802.11b */
  1970. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
  1971. return false;
  1972. *op_class = 82; /* channel 14 */
  1973. return true;
  1974. }
  1975. switch (chandef->width) {
  1976. case NL80211_CHAN_WIDTH_80:
  1977. vht_opclass = 128;
  1978. break;
  1979. case NL80211_CHAN_WIDTH_160:
  1980. vht_opclass = 129;
  1981. break;
  1982. case NL80211_CHAN_WIDTH_80P80:
  1983. vht_opclass = 130;
  1984. break;
  1985. case NL80211_CHAN_WIDTH_10:
  1986. case NL80211_CHAN_WIDTH_5:
  1987. return false; /* unsupported for now */
  1988. default:
  1989. vht_opclass = 0;
  1990. break;
  1991. }
  1992. /* 5 GHz, channels 36..48 */
  1993. if (freq >= 5180 && freq <= 5240) {
  1994. if (vht_opclass) {
  1995. *op_class = vht_opclass;
  1996. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1997. if (freq > chandef->chan->center_freq)
  1998. *op_class = 116;
  1999. else
  2000. *op_class = 117;
  2001. } else {
  2002. *op_class = 115;
  2003. }
  2004. return true;
  2005. }
  2006. /* 5 GHz, channels 52..64 */
  2007. if (freq >= 5260 && freq <= 5320) {
  2008. if (vht_opclass) {
  2009. *op_class = vht_opclass;
  2010. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  2011. if (freq > chandef->chan->center_freq)
  2012. *op_class = 119;
  2013. else
  2014. *op_class = 120;
  2015. } else {
  2016. *op_class = 118;
  2017. }
  2018. return true;
  2019. }
  2020. /* 5 GHz, channels 100..144 */
  2021. if (freq >= 5500 && freq <= 5720) {
  2022. if (vht_opclass) {
  2023. *op_class = vht_opclass;
  2024. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  2025. if (freq > chandef->chan->center_freq)
  2026. *op_class = 122;
  2027. else
  2028. *op_class = 123;
  2029. } else {
  2030. *op_class = 121;
  2031. }
  2032. return true;
  2033. }
  2034. /* 5 GHz, channels 149..169 */
  2035. if (freq >= 5745 && freq <= 5845) {
  2036. if (vht_opclass) {
  2037. *op_class = vht_opclass;
  2038. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  2039. if (freq > chandef->chan->center_freq)
  2040. *op_class = 126;
  2041. else
  2042. *op_class = 127;
  2043. } else if (freq <= 5805) {
  2044. *op_class = 124;
  2045. } else {
  2046. *op_class = 125;
  2047. }
  2048. return true;
  2049. }
  2050. /* 56.16 GHz, channel 1..4 */
  2051. if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 6) {
  2052. if (chandef->width >= NL80211_CHAN_WIDTH_40)
  2053. return false;
  2054. *op_class = 180;
  2055. return true;
  2056. }
  2057. /* not supported yet */
  2058. return false;
  2059. }
  2060. EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
  2061. static int cfg80211_wdev_bi(struct wireless_dev *wdev)
  2062. {
  2063. switch (wdev->iftype) {
  2064. case NL80211_IFTYPE_AP:
  2065. case NL80211_IFTYPE_P2P_GO:
  2066. WARN_ON(wdev->valid_links);
  2067. return wdev->links[0].ap.beacon_interval;
  2068. case NL80211_IFTYPE_MESH_POINT:
  2069. return wdev->u.mesh.beacon_interval;
  2070. case NL80211_IFTYPE_ADHOC:
  2071. return wdev->u.ibss.beacon_interval;
  2072. default:
  2073. break;
  2074. }
  2075. return 0;
  2076. }
  2077. static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
  2078. u32 *beacon_int_gcd,
  2079. bool *beacon_int_different,
  2080. int radio_idx)
  2081. {
  2082. struct cfg80211_registered_device *rdev;
  2083. struct wireless_dev *wdev;
  2084. *beacon_int_gcd = 0;
  2085. *beacon_int_different = false;
  2086. rdev = wiphy_to_rdev(wiphy);
  2087. list_for_each_entry(wdev, &wiphy->wdev_list, list) {
  2088. int wdev_bi;
  2089. /* this feature isn't supported with MLO */
  2090. if (wdev->valid_links)
  2091. continue;
  2092. /* skip wdevs not active on the given wiphy radio */
  2093. if (radio_idx >= 0 &&
  2094. !(rdev_get_radio_mask(rdev, wdev->netdev) & BIT(radio_idx)))
  2095. continue;
  2096. wdev_bi = cfg80211_wdev_bi(wdev);
  2097. if (!wdev_bi)
  2098. continue;
  2099. if (!*beacon_int_gcd) {
  2100. *beacon_int_gcd = wdev_bi;
  2101. continue;
  2102. }
  2103. if (wdev_bi == *beacon_int_gcd)
  2104. continue;
  2105. *beacon_int_different = true;
  2106. *beacon_int_gcd = gcd(*beacon_int_gcd, wdev_bi);
  2107. }
  2108. if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
  2109. if (*beacon_int_gcd)
  2110. *beacon_int_different = true;
  2111. *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
  2112. }
  2113. }
  2114. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  2115. enum nl80211_iftype iftype, u32 beacon_int)
  2116. {
  2117. /*
  2118. * This is just a basic pre-condition check; if interface combinations
  2119. * are possible the driver must already be checking those with a call
  2120. * to cfg80211_check_combinations(), in which case we'll validate more
  2121. * through the cfg80211_calculate_bi_data() call and code in
  2122. * cfg80211_iter_combinations().
  2123. */
  2124. if (beacon_int < 10 || beacon_int > 10000)
  2125. return -EINVAL;
  2126. return 0;
  2127. }
  2128. int cfg80211_iter_combinations(struct wiphy *wiphy,
  2129. struct iface_combination_params *params,
  2130. void (*iter)(const struct ieee80211_iface_combination *c,
  2131. void *data),
  2132. void *data)
  2133. {
  2134. const struct wiphy_radio *radio = NULL;
  2135. const struct ieee80211_iface_combination *c, *cs;
  2136. const struct ieee80211_regdomain *regdom;
  2137. enum nl80211_dfs_regions region = 0;
  2138. int i, j, n, iftype;
  2139. int num_interfaces = 0;
  2140. u32 used_iftypes = 0;
  2141. u32 beacon_int_gcd;
  2142. bool beacon_int_different;
  2143. if (params->radio_idx >= 0)
  2144. radio = &wiphy->radio[params->radio_idx];
  2145. /*
  2146. * This is a bit strange, since the iteration used to rely only on
  2147. * the data given by the driver, but here it now relies on context,
  2148. * in form of the currently operating interfaces.
  2149. * This is OK for all current users, and saves us from having to
  2150. * push the GCD calculations into all the drivers.
  2151. * In the future, this should probably rely more on data that's in
  2152. * cfg80211 already - the only thing not would appear to be any new
  2153. * interfaces (while being brought up) and channel/radar data.
  2154. */
  2155. cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
  2156. &beacon_int_gcd, &beacon_int_different,
  2157. params->radio_idx);
  2158. if (params->radar_detect) {
  2159. rcu_read_lock();
  2160. regdom = rcu_dereference(cfg80211_regdomain);
  2161. if (regdom)
  2162. region = regdom->dfs_region;
  2163. rcu_read_unlock();
  2164. }
  2165. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  2166. num_interfaces += params->iftype_num[iftype];
  2167. if (params->iftype_num[iftype] > 0 &&
  2168. !cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
  2169. used_iftypes |= BIT(iftype);
  2170. }
  2171. if (radio) {
  2172. cs = radio->iface_combinations;
  2173. n = radio->n_iface_combinations;
  2174. } else {
  2175. cs = wiphy->iface_combinations;
  2176. n = wiphy->n_iface_combinations;
  2177. }
  2178. for (i = 0; i < n; i++) {
  2179. struct ieee80211_iface_limit *limits;
  2180. u32 all_iftypes = 0;
  2181. c = &cs[i];
  2182. if (num_interfaces > c->max_interfaces)
  2183. continue;
  2184. if (params->num_different_channels > c->num_different_channels)
  2185. continue;
  2186. limits = kmemdup_array(c->limits, c->n_limits, sizeof(*limits),
  2187. GFP_KERNEL);
  2188. if (!limits)
  2189. return -ENOMEM;
  2190. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  2191. if (cfg80211_iftype_allowed(wiphy, iftype, 0, 1))
  2192. continue;
  2193. for (j = 0; j < c->n_limits; j++) {
  2194. all_iftypes |= limits[j].types;
  2195. if (!(limits[j].types & BIT(iftype)))
  2196. continue;
  2197. if (limits[j].max < params->iftype_num[iftype])
  2198. goto cont;
  2199. limits[j].max -= params->iftype_num[iftype];
  2200. }
  2201. }
  2202. if (params->radar_detect !=
  2203. (c->radar_detect_widths & params->radar_detect))
  2204. goto cont;
  2205. if (params->radar_detect && c->radar_detect_regions &&
  2206. !(c->radar_detect_regions & BIT(region)))
  2207. goto cont;
  2208. /* Finally check that all iftypes that we're currently
  2209. * using are actually part of this combination. If they
  2210. * aren't then we can't use this combination and have
  2211. * to continue to the next.
  2212. */
  2213. if ((all_iftypes & used_iftypes) != used_iftypes)
  2214. goto cont;
  2215. if (beacon_int_gcd) {
  2216. if (c->beacon_int_min_gcd &&
  2217. beacon_int_gcd < c->beacon_int_min_gcd)
  2218. goto cont;
  2219. if (!c->beacon_int_min_gcd && beacon_int_different)
  2220. goto cont;
  2221. }
  2222. /* This combination covered all interface types and
  2223. * supported the requested numbers, so we're good.
  2224. */
  2225. (*iter)(c, data);
  2226. cont:
  2227. kfree(limits);
  2228. }
  2229. return 0;
  2230. }
  2231. EXPORT_SYMBOL(cfg80211_iter_combinations);
  2232. static void
  2233. cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
  2234. void *data)
  2235. {
  2236. int *num = data;
  2237. (*num)++;
  2238. }
  2239. int cfg80211_check_combinations(struct wiphy *wiphy,
  2240. struct iface_combination_params *params)
  2241. {
  2242. int err, num = 0;
  2243. err = cfg80211_iter_combinations(wiphy, params,
  2244. cfg80211_iter_sum_ifcombs, &num);
  2245. if (err)
  2246. return err;
  2247. if (num == 0)
  2248. return -EBUSY;
  2249. return 0;
  2250. }
  2251. EXPORT_SYMBOL(cfg80211_check_combinations);
  2252. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  2253. const u8 *rates, unsigned int n_rates,
  2254. u32 *mask)
  2255. {
  2256. int i, j;
  2257. if (!sband)
  2258. return -EINVAL;
  2259. if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
  2260. return -EINVAL;
  2261. *mask = 0;
  2262. for (i = 0; i < n_rates; i++) {
  2263. int rate = (rates[i] & 0x7f) * 5;
  2264. bool found = false;
  2265. for (j = 0; j < sband->n_bitrates; j++) {
  2266. if (sband->bitrates[j].bitrate == rate) {
  2267. found = true;
  2268. *mask |= BIT(j);
  2269. break;
  2270. }
  2271. }
  2272. if (!found)
  2273. return -EINVAL;
  2274. }
  2275. /*
  2276. * mask must have at least one bit set here since we
  2277. * didn't accept a 0-length rates array nor allowed
  2278. * entries in the array that didn't exist
  2279. */
  2280. return 0;
  2281. }
  2282. unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
  2283. {
  2284. enum nl80211_band band;
  2285. unsigned int n_channels = 0;
  2286. for (band = 0; band < NUM_NL80211_BANDS; band++)
  2287. if (wiphy->bands[band])
  2288. n_channels += wiphy->bands[band]->n_channels;
  2289. return n_channels;
  2290. }
  2291. EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
  2292. int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
  2293. struct station_info *sinfo)
  2294. {
  2295. struct cfg80211_registered_device *rdev;
  2296. struct wireless_dev *wdev;
  2297. int ret;
  2298. wdev = dev->ieee80211_ptr;
  2299. if (!wdev)
  2300. return -EOPNOTSUPP;
  2301. rdev = wiphy_to_rdev(wdev->wiphy);
  2302. if (!rdev->ops->get_station)
  2303. return -EOPNOTSUPP;
  2304. memset(sinfo, 0, sizeof(*sinfo));
  2305. wiphy_lock(&rdev->wiphy);
  2306. ret = rdev_get_station(rdev, dev, mac_addr, sinfo);
  2307. wiphy_unlock(&rdev->wiphy);
  2308. return ret;
  2309. }
  2310. EXPORT_SYMBOL(cfg80211_get_station);
  2311. void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
  2312. {
  2313. int i;
  2314. if (!f)
  2315. return;
  2316. kfree(f->serv_spec_info);
  2317. kfree(f->srf_bf);
  2318. kfree(f->srf_macs);
  2319. for (i = 0; i < f->num_rx_filters; i++)
  2320. kfree(f->rx_filters[i].filter);
  2321. for (i = 0; i < f->num_tx_filters; i++)
  2322. kfree(f->tx_filters[i].filter);
  2323. kfree(f->rx_filters);
  2324. kfree(f->tx_filters);
  2325. kfree(f);
  2326. }
  2327. EXPORT_SYMBOL(cfg80211_free_nan_func);
  2328. bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
  2329. u32 center_freq_khz, u32 bw_khz)
  2330. {
  2331. u32 start_freq_khz, end_freq_khz;
  2332. start_freq_khz = center_freq_khz - (bw_khz / 2);
  2333. end_freq_khz = center_freq_khz + (bw_khz / 2);
  2334. if (start_freq_khz >= freq_range->start_freq_khz &&
  2335. end_freq_khz <= freq_range->end_freq_khz)
  2336. return true;
  2337. return false;
  2338. }
  2339. int cfg80211_sinfo_alloc_tid_stats(struct station_info *sinfo, gfp_t gfp)
  2340. {
  2341. sinfo->pertid = kcalloc(IEEE80211_NUM_TIDS + 1,
  2342. sizeof(*(sinfo->pertid)),
  2343. gfp);
  2344. if (!sinfo->pertid)
  2345. return -ENOMEM;
  2346. return 0;
  2347. }
  2348. EXPORT_SYMBOL(cfg80211_sinfo_alloc_tid_stats);
  2349. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  2350. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  2351. const unsigned char rfc1042_header[] __aligned(2) =
  2352. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  2353. EXPORT_SYMBOL(rfc1042_header);
  2354. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  2355. const unsigned char bridge_tunnel_header[] __aligned(2) =
  2356. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  2357. EXPORT_SYMBOL(bridge_tunnel_header);
  2358. /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
  2359. struct iapp_layer2_update {
  2360. u8 da[ETH_ALEN]; /* broadcast */
  2361. u8 sa[ETH_ALEN]; /* STA addr */
  2362. __be16 len; /* 6 */
  2363. u8 dsap; /* 0 */
  2364. u8 ssap; /* 0 */
  2365. u8 control;
  2366. u8 xid_info[3];
  2367. } __packed;
  2368. void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
  2369. {
  2370. struct iapp_layer2_update *msg;
  2371. struct sk_buff *skb;
  2372. /* Send Level 2 Update Frame to update forwarding tables in layer 2
  2373. * bridge devices */
  2374. skb = dev_alloc_skb(sizeof(*msg));
  2375. if (!skb)
  2376. return;
  2377. msg = skb_put(skb, sizeof(*msg));
  2378. /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
  2379. * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
  2380. eth_broadcast_addr(msg->da);
  2381. ether_addr_copy(msg->sa, addr);
  2382. msg->len = htons(6);
  2383. msg->dsap = 0;
  2384. msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
  2385. msg->control = 0xaf; /* XID response lsb.1111F101.
  2386. * F=0 (no poll command; unsolicited frame) */
  2387. msg->xid_info[0] = 0x81; /* XID format identifier */
  2388. msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
  2389. msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
  2390. skb->dev = dev;
  2391. skb->protocol = eth_type_trans(skb, dev);
  2392. memset(skb->cb, 0, sizeof(skb->cb));
  2393. netif_rx(skb);
  2394. }
  2395. EXPORT_SYMBOL(cfg80211_send_layer2_update);
  2396. int ieee80211_get_vht_max_nss(struct ieee80211_vht_cap *cap,
  2397. enum ieee80211_vht_chanwidth bw,
  2398. int mcs, bool ext_nss_bw_capable,
  2399. unsigned int max_vht_nss)
  2400. {
  2401. u16 map = le16_to_cpu(cap->supp_mcs.rx_mcs_map);
  2402. int ext_nss_bw;
  2403. int supp_width;
  2404. int i, mcs_encoding;
  2405. if (map == 0xffff)
  2406. return 0;
  2407. if (WARN_ON(mcs > 9 || max_vht_nss > 8))
  2408. return 0;
  2409. if (mcs <= 7)
  2410. mcs_encoding = 0;
  2411. else if (mcs == 8)
  2412. mcs_encoding = 1;
  2413. else
  2414. mcs_encoding = 2;
  2415. if (!max_vht_nss) {
  2416. /* find max_vht_nss for the given MCS */
  2417. for (i = 7; i >= 0; i--) {
  2418. int supp = (map >> (2 * i)) & 3;
  2419. if (supp == 3)
  2420. continue;
  2421. if (supp >= mcs_encoding) {
  2422. max_vht_nss = i + 1;
  2423. break;
  2424. }
  2425. }
  2426. }
  2427. if (!(cap->supp_mcs.tx_mcs_map &
  2428. cpu_to_le16(IEEE80211_VHT_EXT_NSS_BW_CAPABLE)))
  2429. return max_vht_nss;
  2430. ext_nss_bw = le32_get_bits(cap->vht_cap_info,
  2431. IEEE80211_VHT_CAP_EXT_NSS_BW_MASK);
  2432. supp_width = le32_get_bits(cap->vht_cap_info,
  2433. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
  2434. /* if not capable, treat ext_nss_bw as 0 */
  2435. if (!ext_nss_bw_capable)
  2436. ext_nss_bw = 0;
  2437. /* This is invalid */
  2438. if (supp_width == 3)
  2439. return 0;
  2440. /* This is an invalid combination so pretend nothing is supported */
  2441. if (supp_width == 2 && (ext_nss_bw == 1 || ext_nss_bw == 2))
  2442. return 0;
  2443. /*
  2444. * Cover all the special cases according to IEEE 802.11-2016
  2445. * Table 9-250. All other cases are either factor of 1 or not
  2446. * valid/supported.
  2447. */
  2448. switch (bw) {
  2449. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  2450. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  2451. if ((supp_width == 1 || supp_width == 2) &&
  2452. ext_nss_bw == 3)
  2453. return 2 * max_vht_nss;
  2454. break;
  2455. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  2456. if (supp_width == 0 &&
  2457. (ext_nss_bw == 1 || ext_nss_bw == 2))
  2458. return max_vht_nss / 2;
  2459. if (supp_width == 0 &&
  2460. ext_nss_bw == 3)
  2461. return (3 * max_vht_nss) / 4;
  2462. if (supp_width == 1 &&
  2463. ext_nss_bw == 3)
  2464. return 2 * max_vht_nss;
  2465. break;
  2466. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  2467. if (supp_width == 0 && ext_nss_bw == 1)
  2468. return 0; /* not possible */
  2469. if (supp_width == 0 &&
  2470. ext_nss_bw == 2)
  2471. return max_vht_nss / 2;
  2472. if (supp_width == 0 &&
  2473. ext_nss_bw == 3)
  2474. return (3 * max_vht_nss) / 4;
  2475. if (supp_width == 1 &&
  2476. ext_nss_bw == 0)
  2477. return 0; /* not possible */
  2478. if (supp_width == 1 &&
  2479. ext_nss_bw == 1)
  2480. return max_vht_nss / 2;
  2481. if (supp_width == 1 &&
  2482. ext_nss_bw == 2)
  2483. return (3 * max_vht_nss) / 4;
  2484. break;
  2485. }
  2486. /* not covered or invalid combination received */
  2487. return max_vht_nss;
  2488. }
  2489. EXPORT_SYMBOL(ieee80211_get_vht_max_nss);
  2490. bool cfg80211_iftype_allowed(struct wiphy *wiphy, enum nl80211_iftype iftype,
  2491. bool is_4addr, u8 check_swif)
  2492. {
  2493. bool is_vlan = iftype == NL80211_IFTYPE_AP_VLAN;
  2494. switch (check_swif) {
  2495. case 0:
  2496. if (is_vlan && is_4addr)
  2497. return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
  2498. return wiphy->interface_modes & BIT(iftype);
  2499. case 1:
  2500. if (!(wiphy->software_iftypes & BIT(iftype)) && is_vlan)
  2501. return wiphy->flags & WIPHY_FLAG_4ADDR_AP;
  2502. return wiphy->software_iftypes & BIT(iftype);
  2503. default:
  2504. break;
  2505. }
  2506. return false;
  2507. }
  2508. EXPORT_SYMBOL(cfg80211_iftype_allowed);
  2509. void cfg80211_remove_link(struct wireless_dev *wdev, unsigned int link_id)
  2510. {
  2511. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  2512. lockdep_assert_wiphy(wdev->wiphy);
  2513. switch (wdev->iftype) {
  2514. case NL80211_IFTYPE_AP:
  2515. case NL80211_IFTYPE_P2P_GO:
  2516. cfg80211_stop_ap(rdev, wdev->netdev, link_id, true);
  2517. break;
  2518. default:
  2519. /* per-link not relevant */
  2520. break;
  2521. }
  2522. rdev_del_intf_link(rdev, wdev, link_id);
  2523. wdev->valid_links &= ~BIT(link_id);
  2524. eth_zero_addr(wdev->links[link_id].addr);
  2525. }
  2526. void cfg80211_remove_links(struct wireless_dev *wdev)
  2527. {
  2528. unsigned int link_id;
  2529. /*
  2530. * links are controlled by upper layers (userspace/cfg)
  2531. * only for AP mode, so only remove them here for AP
  2532. */
  2533. if (wdev->iftype != NL80211_IFTYPE_AP)
  2534. return;
  2535. if (wdev->valid_links) {
  2536. for_each_valid_link(wdev, link_id)
  2537. cfg80211_remove_link(wdev, link_id);
  2538. }
  2539. }
  2540. int cfg80211_remove_virtual_intf(struct cfg80211_registered_device *rdev,
  2541. struct wireless_dev *wdev)
  2542. {
  2543. cfg80211_remove_links(wdev);
  2544. return rdev_del_virtual_intf(rdev, wdev);
  2545. }
  2546. const struct wiphy_iftype_ext_capab *
  2547. cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
  2548. {
  2549. int i;
  2550. for (i = 0; i < wiphy->num_iftype_ext_capab; i++) {
  2551. if (wiphy->iftype_ext_capab[i].iftype == type)
  2552. return &wiphy->iftype_ext_capab[i];
  2553. }
  2554. return NULL;
  2555. }
  2556. EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);
  2557. static bool
  2558. ieee80211_radio_freq_range_valid(const struct wiphy_radio *radio,
  2559. u32 freq, u32 width)
  2560. {
  2561. const struct wiphy_radio_freq_range *r;
  2562. int i;
  2563. for (i = 0; i < radio->n_freq_range; i++) {
  2564. r = &radio->freq_range[i];
  2565. if (freq - width / 2 >= r->start_freq &&
  2566. freq + width / 2 <= r->end_freq)
  2567. return true;
  2568. }
  2569. return false;
  2570. }
  2571. bool cfg80211_radio_chandef_valid(const struct wiphy_radio *radio,
  2572. const struct cfg80211_chan_def *chandef)
  2573. {
  2574. u32 freq, width;
  2575. freq = ieee80211_chandef_to_khz(chandef);
  2576. width = nl80211_chan_width_to_mhz(chandef->width);
  2577. if (!ieee80211_radio_freq_range_valid(radio, freq, width))
  2578. return false;
  2579. freq = MHZ_TO_KHZ(chandef->center_freq2);
  2580. if (freq && !ieee80211_radio_freq_range_valid(radio, freq, width))
  2581. return false;
  2582. return true;
  2583. }
  2584. EXPORT_SYMBOL(cfg80211_radio_chandef_valid);