util.c 61 KB

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