util.c 74 KB

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