util.c 53 KB

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