util.c 48 KB

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