rtw_ieee80211.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2017 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. *****************************************************************************/
  15. #define _IEEE80211_C
  16. #ifdef CONFIG_PLATFORM_INTEL_BYT
  17. #include <linux/fs.h>
  18. #endif
  19. #include <drv_types.h>
  20. u8 RTW_WPA_OUI_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
  21. u16 RTW_WPA_VERSION = 1;
  22. u8 WPA_AUTH_KEY_MGMT_NONE[] = { 0x00, 0x50, 0xf2, 0 };
  23. u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x50, 0xf2, 1 };
  24. u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x50, 0xf2, 2 };
  25. u8 WPA_CIPHER_SUITE_NONE[] = { 0x00, 0x50, 0xf2, 0 };
  26. u8 WPA_CIPHER_SUITE_WEP40[] = { 0x00, 0x50, 0xf2, 1 };
  27. u8 WPA_CIPHER_SUITE_TKIP[] = { 0x00, 0x50, 0xf2, 2 };
  28. u8 WPA_CIPHER_SUITE_WRAP[] = { 0x00, 0x50, 0xf2, 3 };
  29. u8 WPA_CIPHER_SUITE_CCMP[] = { 0x00, 0x50, 0xf2, 4 };
  30. u8 WPA_CIPHER_SUITE_WEP104[] = { 0x00, 0x50, 0xf2, 5 };
  31. u16 RSN_VERSION_BSD = 1;
  32. u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x0f, 0xac, 1 };
  33. u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x0f, 0xac, 2 };
  34. u8 RSN_CIPHER_SUITE_NONE[] = { 0x00, 0x0f, 0xac, 0 };
  35. u8 RSN_CIPHER_SUITE_WEP40[] = { 0x00, 0x0f, 0xac, 1 };
  36. u8 RSN_CIPHER_SUITE_TKIP[] = { 0x00, 0x0f, 0xac, 2 };
  37. u8 RSN_CIPHER_SUITE_WRAP[] = { 0x00, 0x0f, 0xac, 3 };
  38. u8 RSN_CIPHER_SUITE_CCMP[] = { 0x00, 0x0f, 0xac, 4 };
  39. u8 RSN_CIPHER_SUITE_WEP104[] = { 0x00, 0x0f, 0xac, 5 };
  40. /* -----------------------------------------------------------
  41. * for adhoc-master to generate ie and provide supported-rate to fw
  42. * ----------------------------------------------------------- */
  43. static u8 WIFI_CCKRATES[] = {
  44. (IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
  45. (IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
  46. (IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
  47. (IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
  48. };
  49. static u8 WIFI_OFDMRATES[] = {
  50. (IEEE80211_OFDM_RATE_6MB),
  51. (IEEE80211_OFDM_RATE_9MB),
  52. (IEEE80211_OFDM_RATE_12MB),
  53. (IEEE80211_OFDM_RATE_18MB),
  54. (IEEE80211_OFDM_RATE_24MB),
  55. IEEE80211_OFDM_RATE_36MB,
  56. IEEE80211_OFDM_RATE_48MB,
  57. IEEE80211_OFDM_RATE_54MB
  58. };
  59. u8 mgn_rates_cck[4] = {MGN_1M, MGN_2M, MGN_5_5M, MGN_11M};
  60. u8 mgn_rates_ofdm[8] = {MGN_6M, MGN_9M, MGN_12M, MGN_18M, MGN_24M, MGN_36M, MGN_48M, MGN_54M};
  61. u8 mgn_rates_mcs0_7[8] = {MGN_MCS0, MGN_MCS1, MGN_MCS2, MGN_MCS3, MGN_MCS4, MGN_MCS5, MGN_MCS6, MGN_MCS7};
  62. u8 mgn_rates_mcs8_15[8] = {MGN_MCS8, MGN_MCS9, MGN_MCS10, MGN_MCS11, MGN_MCS12, MGN_MCS13, MGN_MCS14, MGN_MCS15};
  63. u8 mgn_rates_mcs16_23[8] = {MGN_MCS16, MGN_MCS17, MGN_MCS18, MGN_MCS19, MGN_MCS20, MGN_MCS21, MGN_MCS22, MGN_MCS23};
  64. u8 mgn_rates_mcs24_31[8] = {MGN_MCS24, MGN_MCS25, MGN_MCS26, MGN_MCS27, MGN_MCS28, MGN_MCS29, MGN_MCS30, MGN_MCS31};
  65. u8 mgn_rates_vht1ss[10] = {MGN_VHT1SS_MCS0, MGN_VHT1SS_MCS1, MGN_VHT1SS_MCS2, MGN_VHT1SS_MCS3, MGN_VHT1SS_MCS4
  66. , MGN_VHT1SS_MCS5, MGN_VHT1SS_MCS6, MGN_VHT1SS_MCS7, MGN_VHT1SS_MCS8, MGN_VHT1SS_MCS9
  67. };
  68. u8 mgn_rates_vht2ss[10] = {MGN_VHT2SS_MCS0, MGN_VHT2SS_MCS1, MGN_VHT2SS_MCS2, MGN_VHT2SS_MCS3, MGN_VHT2SS_MCS4
  69. , MGN_VHT2SS_MCS5, MGN_VHT2SS_MCS6, MGN_VHT2SS_MCS7, MGN_VHT2SS_MCS8, MGN_VHT2SS_MCS9
  70. };
  71. u8 mgn_rates_vht3ss[10] = {MGN_VHT3SS_MCS0, MGN_VHT3SS_MCS1, MGN_VHT3SS_MCS2, MGN_VHT3SS_MCS3, MGN_VHT3SS_MCS4
  72. , MGN_VHT3SS_MCS5, MGN_VHT3SS_MCS6, MGN_VHT3SS_MCS7, MGN_VHT3SS_MCS8, MGN_VHT3SS_MCS9
  73. };
  74. u8 mgn_rates_vht4ss[10] = {MGN_VHT4SS_MCS0, MGN_VHT4SS_MCS1, MGN_VHT4SS_MCS2, MGN_VHT4SS_MCS3, MGN_VHT4SS_MCS4
  75. , MGN_VHT4SS_MCS5, MGN_VHT4SS_MCS6, MGN_VHT4SS_MCS7, MGN_VHT4SS_MCS8, MGN_VHT4SS_MCS9
  76. };
  77. static const char *const _rate_section_str[] = {
  78. "CCK",
  79. "OFDM",
  80. "HT_1SS",
  81. "HT_2SS",
  82. "HT_3SS",
  83. "HT_4SS",
  84. "VHT_1SS",
  85. "VHT_2SS",
  86. "VHT_3SS",
  87. "VHT_4SS",
  88. "RATE_SECTION_UNKNOWN",
  89. };
  90. const char *rate_section_str(u8 section)
  91. {
  92. section = (section >= RATE_SECTION_NUM) ? RATE_SECTION_NUM : section;
  93. return _rate_section_str[section];
  94. }
  95. struct rate_section_ent rates_by_sections[RATE_SECTION_NUM] = {
  96. {RF_1TX, 4, mgn_rates_cck},
  97. {RF_1TX, 8, mgn_rates_ofdm},
  98. {RF_1TX, 8, mgn_rates_mcs0_7},
  99. {RF_2TX, 8, mgn_rates_mcs8_15},
  100. {RF_3TX, 8, mgn_rates_mcs16_23},
  101. {RF_4TX, 8, mgn_rates_mcs24_31},
  102. {RF_1TX, 10, mgn_rates_vht1ss},
  103. {RF_2TX, 10, mgn_rates_vht2ss},
  104. {RF_3TX, 10, mgn_rates_vht3ss},
  105. {RF_4TX, 10, mgn_rates_vht4ss},
  106. };
  107. int rtw_get_bit_value_from_ieee_value(u8 val)
  108. {
  109. unsigned char dot11_rate_table[] = {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108, 0}; /* last element must be zero!! */
  110. int i = 0;
  111. while (dot11_rate_table[i] != 0) {
  112. if (dot11_rate_table[i] == val)
  113. return BIT(i);
  114. i++;
  115. }
  116. return 0;
  117. }
  118. uint rtw_get_cckrate_size(u8 *rate, u32 rate_length)
  119. {
  120. int i = 0;
  121. while(i < rate_length){
  122. RTW_DBG("%s, rate[%d]=%u\n", __FUNCTION__, i, rate[i]);
  123. if (((rate[i] & 0x7f) == 2) || ((rate[i] & 0x7f) == 4) ||
  124. ((rate[i] & 0x7f) == 11) || ((rate[i] & 0x7f) == 22))
  125. i++;
  126. else
  127. break;
  128. }
  129. return i;
  130. }
  131. uint rtw_is_cckrates_included(u8 *rate)
  132. {
  133. u32 i = 0;
  134. while (rate[i] != 0) {
  135. if ((((rate[i]) & 0x7f) == 2) || (((rate[i]) & 0x7f) == 4) ||
  136. (((rate[i]) & 0x7f) == 11) || (((rate[i]) & 0x7f) == 22))
  137. return _TRUE;
  138. i++;
  139. }
  140. return _FALSE;
  141. }
  142. uint rtw_is_cckratesonly_included(u8 *rate)
  143. {
  144. u32 i = 0;
  145. while (rate[i] != 0) {
  146. if ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
  147. (((rate[i]) & 0x7f) != 11) && (((rate[i]) & 0x7f) != 22))
  148. return _FALSE;
  149. i++;
  150. }
  151. return _TRUE;
  152. }
  153. int rtw_check_network_type(unsigned char *rate, int ratelen, int channel)
  154. {
  155. if (channel > 14) {
  156. if ((rtw_is_cckrates_included(rate)) == _TRUE)
  157. return WIRELESS_INVALID;
  158. else
  159. return WIRELESS_11A;
  160. } else { /* could be pure B, pure G, or B/G */
  161. if ((rtw_is_cckratesonly_included(rate)) == _TRUE)
  162. return WIRELESS_11B;
  163. else if ((rtw_is_cckrates_included(rate)) == _TRUE)
  164. return WIRELESS_11BG;
  165. else
  166. return WIRELESS_11G;
  167. }
  168. }
  169. u8 *rtw_set_fixed_ie(unsigned char *pbuf, unsigned int len, unsigned char *source,
  170. unsigned int *frlen)
  171. {
  172. _rtw_memcpy((void *)pbuf, (void *)source, len);
  173. *frlen = *frlen + len;
  174. return pbuf + len;
  175. }
  176. /* rtw_set_ie will update frame length */
  177. u8 *rtw_set_ie
  178. (
  179. u8 *pbuf,
  180. sint index,
  181. uint len,
  182. const u8 *source,
  183. uint *frlen /* frame length */
  184. )
  185. {
  186. *pbuf = (u8)index;
  187. *(pbuf + 1) = (u8)len;
  188. if (len > 0)
  189. _rtw_memcpy((void *)(pbuf + 2), (void *)source, len);
  190. if (frlen)
  191. *frlen = *frlen + (len + 2);
  192. return pbuf + len + 2;
  193. }
  194. inline u8 *rtw_set_ie_ch_switch(u8 *buf, u32 *buf_len, u8 ch_switch_mode,
  195. u8 new_ch, u8 ch_switch_cnt)
  196. {
  197. u8 ie_data[3];
  198. ie_data[0] = ch_switch_mode;
  199. ie_data[1] = new_ch;
  200. ie_data[2] = ch_switch_cnt;
  201. return rtw_set_ie(buf, WLAN_EID_CHANNEL_SWITCH, 3, ie_data, buf_len);
  202. }
  203. inline u8 secondary_ch_offset_to_hal_ch_offset(u8 ch_offset)
  204. {
  205. if (ch_offset == SCN)
  206. return HAL_PRIME_CHNL_OFFSET_DONT_CARE;
  207. else if (ch_offset == SCA)
  208. return HAL_PRIME_CHNL_OFFSET_LOWER;
  209. else if (ch_offset == SCB)
  210. return HAL_PRIME_CHNL_OFFSET_UPPER;
  211. return HAL_PRIME_CHNL_OFFSET_DONT_CARE;
  212. }
  213. inline u8 hal_ch_offset_to_secondary_ch_offset(u8 ch_offset)
  214. {
  215. if (ch_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE)
  216. return SCN;
  217. else if (ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
  218. return SCA;
  219. else if (ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
  220. return SCB;
  221. return SCN;
  222. }
  223. inline u8 *rtw_set_ie_secondary_ch_offset(u8 *buf, u32 *buf_len, u8 secondary_ch_offset)
  224. {
  225. return rtw_set_ie(buf, WLAN_EID_SECONDARY_CHANNEL_OFFSET, 1, &secondary_ch_offset, buf_len);
  226. }
  227. inline u8 *rtw_set_ie_mesh_ch_switch_parm(u8 *buf, u32 *buf_len, u8 ttl,
  228. u8 flags, u16 reason, u16 precedence)
  229. {
  230. u8 ie_data[6];
  231. ie_data[0] = ttl;
  232. ie_data[1] = flags;
  233. RTW_PUT_LE16((u8 *)&ie_data[2], reason);
  234. RTW_PUT_LE16((u8 *)&ie_data[4], precedence);
  235. return rtw_set_ie(buf, 0x118, 6, ie_data, buf_len);
  236. }
  237. /*----------------------------------------------------------------------------
  238. index: the information element id index, limit is the limit for search
  239. -----------------------------------------------------------------------------*/
  240. u8 *rtw_get_ie(const u8 *pbuf, sint index, sint *len, sint limit)
  241. {
  242. sint tmp, i;
  243. const u8 *p;
  244. if (limit < 1) {
  245. return NULL;
  246. }
  247. p = pbuf;
  248. i = 0;
  249. *len = 0;
  250. while (1) {
  251. if (*p == index) {
  252. *len = *(p + 1);
  253. return (u8 *)p;
  254. } else {
  255. tmp = *(p + 1);
  256. p += (tmp + 2);
  257. i += (tmp + 2);
  258. }
  259. if (i >= limit)
  260. break;
  261. }
  262. return NULL;
  263. }
  264. /**
  265. * rtw_get_ie_ex - Search specific IE from a series of IEs
  266. * @in_ie: Address of IEs to search
  267. * @in_len: Length limit from in_ie
  268. * @eid: Element ID to match
  269. * @oui: OUI to match
  270. * @oui_len: OUI length
  271. * @ie: If not NULL and the specific IE is found, the IE will be copied to the buf starting from the specific IE
  272. * @ielen: If not NULL and the specific IE is found, will set to the length of the entire IE
  273. *
  274. * Returns: The address of the specific IE found, or NULL
  275. */
  276. u8 *rtw_get_ie_ex(const u8 *in_ie, uint in_len, u8 eid, const u8 *oui, u8 oui_len, u8 *ie, uint *ielen)
  277. {
  278. uint cnt;
  279. const u8 *target_ie = NULL;
  280. if (ielen)
  281. *ielen = 0;
  282. if (!in_ie || in_len <= 0)
  283. return (u8 *)target_ie;
  284. cnt = 0;
  285. while (cnt < in_len) {
  286. if (eid == in_ie[cnt]
  287. && (!oui || _rtw_memcmp(&in_ie[cnt + 2], oui, oui_len) == _TRUE)) {
  288. target_ie = &in_ie[cnt];
  289. if (ie)
  290. _rtw_memcpy(ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  291. if (ielen)
  292. *ielen = in_ie[cnt + 1] + 2;
  293. break;
  294. } else {
  295. cnt += in_ie[cnt + 1] + 2; /* goto next */
  296. }
  297. }
  298. return (u8 *)target_ie;
  299. }
  300. /**
  301. * rtw_ies_remove_ie - Find matching IEs and remove
  302. * @ies: Address of IEs to search
  303. * @ies_len: Pointer of length of ies, will update to new length
  304. * @offset: The offset to start scarch
  305. * @eid: Element ID to match
  306. * @oui: OUI to match
  307. * @oui_len: OUI length
  308. *
  309. * Returns: _SUCCESS: ies is updated, _FAIL: not updated
  310. */
  311. int rtw_ies_remove_ie(u8 *ies, uint *ies_len, uint offset, u8 eid, u8 *oui, u8 oui_len)
  312. {
  313. int ret = _FAIL;
  314. u8 *target_ie;
  315. u32 target_ielen;
  316. u8 *start;
  317. uint search_len;
  318. if (!ies || !ies_len || *ies_len <= offset)
  319. goto exit;
  320. start = ies + offset;
  321. search_len = *ies_len - offset;
  322. while (1) {
  323. target_ie = rtw_get_ie_ex(start, search_len, eid, oui, oui_len, NULL, &target_ielen);
  324. if (target_ie && target_ielen) {
  325. u8 *remain_ies = target_ie + target_ielen;
  326. uint remain_len = search_len - (remain_ies - start);
  327. _rtw_memmove(target_ie, remain_ies, remain_len);
  328. *ies_len = *ies_len - target_ielen;
  329. ret = _SUCCESS;
  330. start = target_ie;
  331. search_len = remain_len;
  332. } else
  333. break;
  334. }
  335. exit:
  336. return ret;
  337. }
  338. /* Returns: remove size OR _FAIL: not updated*/
  339. int rtw_remove_ie_g_rate(u8 *ie, uint *ie_len, uint offset, u8 eid)
  340. {
  341. int ret = _FAIL;
  342. u8 *tem_target_ie;
  343. u8 *target_ie;
  344. u32 target_ielen,temp_target_ielen,cck_rate_size,rm_size;
  345. u8 *start;
  346. uint search_len;
  347. u8 *remain_ies;
  348. uint remain_len;
  349. if (!ie || !ie_len || *ie_len <= offset)
  350. goto exit;
  351. start = ie + offset;
  352. search_len = *ie_len - offset;
  353. while (1) {
  354. tem_target_ie=rtw_get_ie(start,eid,&temp_target_ielen,search_len);
  355. /*if(tem_target_ie)
  356. RTW_INFO("%s, tem_target_ie=%u\n", __FUNCTION__,*tem_target_ie);*/
  357. if (tem_target_ie && temp_target_ielen) {
  358. cck_rate_size = rtw_get_cckrate_size((tem_target_ie+2), temp_target_ielen);
  359. rm_size = temp_target_ielen - cck_rate_size;
  360. RTW_DBG("%s,cck_rate_size=%u rm_size=%u\n", __FUNCTION__, cck_rate_size, rm_size);
  361. temp_target_ielen=temp_target_ielen + 2;/*org size of Supposrted Rates(include id + length)*/
  362. /*RTW_INFO("%s, temp_target_ielen=%u\n", __FUNCTION__,temp_target_ielen);*/
  363. remain_ies = tem_target_ie + temp_target_ielen;
  364. remain_len = search_len - (remain_ies - start);
  365. target_ielen=cck_rate_size;/*discount g mode rate 6, 9 12,18Mbps,id , length*/
  366. *(tem_target_ie+1)=target_ielen;/*set new length to Supposrted Rates*/
  367. target_ie=tem_target_ie+target_ielen + 2;/*set target ie to address of rate 6Mbps */
  368. _rtw_memmove(target_ie, remain_ies, remain_len);
  369. *ie_len = *ie_len - rm_size;
  370. ret = rm_size;
  371. start = target_ie;
  372. search_len = remain_len;
  373. } else
  374. break;
  375. }
  376. exit:
  377. return ret;
  378. }
  379. void rtw_set_supported_rate(u8 *SupportedRates, uint mode)
  380. {
  381. _rtw_memset(SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
  382. switch (mode) {
  383. case WIRELESS_11B:
  384. _rtw_memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
  385. break;
  386. case WIRELESS_11G:
  387. case WIRELESS_11A:
  388. case WIRELESS_11_5N:
  389. case WIRELESS_11A_5N: /* Todo: no basic rate for ofdm ? */
  390. case WIRELESS_11_5AC:
  391. _rtw_memcpy(SupportedRates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
  392. break;
  393. case WIRELESS_11BG:
  394. case WIRELESS_11G_24N:
  395. case WIRELESS_11_24N:
  396. case WIRELESS_11BG_24N:
  397. _rtw_memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
  398. _rtw_memcpy(SupportedRates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
  399. break;
  400. }
  401. }
  402. uint rtw_get_rateset_len(u8 *rateset)
  403. {
  404. uint i = 0;
  405. while (1) {
  406. if ((rateset[i]) == 0)
  407. break;
  408. if (i > 12)
  409. break;
  410. i++;
  411. }
  412. return i;
  413. }
  414. int rtw_generate_ie(struct registry_priv *pregistrypriv)
  415. {
  416. u8 wireless_mode;
  417. int sz = 0, rateLen;
  418. WLAN_BSSID_EX *pdev_network = &pregistrypriv->dev_network;
  419. u8 *ie = pdev_network->IEs;
  420. /* timestamp will be inserted by hardware */
  421. sz += 8;
  422. ie += sz;
  423. /* beacon interval : 2bytes */
  424. *(u16 *)ie = cpu_to_le16((u16)pdev_network->Configuration.BeaconPeriod); /* BCN_INTERVAL; */
  425. sz += 2;
  426. ie += 2;
  427. /* capability info */
  428. *(u16 *)ie = 0;
  429. *(u16 *)ie |= cpu_to_le16(cap_IBSS);
  430. if (pregistrypriv->preamble == PREAMBLE_SHORT)
  431. *(u16 *)ie |= cpu_to_le16(cap_ShortPremble);
  432. if (pdev_network->Privacy)
  433. *(u16 *)ie |= cpu_to_le16(cap_Privacy);
  434. sz += 2;
  435. ie += 2;
  436. /* SSID */
  437. ie = rtw_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength, pdev_network->Ssid.Ssid, &sz);
  438. /* supported rates */
  439. if (pregistrypriv->wireless_mode == WIRELESS_11ABGN) {
  440. if (pdev_network->Configuration.DSConfig > 14)
  441. wireless_mode = WIRELESS_11A_5N;
  442. else
  443. wireless_mode = WIRELESS_11BG_24N;
  444. } else if (pregistrypriv->wireless_mode == WIRELESS_MODE_MAX) { /* WIRELESS_11ABGN | WIRELESS_11AC */
  445. if (pdev_network->Configuration.DSConfig > 14)
  446. wireless_mode = WIRELESS_11_5AC;
  447. else
  448. wireless_mode = WIRELESS_11BG_24N;
  449. } else
  450. wireless_mode = pregistrypriv->wireless_mode;
  451. rtw_set_supported_rate(pdev_network->SupportedRates, wireless_mode) ;
  452. rateLen = rtw_get_rateset_len(pdev_network->SupportedRates);
  453. if (rateLen > 8) {
  454. ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, 8, pdev_network->SupportedRates, &sz);
  455. /* ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz); */
  456. } else
  457. ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, rateLen, pdev_network->SupportedRates, &sz);
  458. /* DS parameter set */
  459. ie = rtw_set_ie(ie, _DSSET_IE_, 1, (u8 *)&(pdev_network->Configuration.DSConfig), &sz);
  460. /* IBSS Parameter Set */
  461. ie = rtw_set_ie(ie, _IBSS_PARA_IE_, 2, (u8 *)&(pdev_network->Configuration.ATIMWindow), &sz);
  462. if (rateLen > 8)
  463. ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz);
  464. #ifdef CONFIG_80211N_HT
  465. /* HT Cap. */
  466. if (is_supported_ht(pregistrypriv->wireless_mode)
  467. && (pregistrypriv->ht_enable == _TRUE)) {
  468. /* todo: */
  469. }
  470. #endif /* CONFIG_80211N_HT */
  471. /* pdev_network->IELength = sz; */ /* update IELength */
  472. /* return _SUCCESS; */
  473. return sz;
  474. }
  475. unsigned char *rtw_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
  476. {
  477. int len;
  478. u16 val16;
  479. unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
  480. u8 *pbuf = pie;
  481. int limit_new = limit;
  482. while (1) {
  483. pbuf = rtw_get_ie(pbuf, _WPA_IE_ID_, &len, limit_new);
  484. if (pbuf) {
  485. /* check if oui matches... */
  486. if (_rtw_memcmp((pbuf + 2), wpa_oui_type, sizeof(wpa_oui_type)) == _FALSE)
  487. goto check_next_ie;
  488. /* check version... */
  489. _rtw_memcpy((u8 *)&val16, (pbuf + 6), sizeof(val16));
  490. val16 = le16_to_cpu(val16);
  491. if (val16 != 0x0001)
  492. goto check_next_ie;
  493. *wpa_ie_len = *(pbuf + 1);
  494. return pbuf;
  495. } else {
  496. *wpa_ie_len = 0;
  497. return NULL;
  498. }
  499. check_next_ie:
  500. limit_new = limit - (pbuf - pie) - 2 - len;
  501. if (limit_new <= 0)
  502. break;
  503. pbuf += (2 + len);
  504. }
  505. *wpa_ie_len = 0;
  506. return NULL;
  507. }
  508. unsigned char *rtw_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
  509. {
  510. return rtw_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
  511. }
  512. int rtw_get_wpa_cipher_suite(u8 *s)
  513. {
  514. if (_rtw_memcmp(s, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN) == _TRUE)
  515. return WPA_CIPHER_NONE;
  516. if (_rtw_memcmp(s, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN) == _TRUE)
  517. return WPA_CIPHER_WEP40;
  518. if (_rtw_memcmp(s, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN) == _TRUE)
  519. return WPA_CIPHER_TKIP;
  520. if (_rtw_memcmp(s, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN) == _TRUE)
  521. return WPA_CIPHER_CCMP;
  522. if (_rtw_memcmp(s, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN) == _TRUE)
  523. return WPA_CIPHER_WEP104;
  524. return 0;
  525. }
  526. int rtw_get_wpa2_cipher_suite(u8 *s)
  527. {
  528. if (_rtw_memcmp(s, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN) == _TRUE)
  529. return WPA_CIPHER_NONE;
  530. if (_rtw_memcmp(s, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN) == _TRUE)
  531. return WPA_CIPHER_WEP40;
  532. if (_rtw_memcmp(s, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN) == _TRUE)
  533. return WPA_CIPHER_TKIP;
  534. if (_rtw_memcmp(s, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN) == _TRUE)
  535. return WPA_CIPHER_CCMP;
  536. if (_rtw_memcmp(s, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN) == _TRUE)
  537. return WPA_CIPHER_WEP104;
  538. return 0;
  539. }
  540. int rtw_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x)
  541. {
  542. int i, ret = _SUCCESS;
  543. int left, count;
  544. u8 *pos;
  545. u8 SUITE_1X[4] = {0x00, 0x50, 0xf2, 1};
  546. if (wpa_ie_len <= 0) {
  547. /* No WPA IE - fail silently */
  548. return _FAIL;
  549. }
  550. if ((*wpa_ie != _WPA_IE_ID_) || (*(wpa_ie + 1) != (u8)(wpa_ie_len - 2)) ||
  551. (_rtw_memcmp(wpa_ie + 2, RTW_WPA_OUI_TYPE, WPA_SELECTOR_LEN) != _TRUE))
  552. return _FAIL;
  553. pos = wpa_ie;
  554. pos += 8;
  555. left = wpa_ie_len - 8;
  556. /* group_cipher */
  557. if (left >= WPA_SELECTOR_LEN) {
  558. *group_cipher = rtw_get_wpa_cipher_suite(pos);
  559. pos += WPA_SELECTOR_LEN;
  560. left -= WPA_SELECTOR_LEN;
  561. } else if (left > 0) {
  562. return _FAIL;
  563. }
  564. /* pairwise_cipher */
  565. if (left >= 2) {
  566. /* count = le16_to_cpu(*(u16*)pos); */
  567. count = RTW_GET_LE16(pos);
  568. pos += 2;
  569. left -= 2;
  570. if (count == 0 || left < count * WPA_SELECTOR_LEN) {
  571. return _FAIL;
  572. }
  573. for (i = 0; i < count; i++) {
  574. *pairwise_cipher |= rtw_get_wpa_cipher_suite(pos);
  575. pos += WPA_SELECTOR_LEN;
  576. left -= WPA_SELECTOR_LEN;
  577. }
  578. } else if (left == 1) {
  579. return _FAIL;
  580. }
  581. if (is_8021x) {
  582. if (left >= 6) {
  583. pos += 2;
  584. if (_rtw_memcmp(pos, SUITE_1X, 4) == 1) {
  585. *is_8021x = 1;
  586. }
  587. }
  588. }
  589. return ret;
  590. }
  591. int rtw_rsne_info_parse(const u8 *ie, uint ie_len, struct rsne_info *info)
  592. {
  593. const u8 *pos = ie;
  594. u16 cnt;
  595. _rtw_memset(info, 0, sizeof(struct rsne_info));
  596. if (ie + ie_len < pos + 4)
  597. goto err;
  598. if (*ie != WLAN_EID_RSN || *(ie + 1) != ie_len - 2)
  599. goto err;
  600. pos += 2 + 2;
  601. /* Group CS */
  602. if (ie + ie_len < pos + 4) {
  603. if (ie + ie_len != pos)
  604. goto err;
  605. goto exit;
  606. }
  607. info->gcs = (u8 *)pos;
  608. pos += 4;
  609. /* Pairwise CS */
  610. if (ie + ie_len < pos + 2) {
  611. if (ie + ie_len != pos)
  612. goto err;
  613. goto exit;
  614. }
  615. cnt = RTW_GET_LE16(pos);
  616. pos += 2;
  617. if (ie + ie_len < pos + 4 * cnt) {
  618. if (ie + ie_len != pos)
  619. goto err;
  620. goto exit;
  621. }
  622. info->pcs_cnt = cnt;
  623. info->pcs_list = (u8 *)pos;
  624. pos += 4 * cnt;
  625. /* AKM */
  626. if (ie + ie_len < pos + 2) {
  627. if (ie + ie_len != pos)
  628. goto err;
  629. goto exit;
  630. }
  631. cnt = RTW_GET_LE16(pos);
  632. pos += 2;
  633. if (ie + ie_len < pos + 4 * cnt) {
  634. if (ie + ie_len != pos)
  635. goto err;
  636. goto exit;
  637. }
  638. info->akm_cnt = cnt;
  639. info->akm_list = (u8 *)pos;
  640. pos += 4 * cnt;
  641. /* RSN cap */
  642. if (ie + ie_len < pos + 2) {
  643. if (ie + ie_len != pos)
  644. goto err;
  645. goto exit;
  646. }
  647. info->cap = (u8 *)pos;
  648. pos += 2;
  649. /* PMKID */
  650. if (ie + ie_len < pos + 2) {
  651. if (ie + ie_len != pos)
  652. goto err;
  653. goto exit;
  654. }
  655. cnt = RTW_GET_LE16(pos);
  656. pos += 2;
  657. if (ie + ie_len < pos + 16 * cnt) {
  658. if (ie + ie_len != pos)
  659. goto err;
  660. goto exit;
  661. }
  662. info->pmkid_cnt = cnt;
  663. info->pmkid_list = (u8 *)pos;
  664. pos += 16 * cnt;
  665. /* Group Mgmt CS */
  666. if (ie + ie_len < pos + 4) {
  667. if (ie + ie_len != pos)
  668. goto err;
  669. goto exit;
  670. }
  671. info->gmcs = (u8 *)pos;
  672. exit:
  673. return _SUCCESS;
  674. err:
  675. info->err = 1;
  676. return _FAIL;
  677. }
  678. int rtw_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x, u8 *mfp_opt)
  679. {
  680. struct rsne_info info;
  681. int i, ret = _SUCCESS;
  682. u8 SUITE_1X[4] = {0x00, 0x0f, 0xac, 0x01};
  683. ret = rtw_rsne_info_parse(rsn_ie, rsn_ie_len, &info);
  684. if (ret != _SUCCESS)
  685. goto exit;
  686. if (group_cipher) {
  687. if (info.gcs)
  688. *group_cipher = rtw_get_wpa2_cipher_suite(info.gcs);
  689. else
  690. *group_cipher = 0;
  691. }
  692. if (pairwise_cipher) {
  693. *pairwise_cipher = 0;
  694. for (i = 0; i < info.pcs_cnt; i++)
  695. *pairwise_cipher |= rtw_get_wpa2_cipher_suite(info.pcs_list + 4 * i);
  696. }
  697. if (is_8021x) {
  698. *is_8021x = 0;
  699. /* here only check the first AKM suite */
  700. if (info.akm_cnt && _rtw_memcmp(SUITE_1X, info.akm_list, 4) == _TRUE)
  701. *is_8021x = 1;
  702. }
  703. if (mfp_opt) {
  704. *mfp_opt = MFP_NO;
  705. if (info.cap)
  706. *mfp_opt = GET_RSN_CAP_MFP_OPTION(info.cap);
  707. }
  708. exit:
  709. return ret;
  710. }
  711. /* #ifdef CONFIG_WAPI_SUPPORT */
  712. int rtw_get_wapi_ie(u8 *in_ie, uint in_len, u8 *wapi_ie, u16 *wapi_len)
  713. {
  714. int len = 0;
  715. u8 authmode;
  716. uint cnt;
  717. u8 wapi_oui1[4] = {0x0, 0x14, 0x72, 0x01};
  718. u8 wapi_oui2[4] = {0x0, 0x14, 0x72, 0x02};
  719. if (wapi_len)
  720. *wapi_len = 0;
  721. if (!in_ie || in_len <= 0)
  722. return len;
  723. cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
  724. while (cnt < in_len) {
  725. authmode = in_ie[cnt];
  726. /* if(authmode==_WAPI_IE_) */
  727. if (authmode == _WAPI_IE_ && (_rtw_memcmp(&in_ie[cnt + 6], wapi_oui1, 4) == _TRUE ||
  728. _rtw_memcmp(&in_ie[cnt + 6], wapi_oui2, 4) == _TRUE)) {
  729. if (wapi_ie)
  730. _rtw_memcpy(wapi_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  731. if (wapi_len)
  732. *wapi_len = in_ie[cnt + 1] + 2;
  733. cnt += in_ie[cnt + 1] + 2; /* get next */
  734. } else {
  735. cnt += in_ie[cnt + 1] + 2; /* get next */
  736. }
  737. }
  738. if (wapi_len)
  739. len = *wapi_len;
  740. return len;
  741. }
  742. /* #endif */
  743. int rtw_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len, u8 *wpa_ie, u16 *wpa_len)
  744. {
  745. u8 authmode, sec_idx;
  746. u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
  747. uint cnt;
  748. /* Search required WPA or WPA2 IE and copy to sec_ie[ ] */
  749. cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
  750. sec_idx = 0;
  751. while (cnt < in_len) {
  752. authmode = in_ie[cnt];
  753. if ((authmode == _WPA_IE_ID_) && (_rtw_memcmp(&in_ie[cnt + 2], &wpa_oui[0], 4) == _TRUE)) {
  754. if (wpa_ie)
  755. _rtw_memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  756. *wpa_len = in_ie[cnt + 1] + 2;
  757. cnt += in_ie[cnt + 1] + 2; /* get next */
  758. } else {
  759. if (authmode == _WPA2_IE_ID_) {
  760. if (rsn_ie)
  761. _rtw_memcpy(rsn_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  762. *rsn_len = in_ie[cnt + 1] + 2;
  763. cnt += in_ie[cnt + 1] + 2; /* get next */
  764. } else {
  765. cnt += in_ie[cnt + 1] + 2; /* get next */
  766. }
  767. }
  768. }
  769. return *rsn_len + *wpa_len;
  770. }
  771. u8 rtw_is_wps_ie(u8 *ie_ptr, uint *wps_ielen)
  772. {
  773. u8 match = _FALSE;
  774. u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
  775. if (ie_ptr == NULL)
  776. return match;
  777. eid = ie_ptr[0];
  778. if ((eid == _WPA_IE_ID_) && (_rtw_memcmp(&ie_ptr[2], wps_oui, 4) == _TRUE)) {
  779. /* RTW_INFO("==> found WPS_IE.....\n"); */
  780. *wps_ielen = ie_ptr[1] + 2;
  781. match = _TRUE;
  782. }
  783. return match;
  784. }
  785. u8 *rtw_get_wps_ie_from_scan_queue(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen, enum bss_type frame_type)
  786. {
  787. u8 *wps = NULL;
  788. RTW_INFO("[%s] frame_type = %d\n", __FUNCTION__, frame_type);
  789. switch (frame_type) {
  790. case BSS_TYPE_BCN:
  791. case BSS_TYPE_PROB_RSP: {
  792. /* Beacon or Probe Response */
  793. wps = rtw_get_wps_ie(in_ie + _PROBERSP_IE_OFFSET_, in_len - _PROBERSP_IE_OFFSET_, wps_ie, wps_ielen);
  794. break;
  795. }
  796. case BSS_TYPE_PROB_REQ: {
  797. /* Probe Request */
  798. wps = rtw_get_wps_ie(in_ie + _PROBEREQ_IE_OFFSET_ , in_len - _PROBEREQ_IE_OFFSET_ , wps_ie, wps_ielen);
  799. break;
  800. }
  801. default:
  802. case BSS_TYPE_UNDEF:
  803. break;
  804. }
  805. return wps;
  806. }
  807. /**
  808. * rtw_get_wps_ie - Search WPS IE from a series of IEs
  809. * @in_ie: Address of IEs to search
  810. * @in_len: Length limit from in_ie
  811. * @wps_ie: If not NULL and WPS IE is found, WPS IE will be copied to the buf starting from wps_ie
  812. * @wps_ielen: If not NULL and WPS IE is found, will set to the length of the entire WPS IE
  813. *
  814. * Returns: The address of the WPS IE found, or NULL
  815. */
  816. u8 *rtw_get_wps_ie(const u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
  817. {
  818. uint cnt;
  819. const u8 *wpsie_ptr = NULL;
  820. u8 eid, wps_oui[4] = {0x00, 0x50, 0xf2, 0x04};
  821. if (wps_ielen)
  822. *wps_ielen = 0;
  823. if (!in_ie) {
  824. rtw_warn_on(1);
  825. return (u8 *)wpsie_ptr;
  826. }
  827. if (in_len <= 0)
  828. return (u8 *)wpsie_ptr;
  829. cnt = 0;
  830. while (cnt + 1 + 4 < in_len) {
  831. eid = in_ie[cnt];
  832. if (cnt + 1 + 4 >= MAX_IE_SZ) {
  833. rtw_warn_on(1);
  834. return NULL;
  835. }
  836. if (eid == WLAN_EID_VENDOR_SPECIFIC && _rtw_memcmp(&in_ie[cnt + 2], wps_oui, 4) == _TRUE) {
  837. wpsie_ptr = in_ie + cnt;
  838. if (wps_ie)
  839. _rtw_memcpy(wps_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  840. if (wps_ielen)
  841. *wps_ielen = in_ie[cnt + 1] + 2;
  842. break;
  843. } else
  844. cnt += in_ie[cnt + 1] + 2;
  845. }
  846. return (u8 *)wpsie_ptr;
  847. }
  848. /**
  849. * rtw_get_wps_attr - Search a specific WPS attribute from a given WPS IE
  850. * @wps_ie: Address of WPS IE to search
  851. * @wps_ielen: Length limit from wps_ie
  852. * @target_attr_id: The attribute ID of WPS attribute to search
  853. * @buf_attr: If not NULL and the WPS attribute is found, WPS attribute will be copied to the buf starting from buf_attr
  854. * @len_attr: If not NULL and the WPS attribute is found, will set to the length of the entire WPS attribute
  855. *
  856. * Returns: the address of the specific WPS attribute found, or NULL
  857. */
  858. u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id , u8 *buf_attr, u32 *len_attr)
  859. {
  860. u8 *attr_ptr = NULL;
  861. u8 *target_attr_ptr = NULL;
  862. u8 wps_oui[4] = {0x00, 0x50, 0xF2, 0x04};
  863. if (len_attr)
  864. *len_attr = 0;
  865. if ((wps_ie[0] != _VENDOR_SPECIFIC_IE_) ||
  866. (_rtw_memcmp(wps_ie + 2, wps_oui , 4) != _TRUE))
  867. return attr_ptr;
  868. /* 6 = 1(Element ID) + 1(Length) + 4(WPS OUI) */
  869. attr_ptr = wps_ie + 6; /* goto first attr */
  870. while (attr_ptr - wps_ie < wps_ielen) {
  871. /* 4 = 2(Attribute ID) + 2(Length) */
  872. u16 attr_id = RTW_GET_BE16(attr_ptr);
  873. u16 attr_data_len = RTW_GET_BE16(attr_ptr + 2);
  874. u16 attr_len = attr_data_len + 4;
  875. /* RTW_INFO("%s attr_ptr:%p, id:%u, length:%u\n", __FUNCTION__, attr_ptr, attr_id, attr_data_len); */
  876. if (attr_id == target_attr_id) {
  877. target_attr_ptr = attr_ptr;
  878. if (buf_attr)
  879. _rtw_memcpy(buf_attr, attr_ptr, attr_len);
  880. if (len_attr)
  881. *len_attr = attr_len;
  882. break;
  883. } else {
  884. attr_ptr += attr_len; /* goto next */
  885. }
  886. }
  887. return target_attr_ptr;
  888. }
  889. /**
  890. * rtw_get_wps_attr_content - Search a specific WPS attribute content from a given WPS IE
  891. * @wps_ie: Address of WPS IE to search
  892. * @wps_ielen: Length limit from wps_ie
  893. * @target_attr_id: The attribute ID of WPS attribute to search
  894. * @buf_content: If not NULL and the WPS attribute is found, WPS attribute content will be copied to the buf starting from buf_content
  895. * @len_content: If not NULL and the WPS attribute is found, will set to the length of the WPS attribute content
  896. *
  897. * Returns: the address of the specific WPS attribute content found, or NULL
  898. */
  899. u8 *rtw_get_wps_attr_content(u8 *wps_ie, uint wps_ielen, u16 target_attr_id , u8 *buf_content, uint *len_content)
  900. {
  901. u8 *attr_ptr;
  902. u32 attr_len;
  903. if (len_content)
  904. *len_content = 0;
  905. attr_ptr = rtw_get_wps_attr(wps_ie, wps_ielen, target_attr_id, NULL, &attr_len);
  906. if (attr_ptr && attr_len) {
  907. if (buf_content)
  908. _rtw_memcpy(buf_content, attr_ptr + 4, attr_len - 4);
  909. if (len_content)
  910. *len_content = attr_len - 4;
  911. return attr_ptr + 4;
  912. }
  913. return NULL;
  914. }
  915. static int rtw_ieee802_11_parse_vendor_specific(u8 *pos, uint elen,
  916. struct rtw_ieee802_11_elems *elems,
  917. int show_errors)
  918. {
  919. unsigned int oui;
  920. /* first 3 bytes in vendor specific information element are the IEEE
  921. * OUI of the vendor. The following byte is used a vendor specific
  922. * sub-type. */
  923. if (elen < 4) {
  924. if (show_errors) {
  925. RTW_INFO("short vendor specific "
  926. "information element ignored (len=%lu)\n",
  927. (unsigned long) elen);
  928. }
  929. return -1;
  930. }
  931. oui = RTW_GET_BE24(pos);
  932. switch (oui) {
  933. case OUI_MICROSOFT:
  934. /* Microsoft/Wi-Fi information elements are further typed and
  935. * subtyped */
  936. switch (pos[3]) {
  937. case 1:
  938. /* Microsoft OUI (00:50:F2) with OUI Type 1:
  939. * real WPA information element */
  940. elems->wpa_ie = pos;
  941. elems->wpa_ie_len = elen;
  942. break;
  943. case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
  944. if (elen < 5) {
  945. RTW_DBG("short WME "
  946. "information element ignored "
  947. "(len=%lu)\n",
  948. (unsigned long) elen);
  949. return -1;
  950. }
  951. switch (pos[4]) {
  952. case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
  953. case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
  954. elems->wme = pos;
  955. elems->wme_len = elen;
  956. break;
  957. case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
  958. elems->wme_tspec = pos;
  959. elems->wme_tspec_len = elen;
  960. break;
  961. default:
  962. RTW_DBG("unknown WME "
  963. "information element ignored "
  964. "(subtype=%d len=%lu)\n",
  965. pos[4], (unsigned long) elen);
  966. return -1;
  967. }
  968. break;
  969. case 4:
  970. /* Wi-Fi Protected Setup (WPS) IE */
  971. elems->wps_ie = pos;
  972. elems->wps_ie_len = elen;
  973. break;
  974. default:
  975. RTW_DBG("Unknown Microsoft "
  976. "information element ignored "
  977. "(type=%d len=%lu)\n",
  978. pos[3], (unsigned long) elen);
  979. return -1;
  980. }
  981. break;
  982. case OUI_BROADCOM:
  983. switch (pos[3]) {
  984. case VENDOR_HT_CAPAB_OUI_TYPE:
  985. elems->vendor_ht_cap = pos;
  986. elems->vendor_ht_cap_len = elen;
  987. break;
  988. default:
  989. RTW_DBG("Unknown Broadcom "
  990. "information element ignored "
  991. "(type=%d len=%lu)\n",
  992. pos[3], (unsigned long) elen);
  993. return -1;
  994. }
  995. break;
  996. default:
  997. RTW_DBG("unknown vendor specific information "
  998. "element ignored (vendor OUI %02x:%02x:%02x "
  999. "len=%lu)\n",
  1000. pos[0], pos[1], pos[2], (unsigned long) elen);
  1001. return -1;
  1002. }
  1003. return 0;
  1004. }
  1005. /**
  1006. * ieee802_11_parse_elems - Parse information elements in management frames
  1007. * @start: Pointer to the start of IEs
  1008. * @len: Length of IE buffer in octets
  1009. * @elems: Data structure for parsed elements
  1010. * @show_errors: Whether to show parsing errors in debug log
  1011. * Returns: Parsing result
  1012. */
  1013. ParseRes rtw_ieee802_11_parse_elems(u8 *start, uint len,
  1014. struct rtw_ieee802_11_elems *elems,
  1015. int show_errors)
  1016. {
  1017. uint left = len;
  1018. u8 *pos = start;
  1019. int unknown = 0;
  1020. _rtw_memset(elems, 0, sizeof(*elems));
  1021. while (left >= 2) {
  1022. u8 id, elen;
  1023. id = *pos++;
  1024. elen = *pos++;
  1025. left -= 2;
  1026. if (elen > left) {
  1027. if (show_errors) {
  1028. RTW_INFO("IEEE 802.11 element "
  1029. "parse failed (id=%d elen=%d "
  1030. "left=%lu)\n",
  1031. id, elen, (unsigned long) left);
  1032. }
  1033. return ParseFailed;
  1034. }
  1035. switch (id) {
  1036. case WLAN_EID_SSID:
  1037. elems->ssid = pos;
  1038. elems->ssid_len = elen;
  1039. break;
  1040. case WLAN_EID_SUPP_RATES:
  1041. elems->supp_rates = pos;
  1042. elems->supp_rates_len = elen;
  1043. break;
  1044. case WLAN_EID_FH_PARAMS:
  1045. elems->fh_params = pos;
  1046. elems->fh_params_len = elen;
  1047. break;
  1048. case WLAN_EID_DS_PARAMS:
  1049. elems->ds_params = pos;
  1050. elems->ds_params_len = elen;
  1051. break;
  1052. case WLAN_EID_CF_PARAMS:
  1053. elems->cf_params = pos;
  1054. elems->cf_params_len = elen;
  1055. break;
  1056. case WLAN_EID_TIM:
  1057. elems->tim = pos;
  1058. elems->tim_len = elen;
  1059. break;
  1060. case WLAN_EID_IBSS_PARAMS:
  1061. elems->ibss_params = pos;
  1062. elems->ibss_params_len = elen;
  1063. break;
  1064. case WLAN_EID_CHALLENGE:
  1065. elems->challenge = pos;
  1066. elems->challenge_len = elen;
  1067. break;
  1068. case WLAN_EID_ERP_INFO:
  1069. elems->erp_info = pos;
  1070. elems->erp_info_len = elen;
  1071. break;
  1072. case WLAN_EID_EXT_SUPP_RATES:
  1073. elems->ext_supp_rates = pos;
  1074. elems->ext_supp_rates_len = elen;
  1075. break;
  1076. case WLAN_EID_VENDOR_SPECIFIC:
  1077. if (rtw_ieee802_11_parse_vendor_specific(pos, elen,
  1078. elems,
  1079. show_errors))
  1080. unknown++;
  1081. break;
  1082. case WLAN_EID_RSN:
  1083. elems->rsn_ie = pos;
  1084. elems->rsn_ie_len = elen;
  1085. break;
  1086. case WLAN_EID_PWR_CAPABILITY:
  1087. elems->power_cap = pos;
  1088. elems->power_cap_len = elen;
  1089. break;
  1090. case WLAN_EID_SUPPORTED_CHANNELS:
  1091. elems->supp_channels = pos;
  1092. elems->supp_channels_len = elen;
  1093. break;
  1094. case WLAN_EID_MOBILITY_DOMAIN:
  1095. elems->mdie = pos;
  1096. elems->mdie_len = elen;
  1097. break;
  1098. case WLAN_EID_FAST_BSS_TRANSITION:
  1099. elems->ftie = pos;
  1100. elems->ftie_len = elen;
  1101. break;
  1102. case WLAN_EID_TIMEOUT_INTERVAL:
  1103. elems->timeout_int = pos;
  1104. elems->timeout_int_len = elen;
  1105. break;
  1106. case WLAN_EID_HT_CAP:
  1107. elems->ht_capabilities = pos;
  1108. elems->ht_capabilities_len = elen;
  1109. break;
  1110. case WLAN_EID_HT_OPERATION:
  1111. elems->ht_operation = pos;
  1112. elems->ht_operation_len = elen;
  1113. break;
  1114. case WLAN_EID_VHT_CAPABILITY:
  1115. elems->vht_capabilities = pos;
  1116. elems->vht_capabilities_len = elen;
  1117. break;
  1118. case WLAN_EID_VHT_OPERATION:
  1119. elems->vht_operation = pos;
  1120. elems->vht_operation_len = elen;
  1121. break;
  1122. case WLAN_EID_VHT_OP_MODE_NOTIFY:
  1123. elems->vht_op_mode_notify = pos;
  1124. elems->vht_op_mode_notify_len = elen;
  1125. break;
  1126. case _EID_RRM_EN_CAP_IE_:
  1127. elems->rm_en_cap = pos;
  1128. elems->rm_en_cap_len = elen;
  1129. break;
  1130. #ifdef CONFIG_RTW_MESH
  1131. case WLAN_EID_PREQ:
  1132. elems->preq = pos;
  1133. elems->preq_len = elen;
  1134. break;
  1135. case WLAN_EID_PREP:
  1136. elems->prep = pos;
  1137. elems->prep_len = elen;
  1138. break;
  1139. case WLAN_EID_PERR:
  1140. elems->perr = pos;
  1141. elems->perr_len = elen;
  1142. break;
  1143. case WLAN_EID_RANN:
  1144. elems->rann = pos;
  1145. elems->rann_len = elen;
  1146. break;
  1147. #endif
  1148. default:
  1149. unknown++;
  1150. if (!show_errors)
  1151. break;
  1152. RTW_DBG("IEEE 802.11 element parse "
  1153. "ignored unknown element (id=%d elen=%d)\n",
  1154. id, elen);
  1155. break;
  1156. }
  1157. left -= elen;
  1158. pos += elen;
  1159. }
  1160. if (left)
  1161. return ParseFailed;
  1162. return unknown ? ParseUnknown : ParseOK;
  1163. }
  1164. static u8 key_char2num(u8 ch);
  1165. static u8 key_char2num(u8 ch)
  1166. {
  1167. if ((ch >= '0') && (ch <= '9'))
  1168. return ch - '0';
  1169. else if ((ch >= 'a') && (ch <= 'f'))
  1170. return ch - 'a' + 10;
  1171. else if ((ch >= 'A') && (ch <= 'F'))
  1172. return ch - 'A' + 10;
  1173. else
  1174. return 0xff;
  1175. }
  1176. u8 str_2char2num(u8 hch, u8 lch);
  1177. u8 str_2char2num(u8 hch, u8 lch)
  1178. {
  1179. return (key_char2num(hch) * 10) + key_char2num(lch);
  1180. }
  1181. u8 key_2char2num(u8 hch, u8 lch);
  1182. u8 key_2char2num(u8 hch, u8 lch)
  1183. {
  1184. return (key_char2num(hch) << 4) | key_char2num(lch);
  1185. }
  1186. void macstr2num(u8 *dst, u8 *src);
  1187. void macstr2num(u8 *dst, u8 *src)
  1188. {
  1189. int jj, kk;
  1190. for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
  1191. dst[jj] = key_2char2num(src[kk], src[kk + 1]);
  1192. }
  1193. u8 convert_ip_addr(u8 hch, u8 mch, u8 lch)
  1194. {
  1195. return (key_char2num(hch) * 100) + (key_char2num(mch) * 10) + key_char2num(lch);
  1196. }
  1197. #ifdef CONFIG_PLATFORM_INTEL_BYT
  1198. #define MAC_ADDRESS_LEN 12
  1199. int rtw_get_mac_addr_intel(unsigned char *buf)
  1200. {
  1201. int ret = 0;
  1202. int i;
  1203. struct file *fp = NULL;
  1204. mm_segment_t oldfs;
  1205. unsigned char c_mac[MAC_ADDRESS_LEN];
  1206. char fname[] = "/config/wifi/mac.txt";
  1207. int jj, kk;
  1208. RTW_INFO("%s Enter\n", __FUNCTION__);
  1209. ret = rtw_retrieve_from_file(fname, c_mac, MAC_ADDRESS_LEN);
  1210. if (ret < MAC_ADDRESS_LEN)
  1211. return -1;
  1212. for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 2)
  1213. buf[jj] = key_2char2num(c_mac[kk], c_mac[kk + 1]);
  1214. RTW_INFO("%s: read from file mac address: "MAC_FMT"\n",
  1215. __FUNCTION__, MAC_ARG(buf));
  1216. return 0;
  1217. }
  1218. #endif /* CONFIG_PLATFORM_INTEL_BYT */
  1219. /*
  1220. * Description:
  1221. * rtw_check_invalid_mac_address:
  1222. * This is only used for checking mac address valid or not.
  1223. *
  1224. * Input:
  1225. * adapter: mac_address pointer.
  1226. * check_local_bit: check locally bit or not.
  1227. *
  1228. * Output:
  1229. * _TRUE: The mac address is invalid.
  1230. * _FALSE: The mac address is valid.
  1231. *
  1232. * Auther: Isaac.Li
  1233. */
  1234. u8 rtw_check_invalid_mac_address(u8 *mac_addr, u8 check_local_bit)
  1235. {
  1236. u8 null_mac_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  1237. u8 multi_mac_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  1238. u8 res = _FALSE;
  1239. if (_rtw_memcmp(mac_addr, null_mac_addr, ETH_ALEN)) {
  1240. res = _TRUE;
  1241. goto func_exit;
  1242. }
  1243. if (_rtw_memcmp(mac_addr, multi_mac_addr, ETH_ALEN)) {
  1244. res = _TRUE;
  1245. goto func_exit;
  1246. }
  1247. if (mac_addr[0] & BIT0) {
  1248. res = _TRUE;
  1249. goto func_exit;
  1250. }
  1251. if (check_local_bit == _TRUE) {
  1252. if (mac_addr[0] & BIT1) {
  1253. res = _TRUE;
  1254. goto func_exit;
  1255. }
  1256. }
  1257. func_exit:
  1258. return res;
  1259. }
  1260. extern char *rtw_initmac;
  1261. /**
  1262. * rtw_macaddr_cfg - Decide the mac address used
  1263. * @out: buf to store mac address decided
  1264. * @hw_mac_addr: mac address from efuse/epprom
  1265. */
  1266. void rtw_macaddr_cfg(u8 *out, const u8 *hw_mac_addr)
  1267. {
  1268. #define DEFAULT_RANDOM_MACADDR 1
  1269. u8 mac[ETH_ALEN];
  1270. if (out == NULL) {
  1271. rtw_warn_on(1);
  1272. return;
  1273. }
  1274. /* Users specify the mac address */
  1275. if (rtw_initmac) {
  1276. int jj, kk;
  1277. for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
  1278. mac[jj] = key_2char2num(rtw_initmac[kk], rtw_initmac[kk + 1]);
  1279. goto err_chk;
  1280. }
  1281. /* platform specified */
  1282. #ifdef CONFIG_PLATFORM_INTEL_BYT
  1283. if (rtw_get_mac_addr_intel(mac) == 0)
  1284. goto err_chk;
  1285. #endif
  1286. /* Use the mac address stored in the Efuse */
  1287. if (hw_mac_addr) {
  1288. _rtw_memcpy(mac, hw_mac_addr, ETH_ALEN);
  1289. goto err_chk;
  1290. }
  1291. err_chk:
  1292. if (rtw_check_invalid_mac_address(mac, _TRUE) == _TRUE) {
  1293. #if DEFAULT_RANDOM_MACADDR
  1294. RTW_ERR("invalid mac addr:"MAC_FMT", assign random MAC\n", MAC_ARG(mac));
  1295. *((u32 *)(&mac[2])) = rtw_random32();
  1296. mac[0] = 0x00;
  1297. mac[1] = 0xe0;
  1298. mac[2] = 0x4c;
  1299. #else
  1300. RTW_ERR("invalid mac addr:"MAC_FMT", assign default one\n", MAC_ARG(mac));
  1301. mac[0] = 0x00;
  1302. mac[1] = 0xe0;
  1303. mac[2] = 0x4c;
  1304. mac[3] = 0x87;
  1305. mac[4] = 0x00;
  1306. mac[5] = 0x00;
  1307. #endif
  1308. }
  1309. _rtw_memcpy(out, mac, ETH_ALEN);
  1310. RTW_INFO("%s mac addr:"MAC_FMT"\n", __func__, MAC_ARG(out));
  1311. }
  1312. #ifdef CONFIG_80211N_HT
  1313. void dump_ht_cap_ie_content(void *sel, const u8 *buf, u32 buf_len)
  1314. {
  1315. if (buf_len != HT_CAP_IE_LEN) {
  1316. RTW_PRINT_SEL(sel, "Invalid HT capability IE len:%d != %d\n", buf_len, HT_CAP_IE_LEN);
  1317. return;
  1318. }
  1319. RTW_PRINT_SEL(sel, "cap_info:%02x%02x:%s\n", *(buf), *(buf + 1)
  1320. , GET_HT_CAP_ELE_CHL_WIDTH(buf) ? " 40MHz" : " 20MHz");
  1321. RTW_PRINT_SEL(sel, "A-MPDU Parameters:"HT_AMPDU_PARA_FMT"\n"
  1322. , HT_AMPDU_PARA_ARG(HT_CAP_ELE_AMPDU_PARA(buf)));
  1323. RTW_PRINT_SEL(sel, "Supported MCS Set:"HT_SUP_MCS_SET_FMT"\n"
  1324. , HT_SUP_MCS_SET_ARG(HT_CAP_ELE_SUP_MCS_SET(buf)));
  1325. }
  1326. void dump_ht_cap_ie(void *sel, const u8 *ie, u32 ie_len)
  1327. {
  1328. const u8 *ht_cap_ie;
  1329. sint ht_cap_ielen;
  1330. ht_cap_ie = rtw_get_ie(ie, WLAN_EID_HT_CAP, &ht_cap_ielen, ie_len);
  1331. if (!ie || ht_cap_ie != ie)
  1332. return;
  1333. dump_ht_cap_ie_content(sel, ht_cap_ie + 2, ht_cap_ielen);
  1334. }
  1335. const char *const _ht_sc_offset_str[] = {
  1336. "SCN",
  1337. "SCA",
  1338. "SC-RSVD",
  1339. "SCB",
  1340. };
  1341. void dump_ht_op_ie_content(void *sel, const u8 *buf, u32 buf_len)
  1342. {
  1343. if (buf_len != HT_OP_IE_LEN) {
  1344. RTW_PRINT_SEL(sel, "Invalid HT operation IE len:%d != %d\n", buf_len, HT_OP_IE_LEN);
  1345. return;
  1346. }
  1347. RTW_PRINT_SEL(sel, "ch:%u%s %s\n"
  1348. , GET_HT_OP_ELE_PRI_CHL(buf)
  1349. , GET_HT_OP_ELE_STA_CHL_WIDTH(buf) ? "" : " 20MHz only"
  1350. , ht_sc_offset_str(GET_HT_OP_ELE_2ND_CHL_OFFSET(buf))
  1351. );
  1352. }
  1353. void dump_ht_op_ie(void *sel, const u8 *ie, u32 ie_len)
  1354. {
  1355. const u8 *ht_op_ie;
  1356. sint ht_op_ielen;
  1357. ht_op_ie = rtw_get_ie(ie, WLAN_EID_HT_OPERATION, &ht_op_ielen, ie_len);
  1358. if (!ie || ht_op_ie != ie)
  1359. return;
  1360. dump_ht_op_ie_content(sel, ht_op_ie + 2, ht_op_ielen);
  1361. }
  1362. #endif /* CONFIG_80211N_HT */
  1363. void dump_ies(void *sel, const u8 *buf, u32 buf_len)
  1364. {
  1365. const u8 *pos = buf;
  1366. u8 id, len;
  1367. while (pos - buf + 1 < buf_len) {
  1368. id = *pos;
  1369. len = *(pos + 1);
  1370. RTW_PRINT_SEL(sel, "%s ID:%u, LEN:%u\n", __FUNCTION__, id, len);
  1371. #ifdef CONFIG_80211N_HT
  1372. dump_ht_cap_ie(sel, pos, len + 2);
  1373. dump_ht_op_ie(sel, pos, len + 2);
  1374. #endif
  1375. #ifdef CONFIG_80211AC_VHT
  1376. dump_vht_cap_ie(sel, pos, len + 2);
  1377. dump_vht_op_ie(sel, pos, len + 2);
  1378. #endif
  1379. dump_wps_ie(sel, pos, len + 2);
  1380. #ifdef CONFIG_P2P
  1381. dump_p2p_ie(sel, pos, len + 2);
  1382. #ifdef CONFIG_WFD
  1383. dump_wfd_ie(sel, pos, len + 2);
  1384. #endif
  1385. #endif
  1386. pos += (2 + len);
  1387. }
  1388. }
  1389. void dump_wps_ie(void *sel, const u8 *ie, u32 ie_len)
  1390. {
  1391. const u8 *pos = ie;
  1392. u16 id;
  1393. u16 len;
  1394. const u8 *wps_ie;
  1395. uint wps_ielen;
  1396. wps_ie = rtw_get_wps_ie(ie, ie_len, NULL, &wps_ielen);
  1397. if (wps_ie != ie || wps_ielen == 0)
  1398. return;
  1399. pos += 6;
  1400. while (pos - ie + 4 <= ie_len) {
  1401. id = RTW_GET_BE16(pos);
  1402. len = RTW_GET_BE16(pos + 2);
  1403. RTW_PRINT_SEL(sel, "%s ID:0x%04x, LEN:%u%s\n", __func__, id, len
  1404. , ((pos - ie + 4 + len) <= ie_len) ? "" : "(exceed ie_len)");
  1405. pos += (4 + len);
  1406. }
  1407. }
  1408. /**
  1409. * rtw_ies_get_chbw - get operation ch, bw, offset from IEs of BSS.
  1410. * @ies: pointer of the first tlv IE
  1411. * @ies_len: length of @ies
  1412. * @ch: pointer of ch, used as output
  1413. * @bw: pointer of bw, used as output
  1414. * @offset: pointer of offset, used as output
  1415. * @ht: check HT IEs
  1416. * @vht: check VHT IEs, if true imply ht is true
  1417. */
  1418. void rtw_ies_get_chbw(u8 *ies, int ies_len, u8 *ch, u8 *bw, u8 *offset, u8 ht, u8 vht)
  1419. {
  1420. u8 *p;
  1421. int ie_len;
  1422. *ch = 0;
  1423. *bw = CHANNEL_WIDTH_20;
  1424. *offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
  1425. p = rtw_get_ie(ies, _DSSET_IE_, &ie_len, ies_len);
  1426. if (p && ie_len > 0)
  1427. *ch = *(p + 2);
  1428. #ifdef CONFIG_80211N_HT
  1429. if (ht || vht) {
  1430. u8 *ht_cap_ie, *ht_op_ie;
  1431. int ht_cap_ielen, ht_op_ielen;
  1432. ht_cap_ie = rtw_get_ie(ies, EID_HTCapability, &ht_cap_ielen, ies_len);
  1433. if (ht_cap_ie && ht_cap_ielen) {
  1434. if (GET_HT_CAP_ELE_CHL_WIDTH(ht_cap_ie + 2))
  1435. *bw = CHANNEL_WIDTH_40;
  1436. }
  1437. ht_op_ie = rtw_get_ie(ies, EID_HTInfo, &ht_op_ielen, ies_len);
  1438. if (ht_op_ie && ht_op_ielen) {
  1439. if (*ch == 0)
  1440. *ch = GET_HT_OP_ELE_PRI_CHL(ht_op_ie + 2);
  1441. else if (*ch != 0 && *ch != GET_HT_OP_ELE_PRI_CHL(ht_op_ie + 2)) {
  1442. RTW_INFO("%s ch inconsistent, DSSS:%u, HT primary:%u\n"
  1443. , __func__, *ch, GET_HT_OP_ELE_PRI_CHL(ht_op_ie + 2));
  1444. }
  1445. if (!GET_HT_OP_ELE_STA_CHL_WIDTH(ht_op_ie + 2))
  1446. *bw = CHANNEL_WIDTH_20;
  1447. if (*bw == CHANNEL_WIDTH_40) {
  1448. switch (GET_HT_OP_ELE_2ND_CHL_OFFSET(ht_op_ie + 2)) {
  1449. case SCA:
  1450. *offset = HAL_PRIME_CHNL_OFFSET_LOWER;
  1451. break;
  1452. case SCB:
  1453. *offset = HAL_PRIME_CHNL_OFFSET_UPPER;
  1454. break;
  1455. }
  1456. }
  1457. }
  1458. #ifdef CONFIG_80211AC_VHT
  1459. if (vht) {
  1460. u8 *vht_op_ie;
  1461. int vht_op_ielen;
  1462. vht_op_ie = rtw_get_ie(ies, EID_VHTOperation, &vht_op_ielen, ies_len);
  1463. if (vht_op_ie && vht_op_ielen) {
  1464. if (GET_VHT_OPERATION_ELE_CHL_WIDTH(vht_op_ie + 2) >= 1)
  1465. *bw = CHANNEL_WIDTH_80;
  1466. }
  1467. }
  1468. #endif /* CONFIG_80211AC_VHT */
  1469. }
  1470. #endif /* CONFIG_80211N_HT */
  1471. }
  1472. void rtw_bss_get_chbw(WLAN_BSSID_EX *bss, u8 *ch, u8 *bw, u8 *offset, u8 ht, u8 vht)
  1473. {
  1474. rtw_ies_get_chbw(bss->IEs + sizeof(NDIS_802_11_FIXED_IEs)
  1475. , bss->IELength - sizeof(NDIS_802_11_FIXED_IEs)
  1476. , ch, bw, offset, ht, vht);
  1477. if (*ch == 0)
  1478. *ch = bss->Configuration.DSConfig;
  1479. else if (*ch != bss->Configuration.DSConfig) {
  1480. RTW_INFO("inconsistent ch - ies:%u bss->Configuration.DSConfig:%u\n"
  1481. , *ch, bss->Configuration.DSConfig);
  1482. *ch = bss->Configuration.DSConfig;
  1483. rtw_warn_on(1);
  1484. }
  1485. }
  1486. /**
  1487. * rtw_is_chbw_grouped - test if the two ch settings can be grouped together
  1488. * @ch_a: ch of set a
  1489. * @bw_a: bw of set a
  1490. * @offset_a: offset of set a
  1491. * @ch_b: ch of set b
  1492. * @bw_b: bw of set b
  1493. * @offset_b: offset of set b
  1494. */
  1495. bool rtw_is_chbw_grouped(u8 ch_a, u8 bw_a, u8 offset_a
  1496. , u8 ch_b, u8 bw_b, u8 offset_b)
  1497. {
  1498. bool is_grouped = _FALSE;
  1499. if (ch_a != ch_b) {
  1500. /* ch is different */
  1501. goto exit;
  1502. } else if ((bw_a == CHANNEL_WIDTH_40 || bw_a == CHANNEL_WIDTH_80)
  1503. && (bw_b == CHANNEL_WIDTH_40 || bw_b == CHANNEL_WIDTH_80)
  1504. ) {
  1505. if (offset_a != offset_b)
  1506. goto exit;
  1507. }
  1508. is_grouped = _TRUE;
  1509. exit:
  1510. return is_grouped;
  1511. }
  1512. /**
  1513. * rtw_sync_chbw - obey g_ch, adjust g_bw, g_offset, bw, offset
  1514. * @req_ch: pointer of the request ch, may be modified further
  1515. * @req_bw: pointer of the request bw, may be modified further
  1516. * @req_offset: pointer of the request offset, may be modified further
  1517. * @g_ch: pointer of the ongoing group ch
  1518. * @g_bw: pointer of the ongoing group bw, may be modified further
  1519. * @g_offset: pointer of the ongoing group offset, may be modified further
  1520. */
  1521. void rtw_sync_chbw(u8 *req_ch, u8 *req_bw, u8 *req_offset
  1522. , u8 *g_ch, u8 *g_bw, u8 *g_offset)
  1523. {
  1524. *req_ch = *g_ch;
  1525. if (*req_bw == CHANNEL_WIDTH_80 && *g_ch <= 14) {
  1526. /*2.4G ch, downgrade to 40Mhz */
  1527. *req_bw = CHANNEL_WIDTH_40;
  1528. }
  1529. switch (*req_bw) {
  1530. case CHANNEL_WIDTH_80:
  1531. if (*g_bw == CHANNEL_WIDTH_40 || *g_bw == CHANNEL_WIDTH_80)
  1532. *req_offset = *g_offset;
  1533. else if (*g_bw == CHANNEL_WIDTH_20)
  1534. rtw_get_offset_by_chbw(*req_ch, *req_bw, req_offset);
  1535. if (*req_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE) {
  1536. RTW_ERR("%s req 80MHz BW without offset, down to 20MHz\n", __func__);
  1537. rtw_warn_on(1);
  1538. *req_bw = CHANNEL_WIDTH_20;
  1539. }
  1540. break;
  1541. case CHANNEL_WIDTH_40:
  1542. if (*g_bw == CHANNEL_WIDTH_40 || *g_bw == CHANNEL_WIDTH_80)
  1543. *req_offset = *g_offset;
  1544. else if (*g_bw == CHANNEL_WIDTH_20)
  1545. rtw_get_offset_by_chbw(*req_ch, *req_bw, req_offset);
  1546. if (*req_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE) {
  1547. RTW_ERR("%s req 40MHz BW without offset, down to 20MHz\n", __func__);
  1548. rtw_warn_on(1);
  1549. *req_bw = CHANNEL_WIDTH_20;
  1550. }
  1551. break;
  1552. case CHANNEL_WIDTH_20:
  1553. *req_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
  1554. break;
  1555. default:
  1556. RTW_ERR("%s req unsupported BW:%u\n", __func__, *req_bw);
  1557. rtw_warn_on(1);
  1558. }
  1559. if (*req_bw > *g_bw) {
  1560. *g_bw = *req_bw;
  1561. *g_offset = *req_offset;
  1562. }
  1563. }
  1564. /**
  1565. * rtw_get_p2p_merged_len - Get merged ie length from muitiple p2p ies.
  1566. * @in_ie: Pointer of the first p2p ie
  1567. * @in_len: Total len of muiltiple p2p ies
  1568. * Returns: Length of merged p2p ie length
  1569. */
  1570. u32 rtw_get_p2p_merged_ies_len(u8 *in_ie, u32 in_len)
  1571. {
  1572. PNDIS_802_11_VARIABLE_IEs pIE;
  1573. u8 OUI[4] = { 0x50, 0x6f, 0x9a, 0x09 };
  1574. int i = 0;
  1575. int len = 0;
  1576. while (i < in_len) {
  1577. pIE = (PNDIS_802_11_VARIABLE_IEs)(in_ie + i);
  1578. if (pIE->ElementID == _VENDOR_SPECIFIC_IE_ && _rtw_memcmp(pIE->data, OUI, 4)) {
  1579. len += pIE->Length - 4; /* 4 is P2P OUI length, don't count it in this loop */
  1580. }
  1581. i += (pIE->Length + 2);
  1582. }
  1583. return len + 4; /* Append P2P OUI length at last. */
  1584. }
  1585. /**
  1586. * rtw_p2p_merge_ies - Merge muitiple p2p ies into one
  1587. * @in_ie: Pointer of the first p2p ie
  1588. * @in_len: Total len of muiltiple p2p ies
  1589. * @merge_ie: Pointer of merged ie
  1590. * Returns: Length of merged p2p ie
  1591. */
  1592. int rtw_p2p_merge_ies(u8 *in_ie, u32 in_len, u8 *merge_ie)
  1593. {
  1594. PNDIS_802_11_VARIABLE_IEs pIE;
  1595. u8 len = 0;
  1596. u8 OUI[4] = { 0x50, 0x6f, 0x9a, 0x09 };
  1597. u8 ELOUI[6] = { 0xDD, 0x00, 0x50, 0x6f, 0x9a, 0x09 }; /* EID;Len;OUI, Len would copy at the end of function */
  1598. int i = 0;
  1599. if (merge_ie != NULL) {
  1600. /* Set first P2P OUI */
  1601. _rtw_memcpy(merge_ie, ELOUI, 6);
  1602. merge_ie += 6;
  1603. while (i < in_len) {
  1604. pIE = (PNDIS_802_11_VARIABLE_IEs)(in_ie + i);
  1605. /* Take out the rest of P2P OUIs */
  1606. if (pIE->ElementID == _VENDOR_SPECIFIC_IE_ && _rtw_memcmp(pIE->data, OUI, 4)) {
  1607. _rtw_memcpy(merge_ie, pIE->data + 4, pIE->Length - 4);
  1608. len += pIE->Length - 4;
  1609. merge_ie += pIE->Length - 4;
  1610. }
  1611. i += (pIE->Length + 2);
  1612. }
  1613. return len + 4; /* 4 is for P2P OUI */
  1614. }
  1615. return 0;
  1616. }
  1617. void dump_p2p_ie(void *sel, const u8 *ie, u32 ie_len)
  1618. {
  1619. const u8 *pos = ie;
  1620. u8 id;
  1621. u16 len;
  1622. const u8 *p2p_ie;
  1623. uint p2p_ielen;
  1624. p2p_ie = rtw_get_p2p_ie(ie, ie_len, NULL, &p2p_ielen);
  1625. if (p2p_ie != ie || p2p_ielen == 0)
  1626. return;
  1627. pos += 6;
  1628. while (pos - ie + 3 <= ie_len) {
  1629. id = *pos;
  1630. len = RTW_GET_LE16(pos + 1);
  1631. RTW_PRINT_SEL(sel, "%s ID:%u, LEN:%u%s\n", __func__, id, len
  1632. , ((pos - ie + 3 + len) <= ie_len) ? "" : "(exceed ie_len)");
  1633. pos += (3 + len);
  1634. }
  1635. }
  1636. /**
  1637. * rtw_get_p2p_ie - Search P2P IE from a series of IEs
  1638. * @in_ie: Address of IEs to search
  1639. * @in_len: Length limit from in_ie
  1640. * @p2p_ie: If not NULL and P2P IE is found, P2P IE will be copied to the buf starting from p2p_ie
  1641. * @p2p_ielen: If not NULL and P2P IE is found, will set to the length of the entire P2P IE
  1642. *
  1643. * Returns: The address of the P2P IE found, or NULL
  1644. */
  1645. u8 *rtw_get_p2p_ie(const u8 *in_ie, int in_len, u8 *p2p_ie, uint *p2p_ielen)
  1646. {
  1647. uint cnt;
  1648. const u8 *p2p_ie_ptr = NULL;
  1649. u8 eid, p2p_oui[4] = {0x50, 0x6F, 0x9A, 0x09};
  1650. if (p2p_ielen)
  1651. *p2p_ielen = 0;
  1652. if (!in_ie || in_len < 0) {
  1653. rtw_warn_on(1);
  1654. return (u8 *)p2p_ie_ptr;
  1655. }
  1656. if (in_len <= 0)
  1657. return (u8 *)p2p_ie_ptr;
  1658. cnt = 0;
  1659. while (cnt + 1 + 4 < in_len) {
  1660. eid = in_ie[cnt];
  1661. if (cnt + 1 + 4 >= MAX_IE_SZ) {
  1662. rtw_warn_on(1);
  1663. return NULL;
  1664. }
  1665. if (eid == WLAN_EID_VENDOR_SPECIFIC && _rtw_memcmp(&in_ie[cnt + 2], p2p_oui, 4) == _TRUE) {
  1666. p2p_ie_ptr = in_ie + cnt;
  1667. if (p2p_ie)
  1668. _rtw_memcpy(p2p_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  1669. if (p2p_ielen)
  1670. *p2p_ielen = in_ie[cnt + 1] + 2;
  1671. break;
  1672. } else
  1673. cnt += in_ie[cnt + 1] + 2;
  1674. }
  1675. return (u8 *)p2p_ie_ptr;
  1676. }
  1677. /**
  1678. * rtw_get_p2p_attr - Search a specific P2P attribute from a given P2P IE
  1679. * @p2p_ie: Address of P2P IE to search
  1680. * @p2p_ielen: Length limit from p2p_ie
  1681. * @target_attr_id: The attribute ID of P2P attribute to search
  1682. * @buf_attr: If not NULL and the P2P attribute is found, P2P attribute will be copied to the buf starting from buf_attr
  1683. * @len_attr: If not NULL and the P2P attribute is found, will set to the length of the entire P2P attribute
  1684. *
  1685. * Returns: the address of the specific WPS attribute found, or NULL
  1686. */
  1687. u8 *rtw_get_p2p_attr(u8 *p2p_ie, uint p2p_ielen, u8 target_attr_id , u8 *buf_attr, u32 *len_attr)
  1688. {
  1689. u8 *attr_ptr = NULL;
  1690. u8 *target_attr_ptr = NULL;
  1691. u8 p2p_oui[4] = {0x50, 0x6F, 0x9A, 0x09};
  1692. if (len_attr)
  1693. *len_attr = 0;
  1694. if (!p2p_ie
  1695. || p2p_ielen <= 6
  1696. || (p2p_ie[0] != WLAN_EID_VENDOR_SPECIFIC)
  1697. || (_rtw_memcmp(p2p_ie + 2, p2p_oui, 4) != _TRUE))
  1698. return attr_ptr;
  1699. /* 6 = 1(Element ID) + 1(Length) + 3 (OUI) + 1(OUI Type) */
  1700. attr_ptr = p2p_ie + 6; /* goto first attr */
  1701. while ((attr_ptr - p2p_ie + 3) <= p2p_ielen) {
  1702. /* 3 = 1(Attribute ID) + 2(Length) */
  1703. u8 attr_id = *attr_ptr;
  1704. u16 attr_data_len = RTW_GET_LE16(attr_ptr + 1);
  1705. u16 attr_len = attr_data_len + 3;
  1706. if (0)
  1707. RTW_INFO("%s attr_ptr:%p, id:%u, length:%u\n", __func__, attr_ptr, attr_id, attr_data_len);
  1708. if ((attr_ptr - p2p_ie + attr_len) > p2p_ielen)
  1709. break;
  1710. if (attr_id == target_attr_id) {
  1711. target_attr_ptr = attr_ptr;
  1712. if (buf_attr)
  1713. _rtw_memcpy(buf_attr, attr_ptr, attr_len);
  1714. if (len_attr)
  1715. *len_attr = attr_len;
  1716. break;
  1717. } else
  1718. attr_ptr += attr_len;
  1719. }
  1720. return target_attr_ptr;
  1721. }
  1722. /**
  1723. * rtw_get_p2p_attr_content - Search a specific P2P attribute content from a given P2P IE
  1724. * @p2p_ie: Address of P2P IE to search
  1725. * @p2p_ielen: Length limit from p2p_ie
  1726. * @target_attr_id: The attribute ID of P2P attribute to search
  1727. * @buf_content: If not NULL and the P2P attribute is found, P2P attribute content will be copied to the buf starting from buf_content
  1728. * @len_content: If not NULL and the P2P attribute is found, will set to the length of the P2P attribute content
  1729. *
  1730. * Returns: the address of the specific P2P attribute content found, or NULL
  1731. */
  1732. u8 *rtw_get_p2p_attr_content(u8 *p2p_ie, uint p2p_ielen, u8 target_attr_id , u8 *buf_content, uint *len_content)
  1733. {
  1734. u8 *attr_ptr;
  1735. u32 attr_len;
  1736. if (len_content)
  1737. *len_content = 0;
  1738. attr_ptr = rtw_get_p2p_attr(p2p_ie, p2p_ielen, target_attr_id, NULL, &attr_len);
  1739. if (attr_ptr && attr_len) {
  1740. if (buf_content)
  1741. _rtw_memcpy(buf_content, attr_ptr + 3, attr_len - 3);
  1742. if (len_content)
  1743. *len_content = attr_len - 3;
  1744. return attr_ptr + 3;
  1745. }
  1746. return NULL;
  1747. }
  1748. u32 rtw_set_p2p_attr_content(u8 *pbuf, u8 attr_id, u16 attr_len, u8 *pdata_attr)
  1749. {
  1750. u32 a_len;
  1751. *pbuf = attr_id;
  1752. /* *(u16*)(pbuf + 1) = cpu_to_le16(attr_len); */
  1753. RTW_PUT_LE16(pbuf + 1, attr_len);
  1754. if (pdata_attr)
  1755. _rtw_memcpy(pbuf + 3, pdata_attr, attr_len);
  1756. a_len = attr_len + 3;
  1757. return a_len;
  1758. }
  1759. uint rtw_del_p2p_ie(u8 *ies, uint ies_len_ori, const char *msg)
  1760. {
  1761. #define DBG_DEL_P2P_IE 0
  1762. u8 *target_ie;
  1763. u32 target_ie_len;
  1764. uint ies_len = ies_len_ori;
  1765. int index = 0;
  1766. while (1) {
  1767. target_ie = rtw_get_p2p_ie(ies, ies_len, NULL, &target_ie_len);
  1768. if (target_ie && target_ie_len) {
  1769. u8 *next_ie = target_ie + target_ie_len;
  1770. uint remain_len = ies_len - (next_ie - ies);
  1771. if (DBG_DEL_P2P_IE && msg) {
  1772. RTW_INFO("%s %d before\n", __func__, index);
  1773. dump_ies(RTW_DBGDUMP, ies, ies_len);
  1774. RTW_INFO("ies:%p, ies_len:%u\n", ies, ies_len);
  1775. RTW_INFO("target_ie:%p, target_ie_len:%u\n", target_ie, target_ie_len);
  1776. RTW_INFO("next_ie:%p, remain_len:%u\n", next_ie, remain_len);
  1777. }
  1778. _rtw_memmove(target_ie, next_ie, remain_len);
  1779. _rtw_memset(target_ie + remain_len, 0, target_ie_len);
  1780. ies_len -= target_ie_len;
  1781. if (DBG_DEL_P2P_IE && msg) {
  1782. RTW_INFO("%s %d after\n", __func__, index);
  1783. dump_ies(RTW_DBGDUMP, ies, ies_len);
  1784. }
  1785. index++;
  1786. } else
  1787. break;
  1788. }
  1789. return ies_len;
  1790. }
  1791. uint rtw_del_p2p_attr(u8 *ie, uint ielen_ori, u8 attr_id)
  1792. {
  1793. #define DBG_DEL_P2P_ATTR 0
  1794. u8 *target_attr;
  1795. u32 target_attr_len;
  1796. uint ielen = ielen_ori;
  1797. int index = 0;
  1798. while (1) {
  1799. target_attr = rtw_get_p2p_attr(ie, ielen, attr_id, NULL, &target_attr_len);
  1800. if (target_attr && target_attr_len) {
  1801. u8 *next_attr = target_attr + target_attr_len;
  1802. uint remain_len = ielen - (next_attr - ie);
  1803. if (DBG_DEL_P2P_ATTR) {
  1804. RTW_INFO("%s %d before\n", __func__, index);
  1805. dump_ies(RTW_DBGDUMP, ie, ielen);
  1806. RTW_INFO("ie:%p, ielen:%u\n", ie, ielen);
  1807. RTW_INFO("target_attr:%p, target_attr_len:%u\n", target_attr, target_attr_len);
  1808. RTW_INFO("next_attr:%p, remain_len:%u\n", next_attr, remain_len);
  1809. }
  1810. _rtw_memmove(target_attr, next_attr, remain_len);
  1811. _rtw_memset(target_attr + remain_len, 0, target_attr_len);
  1812. *(ie + 1) -= target_attr_len;
  1813. ielen -= target_attr_len;
  1814. if (DBG_DEL_P2P_ATTR) {
  1815. RTW_INFO("%s %d after\n", __func__, index);
  1816. dump_ies(RTW_DBGDUMP, ie, ielen);
  1817. }
  1818. index++;
  1819. } else
  1820. break;
  1821. }
  1822. return ielen;
  1823. }
  1824. inline u8 *rtw_bss_ex_get_p2p_ie(WLAN_BSSID_EX *bss_ex, u8 *p2p_ie, uint *p2p_ielen)
  1825. {
  1826. return rtw_get_p2p_ie(BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex), p2p_ie, p2p_ielen);
  1827. }
  1828. void rtw_bss_ex_del_p2p_ie(WLAN_BSSID_EX *bss_ex)
  1829. {
  1830. #define DBG_BSS_EX_DEL_P2P_IE 0
  1831. u8 *ies = BSS_EX_TLV_IES(bss_ex);
  1832. uint ies_len_ori = BSS_EX_TLV_IES_LEN(bss_ex);
  1833. uint ies_len;
  1834. ies_len = rtw_del_p2p_ie(ies, ies_len_ori, DBG_BSS_EX_DEL_P2P_IE ? __func__ : NULL);
  1835. bss_ex->IELength -= ies_len_ori - ies_len;
  1836. }
  1837. void rtw_bss_ex_del_p2p_attr(WLAN_BSSID_EX *bss_ex, u8 attr_id)
  1838. {
  1839. #define DBG_BSS_EX_DEL_P2P_ATTR 0
  1840. u8 *ies = BSS_EX_TLV_IES(bss_ex);
  1841. uint ies_len = BSS_EX_TLV_IES_LEN(bss_ex);
  1842. u8 *ie;
  1843. uint ie_len, ie_len_ori;
  1844. int index = 0;
  1845. while (1) {
  1846. ie = rtw_get_p2p_ie(ies, ies_len, NULL, &ie_len_ori);
  1847. if (ie) {
  1848. u8 *next_ie_ori = ie + ie_len_ori;
  1849. uint remain_len = bss_ex->IELength - (next_ie_ori - bss_ex->IEs);
  1850. u8 has_target_attr = 0;
  1851. if (DBG_BSS_EX_DEL_P2P_ATTR) {
  1852. if (rtw_get_p2p_attr(ie, ie_len_ori, attr_id, NULL, NULL)) {
  1853. RTW_INFO("%s %d before\n", __func__, index);
  1854. dump_ies(RTW_DBGDUMP, BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex));
  1855. RTW_INFO("ies:%p, ies_len:%u\n", ies, ies_len);
  1856. RTW_INFO("ie:%p, ie_len_ori:%u\n", ie, ie_len_ori);
  1857. RTW_INFO("next_ie_ori:%p, remain_len:%u\n", next_ie_ori, remain_len);
  1858. has_target_attr = 1;
  1859. }
  1860. }
  1861. ie_len = rtw_del_p2p_attr(ie, ie_len_ori, attr_id);
  1862. if (ie_len != ie_len_ori) {
  1863. u8 *next_ie = ie + ie_len;
  1864. _rtw_memmove(next_ie, next_ie_ori, remain_len);
  1865. _rtw_memset(next_ie + remain_len, 0, ie_len_ori - ie_len);
  1866. bss_ex->IELength -= ie_len_ori - ie_len;
  1867. ies = next_ie;
  1868. } else
  1869. ies = next_ie_ori;
  1870. if (DBG_BSS_EX_DEL_P2P_ATTR) {
  1871. if (has_target_attr) {
  1872. RTW_INFO("%s %d after\n", __func__, index);
  1873. dump_ies(RTW_DBGDUMP, BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex));
  1874. }
  1875. }
  1876. ies_len = remain_len;
  1877. index++;
  1878. } else
  1879. break;
  1880. }
  1881. }
  1882. void dump_wfd_ie(void *sel, const u8 *ie, u32 ie_len)
  1883. {
  1884. const u8 *pos = ie;
  1885. u8 id;
  1886. u16 len;
  1887. const u8 *wfd_ie;
  1888. uint wfd_ielen;
  1889. wfd_ie = rtw_get_wfd_ie(ie, ie_len, NULL, &wfd_ielen);
  1890. if (wfd_ie != ie || wfd_ielen == 0)
  1891. return;
  1892. pos += 6;
  1893. while (pos - ie + 3 <= ie_len) {
  1894. id = *pos;
  1895. len = RTW_GET_BE16(pos + 1);
  1896. RTW_PRINT_SEL(sel, "%s ID:%u, LEN:%u%s\n", __func__, id, len
  1897. , ((pos - ie + 3 + len) <= ie_len) ? "" : "(exceed ie_len)");
  1898. pos += (3 + len);
  1899. }
  1900. }
  1901. /**
  1902. * rtw_get_wfd_ie - Search WFD IE from a series of IEs
  1903. * @in_ie: Address of IEs to search
  1904. * @in_len: Length limit from in_ie
  1905. * @wfd_ie: If not NULL and WFD IE is found, WFD IE will be copied to the buf starting from wfd_ie
  1906. * @wfd_ielen: If not NULL and WFD IE is found, will set to the length of the entire WFD IE
  1907. *
  1908. * Returns: The address of the P2P IE found, or NULL
  1909. */
  1910. u8 *rtw_get_wfd_ie(const u8 *in_ie, int in_len, u8 *wfd_ie, uint *wfd_ielen)
  1911. {
  1912. uint cnt;
  1913. const u8 *wfd_ie_ptr = NULL;
  1914. u8 eid, wfd_oui[4] = {0x50, 0x6F, 0x9A, 0x0A};
  1915. if (wfd_ielen)
  1916. *wfd_ielen = 0;
  1917. if (!in_ie || in_len < 0) {
  1918. rtw_warn_on(1);
  1919. return (u8 *)wfd_ie_ptr;
  1920. }
  1921. if (in_len <= 0)
  1922. return (u8 *)wfd_ie_ptr;
  1923. cnt = 0;
  1924. while (cnt + 1 + 4 < in_len) {
  1925. eid = in_ie[cnt];
  1926. if (cnt + 1 + 4 >= MAX_IE_SZ) {
  1927. rtw_warn_on(1);
  1928. return NULL;
  1929. }
  1930. if (eid == WLAN_EID_VENDOR_SPECIFIC && _rtw_memcmp(&in_ie[cnt + 2], wfd_oui, 4) == _TRUE) {
  1931. wfd_ie_ptr = in_ie + cnt;
  1932. if (wfd_ie)
  1933. _rtw_memcpy(wfd_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
  1934. if (wfd_ielen)
  1935. *wfd_ielen = in_ie[cnt + 1] + 2;
  1936. break;
  1937. } else
  1938. cnt += in_ie[cnt + 1] + 2;
  1939. }
  1940. return (u8 *)wfd_ie_ptr;
  1941. }
  1942. /**
  1943. * rtw_get_wfd_attr - Search a specific WFD attribute from a given WFD IE
  1944. * @wfd_ie: Address of WFD IE to search
  1945. * @wfd_ielen: Length limit from wfd_ie
  1946. * @target_attr_id: The attribute ID of WFD attribute to search
  1947. * @buf_attr: If not NULL and the WFD attribute is found, WFD attribute will be copied to the buf starting from buf_attr
  1948. * @len_attr: If not NULL and the WFD attribute is found, will set to the length of the entire WFD attribute
  1949. *
  1950. * Returns: the address of the specific WPS attribute found, or NULL
  1951. */
  1952. u8 *rtw_get_wfd_attr(u8 *wfd_ie, uint wfd_ielen, u8 target_attr_id, u8 *buf_attr, u32 *len_attr)
  1953. {
  1954. u8 *attr_ptr = NULL;
  1955. u8 *target_attr_ptr = NULL;
  1956. u8 wfd_oui[4] = {0x50, 0x6F, 0x9A, 0x0A};
  1957. if (len_attr)
  1958. *len_attr = 0;
  1959. if (!wfd_ie
  1960. || wfd_ielen <= 6
  1961. || (wfd_ie[0] != WLAN_EID_VENDOR_SPECIFIC)
  1962. || (_rtw_memcmp(wfd_ie + 2, wfd_oui, 4) != _TRUE))
  1963. return attr_ptr;
  1964. /* 6 = 1(Element ID) + 1(Length) + 3 (OUI) + 1(OUI Type) */
  1965. attr_ptr = wfd_ie + 6; /* goto first attr */
  1966. while ((attr_ptr - wfd_ie + 3) <= wfd_ielen) {
  1967. /* 3 = 1(Attribute ID) + 2(Length) */
  1968. u8 attr_id = *attr_ptr;
  1969. u16 attr_data_len = RTW_GET_BE16(attr_ptr + 1);
  1970. u16 attr_len = attr_data_len + 3;
  1971. if (0)
  1972. RTW_INFO("%s attr_ptr:%p, id:%u, length:%u\n", __func__, attr_ptr, attr_id, attr_data_len);
  1973. if ((attr_ptr - wfd_ie + attr_len) > wfd_ielen)
  1974. break;
  1975. if (attr_id == target_attr_id) {
  1976. target_attr_ptr = attr_ptr;
  1977. if (buf_attr)
  1978. _rtw_memcpy(buf_attr, attr_ptr, attr_len);
  1979. if (len_attr)
  1980. *len_attr = attr_len;
  1981. break;
  1982. } else
  1983. attr_ptr += attr_len;
  1984. }
  1985. return target_attr_ptr;
  1986. }
  1987. /**
  1988. * rtw_get_wfd_attr_content - Search a specific WFD attribute content from a given WFD IE
  1989. * @wfd_ie: Address of WFD IE to search
  1990. * @wfd_ielen: Length limit from wfd_ie
  1991. * @target_attr_id: The attribute ID of WFD attribute to search
  1992. * @buf_content: If not NULL and the WFD attribute is found, WFD attribute content will be copied to the buf starting from buf_content
  1993. * @len_content: If not NULL and the WFD attribute is found, will set to the length of the WFD attribute content
  1994. *
  1995. * Returns: the address of the specific WFD attribute content found, or NULL
  1996. */
  1997. u8 *rtw_get_wfd_attr_content(u8 *wfd_ie, uint wfd_ielen, u8 target_attr_id, u8 *buf_content, uint *len_content)
  1998. {
  1999. u8 *attr_ptr;
  2000. u32 attr_len;
  2001. if (len_content)
  2002. *len_content = 0;
  2003. attr_ptr = rtw_get_wfd_attr(wfd_ie, wfd_ielen, target_attr_id, NULL, &attr_len);
  2004. if (attr_ptr && attr_len) {
  2005. if (buf_content)
  2006. _rtw_memcpy(buf_content, attr_ptr + 3, attr_len - 3);
  2007. if (len_content)
  2008. *len_content = attr_len - 3;
  2009. return attr_ptr + 3;
  2010. }
  2011. return NULL;
  2012. }
  2013. uint rtw_del_wfd_ie(u8 *ies, uint ies_len_ori, const char *msg)
  2014. {
  2015. #define DBG_DEL_WFD_IE 0
  2016. u8 *target_ie;
  2017. u32 target_ie_len;
  2018. uint ies_len = ies_len_ori;
  2019. int index = 0;
  2020. while (1) {
  2021. target_ie = rtw_get_wfd_ie(ies, ies_len, NULL, &target_ie_len);
  2022. if (target_ie && target_ie_len) {
  2023. u8 *next_ie = target_ie + target_ie_len;
  2024. uint remain_len = ies_len - (next_ie - ies);
  2025. if (DBG_DEL_WFD_IE && msg) {
  2026. RTW_INFO("%s %d before\n", __func__, index);
  2027. dump_ies(RTW_DBGDUMP, ies, ies_len);
  2028. RTW_INFO("ies:%p, ies_len:%u\n", ies, ies_len);
  2029. RTW_INFO("target_ie:%p, target_ie_len:%u\n", target_ie, target_ie_len);
  2030. RTW_INFO("next_ie:%p, remain_len:%u\n", next_ie, remain_len);
  2031. }
  2032. _rtw_memmove(target_ie, next_ie, remain_len);
  2033. _rtw_memset(target_ie + remain_len, 0, target_ie_len);
  2034. ies_len -= target_ie_len;
  2035. if (DBG_DEL_WFD_IE && msg) {
  2036. RTW_INFO("%s %d after\n", __func__, index);
  2037. dump_ies(RTW_DBGDUMP, ies, ies_len);
  2038. }
  2039. index++;
  2040. } else
  2041. break;
  2042. }
  2043. return ies_len;
  2044. }
  2045. uint rtw_del_wfd_attr(u8 *ie, uint ielen_ori, u8 attr_id)
  2046. {
  2047. #define DBG_DEL_WFD_ATTR 0
  2048. u8 *target_attr;
  2049. u32 target_attr_len;
  2050. uint ielen = ielen_ori;
  2051. int index = 0;
  2052. while (1) {
  2053. target_attr = rtw_get_wfd_attr(ie, ielen, attr_id, NULL, &target_attr_len);
  2054. if (target_attr && target_attr_len) {
  2055. u8 *next_attr = target_attr + target_attr_len;
  2056. uint remain_len = ielen - (next_attr - ie);
  2057. if (DBG_DEL_WFD_ATTR) {
  2058. RTW_INFO("%s %d before\n", __func__, index);
  2059. dump_ies(RTW_DBGDUMP, ie, ielen);
  2060. RTW_INFO("ie:%p, ielen:%u\n", ie, ielen);
  2061. RTW_INFO("target_attr:%p, target_attr_len:%u\n", target_attr, target_attr_len);
  2062. RTW_INFO("next_attr:%p, remain_len:%u\n", next_attr, remain_len);
  2063. }
  2064. _rtw_memmove(target_attr, next_attr, remain_len);
  2065. _rtw_memset(target_attr + remain_len, 0, target_attr_len);
  2066. *(ie + 1) -= target_attr_len;
  2067. ielen -= target_attr_len;
  2068. if (DBG_DEL_WFD_ATTR) {
  2069. RTW_INFO("%s %d after\n", __func__, index);
  2070. dump_ies(RTW_DBGDUMP, ie, ielen);
  2071. }
  2072. index++;
  2073. } else
  2074. break;
  2075. }
  2076. return ielen;
  2077. }
  2078. inline u8 *rtw_bss_ex_get_wfd_ie(WLAN_BSSID_EX *bss_ex, u8 *wfd_ie, uint *wfd_ielen)
  2079. {
  2080. return rtw_get_wfd_ie(BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex), wfd_ie, wfd_ielen);
  2081. }
  2082. void rtw_bss_ex_del_wfd_ie(WLAN_BSSID_EX *bss_ex)
  2083. {
  2084. #define DBG_BSS_EX_DEL_WFD_IE 0
  2085. u8 *ies = BSS_EX_TLV_IES(bss_ex);
  2086. uint ies_len_ori = BSS_EX_TLV_IES_LEN(bss_ex);
  2087. uint ies_len;
  2088. ies_len = rtw_del_wfd_ie(ies, ies_len_ori, DBG_BSS_EX_DEL_WFD_IE ? __func__ : NULL);
  2089. bss_ex->IELength -= ies_len_ori - ies_len;
  2090. }
  2091. void rtw_bss_ex_del_wfd_attr(WLAN_BSSID_EX *bss_ex, u8 attr_id)
  2092. {
  2093. #define DBG_BSS_EX_DEL_WFD_ATTR 0
  2094. u8 *ies = BSS_EX_TLV_IES(bss_ex);
  2095. uint ies_len = BSS_EX_TLV_IES_LEN(bss_ex);
  2096. u8 *ie;
  2097. uint ie_len, ie_len_ori;
  2098. int index = 0;
  2099. while (1) {
  2100. ie = rtw_get_wfd_ie(ies, ies_len, NULL, &ie_len_ori);
  2101. if (ie) {
  2102. u8 *next_ie_ori = ie + ie_len_ori;
  2103. uint remain_len = bss_ex->IELength - (next_ie_ori - bss_ex->IEs);
  2104. u8 has_target_attr = 0;
  2105. if (DBG_BSS_EX_DEL_WFD_ATTR) {
  2106. if (rtw_get_wfd_attr(ie, ie_len_ori, attr_id, NULL, NULL)) {
  2107. RTW_INFO("%s %d before\n", __func__, index);
  2108. dump_ies(RTW_DBGDUMP, BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex));
  2109. RTW_INFO("ies:%p, ies_len:%u\n", ies, ies_len);
  2110. RTW_INFO("ie:%p, ie_len_ori:%u\n", ie, ie_len_ori);
  2111. RTW_INFO("next_ie_ori:%p, remain_len:%u\n", next_ie_ori, remain_len);
  2112. has_target_attr = 1;
  2113. }
  2114. }
  2115. ie_len = rtw_del_wfd_attr(ie, ie_len_ori, attr_id);
  2116. if (ie_len != ie_len_ori) {
  2117. u8 *next_ie = ie + ie_len;
  2118. _rtw_memmove(next_ie, next_ie_ori, remain_len);
  2119. _rtw_memset(next_ie + remain_len, 0, ie_len_ori - ie_len);
  2120. bss_ex->IELength -= ie_len_ori - ie_len;
  2121. ies = next_ie;
  2122. } else
  2123. ies = next_ie_ori;
  2124. if (DBG_BSS_EX_DEL_WFD_ATTR) {
  2125. if (has_target_attr) {
  2126. RTW_INFO("%s %d after\n", __func__, index);
  2127. dump_ies(RTW_DBGDUMP, BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex));
  2128. }
  2129. }
  2130. ies_len = remain_len;
  2131. index++;
  2132. } else
  2133. break;
  2134. }
  2135. }
  2136. /* Baron adds to avoid FreeBSD warning */
  2137. int ieee80211_is_empty_essid(const char *essid, int essid_len)
  2138. {
  2139. /* Single white space is for Linksys APs */
  2140. if (essid_len == 1 && essid[0] == ' ')
  2141. return 1;
  2142. /* Otherwise, if the entire essid is 0, we assume it is hidden */
  2143. while (essid_len) {
  2144. essid_len--;
  2145. if (essid[essid_len] != '\0')
  2146. return 0;
  2147. }
  2148. return 1;
  2149. }
  2150. int ieee80211_get_hdrlen(u16 fc)
  2151. {
  2152. int hdrlen = 24;
  2153. switch (WLAN_FC_GET_TYPE(fc)) {
  2154. case RTW_IEEE80211_FTYPE_DATA:
  2155. if (fc & RTW_IEEE80211_STYPE_QOS_DATA)
  2156. hdrlen += 2;
  2157. if ((fc & RTW_IEEE80211_FCTL_FROMDS) && (fc & RTW_IEEE80211_FCTL_TODS))
  2158. hdrlen += 6; /* Addr4 */
  2159. break;
  2160. case RTW_IEEE80211_FTYPE_CTL:
  2161. switch (WLAN_FC_GET_STYPE(fc)) {
  2162. case RTW_IEEE80211_STYPE_CTS:
  2163. case RTW_IEEE80211_STYPE_ACK:
  2164. hdrlen = 10;
  2165. break;
  2166. default:
  2167. hdrlen = 16;
  2168. break;
  2169. }
  2170. break;
  2171. }
  2172. return hdrlen;
  2173. }
  2174. int rtw_get_cipher_info(struct wlan_network *pnetwork)
  2175. {
  2176. u32 wpa_ielen;
  2177. unsigned char *pbuf;
  2178. int group_cipher = 0, pairwise_cipher = 0, is8021x = 0;
  2179. int ret = _FAIL;
  2180. pbuf = rtw_get_wpa_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength - 12);
  2181. if (pbuf && (wpa_ielen > 0)) {
  2182. if (_SUCCESS == rtw_parse_wpa_ie(pbuf, wpa_ielen + 2, &group_cipher, &pairwise_cipher, &is8021x)) {
  2183. pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
  2184. pnetwork->BcnInfo.group_cipher = group_cipher;
  2185. pnetwork->BcnInfo.is_8021x = is8021x;
  2186. ret = _SUCCESS;
  2187. }
  2188. } else {
  2189. pbuf = rtw_get_wpa2_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength - 12);
  2190. if (pbuf && (wpa_ielen > 0)) {
  2191. if (_SUCCESS == rtw_parse_wpa2_ie(pbuf, wpa_ielen + 2, &group_cipher, &pairwise_cipher, &is8021x, NULL)) {
  2192. pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
  2193. pnetwork->BcnInfo.group_cipher = group_cipher;
  2194. pnetwork->BcnInfo.is_8021x = is8021x;
  2195. ret = _SUCCESS;
  2196. }
  2197. }
  2198. }
  2199. return ret;
  2200. }
  2201. void rtw_get_bcn_info(struct wlan_network *pnetwork)
  2202. {
  2203. unsigned short cap = 0;
  2204. u8 bencrypt = 0;
  2205. /* u8 wpa_ie[255],rsn_ie[255]; */
  2206. u16 wpa_len = 0, rsn_len = 0;
  2207. struct HT_info_element *pht_info = NULL;
  2208. struct rtw_ieee80211_ht_cap *pht_cap = NULL;
  2209. unsigned int len;
  2210. unsigned char *p;
  2211. _rtw_memcpy((u8 *)&cap, rtw_get_capability_from_ie(pnetwork->network.IEs), 2);
  2212. cap = le16_to_cpu(cap);
  2213. if (cap & WLAN_CAPABILITY_PRIVACY) {
  2214. bencrypt = 1;
  2215. pnetwork->network.Privacy = 1;
  2216. } else
  2217. pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_OPENSYS;
  2218. rtw_get_sec_ie(pnetwork->network.IEs , pnetwork->network.IELength, NULL, &rsn_len, NULL, &wpa_len);
  2219. if (rsn_len > 0)
  2220. pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA2;
  2221. else if (wpa_len > 0)
  2222. pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA;
  2223. else {
  2224. if (bencrypt)
  2225. pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WEP;
  2226. }
  2227. rtw_get_cipher_info(pnetwork);
  2228. /* get bwmode and ch_offset */
  2229. /* parsing HT_CAP_IE */
  2230. p = rtw_get_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, pnetwork->network.IELength - _FIXED_IE_LENGTH_);
  2231. if (p && len > 0) {
  2232. pht_cap = (struct rtw_ieee80211_ht_cap *)(p + 2);
  2233. pnetwork->BcnInfo.ht_cap_info = pht_cap->cap_info;
  2234. } else
  2235. pnetwork->BcnInfo.ht_cap_info = 0;
  2236. /* parsing HT_INFO_IE */
  2237. p = rtw_get_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_, _HT_ADD_INFO_IE_, &len, pnetwork->network.IELength - _FIXED_IE_LENGTH_);
  2238. if (p && len > 0) {
  2239. pht_info = (struct HT_info_element *)(p + 2);
  2240. pnetwork->BcnInfo.ht_info_infos_0 = pht_info->infos[0];
  2241. } else
  2242. pnetwork->BcnInfo.ht_info_infos_0 = 0;
  2243. }
  2244. u8 rtw_ht_mcsset_to_nss(u8 *supp_mcs_set)
  2245. {
  2246. u8 nss = 1;
  2247. if (supp_mcs_set[3])
  2248. nss = 4;
  2249. else if (supp_mcs_set[2])
  2250. nss = 3;
  2251. else if (supp_mcs_set[1])
  2252. nss = 2;
  2253. else if (supp_mcs_set[0])
  2254. nss = 1;
  2255. else
  2256. RTW_INFO("%s,%d, warning! supp_mcs_set is zero\n", __func__, __LINE__);
  2257. /* RTW_INFO("%s HT: %dSS\n", __FUNCTION__, nss); */
  2258. return nss;
  2259. }
  2260. u32 rtw_ht_mcs_set_to_bitmap(u8 *mcs_set, u8 nss)
  2261. {
  2262. u8 i;
  2263. u32 bitmap = 0;
  2264. for (i = 0; i < nss; i++)
  2265. bitmap |= mcs_set[i] << (i * 8);
  2266. RTW_INFO("ht_mcs_set=%02x %02x %02x %02x, nss=%u, bitmap=%08x\n"
  2267. , mcs_set[0], mcs_set[1], mcs_set[2], mcs_set[3], nss, bitmap);
  2268. return bitmap;
  2269. }
  2270. /* show MCS rate, unit: 100Kbps */
  2271. u16 rtw_mcs_rate(u8 rf_type, u8 bw_40MHz, u8 short_GI, unsigned char *MCS_rate)
  2272. {
  2273. u16 max_rate = 0;
  2274. if (MCS_rate[3]) {
  2275. if (MCS_rate[3] & BIT(7))
  2276. max_rate = (bw_40MHz) ? ((short_GI) ? 6000 : 5400) : ((short_GI) ? 2889 : 2600);
  2277. else if (MCS_rate[3] & BIT(6))
  2278. max_rate = (bw_40MHz) ? ((short_GI) ? 5400 : 4860) : ((short_GI) ? 2600 : 2340);
  2279. else if (MCS_rate[3] & BIT(5))
  2280. max_rate = (bw_40MHz) ? ((short_GI) ? 4800 : 4320) : ((short_GI) ? 2311 : 2080);
  2281. else if (MCS_rate[3] & BIT(4))
  2282. max_rate = (bw_40MHz) ? ((short_GI) ? 3600 : 3240) : ((short_GI) ? 1733 : 1560);
  2283. else if (MCS_rate[3] & BIT(3))
  2284. max_rate = (bw_40MHz) ? ((short_GI) ? 2400 : 2160) : ((short_GI) ? 1156 : 1040);
  2285. else if (MCS_rate[3] & BIT(2))
  2286. max_rate = (bw_40MHz) ? ((short_GI) ? 1800 : 1620) : ((short_GI) ? 867 : 780);
  2287. else if (MCS_rate[3] & BIT(1))
  2288. max_rate = (bw_40MHz) ? ((short_GI) ? 1200 : 1080) : ((short_GI) ? 578 : 520);
  2289. else if (MCS_rate[3] & BIT(0))
  2290. max_rate = (bw_40MHz) ? ((short_GI) ? 600 : 540) : ((short_GI) ? 289 : 260);
  2291. } else if (MCS_rate[2]) {
  2292. if (MCS_rate[2] & BIT(7))
  2293. max_rate = (bw_40MHz) ? ((short_GI) ? 4500 : 4050) : ((short_GI) ? 2167 : 1950);
  2294. else if (MCS_rate[2] & BIT(6))
  2295. max_rate = (bw_40MHz) ? ((short_GI) ? 4050 : 3645) : ((short_GI) ? 1950 : 1750);
  2296. else if (MCS_rate[2] & BIT(5))
  2297. max_rate = (bw_40MHz) ? ((short_GI) ? 3600 : 3240) : ((short_GI) ? 1733 : 1560);
  2298. else if (MCS_rate[2] & BIT(4))
  2299. max_rate = (bw_40MHz) ? ((short_GI) ? 2700 : 2430) : ((short_GI) ? 1300 : 1170);
  2300. else if (MCS_rate[2] & BIT(3))
  2301. max_rate = (bw_40MHz) ? ((short_GI) ? 1800 : 1620) : ((short_GI) ? 867 : 780);
  2302. else if (MCS_rate[2] & BIT(2))
  2303. max_rate = (bw_40MHz) ? ((short_GI) ? 1350 : 1215) : ((short_GI) ? 650 : 585);
  2304. else if (MCS_rate[2] & BIT(1))
  2305. max_rate = (bw_40MHz) ? ((short_GI) ? 900 : 810) : ((short_GI) ? 433 : 390);
  2306. else if (MCS_rate[2] & BIT(0))
  2307. max_rate = (bw_40MHz) ? ((short_GI) ? 450 : 405) : ((short_GI) ? 217 : 195);
  2308. } else if (MCS_rate[1]) {
  2309. if (MCS_rate[1] & BIT(7))
  2310. max_rate = (bw_40MHz) ? ((short_GI) ? 3000 : 2700) : ((short_GI) ? 1444 : 1300);
  2311. else if (MCS_rate[1] & BIT(6))
  2312. max_rate = (bw_40MHz) ? ((short_GI) ? 2700 : 2430) : ((short_GI) ? 1300 : 1170);
  2313. else if (MCS_rate[1] & BIT(5))
  2314. max_rate = (bw_40MHz) ? ((short_GI) ? 2400 : 2160) : ((short_GI) ? 1156 : 1040);
  2315. else if (MCS_rate[1] & BIT(4))
  2316. max_rate = (bw_40MHz) ? ((short_GI) ? 1800 : 1620) : ((short_GI) ? 867 : 780);
  2317. else if (MCS_rate[1] & BIT(3))
  2318. max_rate = (bw_40MHz) ? ((short_GI) ? 1200 : 1080) : ((short_GI) ? 578 : 520);
  2319. else if (MCS_rate[1] & BIT(2))
  2320. max_rate = (bw_40MHz) ? ((short_GI) ? 900 : 810) : ((short_GI) ? 433 : 390);
  2321. else if (MCS_rate[1] & BIT(1))
  2322. max_rate = (bw_40MHz) ? ((short_GI) ? 600 : 540) : ((short_GI) ? 289 : 260);
  2323. else if (MCS_rate[1] & BIT(0))
  2324. max_rate = (bw_40MHz) ? ((short_GI) ? 300 : 270) : ((short_GI) ? 144 : 130);
  2325. } else {
  2326. if (MCS_rate[0] & BIT(7))
  2327. max_rate = (bw_40MHz) ? ((short_GI) ? 1500 : 1350) : ((short_GI) ? 722 : 650);
  2328. else if (MCS_rate[0] & BIT(6))
  2329. max_rate = (bw_40MHz) ? ((short_GI) ? 1350 : 1215) : ((short_GI) ? 650 : 585);
  2330. else if (MCS_rate[0] & BIT(5))
  2331. max_rate = (bw_40MHz) ? ((short_GI) ? 1200 : 1080) : ((short_GI) ? 578 : 520);
  2332. else if (MCS_rate[0] & BIT(4))
  2333. max_rate = (bw_40MHz) ? ((short_GI) ? 900 : 810) : ((short_GI) ? 433 : 390);
  2334. else if (MCS_rate[0] & BIT(3))
  2335. max_rate = (bw_40MHz) ? ((short_GI) ? 600 : 540) : ((short_GI) ? 289 : 260);
  2336. else if (MCS_rate[0] & BIT(2))
  2337. max_rate = (bw_40MHz) ? ((short_GI) ? 450 : 405) : ((short_GI) ? 217 : 195);
  2338. else if (MCS_rate[0] & BIT(1))
  2339. max_rate = (bw_40MHz) ? ((short_GI) ? 300 : 270) : ((short_GI) ? 144 : 130);
  2340. else if (MCS_rate[0] & BIT(0))
  2341. max_rate = (bw_40MHz) ? ((short_GI) ? 150 : 135) : ((short_GI) ? 72 : 65);
  2342. }
  2343. return max_rate;
  2344. }
  2345. int rtw_action_frame_parse(const u8 *frame, u32 frame_len, u8 *category, u8 *action)
  2346. {
  2347. const u8 *frame_body = frame + sizeof(struct rtw_ieee80211_hdr_3addr);
  2348. u16 fc;
  2349. u8 c;
  2350. u8 a = ACT_PUBLIC_MAX;
  2351. fc = le16_to_cpu(((struct rtw_ieee80211_hdr_3addr *)frame)->frame_ctl);
  2352. if ((fc & (RTW_IEEE80211_FCTL_FTYPE | RTW_IEEE80211_FCTL_STYPE))
  2353. != (RTW_IEEE80211_FTYPE_MGMT | RTW_IEEE80211_STYPE_ACTION)
  2354. )
  2355. return _FALSE;
  2356. c = frame_body[0];
  2357. switch (c) {
  2358. case RTW_WLAN_CATEGORY_P2P: /* vendor-specific */
  2359. break;
  2360. default:
  2361. a = frame_body[1];
  2362. }
  2363. if (category)
  2364. *category = c;
  2365. if (action)
  2366. *action = a;
  2367. return _TRUE;
  2368. }
  2369. static const char *_action_public_str[] = {
  2370. "ACT_PUB_BSSCOEXIST",
  2371. "ACT_PUB_DSE_ENABLE",
  2372. "ACT_PUB_DSE_DEENABLE",
  2373. "ACT_PUB_DSE_REG_LOCATION",
  2374. "ACT_PUB_EXT_CHL_SWITCH",
  2375. "ACT_PUB_DSE_MSR_REQ",
  2376. "ACT_PUB_DSE_MSR_RPRT",
  2377. "ACT_PUB_MP",
  2378. "ACT_PUB_DSE_PWR_CONSTRAINT",
  2379. "ACT_PUB_VENDOR",
  2380. "ACT_PUB_GAS_INITIAL_REQ",
  2381. "ACT_PUB_GAS_INITIAL_RSP",
  2382. "ACT_PUB_GAS_COMEBACK_REQ",
  2383. "ACT_PUB_GAS_COMEBACK_RSP",
  2384. "ACT_PUB_TDLS_DISCOVERY_RSP",
  2385. "ACT_PUB_LOCATION_TRACK",
  2386. "ACT_PUB_RSVD",
  2387. };
  2388. const char *action_public_str(u8 action)
  2389. {
  2390. action = (action >= ACT_PUBLIC_MAX) ? ACT_PUBLIC_MAX : action;
  2391. return _action_public_str[action];
  2392. }