rtw_ieee80211.c 72 KB

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