scan.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NXP Wireless LAN device driver: scan ioctl and command handling
  4. *
  5. * Copyright 2011-2020 NXP
  6. */
  7. #include "decl.h"
  8. #include "ioctl.h"
  9. #include "util.h"
  10. #include "fw.h"
  11. #include "main.h"
  12. #include "11n.h"
  13. #include "cfg80211.h"
  14. /* The maximum number of channels the firmware can scan per command */
  15. #define MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN 14
  16. #define MWIFIEX_DEF_CHANNELS_PER_SCAN_CMD 4
  17. /* Memory needed to store a max sized Channel List TLV for a firmware scan */
  18. #define CHAN_TLV_MAX_SIZE (sizeof(struct mwifiex_ie_types_header) \
  19. + (MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN \
  20. *sizeof(struct mwifiex_chan_scan_param_set)))
  21. /* Memory needed to store supported rate */
  22. #define RATE_TLV_MAX_SIZE (sizeof(struct mwifiex_ie_types_rates_param_set) \
  23. + HOSTCMD_SUPPORTED_RATES)
  24. /* Memory needed to store a max number/size WildCard SSID TLV for a firmware
  25. scan */
  26. #define WILDCARD_SSID_TLV_MAX_SIZE \
  27. (MWIFIEX_MAX_SSID_LIST_LENGTH * \
  28. (sizeof(struct mwifiex_ie_types_wildcard_ssid_params) \
  29. + IEEE80211_MAX_SSID_LEN))
  30. /* Maximum memory needed for a mwifiex_scan_cmd_config with all TLVs at max */
  31. #define MAX_SCAN_CFG_ALLOC (sizeof(struct mwifiex_scan_cmd_config) \
  32. + sizeof(struct mwifiex_ie_types_num_probes) \
  33. + sizeof(struct mwifiex_ie_types_htcap) \
  34. + CHAN_TLV_MAX_SIZE \
  35. + RATE_TLV_MAX_SIZE \
  36. + WILDCARD_SSID_TLV_MAX_SIZE)
  37. union mwifiex_scan_cmd_config_tlv {
  38. /* Scan configuration (variable length) */
  39. struct mwifiex_scan_cmd_config config;
  40. /* Max allocated block */
  41. u8 config_alloc_buf[MAX_SCAN_CFG_ALLOC];
  42. };
  43. enum cipher_suite {
  44. CIPHER_SUITE_TKIP,
  45. CIPHER_SUITE_CCMP,
  46. CIPHER_SUITE_MAX
  47. };
  48. static u8 mwifiex_wpa_oui[CIPHER_SUITE_MAX][4] = {
  49. { 0x00, 0x50, 0xf2, 0x02 }, /* TKIP */
  50. { 0x00, 0x50, 0xf2, 0x04 }, /* AES */
  51. };
  52. static u8 mwifiex_rsn_oui[CIPHER_SUITE_MAX][4] = {
  53. { 0x00, 0x0f, 0xac, 0x02 }, /* TKIP */
  54. { 0x00, 0x0f, 0xac, 0x04 }, /* AES */
  55. };
  56. static void
  57. _dbg_security_flags(int log_level, const char *func, const char *desc,
  58. struct mwifiex_private *priv,
  59. struct mwifiex_bssdescriptor *bss_desc)
  60. {
  61. _mwifiex_dbg(priv->adapter, log_level,
  62. "info: %s: %s:\twpa_ie=%#x wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s\tEncMode=%#x privacy=%#x\n",
  63. func, desc,
  64. bss_desc->bcn_wpa_ie ?
  65. bss_desc->bcn_wpa_ie->vend_hdr.element_id : 0,
  66. bss_desc->bcn_rsn_ie ?
  67. bss_desc->bcn_rsn_ie->ieee_hdr.element_id : 0,
  68. priv->sec_info.wep_enabled ? "e" : "d",
  69. priv->sec_info.wpa_enabled ? "e" : "d",
  70. priv->sec_info.wpa2_enabled ? "e" : "d",
  71. priv->sec_info.encryption_mode,
  72. bss_desc->privacy);
  73. }
  74. #define dbg_security_flags(mask, desc, priv, bss_desc) \
  75. _dbg_security_flags(MWIFIEX_DBG_##mask, desc, __func__, priv, bss_desc)
  76. static bool
  77. has_ieee_hdr(struct ieee_types_generic *ie, u8 key)
  78. {
  79. return (ie && ie->ieee_hdr.element_id == key);
  80. }
  81. static bool
  82. has_vendor_hdr(struct ieee_types_vendor_specific *ie, u8 key)
  83. {
  84. return (ie && ie->vend_hdr.element_id == key);
  85. }
  86. /*
  87. * This function parses a given IE for a given OUI.
  88. *
  89. * This is used to parse a WPA/RSN IE to find if it has
  90. * a given oui in PTK.
  91. */
  92. static u8
  93. mwifiex_search_oui_in_ie(struct ie_body *iebody, u8 *oui)
  94. {
  95. u8 count;
  96. count = iebody->ptk_cnt[0];
  97. /* There could be multiple OUIs for PTK hence
  98. 1) Take the length.
  99. 2) Check all the OUIs for AES.
  100. 3) If one of them is AES then pass success. */
  101. while (count) {
  102. if (!memcmp(iebody->ptk_body, oui, sizeof(iebody->ptk_body)))
  103. return MWIFIEX_OUI_PRESENT;
  104. --count;
  105. if (count)
  106. iebody = (struct ie_body *) ((u8 *) iebody +
  107. sizeof(iebody->ptk_body));
  108. }
  109. pr_debug("info: %s: OUI is not found in PTK\n", __func__);
  110. return MWIFIEX_OUI_NOT_PRESENT;
  111. }
  112. /*
  113. * This function checks if a given OUI is present in a RSN IE.
  114. *
  115. * The function first checks if a RSN IE is present or not in the
  116. * BSS descriptor. It tries to locate the OUI only if such an IE is
  117. * present.
  118. */
  119. static u8
  120. mwifiex_is_rsn_oui_present(struct mwifiex_bssdescriptor *bss_desc, u32 cipher)
  121. {
  122. u8 *oui;
  123. struct ie_body *iebody;
  124. u8 ret = MWIFIEX_OUI_NOT_PRESENT;
  125. if (has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN)) {
  126. iebody = (struct ie_body *)
  127. (((u8 *) bss_desc->bcn_rsn_ie->data) +
  128. RSN_GTK_OUI_OFFSET);
  129. oui = &mwifiex_rsn_oui[cipher][0];
  130. ret = mwifiex_search_oui_in_ie(iebody, oui);
  131. if (ret)
  132. return ret;
  133. }
  134. return ret;
  135. }
  136. /*
  137. * This function checks if a given OUI is present in a WPA IE.
  138. *
  139. * The function first checks if a WPA IE is present or not in the
  140. * BSS descriptor. It tries to locate the OUI only if such an IE is
  141. * present.
  142. */
  143. static u8
  144. mwifiex_is_wpa_oui_present(struct mwifiex_bssdescriptor *bss_desc, u32 cipher)
  145. {
  146. u8 *oui;
  147. struct ie_body *iebody;
  148. u8 ret = MWIFIEX_OUI_NOT_PRESENT;
  149. if (has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC)) {
  150. iebody = (struct ie_body *)((u8 *)bss_desc->bcn_wpa_ie->data +
  151. WPA_GTK_OUI_OFFSET);
  152. oui = &mwifiex_wpa_oui[cipher][0];
  153. ret = mwifiex_search_oui_in_ie(iebody, oui);
  154. if (ret)
  155. return ret;
  156. }
  157. return ret;
  158. }
  159. /*
  160. * This function compares two SSIDs and checks if they match.
  161. */
  162. s32
  163. mwifiex_ssid_cmp(struct cfg80211_ssid *ssid1, struct cfg80211_ssid *ssid2)
  164. {
  165. if (!ssid1 || !ssid2 || (ssid1->ssid_len != ssid2->ssid_len))
  166. return -1;
  167. return memcmp(ssid1->ssid, ssid2->ssid, ssid1->ssid_len);
  168. }
  169. /*
  170. * This function checks if wapi is enabled in driver and scanned network is
  171. * compatible with it.
  172. */
  173. static bool
  174. mwifiex_is_bss_wapi(struct mwifiex_private *priv,
  175. struct mwifiex_bssdescriptor *bss_desc)
  176. {
  177. if (priv->sec_info.wapi_enabled &&
  178. has_ieee_hdr(bss_desc->bcn_wapi_ie, WLAN_EID_BSS_AC_ACCESS_DELAY))
  179. return true;
  180. return false;
  181. }
  182. /*
  183. * This function checks if driver is configured with no security mode and
  184. * scanned network is compatible with it.
  185. */
  186. static bool
  187. mwifiex_is_bss_no_sec(struct mwifiex_private *priv,
  188. struct mwifiex_bssdescriptor *bss_desc)
  189. {
  190. if (!priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
  191. !priv->sec_info.wpa2_enabled &&
  192. !has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC) &&
  193. !has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN) &&
  194. !priv->sec_info.encryption_mode && !bss_desc->privacy) {
  195. return true;
  196. }
  197. return false;
  198. }
  199. /*
  200. * This function checks if static WEP is enabled in driver and scanned network
  201. * is compatible with it.
  202. */
  203. static bool
  204. mwifiex_is_bss_static_wep(struct mwifiex_private *priv,
  205. struct mwifiex_bssdescriptor *bss_desc)
  206. {
  207. if (priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
  208. !priv->sec_info.wpa2_enabled && bss_desc->privacy) {
  209. return true;
  210. }
  211. return false;
  212. }
  213. /*
  214. * This function checks if wpa is enabled in driver and scanned network is
  215. * compatible with it.
  216. */
  217. static bool
  218. mwifiex_is_bss_wpa(struct mwifiex_private *priv,
  219. struct mwifiex_bssdescriptor *bss_desc)
  220. {
  221. if (!priv->sec_info.wep_enabled && priv->sec_info.wpa_enabled &&
  222. !priv->sec_info.wpa2_enabled &&
  223. has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC)
  224. /*
  225. * Privacy bit may NOT be set in some APs like
  226. * LinkSys WRT54G && bss_desc->privacy
  227. */
  228. ) {
  229. dbg_security_flags(INFO, "WPA", priv, bss_desc);
  230. return true;
  231. }
  232. return false;
  233. }
  234. /*
  235. * This function checks if wpa2 is enabled in driver and scanned network is
  236. * compatible with it.
  237. */
  238. static bool
  239. mwifiex_is_bss_wpa2(struct mwifiex_private *priv,
  240. struct mwifiex_bssdescriptor *bss_desc)
  241. {
  242. if (!priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
  243. priv->sec_info.wpa2_enabled &&
  244. has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN)) {
  245. /*
  246. * Privacy bit may NOT be set in some APs like
  247. * LinkSys WRT54G && bss_desc->privacy
  248. */
  249. dbg_security_flags(INFO, "WAP2", priv, bss_desc);
  250. return true;
  251. }
  252. return false;
  253. }
  254. /*
  255. * This function checks if adhoc AES is enabled in driver and scanned network is
  256. * compatible with it.
  257. */
  258. static bool
  259. mwifiex_is_bss_adhoc_aes(struct mwifiex_private *priv,
  260. struct mwifiex_bssdescriptor *bss_desc)
  261. {
  262. if (!priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
  263. !priv->sec_info.wpa2_enabled &&
  264. !has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC) &&
  265. !has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN) &&
  266. !priv->sec_info.encryption_mode && bss_desc->privacy) {
  267. return true;
  268. }
  269. return false;
  270. }
  271. /*
  272. * This function checks if dynamic WEP is enabled in driver and scanned network
  273. * is compatible with it.
  274. */
  275. static bool
  276. mwifiex_is_bss_dynamic_wep(struct mwifiex_private *priv,
  277. struct mwifiex_bssdescriptor *bss_desc)
  278. {
  279. if (!priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
  280. !priv->sec_info.wpa2_enabled &&
  281. !has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC) &&
  282. !has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN) &&
  283. priv->sec_info.encryption_mode && bss_desc->privacy) {
  284. dbg_security_flags(INFO, "dynamic", priv, bss_desc);
  285. return true;
  286. }
  287. return false;
  288. }
  289. /*
  290. * This function checks if a scanned network is compatible with the driver
  291. * settings.
  292. *
  293. * WEP WPA WPA2 ad-hoc encrypt Network
  294. * enabled enabled enabled AES mode Privacy WPA WPA2 Compatible
  295. * 0 0 0 0 NONE 0 0 0 yes No security
  296. * 0 1 0 0 x 1x 1 x yes WPA (disable
  297. * HT if no AES)
  298. * 0 0 1 0 x 1x x 1 yes WPA2 (disable
  299. * HT if no AES)
  300. * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
  301. * 1 0 0 0 NONE 1 0 0 yes Static WEP
  302. * (disable HT)
  303. * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
  304. *
  305. * Compatibility is not matched while roaming, except for mode.
  306. */
  307. static s32
  308. mwifiex_is_network_compatible(struct mwifiex_private *priv,
  309. struct mwifiex_bssdescriptor *bss_desc, u32 mode)
  310. {
  311. struct mwifiex_adapter *adapter = priv->adapter;
  312. bss_desc->disable_11n = false;
  313. /* Don't check for compatibility if roaming */
  314. if (priv->media_connected &&
  315. (priv->bss_mode == NL80211_IFTYPE_STATION) &&
  316. (bss_desc->bss_mode == NL80211_IFTYPE_STATION))
  317. return 0;
  318. if (priv->wps.session_enable) {
  319. mwifiex_dbg(adapter, IOCTL,
  320. "info: return success directly in WPS period\n");
  321. return 0;
  322. }
  323. if (bss_desc->chan_sw_ie_present) {
  324. mwifiex_dbg(adapter, INFO,
  325. "Don't connect to AP with WLAN_EID_CHANNEL_SWITCH\n");
  326. return -1;
  327. }
  328. if (mwifiex_is_bss_wapi(priv, bss_desc)) {
  329. mwifiex_dbg(adapter, INFO,
  330. "info: return success for WAPI AP\n");
  331. return 0;
  332. }
  333. if (bss_desc->bss_mode == mode) {
  334. if (mwifiex_is_bss_no_sec(priv, bss_desc)) {
  335. /* No security */
  336. return 0;
  337. } else if (mwifiex_is_bss_static_wep(priv, bss_desc)) {
  338. /* Static WEP enabled */
  339. mwifiex_dbg(adapter, INFO,
  340. "info: Disable 11n in WEP mode.\n");
  341. bss_desc->disable_11n = true;
  342. return 0;
  343. } else if (mwifiex_is_bss_wpa(priv, bss_desc)) {
  344. /* WPA enabled */
  345. if (((priv->adapter->config_bands & BAND_GN ||
  346. priv->adapter->config_bands & BAND_AN) &&
  347. bss_desc->bcn_ht_cap) &&
  348. !mwifiex_is_wpa_oui_present(bss_desc,
  349. CIPHER_SUITE_CCMP)) {
  350. if (mwifiex_is_wpa_oui_present
  351. (bss_desc, CIPHER_SUITE_TKIP)) {
  352. mwifiex_dbg(adapter, INFO,
  353. "info: Disable 11n if AES\t"
  354. "is not supported by AP\n");
  355. bss_desc->disable_11n = true;
  356. } else {
  357. return -1;
  358. }
  359. }
  360. return 0;
  361. } else if (mwifiex_is_bss_wpa2(priv, bss_desc)) {
  362. /* WPA2 enabled */
  363. if (((priv->adapter->config_bands & BAND_GN ||
  364. priv->adapter->config_bands & BAND_AN) &&
  365. bss_desc->bcn_ht_cap) &&
  366. !mwifiex_is_rsn_oui_present(bss_desc,
  367. CIPHER_SUITE_CCMP)) {
  368. if (mwifiex_is_rsn_oui_present
  369. (bss_desc, CIPHER_SUITE_TKIP)) {
  370. mwifiex_dbg(adapter, INFO,
  371. "info: Disable 11n if AES\t"
  372. "is not supported by AP\n");
  373. bss_desc->disable_11n = true;
  374. } else {
  375. return -1;
  376. }
  377. }
  378. return 0;
  379. } else if (mwifiex_is_bss_adhoc_aes(priv, bss_desc)) {
  380. /* Ad-hoc AES enabled */
  381. return 0;
  382. } else if (mwifiex_is_bss_dynamic_wep(priv, bss_desc)) {
  383. /* Dynamic WEP enabled */
  384. return 0;
  385. }
  386. /* Security doesn't match */
  387. dbg_security_flags(ERROR, "failed", priv, bss_desc);
  388. return -1;
  389. }
  390. /* Mode doesn't match */
  391. return -1;
  392. }
  393. /*
  394. * This function creates a channel list for the driver to scan, based
  395. * on region/band information.
  396. *
  397. * This routine is used for any scan that is not provided with a
  398. * specific channel list to scan.
  399. */
  400. static int
  401. mwifiex_scan_create_channel_list(struct mwifiex_private *priv,
  402. const struct mwifiex_user_scan_cfg
  403. *user_scan_in,
  404. struct mwifiex_chan_scan_param_set
  405. *scan_chan_list,
  406. u8 filtered_scan)
  407. {
  408. enum nl80211_band band;
  409. struct ieee80211_supported_band *sband;
  410. struct ieee80211_channel *ch;
  411. struct mwifiex_adapter *adapter = priv->adapter;
  412. int chan_idx = 0, i;
  413. for (band = 0; (band < NUM_NL80211_BANDS) ; band++) {
  414. if (!priv->wdev.wiphy->bands[band])
  415. continue;
  416. sband = priv->wdev.wiphy->bands[band];
  417. for (i = 0; (i < sband->n_channels) ; i++) {
  418. ch = &sband->channels[i];
  419. if (ch->flags & IEEE80211_CHAN_DISABLED)
  420. continue;
  421. scan_chan_list[chan_idx].radio_type = band;
  422. if (user_scan_in &&
  423. user_scan_in->chan_list[0].scan_time)
  424. scan_chan_list[chan_idx].max_scan_time =
  425. cpu_to_le16((u16) user_scan_in->
  426. chan_list[0].scan_time);
  427. else if ((ch->flags & IEEE80211_CHAN_NO_IR) ||
  428. (ch->flags & IEEE80211_CHAN_RADAR))
  429. scan_chan_list[chan_idx].max_scan_time =
  430. cpu_to_le16(adapter->passive_scan_time);
  431. else
  432. scan_chan_list[chan_idx].max_scan_time =
  433. cpu_to_le16(adapter->active_scan_time);
  434. if (ch->flags & IEEE80211_CHAN_NO_IR)
  435. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  436. |= (MWIFIEX_PASSIVE_SCAN |
  437. MWIFIEX_HIDDEN_SSID_REPORT);
  438. else
  439. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  440. &= ~MWIFIEX_PASSIVE_SCAN;
  441. scan_chan_list[chan_idx].chan_number =
  442. (u32) ch->hw_value;
  443. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  444. |= MWIFIEX_DISABLE_CHAN_FILT;
  445. if (filtered_scan &&
  446. !((ch->flags & IEEE80211_CHAN_NO_IR) ||
  447. (ch->flags & IEEE80211_CHAN_RADAR)))
  448. scan_chan_list[chan_idx].max_scan_time =
  449. cpu_to_le16(adapter->specific_scan_time);
  450. chan_idx++;
  451. }
  452. }
  453. return chan_idx;
  454. }
  455. /* This function creates a channel list tlv for bgscan config, based
  456. * on region/band information.
  457. */
  458. static int
  459. mwifiex_bgscan_create_channel_list(struct mwifiex_private *priv,
  460. const struct mwifiex_bg_scan_cfg
  461. *bgscan_cfg_in,
  462. struct mwifiex_chan_scan_param_set
  463. *scan_chan_list)
  464. {
  465. enum nl80211_band band;
  466. struct ieee80211_supported_band *sband;
  467. struct ieee80211_channel *ch;
  468. struct mwifiex_adapter *adapter = priv->adapter;
  469. int chan_idx = 0, i;
  470. for (band = 0; (band < NUM_NL80211_BANDS); band++) {
  471. if (!priv->wdev.wiphy->bands[band])
  472. continue;
  473. sband = priv->wdev.wiphy->bands[band];
  474. for (i = 0; (i < sband->n_channels) ; i++) {
  475. ch = &sband->channels[i];
  476. if (ch->flags & IEEE80211_CHAN_DISABLED)
  477. continue;
  478. scan_chan_list[chan_idx].radio_type = band;
  479. if (bgscan_cfg_in->chan_list[0].scan_time)
  480. scan_chan_list[chan_idx].max_scan_time =
  481. cpu_to_le16((u16)bgscan_cfg_in->
  482. chan_list[0].scan_time);
  483. else if (ch->flags & IEEE80211_CHAN_NO_IR)
  484. scan_chan_list[chan_idx].max_scan_time =
  485. cpu_to_le16(adapter->passive_scan_time);
  486. else
  487. scan_chan_list[chan_idx].max_scan_time =
  488. cpu_to_le16(adapter->
  489. specific_scan_time);
  490. if (ch->flags & IEEE80211_CHAN_NO_IR)
  491. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  492. |= MWIFIEX_PASSIVE_SCAN;
  493. else
  494. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  495. &= ~MWIFIEX_PASSIVE_SCAN;
  496. scan_chan_list[chan_idx].chan_number =
  497. (u32)ch->hw_value;
  498. chan_idx++;
  499. }
  500. }
  501. return chan_idx;
  502. }
  503. /* This function appends rate TLV to scan config command. */
  504. static int
  505. mwifiex_append_rate_tlv(struct mwifiex_private *priv,
  506. struct mwifiex_scan_cmd_config *scan_cfg_out,
  507. u8 radio)
  508. {
  509. struct mwifiex_ie_types_rates_param_set *rates_tlv;
  510. u8 rates[MWIFIEX_SUPPORTED_RATES], *tlv_pos;
  511. u32 rates_size;
  512. memset(rates, 0, sizeof(rates));
  513. tlv_pos = (u8 *)scan_cfg_out->tlv_buf + scan_cfg_out->tlv_buf_len;
  514. if (priv->scan_request)
  515. rates_size = mwifiex_get_rates_from_cfg80211(priv, rates,
  516. radio);
  517. else
  518. rates_size = mwifiex_get_supported_rates(priv, rates);
  519. mwifiex_dbg(priv->adapter, CMD,
  520. "info: SCAN_CMD: Rates size = %d\n",
  521. rates_size);
  522. rates_tlv = (struct mwifiex_ie_types_rates_param_set *)tlv_pos;
  523. rates_tlv->header.type = cpu_to_le16(WLAN_EID_SUPP_RATES);
  524. rates_tlv->header.len = cpu_to_le16((u16) rates_size);
  525. memcpy(rates_tlv->rates, rates, rates_size);
  526. scan_cfg_out->tlv_buf_len += sizeof(rates_tlv->header) + rates_size;
  527. return rates_size;
  528. }
  529. /*
  530. * This function constructs and sends multiple scan config commands to
  531. * the firmware.
  532. *
  533. * Previous routines in the code flow have created a scan command configuration
  534. * with any requested TLVs. This function splits the channel TLV into maximum
  535. * channels supported per scan lists and sends the portion of the channel TLV,
  536. * along with the other TLVs, to the firmware.
  537. */
  538. static int
  539. mwifiex_scan_channel_list(struct mwifiex_private *priv,
  540. u32 max_chan_per_scan, u8 filtered_scan,
  541. struct mwifiex_scan_cmd_config *scan_cfg_out,
  542. struct mwifiex_ie_types_chan_list_param_set
  543. *chan_tlv_out,
  544. struct mwifiex_chan_scan_param_set *scan_chan_list)
  545. {
  546. struct mwifiex_adapter *adapter = priv->adapter;
  547. int ret = 0;
  548. struct mwifiex_chan_scan_param_set *tmp_chan_list;
  549. struct mwifiex_chan_scan_param_set *start_chan;
  550. u32 tlv_idx, rates_size, cmd_no;
  551. u32 total_scan_time;
  552. u32 done_early;
  553. u8 radio_type;
  554. if (!scan_cfg_out || !chan_tlv_out || !scan_chan_list) {
  555. mwifiex_dbg(priv->adapter, ERROR,
  556. "info: Scan: Null detect: %p, %p, %p\n",
  557. scan_cfg_out, chan_tlv_out, scan_chan_list);
  558. return -1;
  559. }
  560. /* Check csa channel expiry before preparing scan list */
  561. mwifiex_11h_get_csa_closed_channel(priv);
  562. chan_tlv_out->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
  563. /* Set the temp channel struct pointer to the start of the desired
  564. list */
  565. tmp_chan_list = scan_chan_list;
  566. /* Loop through the desired channel list, sending a new firmware scan
  567. commands for each max_chan_per_scan channels (or for 1,6,11
  568. individually if configured accordingly) */
  569. while (tmp_chan_list->chan_number) {
  570. tlv_idx = 0;
  571. total_scan_time = 0;
  572. radio_type = 0;
  573. chan_tlv_out->header.len = 0;
  574. start_chan = tmp_chan_list;
  575. done_early = false;
  576. /*
  577. * Construct the Channel TLV for the scan command. Continue to
  578. * insert channel TLVs until:
  579. * - the tlv_idx hits the maximum configured per scan command
  580. * - the next channel to insert is 0 (end of desired channel
  581. * list)
  582. * - done_early is set (controlling individual scanning of
  583. * 1,6,11)
  584. */
  585. while (tlv_idx < max_chan_per_scan &&
  586. tmp_chan_list->chan_number && !done_early) {
  587. if (tmp_chan_list->chan_number == priv->csa_chan) {
  588. tmp_chan_list++;
  589. continue;
  590. }
  591. radio_type = tmp_chan_list->radio_type;
  592. mwifiex_dbg(priv->adapter, INFO,
  593. "info: Scan: Chan(%3d), Radio(%d),\t"
  594. "Mode(%d, %d), Dur(%d)\n",
  595. tmp_chan_list->chan_number,
  596. tmp_chan_list->radio_type,
  597. tmp_chan_list->chan_scan_mode_bitmap
  598. & MWIFIEX_PASSIVE_SCAN,
  599. (tmp_chan_list->chan_scan_mode_bitmap
  600. & MWIFIEX_DISABLE_CHAN_FILT) >> 1,
  601. le16_to_cpu(tmp_chan_list->max_scan_time));
  602. /* Copy the current channel TLV to the command being
  603. prepared */
  604. memcpy(chan_tlv_out->chan_scan_param + tlv_idx,
  605. tmp_chan_list,
  606. sizeof(chan_tlv_out->chan_scan_param));
  607. /* Increment the TLV header length by the size
  608. appended */
  609. le16_unaligned_add_cpu(&chan_tlv_out->header.len,
  610. sizeof(
  611. chan_tlv_out->chan_scan_param));
  612. /*
  613. * The tlv buffer length is set to the number of bytes
  614. * of the between the channel tlv pointer and the start
  615. * of the tlv buffer. This compensates for any TLVs
  616. * that were appended before the channel list.
  617. */
  618. scan_cfg_out->tlv_buf_len = (u32) ((u8 *) chan_tlv_out -
  619. scan_cfg_out->tlv_buf);
  620. /* Add the size of the channel tlv header and the data
  621. length */
  622. scan_cfg_out->tlv_buf_len +=
  623. (sizeof(chan_tlv_out->header)
  624. + le16_to_cpu(chan_tlv_out->header.len));
  625. /* Increment the index to the channel tlv we are
  626. constructing */
  627. tlv_idx++;
  628. /* Count the total scan time per command */
  629. total_scan_time +=
  630. le16_to_cpu(tmp_chan_list->max_scan_time);
  631. done_early = false;
  632. /* Stop the loop if the *current* channel is in the
  633. 1,6,11 set and we are not filtering on a BSSID
  634. or SSID. */
  635. if (!filtered_scan &&
  636. (tmp_chan_list->chan_number == 1 ||
  637. tmp_chan_list->chan_number == 6 ||
  638. tmp_chan_list->chan_number == 11))
  639. done_early = true;
  640. /* Increment the tmp pointer to the next channel to
  641. be scanned */
  642. tmp_chan_list++;
  643. /* Stop the loop if the *next* channel is in the 1,6,11
  644. set. This will cause it to be the only channel
  645. scanned on the next interation */
  646. if (!filtered_scan &&
  647. (tmp_chan_list->chan_number == 1 ||
  648. tmp_chan_list->chan_number == 6 ||
  649. tmp_chan_list->chan_number == 11))
  650. done_early = true;
  651. }
  652. /* The total scan time should be less than scan command timeout
  653. value */
  654. if (total_scan_time > MWIFIEX_MAX_TOTAL_SCAN_TIME) {
  655. mwifiex_dbg(priv->adapter, ERROR,
  656. "total scan time %dms\t"
  657. "is over limit (%dms), scan skipped\n",
  658. total_scan_time,
  659. MWIFIEX_MAX_TOTAL_SCAN_TIME);
  660. ret = -1;
  661. break;
  662. }
  663. rates_size = mwifiex_append_rate_tlv(priv, scan_cfg_out,
  664. radio_type);
  665. priv->adapter->scan_channels = start_chan;
  666. /* Send the scan command to the firmware with the specified
  667. cfg */
  668. if (priv->adapter->ext_scan)
  669. cmd_no = HostCmd_CMD_802_11_SCAN_EXT;
  670. else
  671. cmd_no = HostCmd_CMD_802_11_SCAN;
  672. ret = mwifiex_send_cmd(priv, cmd_no, HostCmd_ACT_GEN_SET,
  673. 0, scan_cfg_out, false);
  674. /* rate IE is updated per scan command but same starting
  675. * pointer is used each time so that rate IE from earlier
  676. * scan_cfg_out->buf is overwritten with new one.
  677. */
  678. scan_cfg_out->tlv_buf_len -=
  679. sizeof(struct mwifiex_ie_types_header) + rates_size;
  680. if (ret) {
  681. mwifiex_cancel_pending_scan_cmd(adapter);
  682. break;
  683. }
  684. }
  685. if (ret)
  686. return -1;
  687. return 0;
  688. }
  689. /*
  690. * This function constructs a scan command configuration structure to use
  691. * in scan commands.
  692. *
  693. * Application layer or other functions can invoke network scanning
  694. * with a scan configuration supplied in a user scan configuration structure.
  695. * This structure is used as the basis of one or many scan command configuration
  696. * commands that are sent to the command processing module and eventually to the
  697. * firmware.
  698. *
  699. * This function creates a scan command configuration structure based on the
  700. * following user supplied parameters (if present):
  701. * - SSID filter
  702. * - BSSID filter
  703. * - Number of Probes to be sent
  704. * - Channel list
  705. *
  706. * If the SSID or BSSID filter is not present, the filter is disabled/cleared.
  707. * If the number of probes is not set, adapter default setting is used.
  708. */
  709. static void
  710. mwifiex_config_scan(struct mwifiex_private *priv,
  711. const struct mwifiex_user_scan_cfg *user_scan_in,
  712. struct mwifiex_scan_cmd_config *scan_cfg_out,
  713. struct mwifiex_ie_types_chan_list_param_set **chan_list_out,
  714. struct mwifiex_chan_scan_param_set *scan_chan_list,
  715. u8 *max_chan_per_scan, u8 *filtered_scan,
  716. u8 *scan_current_only)
  717. {
  718. struct mwifiex_adapter *adapter = priv->adapter;
  719. struct mwifiex_ie_types_num_probes *num_probes_tlv;
  720. struct mwifiex_ie_types_scan_chan_gap *chan_gap_tlv;
  721. struct mwifiex_ie_types_random_mac *random_mac_tlv;
  722. struct mwifiex_ie_types_wildcard_ssid_params *wildcard_ssid_tlv;
  723. struct mwifiex_ie_types_bssid_list *bssid_tlv;
  724. u8 *tlv_pos;
  725. u32 num_probes;
  726. u32 ssid_len;
  727. u32 chan_idx;
  728. u32 scan_type;
  729. u16 scan_dur;
  730. u8 channel;
  731. u8 radio_type;
  732. int i;
  733. u8 ssid_filter;
  734. struct mwifiex_ie_types_htcap *ht_cap;
  735. struct mwifiex_ie_types_bss_mode *bss_mode;
  736. const u8 zero_mac[6] = {0, 0, 0, 0, 0, 0};
  737. /* The tlv_buf_len is calculated for each scan command. The TLVs added
  738. in this routine will be preserved since the routine that sends the
  739. command will append channelTLVs at *chan_list_out. The difference
  740. between the *chan_list_out and the tlv_buf start will be used to
  741. calculate the size of anything we add in this routine. */
  742. scan_cfg_out->tlv_buf_len = 0;
  743. /* Running tlv pointer. Assigned to chan_list_out at end of function
  744. so later routines know where channels can be added to the command
  745. buf */
  746. tlv_pos = scan_cfg_out->tlv_buf;
  747. /* Initialize the scan as un-filtered; the flag is later set to TRUE
  748. below if a SSID or BSSID filter is sent in the command */
  749. *filtered_scan = false;
  750. /* Initialize the scan as not being only on the current channel. If
  751. the channel list is customized, only contains one channel, and is
  752. the active channel, this is set true and data flow is not halted. */
  753. *scan_current_only = false;
  754. if (user_scan_in) {
  755. u8 tmpaddr[ETH_ALEN];
  756. /* Default the ssid_filter flag to TRUE, set false under
  757. certain wildcard conditions and qualified by the existence
  758. of an SSID list before marking the scan as filtered */
  759. ssid_filter = true;
  760. /* Set the BSS type scan filter, use Adapter setting if
  761. unset */
  762. scan_cfg_out->bss_mode =
  763. (u8)(user_scan_in->bss_mode ?: adapter->scan_mode);
  764. /* Set the number of probes to send, use Adapter setting
  765. if unset */
  766. num_probes = user_scan_in->num_probes ?: adapter->scan_probes;
  767. /*
  768. * Set the BSSID filter to the incoming configuration,
  769. * if non-zero. If not set, it will remain disabled
  770. * (all zeros).
  771. */
  772. memcpy(scan_cfg_out->specific_bssid,
  773. user_scan_in->specific_bssid,
  774. sizeof(scan_cfg_out->specific_bssid));
  775. memcpy(tmpaddr, scan_cfg_out->specific_bssid, ETH_ALEN);
  776. if (adapter->ext_scan &&
  777. !is_zero_ether_addr(tmpaddr)) {
  778. bssid_tlv =
  779. (struct mwifiex_ie_types_bssid_list *)tlv_pos;
  780. bssid_tlv->header.type = cpu_to_le16(TLV_TYPE_BSSID);
  781. bssid_tlv->header.len = cpu_to_le16(ETH_ALEN);
  782. memcpy(bssid_tlv->bssid, user_scan_in->specific_bssid,
  783. ETH_ALEN);
  784. tlv_pos += sizeof(struct mwifiex_ie_types_bssid_list);
  785. }
  786. for (i = 0; i < user_scan_in->num_ssids; i++) {
  787. ssid_len = user_scan_in->ssid_list[i].ssid_len;
  788. wildcard_ssid_tlv =
  789. (struct mwifiex_ie_types_wildcard_ssid_params *)
  790. tlv_pos;
  791. wildcard_ssid_tlv->header.type =
  792. cpu_to_le16(TLV_TYPE_WILDCARDSSID);
  793. wildcard_ssid_tlv->header.len = cpu_to_le16(
  794. (u16) (ssid_len + sizeof(wildcard_ssid_tlv->
  795. max_ssid_length)));
  796. /*
  797. * max_ssid_length = 0 tells firmware to perform
  798. * specific scan for the SSID filled, whereas
  799. * max_ssid_length = IEEE80211_MAX_SSID_LEN is for
  800. * wildcard scan.
  801. */
  802. if (ssid_len)
  803. wildcard_ssid_tlv->max_ssid_length = 0;
  804. else
  805. wildcard_ssid_tlv->max_ssid_length =
  806. IEEE80211_MAX_SSID_LEN;
  807. if (!memcmp(user_scan_in->ssid_list[i].ssid,
  808. "DIRECT-", 7))
  809. wildcard_ssid_tlv->max_ssid_length = 0xfe;
  810. memcpy(wildcard_ssid_tlv->ssid,
  811. user_scan_in->ssid_list[i].ssid, ssid_len);
  812. tlv_pos += (sizeof(wildcard_ssid_tlv->header)
  813. + le16_to_cpu(wildcard_ssid_tlv->header.len));
  814. mwifiex_dbg(adapter, INFO,
  815. "info: scan: ssid[%d]: %s, %d\n",
  816. i, wildcard_ssid_tlv->ssid,
  817. wildcard_ssid_tlv->max_ssid_length);
  818. /* Empty wildcard ssid with a maxlen will match many or
  819. potentially all SSIDs (maxlen == 32), therefore do
  820. not treat the scan as
  821. filtered. */
  822. if (!ssid_len && wildcard_ssid_tlv->max_ssid_length)
  823. ssid_filter = false;
  824. }
  825. /*
  826. * The default number of channels sent in the command is low to
  827. * ensure the response buffer from the firmware does not
  828. * truncate scan results. That is not an issue with an SSID
  829. * or BSSID filter applied to the scan results in the firmware.
  830. */
  831. memcpy(tmpaddr, scan_cfg_out->specific_bssid, ETH_ALEN);
  832. if ((i && ssid_filter) ||
  833. !is_zero_ether_addr(tmpaddr))
  834. *filtered_scan = true;
  835. if (user_scan_in->scan_chan_gap) {
  836. mwifiex_dbg(adapter, INFO,
  837. "info: scan: channel gap = %d\n",
  838. user_scan_in->scan_chan_gap);
  839. *max_chan_per_scan =
  840. MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN;
  841. chan_gap_tlv = (void *)tlv_pos;
  842. chan_gap_tlv->header.type =
  843. cpu_to_le16(TLV_TYPE_SCAN_CHANNEL_GAP);
  844. chan_gap_tlv->header.len =
  845. cpu_to_le16(sizeof(chan_gap_tlv->chan_gap));
  846. chan_gap_tlv->chan_gap =
  847. cpu_to_le16((user_scan_in->scan_chan_gap));
  848. tlv_pos +=
  849. sizeof(struct mwifiex_ie_types_scan_chan_gap);
  850. }
  851. if (!ether_addr_equal(user_scan_in->random_mac, zero_mac)) {
  852. random_mac_tlv = (void *)tlv_pos;
  853. random_mac_tlv->header.type =
  854. cpu_to_le16(TLV_TYPE_RANDOM_MAC);
  855. random_mac_tlv->header.len =
  856. cpu_to_le16(sizeof(random_mac_tlv->mac));
  857. ether_addr_copy(random_mac_tlv->mac,
  858. user_scan_in->random_mac);
  859. tlv_pos +=
  860. sizeof(struct mwifiex_ie_types_random_mac);
  861. }
  862. } else {
  863. scan_cfg_out->bss_mode = (u8) adapter->scan_mode;
  864. num_probes = adapter->scan_probes;
  865. }
  866. /*
  867. * If a specific BSSID or SSID is used, the number of channels in the
  868. * scan command will be increased to the absolute maximum.
  869. */
  870. if (*filtered_scan) {
  871. *max_chan_per_scan = MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN;
  872. } else {
  873. if (!priv->media_connected)
  874. *max_chan_per_scan = MWIFIEX_DEF_CHANNELS_PER_SCAN_CMD;
  875. else
  876. *max_chan_per_scan =
  877. MWIFIEX_DEF_CHANNELS_PER_SCAN_CMD / 2;
  878. }
  879. if (adapter->ext_scan) {
  880. bss_mode = (struct mwifiex_ie_types_bss_mode *)tlv_pos;
  881. bss_mode->header.type = cpu_to_le16(TLV_TYPE_BSS_MODE);
  882. bss_mode->header.len = cpu_to_le16(sizeof(bss_mode->bss_mode));
  883. bss_mode->bss_mode = scan_cfg_out->bss_mode;
  884. tlv_pos += sizeof(bss_mode->header) +
  885. le16_to_cpu(bss_mode->header.len);
  886. }
  887. /* If the input config or adapter has the number of Probes set,
  888. add tlv */
  889. if (num_probes) {
  890. mwifiex_dbg(adapter, INFO,
  891. "info: scan: num_probes = %d\n",
  892. num_probes);
  893. num_probes_tlv = (struct mwifiex_ie_types_num_probes *) tlv_pos;
  894. num_probes_tlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
  895. num_probes_tlv->header.len =
  896. cpu_to_le16(sizeof(num_probes_tlv->num_probes));
  897. num_probes_tlv->num_probes = cpu_to_le16((u16) num_probes);
  898. tlv_pos += sizeof(num_probes_tlv->header) +
  899. le16_to_cpu(num_probes_tlv->header.len);
  900. }
  901. if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info) &&
  902. (priv->adapter->config_bands & BAND_GN ||
  903. priv->adapter->config_bands & BAND_AN)) {
  904. ht_cap = (struct mwifiex_ie_types_htcap *) tlv_pos;
  905. memset(ht_cap, 0, sizeof(struct mwifiex_ie_types_htcap));
  906. ht_cap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
  907. ht_cap->header.len =
  908. cpu_to_le16(sizeof(struct ieee80211_ht_cap));
  909. radio_type =
  910. mwifiex_band_to_radio_type(priv->adapter->config_bands);
  911. mwifiex_fill_cap_info(priv, radio_type, &ht_cap->ht_cap);
  912. tlv_pos += sizeof(struct mwifiex_ie_types_htcap);
  913. }
  914. /* Append vendor specific IE TLV */
  915. mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_SCAN, &tlv_pos);
  916. /*
  917. * Set the output for the channel TLV to the address in the tlv buffer
  918. * past any TLVs that were added in this function (SSID, num_probes).
  919. * Channel TLVs will be added past this for each scan command,
  920. * preserving the TLVs that were previously added.
  921. */
  922. *chan_list_out =
  923. (struct mwifiex_ie_types_chan_list_param_set *) tlv_pos;
  924. if (user_scan_in && user_scan_in->chan_list[0].chan_number) {
  925. mwifiex_dbg(adapter, INFO,
  926. "info: Scan: Using supplied channel list\n");
  927. for (chan_idx = 0;
  928. chan_idx < MWIFIEX_USER_SCAN_CHAN_MAX &&
  929. user_scan_in->chan_list[chan_idx].chan_number;
  930. chan_idx++) {
  931. channel = user_scan_in->chan_list[chan_idx].chan_number;
  932. scan_chan_list[chan_idx].chan_number = channel;
  933. radio_type =
  934. user_scan_in->chan_list[chan_idx].radio_type;
  935. scan_chan_list[chan_idx].radio_type = radio_type;
  936. scan_type = user_scan_in->chan_list[chan_idx].scan_type;
  937. if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
  938. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  939. |= (MWIFIEX_PASSIVE_SCAN |
  940. MWIFIEX_HIDDEN_SSID_REPORT);
  941. else
  942. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  943. &= ~MWIFIEX_PASSIVE_SCAN;
  944. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  945. |= MWIFIEX_DISABLE_CHAN_FILT;
  946. if (user_scan_in->chan_list[chan_idx].scan_time) {
  947. scan_dur = (u16) user_scan_in->
  948. chan_list[chan_idx].scan_time;
  949. } else {
  950. if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
  951. scan_dur = adapter->passive_scan_time;
  952. else if (*filtered_scan)
  953. scan_dur = adapter->specific_scan_time;
  954. else
  955. scan_dur = adapter->active_scan_time;
  956. }
  957. scan_chan_list[chan_idx].min_scan_time =
  958. cpu_to_le16(scan_dur);
  959. scan_chan_list[chan_idx].max_scan_time =
  960. cpu_to_le16(scan_dur);
  961. }
  962. /* Check if we are only scanning the current channel */
  963. if ((chan_idx == 1) &&
  964. (user_scan_in->chan_list[0].chan_number ==
  965. priv->curr_bss_params.bss_descriptor.channel)) {
  966. *scan_current_only = true;
  967. mwifiex_dbg(adapter, INFO,
  968. "info: Scan: Scanning current channel only\n");
  969. }
  970. } else {
  971. mwifiex_dbg(adapter, INFO,
  972. "info: Scan: Creating full region channel list\n");
  973. mwifiex_scan_create_channel_list(priv, user_scan_in,
  974. scan_chan_list,
  975. *filtered_scan);
  976. }
  977. }
  978. /*
  979. * This function inspects the scan response buffer for pointers to
  980. * expected TLVs.
  981. *
  982. * TLVs can be included at the end of the scan response BSS information.
  983. *
  984. * Data in the buffer is parsed pointers to TLVs that can potentially
  985. * be passed back in the response.
  986. */
  987. static void
  988. mwifiex_ret_802_11_scan_get_tlv_ptrs(struct mwifiex_adapter *adapter,
  989. struct mwifiex_ie_types_data *tlv,
  990. u32 tlv_buf_size, u32 req_tlv_type,
  991. struct mwifiex_ie_types_data **tlv_data)
  992. {
  993. struct mwifiex_ie_types_data *current_tlv;
  994. u32 tlv_buf_left;
  995. u32 tlv_type;
  996. u32 tlv_len;
  997. current_tlv = tlv;
  998. tlv_buf_left = tlv_buf_size;
  999. *tlv_data = NULL;
  1000. mwifiex_dbg(adapter, INFO,
  1001. "info: SCAN_RESP: tlv_buf_size = %d\n",
  1002. tlv_buf_size);
  1003. while (tlv_buf_left >= sizeof(struct mwifiex_ie_types_header)) {
  1004. tlv_type = le16_to_cpu(current_tlv->header.type);
  1005. tlv_len = le16_to_cpu(current_tlv->header.len);
  1006. if (sizeof(tlv->header) + tlv_len > tlv_buf_left) {
  1007. mwifiex_dbg(adapter, ERROR,
  1008. "SCAN_RESP: TLV buffer corrupt\n");
  1009. break;
  1010. }
  1011. if (req_tlv_type == tlv_type) {
  1012. switch (tlv_type) {
  1013. case TLV_TYPE_TSFTIMESTAMP:
  1014. mwifiex_dbg(adapter, INFO,
  1015. "info: SCAN_RESP: TSF\t"
  1016. "timestamp TLV, len = %d\n",
  1017. tlv_len);
  1018. *tlv_data = current_tlv;
  1019. break;
  1020. case TLV_TYPE_CHANNELBANDLIST:
  1021. mwifiex_dbg(adapter, INFO,
  1022. "info: SCAN_RESP: channel\t"
  1023. "band list TLV, len = %d\n",
  1024. tlv_len);
  1025. *tlv_data = current_tlv;
  1026. break;
  1027. default:
  1028. mwifiex_dbg(adapter, ERROR,
  1029. "SCAN_RESP: unhandled TLV = %d\n",
  1030. tlv_type);
  1031. /* Give up, this seems corrupted */
  1032. return;
  1033. }
  1034. }
  1035. if (*tlv_data)
  1036. break;
  1037. tlv_buf_left -= (sizeof(tlv->header) + tlv_len);
  1038. current_tlv =
  1039. (struct mwifiex_ie_types_data *) (current_tlv->data +
  1040. tlv_len);
  1041. } /* while */
  1042. }
  1043. /*
  1044. * This function parses provided beacon buffer and updates
  1045. * respective fields in bss descriptor structure.
  1046. */
  1047. int mwifiex_update_bss_desc_with_ie(struct mwifiex_adapter *adapter,
  1048. struct mwifiex_bssdescriptor *bss_entry)
  1049. {
  1050. u8 element_id;
  1051. struct ieee_types_fh_param_set *fh_param_set;
  1052. struct ieee_types_ds_param_set *ds_param_set;
  1053. struct ieee_types_cf_param_set *cf_param_set;
  1054. struct ieee_types_ibss_param_set *ibss_param_set;
  1055. u8 *current_ptr;
  1056. u8 *rate;
  1057. u8 element_len;
  1058. u16 total_ie_len;
  1059. u8 bytes_to_copy;
  1060. u8 rate_size;
  1061. u8 found_data_rate_ie;
  1062. u32 bytes_left;
  1063. struct ieee_types_vendor_specific *vendor_ie;
  1064. const u8 wpa_oui[4] = { 0x00, 0x50, 0xf2, 0x01 };
  1065. const u8 wmm_oui[4] = { 0x00, 0x50, 0xf2, 0x02 };
  1066. found_data_rate_ie = false;
  1067. rate_size = 0;
  1068. current_ptr = bss_entry->beacon_buf;
  1069. bytes_left = bss_entry->beacon_buf_size;
  1070. /* Process variable IE */
  1071. while (bytes_left >= 2) {
  1072. element_id = *current_ptr;
  1073. element_len = *(current_ptr + 1);
  1074. total_ie_len = element_len + sizeof(struct ieee_types_header);
  1075. if (bytes_left < total_ie_len) {
  1076. mwifiex_dbg(adapter, ERROR,
  1077. "err: InterpretIE: in processing\t"
  1078. "IE, bytes left < IE length\n");
  1079. return -EINVAL;
  1080. }
  1081. switch (element_id) {
  1082. case WLAN_EID_SSID:
  1083. if (element_len > IEEE80211_MAX_SSID_LEN)
  1084. return -EINVAL;
  1085. bss_entry->ssid.ssid_len = element_len;
  1086. memcpy(bss_entry->ssid.ssid, (current_ptr + 2),
  1087. element_len);
  1088. mwifiex_dbg(adapter, INFO,
  1089. "info: InterpretIE: ssid: %-32s\n",
  1090. bss_entry->ssid.ssid);
  1091. break;
  1092. case WLAN_EID_SUPP_RATES:
  1093. if (element_len > MWIFIEX_SUPPORTED_RATES)
  1094. return -EINVAL;
  1095. memcpy(bss_entry->data_rates, current_ptr + 2,
  1096. element_len);
  1097. memcpy(bss_entry->supported_rates, current_ptr + 2,
  1098. element_len);
  1099. rate_size = element_len;
  1100. found_data_rate_ie = true;
  1101. break;
  1102. case WLAN_EID_FH_PARAMS:
  1103. if (total_ie_len < sizeof(*fh_param_set))
  1104. return -EINVAL;
  1105. fh_param_set =
  1106. (struct ieee_types_fh_param_set *) current_ptr;
  1107. memcpy(&bss_entry->phy_param_set.fh_param_set,
  1108. fh_param_set,
  1109. sizeof(struct ieee_types_fh_param_set));
  1110. break;
  1111. case WLAN_EID_DS_PARAMS:
  1112. if (total_ie_len < sizeof(*ds_param_set))
  1113. return -EINVAL;
  1114. ds_param_set =
  1115. (struct ieee_types_ds_param_set *) current_ptr;
  1116. bss_entry->channel = ds_param_set->current_chan;
  1117. memcpy(&bss_entry->phy_param_set.ds_param_set,
  1118. ds_param_set,
  1119. sizeof(struct ieee_types_ds_param_set));
  1120. break;
  1121. case WLAN_EID_CF_PARAMS:
  1122. if (total_ie_len < sizeof(*cf_param_set))
  1123. return -EINVAL;
  1124. cf_param_set =
  1125. (struct ieee_types_cf_param_set *) current_ptr;
  1126. memcpy(&bss_entry->ss_param_set.cf_param_set,
  1127. cf_param_set,
  1128. sizeof(struct ieee_types_cf_param_set));
  1129. break;
  1130. case WLAN_EID_IBSS_PARAMS:
  1131. if (total_ie_len < sizeof(*ibss_param_set))
  1132. return -EINVAL;
  1133. ibss_param_set =
  1134. (struct ieee_types_ibss_param_set *)
  1135. current_ptr;
  1136. memcpy(&bss_entry->ss_param_set.ibss_param_set,
  1137. ibss_param_set,
  1138. sizeof(struct ieee_types_ibss_param_set));
  1139. break;
  1140. case WLAN_EID_ERP_INFO:
  1141. if (!element_len)
  1142. return -EINVAL;
  1143. bss_entry->erp_flags = *(current_ptr + 2);
  1144. break;
  1145. case WLAN_EID_PWR_CONSTRAINT:
  1146. if (!element_len)
  1147. return -EINVAL;
  1148. bss_entry->local_constraint = *(current_ptr + 2);
  1149. bss_entry->sensed_11h = true;
  1150. break;
  1151. case WLAN_EID_CHANNEL_SWITCH:
  1152. bss_entry->chan_sw_ie_present = true;
  1153. fallthrough;
  1154. case WLAN_EID_PWR_CAPABILITY:
  1155. case WLAN_EID_TPC_REPORT:
  1156. case WLAN_EID_QUIET:
  1157. bss_entry->sensed_11h = true;
  1158. break;
  1159. case WLAN_EID_EXT_SUPP_RATES:
  1160. /*
  1161. * Only process extended supported rate
  1162. * if data rate is already found.
  1163. * Data rate IE should come before
  1164. * extended supported rate IE
  1165. */
  1166. if (found_data_rate_ie) {
  1167. if ((element_len + rate_size) >
  1168. MWIFIEX_SUPPORTED_RATES)
  1169. bytes_to_copy =
  1170. (MWIFIEX_SUPPORTED_RATES -
  1171. rate_size);
  1172. else
  1173. bytes_to_copy = element_len;
  1174. rate = (u8 *) bss_entry->data_rates;
  1175. rate += rate_size;
  1176. memcpy(rate, current_ptr + 2, bytes_to_copy);
  1177. rate = (u8 *) bss_entry->supported_rates;
  1178. rate += rate_size;
  1179. memcpy(rate, current_ptr + 2, bytes_to_copy);
  1180. }
  1181. break;
  1182. case WLAN_EID_VENDOR_SPECIFIC:
  1183. vendor_ie = (struct ieee_types_vendor_specific *)
  1184. current_ptr;
  1185. /* 802.11 requires at least 3-byte OUI. */
  1186. if (element_len < sizeof(vendor_ie->vend_hdr.oui.oui))
  1187. return -EINVAL;
  1188. /* Not long enough for a match? Skip it. */
  1189. if (element_len < sizeof(wpa_oui))
  1190. break;
  1191. if (!memcmp(&vendor_ie->vend_hdr.oui, wpa_oui,
  1192. sizeof(wpa_oui))) {
  1193. bss_entry->bcn_wpa_ie =
  1194. (struct ieee_types_vendor_specific *)
  1195. current_ptr;
  1196. bss_entry->wpa_offset = (u16)
  1197. (current_ptr - bss_entry->beacon_buf);
  1198. } else if (!memcmp(&vendor_ie->vend_hdr.oui, wmm_oui,
  1199. sizeof(wmm_oui))) {
  1200. if (total_ie_len ==
  1201. sizeof(struct ieee_types_wmm_parameter) ||
  1202. total_ie_len ==
  1203. sizeof(struct ieee_types_wmm_info))
  1204. /*
  1205. * Only accept and copy the WMM IE if
  1206. * it matches the size expected for the
  1207. * WMM Info IE or the WMM Parameter IE.
  1208. */
  1209. memcpy((u8 *) &bss_entry->wmm_ie,
  1210. current_ptr, total_ie_len);
  1211. }
  1212. break;
  1213. case WLAN_EID_RSN:
  1214. bss_entry->bcn_rsn_ie =
  1215. (struct ieee_types_generic *) current_ptr;
  1216. bss_entry->rsn_offset = (u16) (current_ptr -
  1217. bss_entry->beacon_buf);
  1218. break;
  1219. case WLAN_EID_BSS_AC_ACCESS_DELAY:
  1220. bss_entry->bcn_wapi_ie =
  1221. (struct ieee_types_generic *) current_ptr;
  1222. bss_entry->wapi_offset = (u16) (current_ptr -
  1223. bss_entry->beacon_buf);
  1224. break;
  1225. case WLAN_EID_HT_CAPABILITY:
  1226. bss_entry->bcn_ht_cap = (struct ieee80211_ht_cap *)
  1227. (current_ptr +
  1228. sizeof(struct ieee_types_header));
  1229. bss_entry->ht_cap_offset = (u16) (current_ptr +
  1230. sizeof(struct ieee_types_header) -
  1231. bss_entry->beacon_buf);
  1232. break;
  1233. case WLAN_EID_HT_OPERATION:
  1234. bss_entry->bcn_ht_oper =
  1235. (struct ieee80211_ht_operation *)(current_ptr +
  1236. sizeof(struct ieee_types_header));
  1237. bss_entry->ht_info_offset = (u16) (current_ptr +
  1238. sizeof(struct ieee_types_header) -
  1239. bss_entry->beacon_buf);
  1240. break;
  1241. case WLAN_EID_VHT_CAPABILITY:
  1242. bss_entry->disable_11ac = false;
  1243. bss_entry->bcn_vht_cap =
  1244. (void *)(current_ptr +
  1245. sizeof(struct ieee_types_header));
  1246. bss_entry->vht_cap_offset =
  1247. (u16)((u8 *)bss_entry->bcn_vht_cap -
  1248. bss_entry->beacon_buf);
  1249. break;
  1250. case WLAN_EID_VHT_OPERATION:
  1251. bss_entry->bcn_vht_oper =
  1252. (void *)(current_ptr +
  1253. sizeof(struct ieee_types_header));
  1254. bss_entry->vht_info_offset =
  1255. (u16)((u8 *)bss_entry->bcn_vht_oper -
  1256. bss_entry->beacon_buf);
  1257. break;
  1258. case WLAN_EID_BSS_COEX_2040:
  1259. bss_entry->bcn_bss_co_2040 = current_ptr;
  1260. bss_entry->bss_co_2040_offset =
  1261. (u16) (current_ptr - bss_entry->beacon_buf);
  1262. break;
  1263. case WLAN_EID_EXT_CAPABILITY:
  1264. bss_entry->bcn_ext_cap = current_ptr;
  1265. bss_entry->ext_cap_offset =
  1266. (u16) (current_ptr - bss_entry->beacon_buf);
  1267. break;
  1268. case WLAN_EID_OPMODE_NOTIF:
  1269. bss_entry->oper_mode = (void *)current_ptr;
  1270. bss_entry->oper_mode_offset =
  1271. (u16)((u8 *)bss_entry->oper_mode -
  1272. bss_entry->beacon_buf);
  1273. break;
  1274. default:
  1275. break;
  1276. }
  1277. current_ptr += total_ie_len;
  1278. bytes_left -= total_ie_len;
  1279. } /* while (bytes_left > 2) */
  1280. return 0;
  1281. }
  1282. /*
  1283. * This function converts radio type scan parameter to a band configuration
  1284. * to be used in join command.
  1285. */
  1286. static u8
  1287. mwifiex_radio_type_to_band(u8 radio_type)
  1288. {
  1289. switch (radio_type) {
  1290. case HostCmd_SCAN_RADIO_TYPE_A:
  1291. return BAND_A;
  1292. case HostCmd_SCAN_RADIO_TYPE_BG:
  1293. default:
  1294. return BAND_G;
  1295. }
  1296. }
  1297. /*
  1298. * This is an internal function used to start a scan based on an input
  1299. * configuration.
  1300. *
  1301. * This uses the input user scan configuration information when provided in
  1302. * order to send the appropriate scan commands to firmware to populate or
  1303. * update the internal driver scan table.
  1304. */
  1305. int mwifiex_scan_networks(struct mwifiex_private *priv,
  1306. const struct mwifiex_user_scan_cfg *user_scan_in)
  1307. {
  1308. int ret;
  1309. struct mwifiex_adapter *adapter = priv->adapter;
  1310. struct cmd_ctrl_node *cmd_node;
  1311. union mwifiex_scan_cmd_config_tlv *scan_cfg_out;
  1312. struct mwifiex_ie_types_chan_list_param_set *chan_list_out;
  1313. struct mwifiex_chan_scan_param_set *scan_chan_list;
  1314. u8 filtered_scan;
  1315. u8 scan_current_chan_only;
  1316. u8 max_chan_per_scan;
  1317. if (adapter->scan_processing) {
  1318. mwifiex_dbg(adapter, WARN,
  1319. "cmd: Scan already in process...\n");
  1320. return -EBUSY;
  1321. }
  1322. if (priv->scan_block) {
  1323. mwifiex_dbg(adapter, WARN,
  1324. "cmd: Scan is blocked during association...\n");
  1325. return -EBUSY;
  1326. }
  1327. if (test_bit(MWIFIEX_SURPRISE_REMOVED, &adapter->work_flags) ||
  1328. test_bit(MWIFIEX_IS_CMD_TIMEDOUT, &adapter->work_flags)) {
  1329. mwifiex_dbg(adapter, ERROR,
  1330. "Ignore scan. Card removed or firmware in bad state\n");
  1331. return -EFAULT;
  1332. }
  1333. spin_lock_bh(&adapter->mwifiex_cmd_lock);
  1334. adapter->scan_processing = true;
  1335. spin_unlock_bh(&adapter->mwifiex_cmd_lock);
  1336. scan_cfg_out = kzalloc(sizeof(union mwifiex_scan_cmd_config_tlv),
  1337. GFP_KERNEL);
  1338. if (!scan_cfg_out) {
  1339. ret = -ENOMEM;
  1340. goto done;
  1341. }
  1342. scan_chan_list = kcalloc(MWIFIEX_USER_SCAN_CHAN_MAX,
  1343. sizeof(struct mwifiex_chan_scan_param_set),
  1344. GFP_KERNEL);
  1345. if (!scan_chan_list) {
  1346. kfree(scan_cfg_out);
  1347. ret = -ENOMEM;
  1348. goto done;
  1349. }
  1350. mwifiex_config_scan(priv, user_scan_in, &scan_cfg_out->config,
  1351. &chan_list_out, scan_chan_list, &max_chan_per_scan,
  1352. &filtered_scan, &scan_current_chan_only);
  1353. ret = mwifiex_scan_channel_list(priv, max_chan_per_scan, filtered_scan,
  1354. &scan_cfg_out->config, chan_list_out,
  1355. scan_chan_list);
  1356. /* Get scan command from scan_pending_q and put to cmd_pending_q */
  1357. if (!ret) {
  1358. spin_lock_bh(&adapter->scan_pending_q_lock);
  1359. if (!list_empty(&adapter->scan_pending_q)) {
  1360. cmd_node = list_first_entry(&adapter->scan_pending_q,
  1361. struct cmd_ctrl_node, list);
  1362. list_del(&cmd_node->list);
  1363. spin_unlock_bh(&adapter->scan_pending_q_lock);
  1364. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node);
  1365. queue_work(adapter->workqueue, &adapter->main_work);
  1366. /* Perform internal scan synchronously */
  1367. if (!priv->scan_request) {
  1368. mwifiex_dbg(adapter, INFO,
  1369. "wait internal scan\n");
  1370. mwifiex_wait_queue_complete(adapter, cmd_node);
  1371. }
  1372. } else {
  1373. spin_unlock_bh(&adapter->scan_pending_q_lock);
  1374. }
  1375. }
  1376. kfree(scan_cfg_out);
  1377. kfree(scan_chan_list);
  1378. done:
  1379. if (ret) {
  1380. spin_lock_bh(&adapter->mwifiex_cmd_lock);
  1381. adapter->scan_processing = false;
  1382. spin_unlock_bh(&adapter->mwifiex_cmd_lock);
  1383. }
  1384. return ret;
  1385. }
  1386. /*
  1387. * This function prepares a scan command to be sent to the firmware.
  1388. *
  1389. * This uses the scan command configuration sent to the command processing
  1390. * module in command preparation stage to configure a scan command structure
  1391. * to send to firmware.
  1392. *
  1393. * The fixed fields specifying the BSS type and BSSID filters as well as a
  1394. * variable number/length of TLVs are sent in the command to firmware.
  1395. *
  1396. * Preparation also includes -
  1397. * - Setting command ID, and proper size
  1398. * - Ensuring correct endian-ness
  1399. */
  1400. int mwifiex_cmd_802_11_scan(struct host_cmd_ds_command *cmd,
  1401. struct mwifiex_scan_cmd_config *scan_cfg)
  1402. {
  1403. struct host_cmd_ds_802_11_scan *scan_cmd = &cmd->params.scan;
  1404. /* Set fixed field variables in scan command */
  1405. scan_cmd->bss_mode = scan_cfg->bss_mode;
  1406. memcpy(scan_cmd->bssid, scan_cfg->specific_bssid,
  1407. sizeof(scan_cmd->bssid));
  1408. memcpy(scan_cmd->tlv_buffer, scan_cfg->tlv_buf, scan_cfg->tlv_buf_len);
  1409. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_SCAN);
  1410. /* Size is equal to the sizeof(fixed portions) + the TLV len + header */
  1411. cmd->size = cpu_to_le16((u16) (sizeof(scan_cmd->bss_mode)
  1412. + sizeof(scan_cmd->bssid)
  1413. + scan_cfg->tlv_buf_len + S_DS_GEN));
  1414. return 0;
  1415. }
  1416. /*
  1417. * This function checks compatibility of requested network with current
  1418. * driver settings.
  1419. */
  1420. int mwifiex_check_network_compatibility(struct mwifiex_private *priv,
  1421. struct mwifiex_bssdescriptor *bss_desc)
  1422. {
  1423. int ret = -1;
  1424. if (!bss_desc)
  1425. return -1;
  1426. if ((mwifiex_get_cfp(priv, (u8) bss_desc->bss_band,
  1427. (u16) bss_desc->channel, 0))) {
  1428. switch (priv->bss_mode) {
  1429. case NL80211_IFTYPE_STATION:
  1430. case NL80211_IFTYPE_ADHOC:
  1431. ret = mwifiex_is_network_compatible(priv, bss_desc,
  1432. priv->bss_mode);
  1433. if (ret)
  1434. mwifiex_dbg(priv->adapter, ERROR,
  1435. "Incompatible network settings\n");
  1436. break;
  1437. default:
  1438. ret = 0;
  1439. }
  1440. }
  1441. return ret;
  1442. }
  1443. /* This function checks if SSID string contains all zeroes or length is zero */
  1444. static bool mwifiex_is_hidden_ssid(struct cfg80211_ssid *ssid)
  1445. {
  1446. int idx;
  1447. for (idx = 0; idx < ssid->ssid_len; idx++) {
  1448. if (ssid->ssid[idx])
  1449. return false;
  1450. }
  1451. return true;
  1452. }
  1453. /* This function checks if any hidden SSID found in passive scan channels
  1454. * and save those channels for specific SSID active scan
  1455. */
  1456. static int mwifiex_save_hidden_ssid_channels(struct mwifiex_private *priv,
  1457. struct cfg80211_bss *bss)
  1458. {
  1459. struct mwifiex_bssdescriptor *bss_desc;
  1460. int ret;
  1461. int chid;
  1462. /* Allocate and fill new bss descriptor */
  1463. bss_desc = kzalloc(sizeof(*bss_desc), GFP_KERNEL);
  1464. if (!bss_desc)
  1465. return -ENOMEM;
  1466. ret = mwifiex_fill_new_bss_desc(priv, bss, bss_desc);
  1467. if (ret)
  1468. goto done;
  1469. if (mwifiex_is_hidden_ssid(&bss_desc->ssid)) {
  1470. mwifiex_dbg(priv->adapter, INFO, "found hidden SSID\n");
  1471. for (chid = 0 ; chid < MWIFIEX_USER_SCAN_CHAN_MAX; chid++) {
  1472. if (priv->hidden_chan[chid].chan_number ==
  1473. bss->channel->hw_value)
  1474. break;
  1475. if (!priv->hidden_chan[chid].chan_number) {
  1476. priv->hidden_chan[chid].chan_number =
  1477. bss->channel->hw_value;
  1478. priv->hidden_chan[chid].radio_type =
  1479. bss->channel->band;
  1480. priv->hidden_chan[chid].scan_type =
  1481. MWIFIEX_SCAN_TYPE_ACTIVE;
  1482. break;
  1483. }
  1484. }
  1485. }
  1486. done:
  1487. /* beacon_ie buffer was allocated in function
  1488. * mwifiex_fill_new_bss_desc(). Free it now.
  1489. */
  1490. kfree(bss_desc->beacon_buf);
  1491. kfree(bss_desc);
  1492. return 0;
  1493. }
  1494. static int mwifiex_update_curr_bss_params(struct mwifiex_private *priv,
  1495. struct cfg80211_bss *bss)
  1496. {
  1497. struct mwifiex_bssdescriptor *bss_desc;
  1498. int ret;
  1499. /* Allocate and fill new bss descriptor */
  1500. bss_desc = kzalloc(sizeof(struct mwifiex_bssdescriptor), GFP_KERNEL);
  1501. if (!bss_desc)
  1502. return -ENOMEM;
  1503. ret = mwifiex_fill_new_bss_desc(priv, bss, bss_desc);
  1504. if (ret)
  1505. goto done;
  1506. ret = mwifiex_check_network_compatibility(priv, bss_desc);
  1507. if (ret)
  1508. goto done;
  1509. spin_lock_bh(&priv->curr_bcn_buf_lock);
  1510. /* Make a copy of current BSSID descriptor */
  1511. memcpy(&priv->curr_bss_params.bss_descriptor, bss_desc,
  1512. sizeof(priv->curr_bss_params.bss_descriptor));
  1513. /* The contents of beacon_ie will be copied to its own buffer
  1514. * in mwifiex_save_curr_bcn()
  1515. */
  1516. mwifiex_save_curr_bcn(priv);
  1517. spin_unlock_bh(&priv->curr_bcn_buf_lock);
  1518. done:
  1519. /* beacon_ie buffer was allocated in function
  1520. * mwifiex_fill_new_bss_desc(). Free it now.
  1521. */
  1522. kfree(bss_desc->beacon_buf);
  1523. kfree(bss_desc);
  1524. return 0;
  1525. }
  1526. static int
  1527. mwifiex_parse_single_response_buf(struct mwifiex_private *priv, u8 **bss_info,
  1528. u32 *bytes_left, u64 fw_tsf, u8 *radio_type,
  1529. bool ext_scan, s32 rssi_val)
  1530. {
  1531. struct mwifiex_adapter *adapter = priv->adapter;
  1532. struct mwifiex_chan_freq_power *cfp;
  1533. struct cfg80211_bss *bss;
  1534. u8 bssid[ETH_ALEN];
  1535. s32 rssi;
  1536. const u8 *ie_buf;
  1537. size_t ie_len;
  1538. u16 channel = 0;
  1539. u16 beacon_size = 0;
  1540. u32 curr_bcn_bytes;
  1541. u32 freq;
  1542. u16 beacon_period;
  1543. u16 cap_info_bitmap;
  1544. u8 *current_ptr;
  1545. u64 timestamp;
  1546. struct mwifiex_fixed_bcn_param *bcn_param;
  1547. struct mwifiex_bss_priv *bss_priv;
  1548. if (*bytes_left >= sizeof(beacon_size)) {
  1549. /* Extract & convert beacon size from command buffer */
  1550. beacon_size = get_unaligned_le16((*bss_info));
  1551. *bytes_left -= sizeof(beacon_size);
  1552. *bss_info += sizeof(beacon_size);
  1553. }
  1554. if (!beacon_size || beacon_size > *bytes_left) {
  1555. *bss_info += *bytes_left;
  1556. *bytes_left = 0;
  1557. return -EFAULT;
  1558. }
  1559. /* Initialize the current working beacon pointer for this BSS
  1560. * iteration
  1561. */
  1562. current_ptr = *bss_info;
  1563. /* Advance the return beacon pointer past the current beacon */
  1564. *bss_info += beacon_size;
  1565. *bytes_left -= beacon_size;
  1566. curr_bcn_bytes = beacon_size;
  1567. /* First 5 fields are bssid, RSSI(for legacy scan only),
  1568. * time stamp, beacon interval, and capability information
  1569. */
  1570. if (curr_bcn_bytes < ETH_ALEN + sizeof(u8) +
  1571. sizeof(struct mwifiex_fixed_bcn_param)) {
  1572. mwifiex_dbg(adapter, ERROR,
  1573. "InterpretIE: not enough bytes left\n");
  1574. return -EFAULT;
  1575. }
  1576. memcpy(bssid, current_ptr, ETH_ALEN);
  1577. current_ptr += ETH_ALEN;
  1578. curr_bcn_bytes -= ETH_ALEN;
  1579. if (!ext_scan) {
  1580. rssi = (s32) *current_ptr;
  1581. rssi = (-rssi) * 100; /* Convert dBm to mBm */
  1582. current_ptr += sizeof(u8);
  1583. curr_bcn_bytes -= sizeof(u8);
  1584. mwifiex_dbg(adapter, INFO,
  1585. "info: InterpretIE: RSSI=%d\n", rssi);
  1586. } else {
  1587. rssi = rssi_val;
  1588. }
  1589. bcn_param = (struct mwifiex_fixed_bcn_param *)current_ptr;
  1590. current_ptr += sizeof(*bcn_param);
  1591. curr_bcn_bytes -= sizeof(*bcn_param);
  1592. timestamp = le64_to_cpu(bcn_param->timestamp);
  1593. beacon_period = le16_to_cpu(bcn_param->beacon_period);
  1594. cap_info_bitmap = le16_to_cpu(bcn_param->cap_info_bitmap);
  1595. mwifiex_dbg(adapter, INFO,
  1596. "info: InterpretIE: capabilities=0x%X\n",
  1597. cap_info_bitmap);
  1598. /* Rest of the current buffer are IE's */
  1599. ie_buf = current_ptr;
  1600. ie_len = curr_bcn_bytes;
  1601. mwifiex_dbg(adapter, INFO,
  1602. "info: InterpretIE: IELength for this AP = %d\n",
  1603. curr_bcn_bytes);
  1604. while (curr_bcn_bytes >= sizeof(struct ieee_types_header)) {
  1605. u8 element_id, element_len;
  1606. element_id = *current_ptr;
  1607. element_len = *(current_ptr + 1);
  1608. if (curr_bcn_bytes < element_len +
  1609. sizeof(struct ieee_types_header)) {
  1610. mwifiex_dbg(adapter, ERROR,
  1611. "%s: bytes left < IE length\n", __func__);
  1612. return -EFAULT;
  1613. }
  1614. if (element_id == WLAN_EID_DS_PARAMS) {
  1615. channel = *(current_ptr +
  1616. sizeof(struct ieee_types_header));
  1617. break;
  1618. }
  1619. current_ptr += element_len + sizeof(struct ieee_types_header);
  1620. curr_bcn_bytes -= element_len +
  1621. sizeof(struct ieee_types_header);
  1622. }
  1623. if (channel) {
  1624. struct ieee80211_channel *chan;
  1625. u8 band;
  1626. /* Skip entry if on csa closed channel */
  1627. if (channel == priv->csa_chan) {
  1628. mwifiex_dbg(adapter, WARN,
  1629. "Dropping entry on csa closed channel\n");
  1630. return 0;
  1631. }
  1632. band = BAND_G;
  1633. if (radio_type)
  1634. band = mwifiex_radio_type_to_band(*radio_type &
  1635. (BIT(0) | BIT(1)));
  1636. cfp = mwifiex_get_cfp(priv, band, channel, 0);
  1637. freq = cfp ? cfp->freq : 0;
  1638. chan = ieee80211_get_channel(priv->wdev.wiphy, freq);
  1639. if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
  1640. bss = cfg80211_inform_bss(priv->wdev.wiphy,
  1641. chan, CFG80211_BSS_FTYPE_UNKNOWN,
  1642. bssid, timestamp,
  1643. cap_info_bitmap, beacon_period,
  1644. ie_buf, ie_len, rssi, GFP_ATOMIC);
  1645. if (bss) {
  1646. bss_priv = (struct mwifiex_bss_priv *)bss->priv;
  1647. bss_priv->band = band;
  1648. bss_priv->fw_tsf = fw_tsf;
  1649. if (priv->media_connected &&
  1650. !memcmp(bssid, priv->curr_bss_params.
  1651. bss_descriptor.mac_address,
  1652. ETH_ALEN))
  1653. mwifiex_update_curr_bss_params(priv,
  1654. bss);
  1655. if ((chan->flags & IEEE80211_CHAN_RADAR) ||
  1656. (chan->flags & IEEE80211_CHAN_NO_IR)) {
  1657. mwifiex_dbg(adapter, INFO,
  1658. "radar or passive channel %d\n",
  1659. channel);
  1660. mwifiex_save_hidden_ssid_channels(priv,
  1661. bss);
  1662. }
  1663. cfg80211_put_bss(priv->wdev.wiphy, bss);
  1664. }
  1665. }
  1666. } else {
  1667. mwifiex_dbg(adapter, WARN, "missing BSS channel IE\n");
  1668. }
  1669. return 0;
  1670. }
  1671. static void mwifiex_complete_scan(struct mwifiex_private *priv)
  1672. {
  1673. struct mwifiex_adapter *adapter = priv->adapter;
  1674. adapter->survey_idx = 0;
  1675. if (adapter->curr_cmd->wait_q_enabled) {
  1676. adapter->cmd_wait_q.status = 0;
  1677. if (!priv->scan_request) {
  1678. mwifiex_dbg(adapter, INFO,
  1679. "complete internal scan\n");
  1680. mwifiex_complete_cmd(adapter, adapter->curr_cmd);
  1681. }
  1682. }
  1683. }
  1684. /* This function checks if any hidden SSID found in passive scan channels
  1685. * and do specific SSID active scan for those channels
  1686. */
  1687. static int
  1688. mwifiex_active_scan_req_for_passive_chan(struct mwifiex_private *priv)
  1689. {
  1690. int ret;
  1691. struct mwifiex_adapter *adapter = priv->adapter;
  1692. u8 id = 0;
  1693. struct mwifiex_user_scan_cfg *user_scan_cfg;
  1694. if (adapter->active_scan_triggered || !priv->scan_request ||
  1695. priv->scan_aborting) {
  1696. adapter->active_scan_triggered = false;
  1697. return 0;
  1698. }
  1699. if (!priv->hidden_chan[0].chan_number) {
  1700. mwifiex_dbg(adapter, INFO, "No BSS with hidden SSID found on DFS channels\n");
  1701. return 0;
  1702. }
  1703. user_scan_cfg = kzalloc(sizeof(*user_scan_cfg), GFP_KERNEL);
  1704. if (!user_scan_cfg)
  1705. return -ENOMEM;
  1706. for (id = 0; id < MWIFIEX_USER_SCAN_CHAN_MAX; id++) {
  1707. if (!priv->hidden_chan[id].chan_number)
  1708. break;
  1709. memcpy(&user_scan_cfg->chan_list[id],
  1710. &priv->hidden_chan[id],
  1711. sizeof(struct mwifiex_user_scan_chan));
  1712. }
  1713. adapter->active_scan_triggered = true;
  1714. if (priv->scan_request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
  1715. ether_addr_copy(user_scan_cfg->random_mac,
  1716. priv->scan_request->mac_addr);
  1717. user_scan_cfg->num_ssids = priv->scan_request->n_ssids;
  1718. user_scan_cfg->ssid_list = priv->scan_request->ssids;
  1719. ret = mwifiex_scan_networks(priv, user_scan_cfg);
  1720. kfree(user_scan_cfg);
  1721. memset(&priv->hidden_chan, 0, sizeof(priv->hidden_chan));
  1722. if (ret) {
  1723. dev_err(priv->adapter->dev, "scan failed: %d\n", ret);
  1724. return ret;
  1725. }
  1726. return 0;
  1727. }
  1728. static void mwifiex_check_next_scan_command(struct mwifiex_private *priv)
  1729. {
  1730. struct mwifiex_adapter *adapter = priv->adapter;
  1731. struct cmd_ctrl_node *cmd_node;
  1732. spin_lock_bh(&adapter->scan_pending_q_lock);
  1733. if (list_empty(&adapter->scan_pending_q)) {
  1734. spin_unlock_bh(&adapter->scan_pending_q_lock);
  1735. spin_lock_bh(&adapter->mwifiex_cmd_lock);
  1736. adapter->scan_processing = false;
  1737. spin_unlock_bh(&adapter->mwifiex_cmd_lock);
  1738. mwifiex_active_scan_req_for_passive_chan(priv);
  1739. if (!adapter->ext_scan)
  1740. mwifiex_complete_scan(priv);
  1741. if (priv->scan_request) {
  1742. struct cfg80211_scan_info info = {
  1743. .aborted = false,
  1744. };
  1745. mwifiex_dbg(adapter, INFO,
  1746. "info: notifying scan done\n");
  1747. cfg80211_scan_done(priv->scan_request, &info);
  1748. priv->scan_request = NULL;
  1749. priv->scan_aborting = false;
  1750. } else {
  1751. priv->scan_aborting = false;
  1752. mwifiex_dbg(adapter, INFO,
  1753. "info: scan already aborted\n");
  1754. }
  1755. } else if ((priv->scan_aborting && !priv->scan_request) ||
  1756. priv->scan_block) {
  1757. spin_unlock_bh(&adapter->scan_pending_q_lock);
  1758. mwifiex_cancel_pending_scan_cmd(adapter);
  1759. spin_lock_bh(&adapter->mwifiex_cmd_lock);
  1760. adapter->scan_processing = false;
  1761. spin_unlock_bh(&adapter->mwifiex_cmd_lock);
  1762. if (!adapter->active_scan_triggered) {
  1763. if (priv->scan_request) {
  1764. struct cfg80211_scan_info info = {
  1765. .aborted = true,
  1766. };
  1767. mwifiex_dbg(adapter, INFO,
  1768. "info: aborting scan\n");
  1769. cfg80211_scan_done(priv->scan_request, &info);
  1770. priv->scan_request = NULL;
  1771. priv->scan_aborting = false;
  1772. } else {
  1773. priv->scan_aborting = false;
  1774. mwifiex_dbg(adapter, INFO,
  1775. "info: scan already aborted\n");
  1776. }
  1777. }
  1778. } else {
  1779. /* Get scan command from scan_pending_q and put to
  1780. * cmd_pending_q
  1781. */
  1782. cmd_node = list_first_entry(&adapter->scan_pending_q,
  1783. struct cmd_ctrl_node, list);
  1784. list_del(&cmd_node->list);
  1785. spin_unlock_bh(&adapter->scan_pending_q_lock);
  1786. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node);
  1787. }
  1788. return;
  1789. }
  1790. void mwifiex_cancel_scan(struct mwifiex_adapter *adapter)
  1791. {
  1792. struct mwifiex_private *priv;
  1793. int i;
  1794. mwifiex_cancel_pending_scan_cmd(adapter);
  1795. if (adapter->scan_processing) {
  1796. spin_lock_bh(&adapter->mwifiex_cmd_lock);
  1797. adapter->scan_processing = false;
  1798. spin_unlock_bh(&adapter->mwifiex_cmd_lock);
  1799. for (i = 0; i < adapter->priv_num; i++) {
  1800. priv = adapter->priv[i];
  1801. if (!priv)
  1802. continue;
  1803. if (priv->scan_request) {
  1804. struct cfg80211_scan_info info = {
  1805. .aborted = true,
  1806. };
  1807. mwifiex_dbg(adapter, INFO,
  1808. "info: aborting scan\n");
  1809. cfg80211_scan_done(priv->scan_request, &info);
  1810. priv->scan_request = NULL;
  1811. priv->scan_aborting = false;
  1812. }
  1813. }
  1814. }
  1815. }
  1816. /*
  1817. * This function handles the command response of scan.
  1818. *
  1819. * The response buffer for the scan command has the following
  1820. * memory layout:
  1821. *
  1822. * .-------------------------------------------------------------.
  1823. * | Header (4 * sizeof(t_u16)): Standard command response hdr |
  1824. * .-------------------------------------------------------------.
  1825. * | BufSize (t_u16) : sizeof the BSS Description data |
  1826. * .-------------------------------------------------------------.
  1827. * | NumOfSet (t_u8) : Number of BSS Descs returned |
  1828. * .-------------------------------------------------------------.
  1829. * | BSSDescription data (variable, size given in BufSize) |
  1830. * .-------------------------------------------------------------.
  1831. * | TLV data (variable, size calculated using Header->Size, |
  1832. * | BufSize and sizeof the fixed fields above) |
  1833. * .-------------------------------------------------------------.
  1834. */
  1835. int mwifiex_ret_802_11_scan(struct mwifiex_private *priv,
  1836. struct host_cmd_ds_command *resp)
  1837. {
  1838. int ret = 0;
  1839. struct mwifiex_adapter *adapter = priv->adapter;
  1840. struct host_cmd_ds_802_11_scan_rsp *scan_rsp;
  1841. struct mwifiex_ie_types_data *tlv_data;
  1842. struct mwifiex_ie_types_tsf_timestamp *tsf_tlv;
  1843. u8 *bss_info;
  1844. u32 scan_resp_size;
  1845. u32 bytes_left;
  1846. u32 idx;
  1847. u32 tlv_buf_size;
  1848. struct mwifiex_ie_types_chan_band_list_param_set *chan_band_tlv;
  1849. struct chan_band_param_set *chan_band;
  1850. u8 is_bgscan_resp;
  1851. __le64 fw_tsf = 0;
  1852. u8 *radio_type;
  1853. struct cfg80211_wowlan_nd_match *pmatch;
  1854. struct cfg80211_sched_scan_request *nd_config = NULL;
  1855. is_bgscan_resp = (le16_to_cpu(resp->command)
  1856. == HostCmd_CMD_802_11_BG_SCAN_QUERY);
  1857. if (is_bgscan_resp)
  1858. scan_rsp = &resp->params.bg_scan_query_resp.scan_resp;
  1859. else
  1860. scan_rsp = &resp->params.scan_resp;
  1861. if (scan_rsp->number_of_sets > MWIFIEX_MAX_AP) {
  1862. mwifiex_dbg(adapter, ERROR,
  1863. "SCAN_RESP: too many AP returned (%d)\n",
  1864. scan_rsp->number_of_sets);
  1865. ret = -1;
  1866. goto check_next_scan;
  1867. }
  1868. /* Check csa channel expiry before parsing scan response */
  1869. mwifiex_11h_get_csa_closed_channel(priv);
  1870. bytes_left = le16_to_cpu(scan_rsp->bss_descript_size);
  1871. mwifiex_dbg(adapter, INFO,
  1872. "info: SCAN_RESP: bss_descript_size %d\n",
  1873. bytes_left);
  1874. scan_resp_size = le16_to_cpu(resp->size);
  1875. mwifiex_dbg(adapter, INFO,
  1876. "info: SCAN_RESP: returned %d APs before parsing\n",
  1877. scan_rsp->number_of_sets);
  1878. bss_info = scan_rsp->bss_desc_and_tlv_buffer;
  1879. /*
  1880. * The size of the TLV buffer is equal to the entire command response
  1881. * size (scan_resp_size) minus the fixed fields (sizeof()'s), the
  1882. * BSS Descriptions (bss_descript_size as bytesLef) and the command
  1883. * response header (S_DS_GEN)
  1884. */
  1885. tlv_buf_size = scan_resp_size - (bytes_left
  1886. + sizeof(scan_rsp->bss_descript_size)
  1887. + sizeof(scan_rsp->number_of_sets)
  1888. + S_DS_GEN);
  1889. tlv_data = (struct mwifiex_ie_types_data *) (scan_rsp->
  1890. bss_desc_and_tlv_buffer +
  1891. bytes_left);
  1892. /* Search the TLV buffer space in the scan response for any valid
  1893. TLVs */
  1894. mwifiex_ret_802_11_scan_get_tlv_ptrs(adapter, tlv_data, tlv_buf_size,
  1895. TLV_TYPE_TSFTIMESTAMP,
  1896. (struct mwifiex_ie_types_data **)
  1897. &tsf_tlv);
  1898. /* Search the TLV buffer space in the scan response for any valid
  1899. TLVs */
  1900. mwifiex_ret_802_11_scan_get_tlv_ptrs(adapter, tlv_data, tlv_buf_size,
  1901. TLV_TYPE_CHANNELBANDLIST,
  1902. (struct mwifiex_ie_types_data **)
  1903. &chan_band_tlv);
  1904. #ifdef CONFIG_PM
  1905. if (priv->wdev.wiphy->wowlan_config)
  1906. nd_config = priv->wdev.wiphy->wowlan_config->nd_config;
  1907. #endif
  1908. if (nd_config) {
  1909. adapter->nd_info =
  1910. kzalloc(struct_size(adapter->nd_info, matches,
  1911. scan_rsp->number_of_sets),
  1912. GFP_ATOMIC);
  1913. if (adapter->nd_info)
  1914. adapter->nd_info->n_matches = scan_rsp->number_of_sets;
  1915. }
  1916. for (idx = 0; idx < scan_rsp->number_of_sets && bytes_left; idx++) {
  1917. /*
  1918. * If the TSF TLV was appended to the scan results, save this
  1919. * entry's TSF value in the fw_tsf field. It is the firmware's
  1920. * TSF value at the time the beacon or probe response was
  1921. * received.
  1922. */
  1923. if (tsf_tlv)
  1924. memcpy(&fw_tsf, &tsf_tlv->tsf_data[idx * TSF_DATA_SIZE],
  1925. sizeof(fw_tsf));
  1926. if (chan_band_tlv) {
  1927. chan_band = &chan_band_tlv->chan_band_param[idx];
  1928. radio_type = &chan_band->radio_type;
  1929. } else {
  1930. radio_type = NULL;
  1931. }
  1932. if (chan_band_tlv && adapter->nd_info) {
  1933. adapter->nd_info->matches[idx] =
  1934. kzalloc(sizeof(*pmatch) + sizeof(u32),
  1935. GFP_ATOMIC);
  1936. pmatch = adapter->nd_info->matches[idx];
  1937. if (pmatch) {
  1938. pmatch->n_channels = 1;
  1939. pmatch->channels[0] = chan_band->chan_number;
  1940. }
  1941. }
  1942. ret = mwifiex_parse_single_response_buf(priv, &bss_info,
  1943. &bytes_left,
  1944. le64_to_cpu(fw_tsf),
  1945. radio_type, false, 0);
  1946. if (ret)
  1947. goto check_next_scan;
  1948. }
  1949. check_next_scan:
  1950. mwifiex_check_next_scan_command(priv);
  1951. return ret;
  1952. }
  1953. /*
  1954. * This function prepares an extended scan command to be sent to the firmware
  1955. *
  1956. * This uses the scan command configuration sent to the command processing
  1957. * module in command preparation stage to configure a extended scan command
  1958. * structure to send to firmware.
  1959. */
  1960. int mwifiex_cmd_802_11_scan_ext(struct mwifiex_private *priv,
  1961. struct host_cmd_ds_command *cmd,
  1962. void *data_buf)
  1963. {
  1964. struct host_cmd_ds_802_11_scan_ext *ext_scan = &cmd->params.ext_scan;
  1965. struct mwifiex_scan_cmd_config *scan_cfg = data_buf;
  1966. memcpy(ext_scan->tlv_buffer, scan_cfg->tlv_buf, scan_cfg->tlv_buf_len);
  1967. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_SCAN_EXT);
  1968. /* Size is equal to the sizeof(fixed portions) + the TLV len + header */
  1969. cmd->size = cpu_to_le16((u16)(sizeof(ext_scan->reserved)
  1970. + scan_cfg->tlv_buf_len + S_DS_GEN));
  1971. return 0;
  1972. }
  1973. /* This function prepares an background scan config command to be sent
  1974. * to the firmware
  1975. */
  1976. int mwifiex_cmd_802_11_bg_scan_config(struct mwifiex_private *priv,
  1977. struct host_cmd_ds_command *cmd,
  1978. void *data_buf)
  1979. {
  1980. struct host_cmd_ds_802_11_bg_scan_config *bgscan_config =
  1981. &cmd->params.bg_scan_config;
  1982. struct mwifiex_bg_scan_cfg *bgscan_cfg_in = data_buf;
  1983. u8 *tlv_pos = bgscan_config->tlv;
  1984. u8 num_probes;
  1985. u32 ssid_len, chan_idx, scan_type, scan_dur, chan_num;
  1986. int i;
  1987. struct mwifiex_ie_types_num_probes *num_probes_tlv;
  1988. struct mwifiex_ie_types_repeat_count *repeat_count_tlv;
  1989. struct mwifiex_ie_types_min_rssi_threshold *rssi_threshold_tlv;
  1990. struct mwifiex_ie_types_bgscan_start_later *start_later_tlv;
  1991. struct mwifiex_ie_types_wildcard_ssid_params *wildcard_ssid_tlv;
  1992. struct mwifiex_ie_types_chan_list_param_set *chan_list_tlv;
  1993. struct mwifiex_chan_scan_param_set *temp_chan;
  1994. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_BG_SCAN_CONFIG);
  1995. cmd->size = cpu_to_le16(sizeof(*bgscan_config) + S_DS_GEN);
  1996. bgscan_config->action = cpu_to_le16(bgscan_cfg_in->action);
  1997. bgscan_config->enable = bgscan_cfg_in->enable;
  1998. bgscan_config->bss_type = bgscan_cfg_in->bss_type;
  1999. bgscan_config->scan_interval =
  2000. cpu_to_le32(bgscan_cfg_in->scan_interval);
  2001. bgscan_config->report_condition =
  2002. cpu_to_le32(bgscan_cfg_in->report_condition);
  2003. /* stop sched scan */
  2004. if (!bgscan_config->enable)
  2005. return 0;
  2006. bgscan_config->chan_per_scan = bgscan_cfg_in->chan_per_scan;
  2007. num_probes = (bgscan_cfg_in->num_probes ? bgscan_cfg_in->
  2008. num_probes : priv->adapter->scan_probes);
  2009. if (num_probes) {
  2010. num_probes_tlv = (struct mwifiex_ie_types_num_probes *)tlv_pos;
  2011. num_probes_tlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
  2012. num_probes_tlv->header.len =
  2013. cpu_to_le16(sizeof(num_probes_tlv->num_probes));
  2014. num_probes_tlv->num_probes = cpu_to_le16((u16)num_probes);
  2015. tlv_pos += sizeof(num_probes_tlv->header) +
  2016. le16_to_cpu(num_probes_tlv->header.len);
  2017. }
  2018. if (bgscan_cfg_in->repeat_count) {
  2019. repeat_count_tlv =
  2020. (struct mwifiex_ie_types_repeat_count *)tlv_pos;
  2021. repeat_count_tlv->header.type =
  2022. cpu_to_le16(TLV_TYPE_REPEAT_COUNT);
  2023. repeat_count_tlv->header.len =
  2024. cpu_to_le16(sizeof(repeat_count_tlv->repeat_count));
  2025. repeat_count_tlv->repeat_count =
  2026. cpu_to_le16(bgscan_cfg_in->repeat_count);
  2027. tlv_pos += sizeof(repeat_count_tlv->header) +
  2028. le16_to_cpu(repeat_count_tlv->header.len);
  2029. }
  2030. if (bgscan_cfg_in->rssi_threshold) {
  2031. rssi_threshold_tlv =
  2032. (struct mwifiex_ie_types_min_rssi_threshold *)tlv_pos;
  2033. rssi_threshold_tlv->header.type =
  2034. cpu_to_le16(TLV_TYPE_RSSI_LOW);
  2035. rssi_threshold_tlv->header.len =
  2036. cpu_to_le16(sizeof(rssi_threshold_tlv->rssi_threshold));
  2037. rssi_threshold_tlv->rssi_threshold =
  2038. cpu_to_le16(bgscan_cfg_in->rssi_threshold);
  2039. tlv_pos += sizeof(rssi_threshold_tlv->header) +
  2040. le16_to_cpu(rssi_threshold_tlv->header.len);
  2041. }
  2042. for (i = 0; i < bgscan_cfg_in->num_ssids; i++) {
  2043. ssid_len = bgscan_cfg_in->ssid_list[i].ssid.ssid_len;
  2044. wildcard_ssid_tlv =
  2045. (struct mwifiex_ie_types_wildcard_ssid_params *)tlv_pos;
  2046. wildcard_ssid_tlv->header.type =
  2047. cpu_to_le16(TLV_TYPE_WILDCARDSSID);
  2048. wildcard_ssid_tlv->header.len = cpu_to_le16(
  2049. (u16)(ssid_len + sizeof(wildcard_ssid_tlv->
  2050. max_ssid_length)));
  2051. /* max_ssid_length = 0 tells firmware to perform
  2052. * specific scan for the SSID filled, whereas
  2053. * max_ssid_length = IEEE80211_MAX_SSID_LEN is for
  2054. * wildcard scan.
  2055. */
  2056. if (ssid_len)
  2057. wildcard_ssid_tlv->max_ssid_length = 0;
  2058. else
  2059. wildcard_ssid_tlv->max_ssid_length =
  2060. IEEE80211_MAX_SSID_LEN;
  2061. memcpy(wildcard_ssid_tlv->ssid,
  2062. bgscan_cfg_in->ssid_list[i].ssid.ssid, ssid_len);
  2063. tlv_pos += (sizeof(wildcard_ssid_tlv->header)
  2064. + le16_to_cpu(wildcard_ssid_tlv->header.len));
  2065. }
  2066. chan_list_tlv = (struct mwifiex_ie_types_chan_list_param_set *)tlv_pos;
  2067. if (bgscan_cfg_in->chan_list[0].chan_number) {
  2068. dev_dbg(priv->adapter->dev, "info: bgscan: Using supplied channel list\n");
  2069. chan_list_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
  2070. for (chan_idx = 0;
  2071. chan_idx < MWIFIEX_BG_SCAN_CHAN_MAX &&
  2072. bgscan_cfg_in->chan_list[chan_idx].chan_number;
  2073. chan_idx++) {
  2074. temp_chan = chan_list_tlv->chan_scan_param + chan_idx;
  2075. /* Increment the TLV header length by size appended */
  2076. le16_unaligned_add_cpu(&chan_list_tlv->header.len,
  2077. sizeof(
  2078. chan_list_tlv->chan_scan_param));
  2079. temp_chan->chan_number =
  2080. bgscan_cfg_in->chan_list[chan_idx].chan_number;
  2081. temp_chan->radio_type =
  2082. bgscan_cfg_in->chan_list[chan_idx].radio_type;
  2083. scan_type =
  2084. bgscan_cfg_in->chan_list[chan_idx].scan_type;
  2085. if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
  2086. temp_chan->chan_scan_mode_bitmap
  2087. |= MWIFIEX_PASSIVE_SCAN;
  2088. else
  2089. temp_chan->chan_scan_mode_bitmap
  2090. &= ~MWIFIEX_PASSIVE_SCAN;
  2091. if (bgscan_cfg_in->chan_list[chan_idx].scan_time) {
  2092. scan_dur = (u16)bgscan_cfg_in->
  2093. chan_list[chan_idx].scan_time;
  2094. } else {
  2095. scan_dur = (scan_type ==
  2096. MWIFIEX_SCAN_TYPE_PASSIVE) ?
  2097. priv->adapter->passive_scan_time :
  2098. priv->adapter->specific_scan_time;
  2099. }
  2100. temp_chan->min_scan_time = cpu_to_le16(scan_dur);
  2101. temp_chan->max_scan_time = cpu_to_le16(scan_dur);
  2102. }
  2103. } else {
  2104. dev_dbg(priv->adapter->dev,
  2105. "info: bgscan: Creating full region channel list\n");
  2106. chan_num =
  2107. mwifiex_bgscan_create_channel_list(priv, bgscan_cfg_in,
  2108. chan_list_tlv->
  2109. chan_scan_param);
  2110. le16_unaligned_add_cpu(&chan_list_tlv->header.len,
  2111. chan_num *
  2112. sizeof(chan_list_tlv->chan_scan_param[0]));
  2113. }
  2114. tlv_pos += (sizeof(chan_list_tlv->header)
  2115. + le16_to_cpu(chan_list_tlv->header.len));
  2116. if (bgscan_cfg_in->start_later) {
  2117. start_later_tlv =
  2118. (struct mwifiex_ie_types_bgscan_start_later *)tlv_pos;
  2119. start_later_tlv->header.type =
  2120. cpu_to_le16(TLV_TYPE_BGSCAN_START_LATER);
  2121. start_later_tlv->header.len =
  2122. cpu_to_le16(sizeof(start_later_tlv->start_later));
  2123. start_later_tlv->start_later =
  2124. cpu_to_le16(bgscan_cfg_in->start_later);
  2125. tlv_pos += sizeof(start_later_tlv->header) +
  2126. le16_to_cpu(start_later_tlv->header.len);
  2127. }
  2128. /* Append vendor specific IE TLV */
  2129. mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_BGSCAN, &tlv_pos);
  2130. le16_unaligned_add_cpu(&cmd->size, tlv_pos - bgscan_config->tlv);
  2131. return 0;
  2132. }
  2133. int mwifiex_stop_bg_scan(struct mwifiex_private *priv)
  2134. {
  2135. struct mwifiex_bg_scan_cfg *bgscan_cfg;
  2136. if (!priv->sched_scanning) {
  2137. dev_dbg(priv->adapter->dev, "bgscan already stopped!\n");
  2138. return 0;
  2139. }
  2140. bgscan_cfg = kzalloc(sizeof(*bgscan_cfg), GFP_KERNEL);
  2141. if (!bgscan_cfg)
  2142. return -ENOMEM;
  2143. bgscan_cfg->bss_type = MWIFIEX_BSS_MODE_INFRA;
  2144. bgscan_cfg->action = MWIFIEX_BGSCAN_ACT_SET;
  2145. bgscan_cfg->enable = false;
  2146. if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_BG_SCAN_CONFIG,
  2147. HostCmd_ACT_GEN_SET, 0, bgscan_cfg, true)) {
  2148. kfree(bgscan_cfg);
  2149. return -EFAULT;
  2150. }
  2151. kfree(bgscan_cfg);
  2152. priv->sched_scanning = false;
  2153. return 0;
  2154. }
  2155. static void
  2156. mwifiex_update_chan_statistics(struct mwifiex_private *priv,
  2157. struct mwifiex_ietypes_chanstats *tlv_stat)
  2158. {
  2159. struct mwifiex_adapter *adapter = priv->adapter;
  2160. u8 i, num_chan;
  2161. struct mwifiex_fw_chan_stats *fw_chan_stats;
  2162. struct mwifiex_chan_stats chan_stats;
  2163. fw_chan_stats = (void *)((u8 *)tlv_stat +
  2164. sizeof(struct mwifiex_ie_types_header));
  2165. num_chan = le16_to_cpu(tlv_stat->header.len) /
  2166. sizeof(struct mwifiex_chan_stats);
  2167. for (i = 0 ; i < num_chan; i++) {
  2168. if (adapter->survey_idx >= adapter->num_in_chan_stats) {
  2169. mwifiex_dbg(adapter, WARN,
  2170. "FW reported too many channel results (max %d)\n",
  2171. adapter->num_in_chan_stats);
  2172. return;
  2173. }
  2174. chan_stats.chan_num = fw_chan_stats->chan_num;
  2175. chan_stats.bandcfg = fw_chan_stats->bandcfg;
  2176. chan_stats.flags = fw_chan_stats->flags;
  2177. chan_stats.noise = fw_chan_stats->noise;
  2178. chan_stats.total_bss = le16_to_cpu(fw_chan_stats->total_bss);
  2179. chan_stats.cca_scan_dur =
  2180. le16_to_cpu(fw_chan_stats->cca_scan_dur);
  2181. chan_stats.cca_busy_dur =
  2182. le16_to_cpu(fw_chan_stats->cca_busy_dur);
  2183. mwifiex_dbg(adapter, INFO,
  2184. "chan=%d, noise=%d, total_network=%d scan_duration=%d, busy_duration=%d\n",
  2185. chan_stats.chan_num,
  2186. chan_stats.noise,
  2187. chan_stats.total_bss,
  2188. chan_stats.cca_scan_dur,
  2189. chan_stats.cca_busy_dur);
  2190. memcpy(&adapter->chan_stats[adapter->survey_idx++], &chan_stats,
  2191. sizeof(struct mwifiex_chan_stats));
  2192. fw_chan_stats++;
  2193. }
  2194. }
  2195. /* This function handles the command response of extended scan */
  2196. int mwifiex_ret_802_11_scan_ext(struct mwifiex_private *priv,
  2197. struct host_cmd_ds_command *resp)
  2198. {
  2199. struct mwifiex_adapter *adapter = priv->adapter;
  2200. struct host_cmd_ds_802_11_scan_ext *ext_scan_resp;
  2201. struct mwifiex_ie_types_header *tlv;
  2202. struct mwifiex_ietypes_chanstats *tlv_stat;
  2203. u16 buf_left, type, len;
  2204. struct host_cmd_ds_command *cmd_ptr;
  2205. struct cmd_ctrl_node *cmd_node;
  2206. bool complete_scan = false;
  2207. mwifiex_dbg(adapter, INFO, "info: EXT scan returns successfully\n");
  2208. ext_scan_resp = &resp->params.ext_scan;
  2209. tlv = (void *)ext_scan_resp->tlv_buffer;
  2210. buf_left = le16_to_cpu(resp->size) - (sizeof(*ext_scan_resp) + S_DS_GEN
  2211. - 1);
  2212. while (buf_left >= sizeof(struct mwifiex_ie_types_header)) {
  2213. type = le16_to_cpu(tlv->type);
  2214. len = le16_to_cpu(tlv->len);
  2215. if (buf_left < (sizeof(struct mwifiex_ie_types_header) + len)) {
  2216. mwifiex_dbg(adapter, ERROR,
  2217. "error processing scan response TLVs");
  2218. break;
  2219. }
  2220. switch (type) {
  2221. case TLV_TYPE_CHANNEL_STATS:
  2222. tlv_stat = (void *)tlv;
  2223. mwifiex_update_chan_statistics(priv, tlv_stat);
  2224. break;
  2225. default:
  2226. break;
  2227. }
  2228. buf_left -= len + sizeof(struct mwifiex_ie_types_header);
  2229. tlv = (void *)((u8 *)tlv + len +
  2230. sizeof(struct mwifiex_ie_types_header));
  2231. }
  2232. spin_lock_bh(&adapter->cmd_pending_q_lock);
  2233. spin_lock_bh(&adapter->scan_pending_q_lock);
  2234. if (list_empty(&adapter->scan_pending_q)) {
  2235. complete_scan = true;
  2236. list_for_each_entry(cmd_node, &adapter->cmd_pending_q, list) {
  2237. cmd_ptr = (void *)cmd_node->cmd_skb->data;
  2238. if (le16_to_cpu(cmd_ptr->command) ==
  2239. HostCmd_CMD_802_11_SCAN_EXT) {
  2240. mwifiex_dbg(adapter, INFO,
  2241. "Scan pending in command pending list");
  2242. complete_scan = false;
  2243. break;
  2244. }
  2245. }
  2246. }
  2247. spin_unlock_bh(&adapter->scan_pending_q_lock);
  2248. spin_unlock_bh(&adapter->cmd_pending_q_lock);
  2249. if (complete_scan)
  2250. mwifiex_complete_scan(priv);
  2251. return 0;
  2252. }
  2253. /* This function This function handles the event extended scan report. It
  2254. * parses extended scan results and informs to cfg80211 stack.
  2255. */
  2256. int mwifiex_handle_event_ext_scan_report(struct mwifiex_private *priv,
  2257. void *buf)
  2258. {
  2259. int ret = 0;
  2260. struct mwifiex_adapter *adapter = priv->adapter;
  2261. u8 *bss_info;
  2262. u32 bytes_left, bytes_left_for_tlv, idx;
  2263. u16 type, len;
  2264. struct mwifiex_ie_types_data *tlv;
  2265. struct mwifiex_ie_types_bss_scan_rsp *scan_rsp_tlv;
  2266. struct mwifiex_ie_types_bss_scan_info *scan_info_tlv;
  2267. u8 *radio_type;
  2268. u64 fw_tsf = 0;
  2269. s32 rssi = 0;
  2270. struct mwifiex_event_scan_result *event_scan = buf;
  2271. u8 num_of_set = event_scan->num_of_set;
  2272. u8 *scan_resp = buf + sizeof(struct mwifiex_event_scan_result);
  2273. u16 scan_resp_size = le16_to_cpu(event_scan->buf_size);
  2274. if (num_of_set > MWIFIEX_MAX_AP) {
  2275. mwifiex_dbg(adapter, ERROR,
  2276. "EXT_SCAN: Invalid number of AP returned (%d)!!\n",
  2277. num_of_set);
  2278. ret = -1;
  2279. goto check_next_scan;
  2280. }
  2281. bytes_left = scan_resp_size;
  2282. mwifiex_dbg(adapter, INFO,
  2283. "EXT_SCAN: size %d, returned %d APs...",
  2284. scan_resp_size, num_of_set);
  2285. mwifiex_dbg_dump(adapter, CMD_D, "EXT_SCAN buffer:", buf,
  2286. scan_resp_size +
  2287. sizeof(struct mwifiex_event_scan_result));
  2288. tlv = (struct mwifiex_ie_types_data *)scan_resp;
  2289. for (idx = 0; idx < num_of_set && bytes_left; idx++) {
  2290. type = le16_to_cpu(tlv->header.type);
  2291. len = le16_to_cpu(tlv->header.len);
  2292. if (bytes_left < sizeof(struct mwifiex_ie_types_header) + len) {
  2293. mwifiex_dbg(adapter, ERROR,
  2294. "EXT_SCAN: Error bytes left < TLV length\n");
  2295. break;
  2296. }
  2297. scan_rsp_tlv = NULL;
  2298. scan_info_tlv = NULL;
  2299. bytes_left_for_tlv = bytes_left;
  2300. /* BSS response TLV with beacon or probe response buffer
  2301. * at the initial position of each descriptor
  2302. */
  2303. if (type != TLV_TYPE_BSS_SCAN_RSP)
  2304. break;
  2305. bss_info = (u8 *)tlv;
  2306. scan_rsp_tlv = (struct mwifiex_ie_types_bss_scan_rsp *)tlv;
  2307. tlv = (struct mwifiex_ie_types_data *)(tlv->data + len);
  2308. bytes_left_for_tlv -=
  2309. (len + sizeof(struct mwifiex_ie_types_header));
  2310. while (bytes_left_for_tlv >=
  2311. sizeof(struct mwifiex_ie_types_header) &&
  2312. le16_to_cpu(tlv->header.type) != TLV_TYPE_BSS_SCAN_RSP) {
  2313. type = le16_to_cpu(tlv->header.type);
  2314. len = le16_to_cpu(tlv->header.len);
  2315. if (bytes_left_for_tlv <
  2316. sizeof(struct mwifiex_ie_types_header) + len) {
  2317. mwifiex_dbg(adapter, ERROR,
  2318. "EXT_SCAN: Error in processing TLV,\t"
  2319. "bytes left < TLV length\n");
  2320. scan_rsp_tlv = NULL;
  2321. bytes_left_for_tlv = 0;
  2322. continue;
  2323. }
  2324. switch (type) {
  2325. case TLV_TYPE_BSS_SCAN_INFO:
  2326. scan_info_tlv =
  2327. (struct mwifiex_ie_types_bss_scan_info *)tlv;
  2328. if (len !=
  2329. sizeof(struct mwifiex_ie_types_bss_scan_info) -
  2330. sizeof(struct mwifiex_ie_types_header)) {
  2331. bytes_left_for_tlv = 0;
  2332. continue;
  2333. }
  2334. break;
  2335. default:
  2336. break;
  2337. }
  2338. tlv = (struct mwifiex_ie_types_data *)(tlv->data + len);
  2339. bytes_left -=
  2340. (len + sizeof(struct mwifiex_ie_types_header));
  2341. bytes_left_for_tlv -=
  2342. (len + sizeof(struct mwifiex_ie_types_header));
  2343. }
  2344. if (!scan_rsp_tlv)
  2345. break;
  2346. /* Advance pointer to the beacon buffer length and
  2347. * update the bytes count so that the function
  2348. * wlan_interpret_bss_desc_with_ie() can handle the
  2349. * scan buffer withut any change
  2350. */
  2351. bss_info += sizeof(u16);
  2352. bytes_left -= sizeof(u16);
  2353. if (scan_info_tlv) {
  2354. rssi = (s32)(s16)(le16_to_cpu(scan_info_tlv->rssi));
  2355. rssi *= 100; /* Convert dBm to mBm */
  2356. mwifiex_dbg(adapter, INFO,
  2357. "info: InterpretIE: RSSI=%d\n", rssi);
  2358. fw_tsf = le64_to_cpu(scan_info_tlv->tsf);
  2359. radio_type = &scan_info_tlv->radio_type;
  2360. } else {
  2361. radio_type = NULL;
  2362. }
  2363. ret = mwifiex_parse_single_response_buf(priv, &bss_info,
  2364. &bytes_left, fw_tsf,
  2365. radio_type, true, rssi);
  2366. if (ret)
  2367. goto check_next_scan;
  2368. }
  2369. check_next_scan:
  2370. if (!event_scan->more_event)
  2371. mwifiex_check_next_scan_command(priv);
  2372. return ret;
  2373. }
  2374. /*
  2375. * This function prepares command for background scan query.
  2376. *
  2377. * Preparation includes -
  2378. * - Setting command ID and proper size
  2379. * - Setting background scan flush parameter
  2380. * - Ensuring correct endian-ness
  2381. */
  2382. int mwifiex_cmd_802_11_bg_scan_query(struct host_cmd_ds_command *cmd)
  2383. {
  2384. struct host_cmd_ds_802_11_bg_scan_query *bg_query =
  2385. &cmd->params.bg_scan_query;
  2386. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_BG_SCAN_QUERY);
  2387. cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_bg_scan_query)
  2388. + S_DS_GEN);
  2389. bg_query->flush = 1;
  2390. return 0;
  2391. }
  2392. /*
  2393. * This function inserts scan command node to the scan pending queue.
  2394. */
  2395. void
  2396. mwifiex_queue_scan_cmd(struct mwifiex_private *priv,
  2397. struct cmd_ctrl_node *cmd_node)
  2398. {
  2399. struct mwifiex_adapter *adapter = priv->adapter;
  2400. cmd_node->wait_q_enabled = true;
  2401. cmd_node->condition = &adapter->scan_wait_q_woken;
  2402. spin_lock_bh(&adapter->scan_pending_q_lock);
  2403. list_add_tail(&cmd_node->list, &adapter->scan_pending_q);
  2404. spin_unlock_bh(&adapter->scan_pending_q_lock);
  2405. }
  2406. /*
  2407. * This function sends a scan command for all available channels to the
  2408. * firmware, filtered on a specific SSID.
  2409. */
  2410. static int mwifiex_scan_specific_ssid(struct mwifiex_private *priv,
  2411. struct cfg80211_ssid *req_ssid)
  2412. {
  2413. struct mwifiex_adapter *adapter = priv->adapter;
  2414. int ret;
  2415. struct mwifiex_user_scan_cfg *scan_cfg;
  2416. if (adapter->scan_processing) {
  2417. mwifiex_dbg(adapter, WARN,
  2418. "cmd: Scan already in process...\n");
  2419. return -EBUSY;
  2420. }
  2421. if (priv->scan_block) {
  2422. mwifiex_dbg(adapter, WARN,
  2423. "cmd: Scan is blocked during association...\n");
  2424. return -EBUSY;
  2425. }
  2426. scan_cfg = kzalloc(sizeof(struct mwifiex_user_scan_cfg), GFP_KERNEL);
  2427. if (!scan_cfg)
  2428. return -ENOMEM;
  2429. scan_cfg->ssid_list = req_ssid;
  2430. scan_cfg->num_ssids = 1;
  2431. ret = mwifiex_scan_networks(priv, scan_cfg);
  2432. kfree(scan_cfg);
  2433. return ret;
  2434. }
  2435. /*
  2436. * Sends IOCTL request to start a scan.
  2437. *
  2438. * This function allocates the IOCTL request buffer, fills it
  2439. * with requisite parameters and calls the IOCTL handler.
  2440. *
  2441. * Scan command can be issued for both normal scan and specific SSID
  2442. * scan, depending upon whether an SSID is provided or not.
  2443. */
  2444. int mwifiex_request_scan(struct mwifiex_private *priv,
  2445. struct cfg80211_ssid *req_ssid)
  2446. {
  2447. int ret;
  2448. if (mutex_lock_interruptible(&priv->async_mutex)) {
  2449. mwifiex_dbg(priv->adapter, ERROR,
  2450. "%s: acquire semaphore fail\n",
  2451. __func__);
  2452. return -1;
  2453. }
  2454. priv->adapter->scan_wait_q_woken = false;
  2455. if (req_ssid && req_ssid->ssid_len != 0)
  2456. /* Specific SSID scan */
  2457. ret = mwifiex_scan_specific_ssid(priv, req_ssid);
  2458. else
  2459. /* Normal scan */
  2460. ret = mwifiex_scan_networks(priv, NULL);
  2461. mutex_unlock(&priv->async_mutex);
  2462. return ret;
  2463. }
  2464. /*
  2465. * This function appends the vendor specific IE TLV to a buffer.
  2466. */
  2467. int
  2468. mwifiex_cmd_append_vsie_tlv(struct mwifiex_private *priv,
  2469. u16 vsie_mask, u8 **buffer)
  2470. {
  2471. int id, ret_len = 0;
  2472. struct mwifiex_ie_types_vendor_param_set *vs_param_set;
  2473. if (!buffer)
  2474. return 0;
  2475. if (!(*buffer))
  2476. return 0;
  2477. /*
  2478. * Traverse through the saved vendor specific IE array and append
  2479. * the selected(scan/assoc/adhoc) IE as TLV to the command
  2480. */
  2481. for (id = 0; id < MWIFIEX_MAX_VSIE_NUM; id++) {
  2482. if (priv->vs_ie[id].mask & vsie_mask) {
  2483. vs_param_set =
  2484. (struct mwifiex_ie_types_vendor_param_set *)
  2485. *buffer;
  2486. vs_param_set->header.type =
  2487. cpu_to_le16(TLV_TYPE_PASSTHROUGH);
  2488. vs_param_set->header.len =
  2489. cpu_to_le16((((u16) priv->vs_ie[id].ie[1])
  2490. & 0x00FF) + 2);
  2491. if (le16_to_cpu(vs_param_set->header.len) >
  2492. MWIFIEX_MAX_VSIE_LEN) {
  2493. mwifiex_dbg(priv->adapter, ERROR,
  2494. "Invalid param length!\n");
  2495. break;
  2496. }
  2497. memcpy(vs_param_set->ie, priv->vs_ie[id].ie,
  2498. le16_to_cpu(vs_param_set->header.len));
  2499. *buffer += le16_to_cpu(vs_param_set->header.len) +
  2500. sizeof(struct mwifiex_ie_types_header);
  2501. ret_len += le16_to_cpu(vs_param_set->header.len) +
  2502. sizeof(struct mwifiex_ie_types_header);
  2503. }
  2504. }
  2505. return ret_len;
  2506. }
  2507. /*
  2508. * This function saves a beacon buffer of the current BSS descriptor.
  2509. *
  2510. * The current beacon buffer is saved so that it can be restored in the
  2511. * following cases that makes the beacon buffer not to contain the current
  2512. * ssid's beacon buffer.
  2513. * - The current ssid was not found somehow in the last scan.
  2514. * - The current ssid was the last entry of the scan table and overloaded.
  2515. */
  2516. void
  2517. mwifiex_save_curr_bcn(struct mwifiex_private *priv)
  2518. {
  2519. struct mwifiex_bssdescriptor *curr_bss =
  2520. &priv->curr_bss_params.bss_descriptor;
  2521. if (!curr_bss->beacon_buf_size)
  2522. return;
  2523. /* allocate beacon buffer at 1st time; or if it's size has changed */
  2524. if (!priv->curr_bcn_buf ||
  2525. priv->curr_bcn_size != curr_bss->beacon_buf_size) {
  2526. priv->curr_bcn_size = curr_bss->beacon_buf_size;
  2527. kfree(priv->curr_bcn_buf);
  2528. priv->curr_bcn_buf = kmalloc(curr_bss->beacon_buf_size,
  2529. GFP_ATOMIC);
  2530. if (!priv->curr_bcn_buf)
  2531. return;
  2532. }
  2533. memcpy(priv->curr_bcn_buf, curr_bss->beacon_buf,
  2534. curr_bss->beacon_buf_size);
  2535. mwifiex_dbg(priv->adapter, INFO,
  2536. "info: current beacon saved %d\n",
  2537. priv->curr_bcn_size);
  2538. curr_bss->beacon_buf = priv->curr_bcn_buf;
  2539. /* adjust the pointers in the current BSS descriptor */
  2540. if (curr_bss->bcn_wpa_ie)
  2541. curr_bss->bcn_wpa_ie =
  2542. (struct ieee_types_vendor_specific *)
  2543. (curr_bss->beacon_buf +
  2544. curr_bss->wpa_offset);
  2545. if (curr_bss->bcn_rsn_ie)
  2546. curr_bss->bcn_rsn_ie = (struct ieee_types_generic *)
  2547. (curr_bss->beacon_buf +
  2548. curr_bss->rsn_offset);
  2549. if (curr_bss->bcn_ht_cap)
  2550. curr_bss->bcn_ht_cap = (struct ieee80211_ht_cap *)
  2551. (curr_bss->beacon_buf +
  2552. curr_bss->ht_cap_offset);
  2553. if (curr_bss->bcn_ht_oper)
  2554. curr_bss->bcn_ht_oper = (struct ieee80211_ht_operation *)
  2555. (curr_bss->beacon_buf +
  2556. curr_bss->ht_info_offset);
  2557. if (curr_bss->bcn_vht_cap)
  2558. curr_bss->bcn_vht_cap = (void *)(curr_bss->beacon_buf +
  2559. curr_bss->vht_cap_offset);
  2560. if (curr_bss->bcn_vht_oper)
  2561. curr_bss->bcn_vht_oper = (void *)(curr_bss->beacon_buf +
  2562. curr_bss->vht_info_offset);
  2563. if (curr_bss->bcn_bss_co_2040)
  2564. curr_bss->bcn_bss_co_2040 =
  2565. (curr_bss->beacon_buf + curr_bss->bss_co_2040_offset);
  2566. if (curr_bss->bcn_ext_cap)
  2567. curr_bss->bcn_ext_cap = curr_bss->beacon_buf +
  2568. curr_bss->ext_cap_offset;
  2569. if (curr_bss->oper_mode)
  2570. curr_bss->oper_mode = (void *)(curr_bss->beacon_buf +
  2571. curr_bss->oper_mode_offset);
  2572. }
  2573. /*
  2574. * This function frees the current BSS descriptor beacon buffer.
  2575. */
  2576. void
  2577. mwifiex_free_curr_bcn(struct mwifiex_private *priv)
  2578. {
  2579. kfree(priv->curr_bcn_buf);
  2580. priv->curr_bcn_buf = NULL;
  2581. }