scan.c 92 KB

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