scan.c 92 KB

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