scan.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. /*
  2. * Marvell Wireless LAN device driver: scan ioctl and command handling
  3. *
  4. * Copyright (C) 2011, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include "decl.h"
  20. #include "ioctl.h"
  21. #include "util.h"
  22. #include "fw.h"
  23. #include "main.h"
  24. #include "11n.h"
  25. #include "cfg80211.h"
  26. /* The maximum number of channels the firmware can scan per command */
  27. #define MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN 14
  28. #define MWIFIEX_CHANNELS_PER_SCAN_CMD 4
  29. /* Memory needed to store a max sized Channel List TLV for a firmware scan */
  30. #define CHAN_TLV_MAX_SIZE (sizeof(struct mwifiex_ie_types_header) \
  31. + (MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN \
  32. *sizeof(struct mwifiex_chan_scan_param_set)))
  33. /* Memory needed to store supported rate */
  34. #define RATE_TLV_MAX_SIZE (sizeof(struct mwifiex_ie_types_rates_param_set) \
  35. + HOSTCMD_SUPPORTED_RATES)
  36. /* Memory needed to store a max number/size WildCard SSID TLV for a firmware
  37. scan */
  38. #define WILDCARD_SSID_TLV_MAX_SIZE \
  39. (MWIFIEX_MAX_SSID_LIST_LENGTH * \
  40. (sizeof(struct mwifiex_ie_types_wildcard_ssid_params) \
  41. + IEEE80211_MAX_SSID_LEN))
  42. /* Maximum memory needed for a mwifiex_scan_cmd_config with all TLVs at max */
  43. #define MAX_SCAN_CFG_ALLOC (sizeof(struct mwifiex_scan_cmd_config) \
  44. + sizeof(struct mwifiex_ie_types_num_probes) \
  45. + sizeof(struct mwifiex_ie_types_htcap) \
  46. + CHAN_TLV_MAX_SIZE \
  47. + RATE_TLV_MAX_SIZE \
  48. + WILDCARD_SSID_TLV_MAX_SIZE)
  49. union mwifiex_scan_cmd_config_tlv {
  50. /* Scan configuration (variable length) */
  51. struct mwifiex_scan_cmd_config config;
  52. /* Max allocated block */
  53. u8 config_alloc_buf[MAX_SCAN_CFG_ALLOC];
  54. };
  55. enum cipher_suite {
  56. CIPHER_SUITE_TKIP,
  57. CIPHER_SUITE_CCMP,
  58. CIPHER_SUITE_MAX
  59. };
  60. static u8 mwifiex_wpa_oui[CIPHER_SUITE_MAX][4] = {
  61. { 0x00, 0x50, 0xf2, 0x02 }, /* TKIP */
  62. { 0x00, 0x50, 0xf2, 0x04 }, /* AES */
  63. };
  64. static u8 mwifiex_rsn_oui[CIPHER_SUITE_MAX][4] = {
  65. { 0x00, 0x0f, 0xac, 0x02 }, /* TKIP */
  66. { 0x00, 0x0f, 0xac, 0x04 }, /* AES */
  67. };
  68. /*
  69. * This function parses a given IE for a given OUI.
  70. *
  71. * This is used to parse a WPA/RSN IE to find if it has
  72. * a given oui in PTK.
  73. */
  74. static u8
  75. mwifiex_search_oui_in_ie(struct ie_body *iebody, u8 *oui)
  76. {
  77. u8 count;
  78. count = iebody->ptk_cnt[0];
  79. /* There could be multiple OUIs for PTK hence
  80. 1) Take the length.
  81. 2) Check all the OUIs for AES.
  82. 3) If one of them is AES then pass success. */
  83. while (count) {
  84. if (!memcmp(iebody->ptk_body, oui, sizeof(iebody->ptk_body)))
  85. return MWIFIEX_OUI_PRESENT;
  86. --count;
  87. if (count)
  88. iebody = (struct ie_body *) ((u8 *) iebody +
  89. sizeof(iebody->ptk_body));
  90. }
  91. pr_debug("info: %s: OUI is not found in PTK\n", __func__);
  92. return MWIFIEX_OUI_NOT_PRESENT;
  93. }
  94. /*
  95. * This function checks if a given OUI is present in a RSN IE.
  96. *
  97. * The function first checks if a RSN IE is present or not in the
  98. * BSS descriptor. It tries to locate the OUI only if such an IE is
  99. * present.
  100. */
  101. static u8
  102. mwifiex_is_rsn_oui_present(struct mwifiex_bssdescriptor *bss_desc, u32 cipher)
  103. {
  104. u8 *oui;
  105. struct ie_body *iebody;
  106. u8 ret = MWIFIEX_OUI_NOT_PRESENT;
  107. if (((bss_desc->bcn_rsn_ie) && ((*(bss_desc->bcn_rsn_ie)).
  108. ieee_hdr.element_id == WLAN_EID_RSN))) {
  109. iebody = (struct ie_body *)
  110. (((u8 *) bss_desc->bcn_rsn_ie->data) +
  111. RSN_GTK_OUI_OFFSET);
  112. oui = &mwifiex_rsn_oui[cipher][0];
  113. ret = mwifiex_search_oui_in_ie(iebody, oui);
  114. if (ret)
  115. return ret;
  116. }
  117. return ret;
  118. }
  119. /*
  120. * This function checks if a given OUI is present in a WPA IE.
  121. *
  122. * The function first checks if a WPA IE is present or not in the
  123. * BSS descriptor. It tries to locate the OUI only if such an IE is
  124. * present.
  125. */
  126. static u8
  127. mwifiex_is_wpa_oui_present(struct mwifiex_bssdescriptor *bss_desc, u32 cipher)
  128. {
  129. u8 *oui;
  130. struct ie_body *iebody;
  131. u8 ret = MWIFIEX_OUI_NOT_PRESENT;
  132. if (((bss_desc->bcn_wpa_ie) && ((*(bss_desc->bcn_wpa_ie)).
  133. vend_hdr.element_id == WLAN_EID_WPA))) {
  134. iebody = (struct ie_body *) bss_desc->bcn_wpa_ie->data;
  135. oui = &mwifiex_wpa_oui[cipher][0];
  136. ret = mwifiex_search_oui_in_ie(iebody, oui);
  137. if (ret)
  138. return ret;
  139. }
  140. return ret;
  141. }
  142. /*
  143. * This function compares two SSIDs and checks if they match.
  144. */
  145. s32
  146. mwifiex_ssid_cmp(struct mwifiex_802_11_ssid *ssid1,
  147. struct mwifiex_802_11_ssid *ssid2)
  148. {
  149. if (!ssid1 || !ssid2 || (ssid1->ssid_len != ssid2->ssid_len))
  150. return -1;
  151. return memcmp(ssid1->ssid, ssid2->ssid, ssid1->ssid_len);
  152. }
  153. /*
  154. * This function checks if wapi is enabled in driver and scanned network is
  155. * compatible with it.
  156. */
  157. static bool
  158. mwifiex_is_network_compatible_for_wapi(struct mwifiex_private *priv,
  159. struct mwifiex_bssdescriptor *bss_desc)
  160. {
  161. if (priv->sec_info.wapi_enabled &&
  162. (bss_desc->bcn_wapi_ie &&
  163. ((*(bss_desc->bcn_wapi_ie)).ieee_hdr.element_id ==
  164. WLAN_EID_BSS_AC_ACCESS_DELAY))) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /*
  170. * This function checks if driver is configured with no security mode and
  171. * scanned network is compatible with it.
  172. */
  173. static bool
  174. mwifiex_is_network_compatible_for_no_sec(struct mwifiex_private *priv,
  175. struct mwifiex_bssdescriptor *bss_desc)
  176. {
  177. if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_DISABLED
  178. && !priv->sec_info.wpa_enabled && !priv->sec_info.wpa2_enabled
  179. && ((!bss_desc->bcn_wpa_ie) ||
  180. ((*(bss_desc->bcn_wpa_ie)).vend_hdr.element_id !=
  181. WLAN_EID_WPA))
  182. && ((!bss_desc->bcn_rsn_ie) ||
  183. ((*(bss_desc->bcn_rsn_ie)).ieee_hdr.element_id !=
  184. WLAN_EID_RSN))
  185. && !priv->sec_info.encryption_mode
  186. && !bss_desc->privacy) {
  187. return true;
  188. }
  189. return false;
  190. }
  191. /*
  192. * This function checks if static WEP is enabled in driver and scanned network
  193. * is compatible with it.
  194. */
  195. static bool
  196. mwifiex_is_network_compatible_for_static_wep(struct mwifiex_private *priv,
  197. struct mwifiex_bssdescriptor *bss_desc)
  198. {
  199. if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_ENABLED
  200. && !priv->sec_info.wpa_enabled && !priv->sec_info.wpa2_enabled
  201. && bss_desc->privacy) {
  202. return true;
  203. }
  204. return false;
  205. }
  206. /*
  207. * This function checks if wpa is enabled in driver and scanned network is
  208. * compatible with it.
  209. */
  210. static bool
  211. mwifiex_is_network_compatible_for_wpa(struct mwifiex_private *priv,
  212. struct mwifiex_bssdescriptor *bss_desc)
  213. {
  214. if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_DISABLED
  215. && priv->sec_info.wpa_enabled && !priv->sec_info.wpa2_enabled
  216. && ((bss_desc->bcn_wpa_ie) && ((*(bss_desc->bcn_wpa_ie)).vend_hdr.
  217. element_id == WLAN_EID_WPA))
  218. /*
  219. * Privacy bit may NOT be set in some APs like
  220. * LinkSys WRT54G && bss_desc->privacy
  221. */
  222. ) {
  223. dev_dbg(priv->adapter->dev, "info: %s: WPA:"
  224. " wpa_ie=%#x wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
  225. "EncMode=%#x privacy=%#x\n", __func__,
  226. (bss_desc->bcn_wpa_ie) ?
  227. (*(bss_desc->bcn_wpa_ie)).
  228. vend_hdr.element_id : 0,
  229. (bss_desc->bcn_rsn_ie) ?
  230. (*(bss_desc->bcn_rsn_ie)).
  231. ieee_hdr.element_id : 0,
  232. (priv->sec_info.wep_status ==
  233. MWIFIEX_802_11_WEP_ENABLED) ? "e" : "d",
  234. (priv->sec_info.wpa_enabled) ? "e" : "d",
  235. (priv->sec_info.wpa2_enabled) ? "e" : "d",
  236. priv->sec_info.encryption_mode,
  237. bss_desc->privacy);
  238. return true;
  239. }
  240. return false;
  241. }
  242. /*
  243. * This function checks if wpa2 is enabled in driver and scanned network is
  244. * compatible with it.
  245. */
  246. static bool
  247. mwifiex_is_network_compatible_for_wpa2(struct mwifiex_private *priv,
  248. struct mwifiex_bssdescriptor *bss_desc)
  249. {
  250. if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_DISABLED
  251. && !priv->sec_info.wpa_enabled && priv->sec_info.wpa2_enabled
  252. && ((bss_desc->bcn_rsn_ie) && ((*(bss_desc->bcn_rsn_ie)).ieee_hdr.
  253. element_id == WLAN_EID_RSN))
  254. /*
  255. * Privacy bit may NOT be set in some APs like
  256. * LinkSys WRT54G && bss_desc->privacy
  257. */
  258. ) {
  259. dev_dbg(priv->adapter->dev, "info: %s: WPA2: "
  260. " wpa_ie=%#x wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
  261. "EncMode=%#x privacy=%#x\n", __func__,
  262. (bss_desc->bcn_wpa_ie) ?
  263. (*(bss_desc->bcn_wpa_ie)).
  264. vend_hdr.element_id : 0,
  265. (bss_desc->bcn_rsn_ie) ?
  266. (*(bss_desc->bcn_rsn_ie)).
  267. ieee_hdr.element_id : 0,
  268. (priv->sec_info.wep_status ==
  269. MWIFIEX_802_11_WEP_ENABLED) ? "e" : "d",
  270. (priv->sec_info.wpa_enabled) ? "e" : "d",
  271. (priv->sec_info.wpa2_enabled) ? "e" : "d",
  272. priv->sec_info.encryption_mode,
  273. bss_desc->privacy);
  274. return true;
  275. }
  276. return false;
  277. }
  278. /*
  279. * This function checks if adhoc AES is enabled in driver and scanned network is
  280. * compatible with it.
  281. */
  282. static bool
  283. mwifiex_is_network_compatible_for_adhoc_aes(struct mwifiex_private *priv,
  284. struct mwifiex_bssdescriptor *bss_desc)
  285. {
  286. if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_DISABLED
  287. && !priv->sec_info.wpa_enabled && !priv->sec_info.wpa2_enabled
  288. && ((!bss_desc->bcn_wpa_ie) || ((*(bss_desc->bcn_wpa_ie)).vend_hdr.
  289. element_id != WLAN_EID_WPA))
  290. && ((!bss_desc->bcn_rsn_ie) || ((*(bss_desc->bcn_rsn_ie)).ieee_hdr.
  291. element_id != WLAN_EID_RSN))
  292. && !priv->sec_info.encryption_mode
  293. && bss_desc->privacy) {
  294. return true;
  295. }
  296. return false;
  297. }
  298. /*
  299. * This function checks if dynamic WEP is enabled in driver and scanned network
  300. * is compatible with it.
  301. */
  302. static bool
  303. mwifiex_is_network_compatible_for_dynamic_wep(struct mwifiex_private *priv,
  304. struct mwifiex_bssdescriptor *bss_desc)
  305. {
  306. if (priv->sec_info.wep_status == MWIFIEX_802_11_WEP_DISABLED
  307. && !priv->sec_info.wpa_enabled && !priv->sec_info.wpa2_enabled
  308. && ((!bss_desc->bcn_wpa_ie) || ((*(bss_desc->bcn_wpa_ie)).vend_hdr.
  309. element_id != WLAN_EID_WPA))
  310. && ((!bss_desc->bcn_rsn_ie) || ((*(bss_desc->bcn_rsn_ie)).ieee_hdr.
  311. element_id != WLAN_EID_RSN))
  312. && priv->sec_info.encryption_mode
  313. && bss_desc->privacy) {
  314. dev_dbg(priv->adapter->dev, "info: %s: dynamic "
  315. "WEP: wpa_ie=%#x wpa2_ie=%#x "
  316. "EncMode=%#x privacy=%#x\n",
  317. __func__,
  318. (bss_desc->bcn_wpa_ie) ?
  319. (*(bss_desc->bcn_wpa_ie)).
  320. vend_hdr.element_id : 0,
  321. (bss_desc->bcn_rsn_ie) ?
  322. (*(bss_desc->bcn_rsn_ie)).
  323. ieee_hdr.element_id : 0,
  324. priv->sec_info.encryption_mode,
  325. bss_desc->privacy);
  326. return true;
  327. }
  328. return false;
  329. }
  330. /*
  331. * This function checks if a scanned network is compatible with the driver
  332. * settings.
  333. *
  334. * WEP WPA WPA2 ad-hoc encrypt Network
  335. * enabled enabled enabled AES mode Privacy WPA WPA2 Compatible
  336. * 0 0 0 0 NONE 0 0 0 yes No security
  337. * 0 1 0 0 x 1x 1 x yes WPA (disable
  338. * HT if no AES)
  339. * 0 0 1 0 x 1x x 1 yes WPA2 (disable
  340. * HT if no AES)
  341. * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
  342. * 1 0 0 0 NONE 1 0 0 yes Static WEP
  343. * (disable HT)
  344. * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
  345. *
  346. * Compatibility is not matched while roaming, except for mode.
  347. */
  348. static s32
  349. mwifiex_is_network_compatible(struct mwifiex_private *priv,
  350. struct mwifiex_bssdescriptor *bss_desc, u32 mode)
  351. {
  352. struct mwifiex_adapter *adapter = priv->adapter;
  353. bss_desc->disable_11n = false;
  354. /* Don't check for compatibility if roaming */
  355. if (priv->media_connected && (priv->bss_mode == NL80211_IFTYPE_STATION)
  356. && (bss_desc->bss_mode == NL80211_IFTYPE_STATION))
  357. return 0;
  358. if (priv->wps.session_enable) {
  359. dev_dbg(adapter->dev,
  360. "info: return success directly in WPS period\n");
  361. return 0;
  362. }
  363. if (mwifiex_is_network_compatible_for_wapi(priv, bss_desc)) {
  364. dev_dbg(adapter->dev, "info: return success for WAPI AP\n");
  365. return 0;
  366. }
  367. if (bss_desc->bss_mode == mode) {
  368. if (mwifiex_is_network_compatible_for_no_sec(priv, bss_desc)) {
  369. /* No security */
  370. return 0;
  371. } else if (mwifiex_is_network_compatible_for_static_wep(priv,
  372. bss_desc)) {
  373. /* Static WEP enabled */
  374. dev_dbg(adapter->dev, "info: Disable 11n in WEP mode.\n");
  375. bss_desc->disable_11n = true;
  376. return 0;
  377. } else if (mwifiex_is_network_compatible_for_wpa(priv,
  378. bss_desc)) {
  379. /* WPA enabled */
  380. if (((priv->adapter->config_bands & BAND_GN
  381. || priv->adapter->config_bands & BAND_AN)
  382. && bss_desc->bcn_ht_cap)
  383. && !mwifiex_is_wpa_oui_present(bss_desc,
  384. CIPHER_SUITE_CCMP)) {
  385. if (mwifiex_is_wpa_oui_present(bss_desc,
  386. CIPHER_SUITE_TKIP)) {
  387. dev_dbg(adapter->dev,
  388. "info: Disable 11n if AES "
  389. "is not supported by AP\n");
  390. bss_desc->disable_11n = true;
  391. } else {
  392. return -1;
  393. }
  394. }
  395. return 0;
  396. } else if (mwifiex_is_network_compatible_for_wpa2(priv,
  397. bss_desc)) {
  398. /* WPA2 enabled */
  399. if (((priv->adapter->config_bands & BAND_GN
  400. || priv->adapter->config_bands & BAND_AN)
  401. && bss_desc->bcn_ht_cap)
  402. && !mwifiex_is_rsn_oui_present(bss_desc,
  403. CIPHER_SUITE_CCMP)) {
  404. if (mwifiex_is_rsn_oui_present(bss_desc,
  405. CIPHER_SUITE_TKIP)) {
  406. dev_dbg(adapter->dev,
  407. "info: Disable 11n if AES "
  408. "is not supported by AP\n");
  409. bss_desc->disable_11n = true;
  410. } else {
  411. return -1;
  412. }
  413. }
  414. return 0;
  415. } else if (mwifiex_is_network_compatible_for_adhoc_aes(priv,
  416. bss_desc)) {
  417. /* Ad-hoc AES enabled */
  418. return 0;
  419. } else if (mwifiex_is_network_compatible_for_dynamic_wep(priv,
  420. bss_desc)) {
  421. /* Dynamic WEP enabled */
  422. return 0;
  423. }
  424. /* Security doesn't match */
  425. dev_dbg(adapter->dev, "info: %s: failed: "
  426. "wpa_ie=%#x wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s EncMode"
  427. "=%#x privacy=%#x\n",
  428. __func__,
  429. (bss_desc->bcn_wpa_ie) ?
  430. (*(bss_desc->bcn_wpa_ie)).vend_hdr.
  431. element_id : 0,
  432. (bss_desc->bcn_rsn_ie) ?
  433. (*(bss_desc->bcn_rsn_ie)).ieee_hdr.
  434. element_id : 0,
  435. (priv->sec_info.wep_status ==
  436. MWIFIEX_802_11_WEP_ENABLED) ? "e" : "d",
  437. (priv->sec_info.wpa_enabled) ? "e" : "d",
  438. (priv->sec_info.wpa2_enabled) ? "e" : "d",
  439. priv->sec_info.encryption_mode, bss_desc->privacy);
  440. return -1;
  441. }
  442. /* Mode doesn't match */
  443. return -1;
  444. }
  445. /*
  446. * This function creates a channel list for the driver to scan, based
  447. * on region/band information.
  448. *
  449. * This routine is used for any scan that is not provided with a
  450. * specific channel list to scan.
  451. */
  452. static void
  453. mwifiex_scan_create_channel_list(struct mwifiex_private *priv,
  454. const struct mwifiex_user_scan_cfg
  455. *user_scan_in,
  456. struct mwifiex_chan_scan_param_set
  457. *scan_chan_list,
  458. u8 filtered_scan)
  459. {
  460. enum ieee80211_band band;
  461. struct ieee80211_supported_band *sband;
  462. struct ieee80211_channel *ch;
  463. struct mwifiex_adapter *adapter = priv->adapter;
  464. int chan_idx = 0, i;
  465. u8 scan_type;
  466. for (band = 0; (band < IEEE80211_NUM_BANDS) ; band++) {
  467. if (!priv->wdev->wiphy->bands[band])
  468. continue;
  469. sband = priv->wdev->wiphy->bands[band];
  470. for (i = 0; (i < sband->n_channels) ; i++) {
  471. ch = &sband->channels[i];
  472. if (ch->flags & IEEE80211_CHAN_DISABLED)
  473. continue;
  474. scan_chan_list[chan_idx].radio_type = band;
  475. scan_type = ch->flags & IEEE80211_CHAN_PASSIVE_SCAN;
  476. if (user_scan_in &&
  477. user_scan_in->chan_list[0].scan_time)
  478. scan_chan_list[chan_idx].max_scan_time =
  479. cpu_to_le16((u16) user_scan_in->
  480. chan_list[0].scan_time);
  481. else if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
  482. scan_chan_list[chan_idx].max_scan_time =
  483. cpu_to_le16(adapter->passive_scan_time);
  484. else
  485. scan_chan_list[chan_idx].max_scan_time =
  486. cpu_to_le16(adapter->active_scan_time);
  487. if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
  488. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  489. |= MWIFIEX_PASSIVE_SCAN;
  490. else
  491. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  492. &= ~MWIFIEX_PASSIVE_SCAN;
  493. scan_chan_list[chan_idx].chan_number =
  494. (u32) ch->hw_value;
  495. if (filtered_scan) {
  496. scan_chan_list[chan_idx].max_scan_time =
  497. cpu_to_le16(adapter->specific_scan_time);
  498. scan_chan_list[chan_idx].chan_scan_mode_bitmap
  499. |= MWIFIEX_DISABLE_CHAN_FILT;
  500. }
  501. chan_idx++;
  502. }
  503. }
  504. }
  505. /*
  506. * This function constructs and sends multiple scan config commands to
  507. * the firmware.
  508. *
  509. * Previous routines in the code flow have created a scan command configuration
  510. * with any requested TLVs. This function splits the channel TLV into maximum
  511. * channels supported per scan lists and sends the portion of the channel TLV,
  512. * along with the other TLVs, to the firmware.
  513. */
  514. static int
  515. mwifiex_scan_channel_list(struct mwifiex_private *priv,
  516. u32 max_chan_per_scan, u8 filtered_scan,
  517. struct mwifiex_scan_cmd_config *scan_cfg_out,
  518. struct mwifiex_ie_types_chan_list_param_set
  519. *chan_tlv_out,
  520. struct mwifiex_chan_scan_param_set *scan_chan_list)
  521. {
  522. int ret = 0;
  523. struct mwifiex_chan_scan_param_set *tmp_chan_list;
  524. struct mwifiex_chan_scan_param_set *start_chan;
  525. u32 tlv_idx;
  526. u32 total_scan_time;
  527. u32 done_early;
  528. if (!scan_cfg_out || !chan_tlv_out || !scan_chan_list) {
  529. dev_dbg(priv->adapter->dev,
  530. "info: Scan: Null detect: %p, %p, %p\n",
  531. scan_cfg_out, chan_tlv_out, scan_chan_list);
  532. return -1;
  533. }
  534. chan_tlv_out->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
  535. /* Set the temp channel struct pointer to the start of the desired
  536. list */
  537. tmp_chan_list = scan_chan_list;
  538. /* Loop through the desired channel list, sending a new firmware scan
  539. commands for each max_chan_per_scan channels (or for 1,6,11
  540. individually if configured accordingly) */
  541. while (tmp_chan_list->chan_number) {
  542. tlv_idx = 0;
  543. total_scan_time = 0;
  544. chan_tlv_out->header.len = 0;
  545. start_chan = tmp_chan_list;
  546. done_early = false;
  547. /*
  548. * Construct the Channel TLV for the scan command. Continue to
  549. * insert channel TLVs until:
  550. * - the tlv_idx hits the maximum configured per scan command
  551. * - the next channel to insert is 0 (end of desired channel
  552. * list)
  553. * - done_early is set (controlling individual scanning of
  554. * 1,6,11)
  555. */
  556. while (tlv_idx < max_chan_per_scan
  557. && tmp_chan_list->chan_number && !done_early) {
  558. dev_dbg(priv->adapter->dev,
  559. "info: Scan: Chan(%3d), Radio(%d),"
  560. " Mode(%d, %d), Dur(%d)\n",
  561. tmp_chan_list->chan_number,
  562. tmp_chan_list->radio_type,
  563. tmp_chan_list->chan_scan_mode_bitmap
  564. & MWIFIEX_PASSIVE_SCAN,
  565. (tmp_chan_list->chan_scan_mode_bitmap
  566. & MWIFIEX_DISABLE_CHAN_FILT) >> 1,
  567. le16_to_cpu(tmp_chan_list->max_scan_time));
  568. /* Copy the current channel TLV to the command being
  569. prepared */
  570. memcpy(chan_tlv_out->chan_scan_param + tlv_idx,
  571. tmp_chan_list,
  572. sizeof(chan_tlv_out->chan_scan_param));
  573. /* Increment the TLV header length by the size
  574. appended */
  575. chan_tlv_out->header.len =
  576. cpu_to_le16(le16_to_cpu(chan_tlv_out->header.len) +
  577. (sizeof(chan_tlv_out->chan_scan_param)));
  578. /*
  579. * The tlv buffer length is set to the number of bytes
  580. * of the between the channel tlv pointer and the start
  581. * of the tlv buffer. This compensates for any TLVs
  582. * that were appended before the channel list.
  583. */
  584. scan_cfg_out->tlv_buf_len = (u32) ((u8 *) chan_tlv_out -
  585. scan_cfg_out->tlv_buf);
  586. /* Add the size of the channel tlv header and the data
  587. length */
  588. scan_cfg_out->tlv_buf_len +=
  589. (sizeof(chan_tlv_out->header)
  590. + le16_to_cpu(chan_tlv_out->header.len));
  591. /* Increment the index to the channel tlv we are
  592. constructing */
  593. tlv_idx++;
  594. /* Count the total scan time per command */
  595. total_scan_time +=
  596. le16_to_cpu(tmp_chan_list->max_scan_time);
  597. done_early = false;
  598. /* Stop the loop if the *current* channel is in the
  599. 1,6,11 set and we are not filtering on a BSSID
  600. or SSID. */
  601. if (!filtered_scan && (tmp_chan_list->chan_number == 1
  602. || tmp_chan_list->chan_number == 6
  603. || tmp_chan_list->chan_number == 11))
  604. done_early = true;
  605. /* Increment the tmp pointer to the next channel to
  606. be scanned */
  607. tmp_chan_list++;
  608. /* Stop the loop if the *next* channel is in the 1,6,11
  609. set. This will cause it to be the only channel
  610. scanned on the next interation */
  611. if (!filtered_scan && (tmp_chan_list->chan_number == 1
  612. || tmp_chan_list->chan_number == 6
  613. || tmp_chan_list->chan_number == 11))
  614. done_early = true;
  615. }
  616. /* The total scan time should be less than scan command timeout
  617. value */
  618. if (total_scan_time > MWIFIEX_MAX_TOTAL_SCAN_TIME) {
  619. dev_err(priv->adapter->dev, "total scan time %dms"
  620. " is over limit (%dms), scan skipped\n",
  621. total_scan_time, MWIFIEX_MAX_TOTAL_SCAN_TIME);
  622. ret = -1;
  623. break;
  624. }
  625. priv->adapter->scan_channels = start_chan;
  626. /* Send the scan command to the firmware with the specified
  627. cfg */
  628. ret = mwifiex_send_cmd_async(priv, HostCmd_CMD_802_11_SCAN,
  629. HostCmd_ACT_GEN_SET, 0,
  630. scan_cfg_out);
  631. if (ret)
  632. break;
  633. }
  634. if (ret)
  635. return -1;
  636. return 0;
  637. }
  638. /*
  639. * This function constructs a scan command configuration structure to use
  640. * in scan commands.
  641. *
  642. * Application layer or other functions can invoke network scanning
  643. * with a scan configuration supplied in a user scan configuration structure.
  644. * This structure is used as the basis of one or many scan command configuration
  645. * commands that are sent to the command processing module and eventually to the
  646. * firmware.
  647. *
  648. * This function creates a scan command configuration structure based on the
  649. * following user supplied parameters (if present):
  650. * - SSID filter
  651. * - BSSID filter
  652. * - Number of Probes to be sent
  653. * - Channel list
  654. *
  655. * If the SSID or BSSID filter is not present, the filter is disabled/cleared.
  656. * If the number of probes is not set, adapter default setting is used.
  657. */
  658. static void
  659. mwifiex_scan_setup_scan_config(struct mwifiex_private *priv,
  660. const struct mwifiex_user_scan_cfg *user_scan_in,
  661. struct mwifiex_scan_cmd_config *scan_cfg_out,
  662. struct mwifiex_ie_types_chan_list_param_set
  663. **chan_list_out,
  664. struct mwifiex_chan_scan_param_set
  665. *scan_chan_list,
  666. u8 *max_chan_per_scan, u8 *filtered_scan,
  667. u8 *scan_current_only)
  668. {
  669. struct mwifiex_adapter *adapter = priv->adapter;
  670. struct mwifiex_ie_types_num_probes *num_probes_tlv;
  671. struct mwifiex_ie_types_wildcard_ssid_params *wildcard_ssid_tlv;
  672. struct mwifiex_ie_types_rates_param_set *rates_tlv;
  673. const u8 zero_mac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
  674. u8 *tlv_pos;
  675. u32 num_probes;
  676. u32 ssid_len;
  677. u32 chan_idx;
  678. u32 scan_type;
  679. u16 scan_dur;
  680. u8 channel;
  681. u8 radio_type;
  682. u32 ssid_idx;
  683. u8 ssid_filter;
  684. u8 rates[MWIFIEX_SUPPORTED_RATES];
  685. u32 rates_size;
  686. struct mwifiex_ie_types_htcap *ht_cap;
  687. /* The tlv_buf_len is calculated for each scan command. The TLVs added
  688. in this routine will be preserved since the routine that sends the
  689. command will append channelTLVs at *chan_list_out. The difference
  690. between the *chan_list_out and the tlv_buf start will be used to
  691. calculate the size of anything we add in this routine. */
  692. scan_cfg_out->tlv_buf_len = 0;
  693. /* Running tlv pointer. Assigned to chan_list_out at end of function
  694. so later routines know where channels can be added to the command
  695. buf */
  696. tlv_pos = scan_cfg_out->tlv_buf;
  697. /* Initialize the scan as un-filtered; the flag is later set to TRUE
  698. below if a SSID or BSSID filter is sent in the command */
  699. *filtered_scan = false;
  700. /* Initialize the scan as not being only on the current channel. If
  701. the channel list is customized, only contains one channel, and is
  702. the active channel, this is set true and data flow is not halted. */
  703. *scan_current_only = false;
  704. if (user_scan_in) {
  705. /* Default the ssid_filter flag to TRUE, set false under
  706. certain wildcard conditions and qualified by the existence
  707. of an SSID list before marking the scan as filtered */
  708. ssid_filter = true;
  709. /* Set the BSS type scan filter, use Adapter setting if
  710. unset */
  711. scan_cfg_out->bss_mode =
  712. (user_scan_in->bss_mode ? (u8) user_scan_in->
  713. bss_mode : (u8) adapter->scan_mode);
  714. /* Set the number of probes to send, use Adapter setting
  715. if unset */
  716. num_probes =
  717. (user_scan_in->num_probes ? user_scan_in->
  718. num_probes : adapter->scan_probes);
  719. /*
  720. * Set the BSSID filter to the incoming configuration,
  721. * if non-zero. If not set, it will remain disabled
  722. * (all zeros).
  723. */
  724. memcpy(scan_cfg_out->specific_bssid,
  725. user_scan_in->specific_bssid,
  726. sizeof(scan_cfg_out->specific_bssid));
  727. for (ssid_idx = 0;
  728. ((ssid_idx < ARRAY_SIZE(user_scan_in->ssid_list))
  729. && (*user_scan_in->ssid_list[ssid_idx].ssid
  730. || user_scan_in->ssid_list[ssid_idx].max_len));
  731. ssid_idx++) {
  732. ssid_len = strlen(user_scan_in->ssid_list[ssid_idx].
  733. ssid) + 1;
  734. wildcard_ssid_tlv =
  735. (struct mwifiex_ie_types_wildcard_ssid_params *)
  736. tlv_pos;
  737. wildcard_ssid_tlv->header.type =
  738. cpu_to_le16(TLV_TYPE_WILDCARDSSID);
  739. wildcard_ssid_tlv->header.len = cpu_to_le16(
  740. (u16) (ssid_len + sizeof(wildcard_ssid_tlv->
  741. max_ssid_length)));
  742. wildcard_ssid_tlv->max_ssid_length =
  743. user_scan_in->ssid_list[ssid_idx].max_len;
  744. memcpy(wildcard_ssid_tlv->ssid,
  745. user_scan_in->ssid_list[ssid_idx].ssid,
  746. ssid_len);
  747. tlv_pos += (sizeof(wildcard_ssid_tlv->header)
  748. + le16_to_cpu(wildcard_ssid_tlv->header.len));
  749. dev_dbg(adapter->dev, "info: scan: ssid_list[%d]: %s, %d\n",
  750. ssid_idx, wildcard_ssid_tlv->ssid,
  751. wildcard_ssid_tlv->max_ssid_length);
  752. /* Empty wildcard ssid with a maxlen will match many or
  753. potentially all SSIDs (maxlen == 32), therefore do
  754. not treat the scan as
  755. filtered. */
  756. if (!ssid_len && wildcard_ssid_tlv->max_ssid_length)
  757. ssid_filter = false;
  758. }
  759. /*
  760. * The default number of channels sent in the command is low to
  761. * ensure the response buffer from the firmware does not
  762. * truncate scan results. That is not an issue with an SSID
  763. * or BSSID filter applied to the scan results in the firmware.
  764. */
  765. if ((ssid_idx && ssid_filter)
  766. || memcmp(scan_cfg_out->specific_bssid, &zero_mac,
  767. sizeof(zero_mac)))
  768. *filtered_scan = true;
  769. } else {
  770. scan_cfg_out->bss_mode = (u8) adapter->scan_mode;
  771. num_probes = adapter->scan_probes;
  772. }
  773. /*
  774. * If a specific BSSID or SSID is used, the number of channels in the
  775. * scan command will be increased to the absolute maximum.
  776. */
  777. if (*filtered_scan)
  778. *max_chan_per_scan = MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN;
  779. else
  780. *max_chan_per_scan = MWIFIEX_CHANNELS_PER_SCAN_CMD;
  781. /* If the input config or adapter has the number of Probes set,
  782. add tlv */
  783. if (num_probes) {
  784. dev_dbg(adapter->dev, "info: scan: num_probes = %d\n",
  785. num_probes);
  786. num_probes_tlv = (struct mwifiex_ie_types_num_probes *) tlv_pos;
  787. num_probes_tlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
  788. num_probes_tlv->header.len =
  789. cpu_to_le16(sizeof(num_probes_tlv->num_probes));
  790. num_probes_tlv->num_probes = cpu_to_le16((u16) num_probes);
  791. tlv_pos += sizeof(num_probes_tlv->header) +
  792. le16_to_cpu(num_probes_tlv->header.len);
  793. }
  794. /* Append rates tlv */
  795. memset(rates, 0, sizeof(rates));
  796. rates_size = mwifiex_get_supported_rates(priv, rates);
  797. rates_tlv = (struct mwifiex_ie_types_rates_param_set *) tlv_pos;
  798. rates_tlv->header.type = cpu_to_le16(WLAN_EID_SUPP_RATES);
  799. rates_tlv->header.len = cpu_to_le16((u16) rates_size);
  800. memcpy(rates_tlv->rates, rates, rates_size);
  801. tlv_pos += sizeof(rates_tlv->header) + rates_size;
  802. dev_dbg(adapter->dev, "info: SCAN_CMD: Rates size = %d\n", rates_size);
  803. if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info)
  804. && (priv->adapter->config_bands & BAND_GN
  805. || priv->adapter->config_bands & BAND_AN)) {
  806. ht_cap = (struct mwifiex_ie_types_htcap *) tlv_pos;
  807. memset(ht_cap, 0, sizeof(struct mwifiex_ie_types_htcap));
  808. ht_cap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
  809. ht_cap->header.len =
  810. cpu_to_le16(sizeof(struct ieee80211_ht_cap));
  811. radio_type =
  812. mwifiex_band_to_radio_type(priv->adapter->config_bands);
  813. mwifiex_fill_cap_info(priv, radio_type, ht_cap);
  814. tlv_pos += sizeof(struct mwifiex_ie_types_htcap);
  815. }
  816. /* Append vendor specific IE TLV */
  817. mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_SCAN, &tlv_pos);
  818. /*
  819. * Set the output for the channel TLV to the address in the tlv buffer
  820. * past any TLVs that were added in this function (SSID, num_probes).
  821. * Channel TLVs will be added past this for each scan command,
  822. * preserving the TLVs that were previously added.
  823. */
  824. *chan_list_out =
  825. (struct mwifiex_ie_types_chan_list_param_set *) tlv_pos;
  826. if (user_scan_in && user_scan_in->chan_list[0].chan_number) {
  827. dev_dbg(adapter->dev, "info: Scan: Using supplied channel list\n");
  828. for (chan_idx = 0;
  829. chan_idx < MWIFIEX_USER_SCAN_CHAN_MAX
  830. && user_scan_in->chan_list[chan_idx].chan_number;
  831. chan_idx++) {
  832. channel = user_scan_in->chan_list[chan_idx].chan_number;
  833. (scan_chan_list + chan_idx)->chan_number = channel;
  834. radio_type =
  835. user_scan_in->chan_list[chan_idx].radio_type;
  836. (scan_chan_list + chan_idx)->radio_type = radio_type;
  837. scan_type = user_scan_in->chan_list[chan_idx].scan_type;
  838. if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
  839. (scan_chan_list +
  840. chan_idx)->chan_scan_mode_bitmap
  841. |= MWIFIEX_PASSIVE_SCAN;
  842. else
  843. (scan_chan_list +
  844. chan_idx)->chan_scan_mode_bitmap
  845. &= ~MWIFIEX_PASSIVE_SCAN;
  846. if (user_scan_in->chan_list[chan_idx].scan_time) {
  847. scan_dur = (u16) user_scan_in->
  848. chan_list[chan_idx].scan_time;
  849. } else {
  850. if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
  851. scan_dur = adapter->passive_scan_time;
  852. else if (*filtered_scan)
  853. scan_dur = adapter->specific_scan_time;
  854. else
  855. scan_dur = adapter->active_scan_time;
  856. }
  857. (scan_chan_list + chan_idx)->min_scan_time =
  858. cpu_to_le16(scan_dur);
  859. (scan_chan_list + chan_idx)->max_scan_time =
  860. cpu_to_le16(scan_dur);
  861. }
  862. /* Check if we are only scanning the current channel */
  863. if ((chan_idx == 1)
  864. && (user_scan_in->chan_list[0].chan_number
  865. == priv->curr_bss_params.bss_descriptor.channel)) {
  866. *scan_current_only = true;
  867. dev_dbg(adapter->dev,
  868. "info: Scan: Scanning current channel only\n");
  869. }
  870. } else {
  871. dev_dbg(adapter->dev,
  872. "info: Scan: Creating full region channel list\n");
  873. mwifiex_scan_create_channel_list(priv, user_scan_in,
  874. scan_chan_list,
  875. *filtered_scan);
  876. }
  877. }
  878. /*
  879. * This function inspects the scan response buffer for pointers to
  880. * expected TLVs.
  881. *
  882. * TLVs can be included at the end of the scan response BSS information.
  883. *
  884. * Data in the buffer is parsed pointers to TLVs that can potentially
  885. * be passed back in the response.
  886. */
  887. static void
  888. mwifiex_ret_802_11_scan_get_tlv_ptrs(struct mwifiex_adapter *adapter,
  889. struct mwifiex_ie_types_data *tlv,
  890. u32 tlv_buf_size, u32 req_tlv_type,
  891. struct mwifiex_ie_types_data **tlv_data)
  892. {
  893. struct mwifiex_ie_types_data *current_tlv;
  894. u32 tlv_buf_left;
  895. u32 tlv_type;
  896. u32 tlv_len;
  897. current_tlv = tlv;
  898. tlv_buf_left = tlv_buf_size;
  899. *tlv_data = NULL;
  900. dev_dbg(adapter->dev, "info: SCAN_RESP: tlv_buf_size = %d\n",
  901. tlv_buf_size);
  902. while (tlv_buf_left >= sizeof(struct mwifiex_ie_types_header)) {
  903. tlv_type = le16_to_cpu(current_tlv->header.type);
  904. tlv_len = le16_to_cpu(current_tlv->header.len);
  905. if (sizeof(tlv->header) + tlv_len > tlv_buf_left) {
  906. dev_err(adapter->dev, "SCAN_RESP: TLV buffer corrupt\n");
  907. break;
  908. }
  909. if (req_tlv_type == tlv_type) {
  910. switch (tlv_type) {
  911. case TLV_TYPE_TSFTIMESTAMP:
  912. dev_dbg(adapter->dev, "info: SCAN_RESP: TSF "
  913. "timestamp TLV, len = %d\n", tlv_len);
  914. *tlv_data = (struct mwifiex_ie_types_data *)
  915. current_tlv;
  916. break;
  917. case TLV_TYPE_CHANNELBANDLIST:
  918. dev_dbg(adapter->dev, "info: SCAN_RESP: channel"
  919. " band list TLV, len = %d\n", tlv_len);
  920. *tlv_data = (struct mwifiex_ie_types_data *)
  921. current_tlv;
  922. break;
  923. default:
  924. dev_err(adapter->dev,
  925. "SCAN_RESP: unhandled TLV = %d\n",
  926. tlv_type);
  927. /* Give up, this seems corrupted */
  928. return;
  929. }
  930. }
  931. if (*tlv_data)
  932. break;
  933. tlv_buf_left -= (sizeof(tlv->header) + tlv_len);
  934. current_tlv =
  935. (struct mwifiex_ie_types_data *) (current_tlv->data +
  936. tlv_len);
  937. } /* while */
  938. }
  939. /*
  940. * This function parses provided beacon buffer and updates
  941. * respective fields in bss descriptor structure.
  942. */
  943. int
  944. mwifiex_update_bss_desc_with_ie(struct mwifiex_adapter *adapter,
  945. struct mwifiex_bssdescriptor *bss_entry,
  946. u8 *ie_buf, u32 ie_len)
  947. {
  948. int ret = 0;
  949. u8 element_id;
  950. struct ieee_types_fh_param_set *fh_param_set;
  951. struct ieee_types_ds_param_set *ds_param_set;
  952. struct ieee_types_cf_param_set *cf_param_set;
  953. struct ieee_types_ibss_param_set *ibss_param_set;
  954. u8 *current_ptr;
  955. u8 *rate;
  956. u8 element_len;
  957. u16 total_ie_len;
  958. u8 bytes_to_copy;
  959. u8 rate_size;
  960. u8 found_data_rate_ie;
  961. u32 bytes_left;
  962. struct ieee_types_vendor_specific *vendor_ie;
  963. const u8 wpa_oui[4] = { 0x00, 0x50, 0xf2, 0x01 };
  964. const u8 wmm_oui[4] = { 0x00, 0x50, 0xf2, 0x02 };
  965. found_data_rate_ie = false;
  966. rate_size = 0;
  967. current_ptr = ie_buf;
  968. bytes_left = ie_len;
  969. bss_entry->beacon_buf = ie_buf;
  970. bss_entry->beacon_buf_size = ie_len;
  971. /* Process variable IE */
  972. while (bytes_left >= 2) {
  973. element_id = *current_ptr;
  974. element_len = *(current_ptr + 1);
  975. total_ie_len = element_len + sizeof(struct ieee_types_header);
  976. if (bytes_left < total_ie_len) {
  977. dev_err(adapter->dev, "err: InterpretIE: in processing"
  978. " IE, bytes left < IE length\n");
  979. return -1;
  980. }
  981. switch (element_id) {
  982. case WLAN_EID_SSID:
  983. bss_entry->ssid.ssid_len = element_len;
  984. memcpy(bss_entry->ssid.ssid, (current_ptr + 2),
  985. element_len);
  986. dev_dbg(adapter->dev, "info: InterpretIE: ssid: "
  987. "%-32s\n", bss_entry->ssid.ssid);
  988. break;
  989. case WLAN_EID_SUPP_RATES:
  990. memcpy(bss_entry->data_rates, current_ptr + 2,
  991. element_len);
  992. memcpy(bss_entry->supported_rates, current_ptr + 2,
  993. element_len);
  994. rate_size = element_len;
  995. found_data_rate_ie = true;
  996. break;
  997. case WLAN_EID_FH_PARAMS:
  998. fh_param_set =
  999. (struct ieee_types_fh_param_set *) current_ptr;
  1000. memcpy(&bss_entry->phy_param_set.fh_param_set,
  1001. fh_param_set,
  1002. sizeof(struct ieee_types_fh_param_set));
  1003. break;
  1004. case WLAN_EID_DS_PARAMS:
  1005. ds_param_set =
  1006. (struct ieee_types_ds_param_set *) current_ptr;
  1007. bss_entry->channel = ds_param_set->current_chan;
  1008. memcpy(&bss_entry->phy_param_set.ds_param_set,
  1009. ds_param_set,
  1010. sizeof(struct ieee_types_ds_param_set));
  1011. break;
  1012. case WLAN_EID_CF_PARAMS:
  1013. cf_param_set =
  1014. (struct ieee_types_cf_param_set *) current_ptr;
  1015. memcpy(&bss_entry->ss_param_set.cf_param_set,
  1016. cf_param_set,
  1017. sizeof(struct ieee_types_cf_param_set));
  1018. break;
  1019. case WLAN_EID_IBSS_PARAMS:
  1020. ibss_param_set =
  1021. (struct ieee_types_ibss_param_set *)
  1022. current_ptr;
  1023. memcpy(&bss_entry->ss_param_set.ibss_param_set,
  1024. ibss_param_set,
  1025. sizeof(struct ieee_types_ibss_param_set));
  1026. break;
  1027. case WLAN_EID_ERP_INFO:
  1028. bss_entry->erp_flags = *(current_ptr + 2);
  1029. break;
  1030. case WLAN_EID_EXT_SUPP_RATES:
  1031. /*
  1032. * Only process extended supported rate
  1033. * if data rate is already found.
  1034. * Data rate IE should come before
  1035. * extended supported rate IE
  1036. */
  1037. if (found_data_rate_ie) {
  1038. if ((element_len + rate_size) >
  1039. MWIFIEX_SUPPORTED_RATES)
  1040. bytes_to_copy =
  1041. (MWIFIEX_SUPPORTED_RATES -
  1042. rate_size);
  1043. else
  1044. bytes_to_copy = element_len;
  1045. rate = (u8 *) bss_entry->data_rates;
  1046. rate += rate_size;
  1047. memcpy(rate, current_ptr + 2, bytes_to_copy);
  1048. rate = (u8 *) bss_entry->supported_rates;
  1049. rate += rate_size;
  1050. memcpy(rate, current_ptr + 2, bytes_to_copy);
  1051. }
  1052. break;
  1053. case WLAN_EID_VENDOR_SPECIFIC:
  1054. vendor_ie = (struct ieee_types_vendor_specific *)
  1055. current_ptr;
  1056. if (!memcmp
  1057. (vendor_ie->vend_hdr.oui, wpa_oui,
  1058. sizeof(wpa_oui))) {
  1059. bss_entry->bcn_wpa_ie =
  1060. (struct ieee_types_vendor_specific *)
  1061. current_ptr;
  1062. bss_entry->wpa_offset = (u16) (current_ptr -
  1063. bss_entry->beacon_buf);
  1064. } else if (!memcmp(vendor_ie->vend_hdr.oui, wmm_oui,
  1065. sizeof(wmm_oui))) {
  1066. if (total_ie_len ==
  1067. sizeof(struct ieee_types_wmm_parameter)
  1068. || total_ie_len ==
  1069. sizeof(struct ieee_types_wmm_info))
  1070. /*
  1071. * Only accept and copy the WMM IE if
  1072. * it matches the size expected for the
  1073. * WMM Info IE or the WMM Parameter IE.
  1074. */
  1075. memcpy((u8 *) &bss_entry->wmm_ie,
  1076. current_ptr, total_ie_len);
  1077. }
  1078. break;
  1079. case WLAN_EID_RSN:
  1080. bss_entry->bcn_rsn_ie =
  1081. (struct ieee_types_generic *) current_ptr;
  1082. bss_entry->rsn_offset = (u16) (current_ptr -
  1083. bss_entry->beacon_buf);
  1084. break;
  1085. case WLAN_EID_BSS_AC_ACCESS_DELAY:
  1086. bss_entry->bcn_wapi_ie =
  1087. (struct ieee_types_generic *) current_ptr;
  1088. bss_entry->wapi_offset = (u16) (current_ptr -
  1089. bss_entry->beacon_buf);
  1090. break;
  1091. case WLAN_EID_HT_CAPABILITY:
  1092. bss_entry->bcn_ht_cap = (struct ieee80211_ht_cap *)
  1093. (current_ptr +
  1094. sizeof(struct ieee_types_header));
  1095. bss_entry->ht_cap_offset = (u16) (current_ptr +
  1096. sizeof(struct ieee_types_header) -
  1097. bss_entry->beacon_buf);
  1098. break;
  1099. case WLAN_EID_HT_INFORMATION:
  1100. bss_entry->bcn_ht_info = (struct ieee80211_ht_info *)
  1101. (current_ptr +
  1102. sizeof(struct ieee_types_header));
  1103. bss_entry->ht_info_offset = (u16) (current_ptr +
  1104. sizeof(struct ieee_types_header) -
  1105. bss_entry->beacon_buf);
  1106. break;
  1107. case WLAN_EID_BSS_COEX_2040:
  1108. bss_entry->bcn_bss_co_2040 = (u8 *) (current_ptr +
  1109. sizeof(struct ieee_types_header));
  1110. bss_entry->bss_co_2040_offset = (u16) (current_ptr +
  1111. sizeof(struct ieee_types_header) -
  1112. bss_entry->beacon_buf);
  1113. break;
  1114. case WLAN_EID_EXT_CAPABILITY:
  1115. bss_entry->bcn_ext_cap = (u8 *) (current_ptr +
  1116. sizeof(struct ieee_types_header));
  1117. bss_entry->ext_cap_offset = (u16) (current_ptr +
  1118. sizeof(struct ieee_types_header) -
  1119. bss_entry->beacon_buf);
  1120. break;
  1121. default:
  1122. break;
  1123. }
  1124. current_ptr += element_len + 2;
  1125. /* Need to account for IE ID and IE Len */
  1126. bytes_left -= (element_len + 2);
  1127. } /* while (bytes_left > 2) */
  1128. return ret;
  1129. }
  1130. /*
  1131. * This function converts radio type scan parameter to a band configuration
  1132. * to be used in join command.
  1133. */
  1134. static u8
  1135. mwifiex_radio_type_to_band(u8 radio_type)
  1136. {
  1137. switch (radio_type) {
  1138. case HostCmd_SCAN_RADIO_TYPE_A:
  1139. return BAND_A;
  1140. case HostCmd_SCAN_RADIO_TYPE_BG:
  1141. default:
  1142. return BAND_G;
  1143. }
  1144. }
  1145. /*
  1146. * This is an internal function used to start a scan based on an input
  1147. * configuration.
  1148. *
  1149. * This uses the input user scan configuration information when provided in
  1150. * order to send the appropriate scan commands to firmware to populate or
  1151. * update the internal driver scan table.
  1152. */
  1153. static int mwifiex_scan_networks(struct mwifiex_private *priv,
  1154. const struct mwifiex_user_scan_cfg *user_scan_in)
  1155. {
  1156. int ret = 0;
  1157. struct mwifiex_adapter *adapter = priv->adapter;
  1158. struct cmd_ctrl_node *cmd_node;
  1159. union mwifiex_scan_cmd_config_tlv *scan_cfg_out;
  1160. struct mwifiex_ie_types_chan_list_param_set *chan_list_out;
  1161. u32 buf_size;
  1162. struct mwifiex_chan_scan_param_set *scan_chan_list;
  1163. u8 filtered_scan;
  1164. u8 scan_current_chan_only;
  1165. u8 max_chan_per_scan;
  1166. unsigned long flags;
  1167. if (adapter->scan_processing) {
  1168. dev_dbg(adapter->dev, "cmd: Scan already in process...\n");
  1169. return ret;
  1170. }
  1171. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  1172. adapter->scan_processing = true;
  1173. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  1174. if (priv->scan_block) {
  1175. dev_dbg(adapter->dev,
  1176. "cmd: Scan is blocked during association...\n");
  1177. return ret;
  1178. }
  1179. scan_cfg_out = kzalloc(sizeof(union mwifiex_scan_cmd_config_tlv),
  1180. GFP_KERNEL);
  1181. if (!scan_cfg_out) {
  1182. dev_err(adapter->dev, "failed to alloc scan_cfg_out\n");
  1183. return -ENOMEM;
  1184. }
  1185. buf_size = sizeof(struct mwifiex_chan_scan_param_set) *
  1186. MWIFIEX_USER_SCAN_CHAN_MAX;
  1187. scan_chan_list = kzalloc(buf_size, GFP_KERNEL);
  1188. if (!scan_chan_list) {
  1189. dev_err(adapter->dev, "failed to alloc scan_chan_list\n");
  1190. kfree(scan_cfg_out);
  1191. return -ENOMEM;
  1192. }
  1193. mwifiex_scan_setup_scan_config(priv, user_scan_in,
  1194. &scan_cfg_out->config, &chan_list_out,
  1195. scan_chan_list, &max_chan_per_scan,
  1196. &filtered_scan, &scan_current_chan_only);
  1197. ret = mwifiex_scan_channel_list(priv, max_chan_per_scan, filtered_scan,
  1198. &scan_cfg_out->config, chan_list_out,
  1199. scan_chan_list);
  1200. /* Get scan command from scan_pending_q and put to cmd_pending_q */
  1201. if (!ret) {
  1202. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  1203. if (!list_empty(&adapter->scan_pending_q)) {
  1204. cmd_node = list_first_entry(&adapter->scan_pending_q,
  1205. struct cmd_ctrl_node, list);
  1206. list_del(&cmd_node->list);
  1207. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  1208. flags);
  1209. adapter->cmd_queued = cmd_node;
  1210. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node,
  1211. true);
  1212. } else {
  1213. spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
  1214. flags);
  1215. }
  1216. } else {
  1217. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  1218. adapter->scan_processing = true;
  1219. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  1220. }
  1221. kfree(scan_cfg_out);
  1222. kfree(scan_chan_list);
  1223. return ret;
  1224. }
  1225. /*
  1226. * Sends IOCTL request to start a scan with user configurations.
  1227. *
  1228. * This function allocates the IOCTL request buffer, fills it
  1229. * with requisite parameters and calls the IOCTL handler.
  1230. *
  1231. * Upon completion, it also generates a wireless event to notify
  1232. * applications.
  1233. */
  1234. int mwifiex_set_user_scan_ioctl(struct mwifiex_private *priv,
  1235. struct mwifiex_user_scan_cfg *scan_req)
  1236. {
  1237. int status;
  1238. priv->adapter->scan_wait_q_woken = false;
  1239. status = mwifiex_scan_networks(priv, scan_req);
  1240. if (!status)
  1241. status = mwifiex_wait_queue_complete(priv->adapter);
  1242. return status;
  1243. }
  1244. /*
  1245. * This function prepares a scan command to be sent to the firmware.
  1246. *
  1247. * This uses the scan command configuration sent to the command processing
  1248. * module in command preparation stage to configure a scan command structure
  1249. * to send to firmware.
  1250. *
  1251. * The fixed fields specifying the BSS type and BSSID filters as well as a
  1252. * variable number/length of TLVs are sent in the command to firmware.
  1253. *
  1254. * Preparation also includes -
  1255. * - Setting command ID, and proper size
  1256. * - Ensuring correct endian-ness
  1257. */
  1258. int mwifiex_cmd_802_11_scan(struct host_cmd_ds_command *cmd,
  1259. struct mwifiex_scan_cmd_config *scan_cfg)
  1260. {
  1261. struct host_cmd_ds_802_11_scan *scan_cmd = &cmd->params.scan;
  1262. /* Set fixed field variables in scan command */
  1263. scan_cmd->bss_mode = scan_cfg->bss_mode;
  1264. memcpy(scan_cmd->bssid, scan_cfg->specific_bssid,
  1265. sizeof(scan_cmd->bssid));
  1266. memcpy(scan_cmd->tlv_buffer, scan_cfg->tlv_buf, scan_cfg->tlv_buf_len);
  1267. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_SCAN);
  1268. /* Size is equal to the sizeof(fixed portions) + the TLV len + header */
  1269. cmd->size = cpu_to_le16((u16) (sizeof(scan_cmd->bss_mode)
  1270. + sizeof(scan_cmd->bssid)
  1271. + scan_cfg->tlv_buf_len + S_DS_GEN));
  1272. return 0;
  1273. }
  1274. /*
  1275. * This function checks compatibility of requested network with current
  1276. * driver settings.
  1277. */
  1278. int mwifiex_check_network_compatibility(struct mwifiex_private *priv,
  1279. struct mwifiex_bssdescriptor *bss_desc)
  1280. {
  1281. int ret = -1;
  1282. if (!bss_desc)
  1283. return -1;
  1284. if ((mwifiex_get_cfp_by_band_and_channel_from_cfg80211(priv,
  1285. (u8) bss_desc->bss_band, (u16) bss_desc->channel))) {
  1286. switch (priv->bss_mode) {
  1287. case NL80211_IFTYPE_STATION:
  1288. case NL80211_IFTYPE_ADHOC:
  1289. ret = mwifiex_is_network_compatible(priv, bss_desc,
  1290. priv->bss_mode);
  1291. if (ret)
  1292. dev_err(priv->adapter->dev, "cannot find ssid "
  1293. "%s\n", bss_desc->ssid.ssid);
  1294. break;
  1295. default:
  1296. ret = 0;
  1297. }
  1298. }
  1299. return ret;
  1300. }
  1301. static int
  1302. mwifiex_update_curr_bss_params(struct mwifiex_private *priv, u8 *bssid,
  1303. s32 rssi, const u8 *ie_buf, size_t ie_len,
  1304. u16 beacon_period, u16 cap_info_bitmap, u8 band)
  1305. {
  1306. struct mwifiex_bssdescriptor *bss_desc = NULL;
  1307. int ret;
  1308. unsigned long flags;
  1309. u8 *beacon_ie;
  1310. /* Allocate and fill new bss descriptor */
  1311. bss_desc = kzalloc(sizeof(struct mwifiex_bssdescriptor),
  1312. GFP_KERNEL);
  1313. if (!bss_desc) {
  1314. dev_err(priv->adapter->dev, " failed to alloc bss_desc\n");
  1315. return -ENOMEM;
  1316. }
  1317. beacon_ie = kmemdup(ie_buf, ie_len, GFP_KERNEL);
  1318. if (!beacon_ie) {
  1319. dev_err(priv->adapter->dev, " failed to alloc beacon_ie\n");
  1320. return -ENOMEM;
  1321. }
  1322. ret = mwifiex_fill_new_bss_desc(priv, bssid, rssi, beacon_ie,
  1323. ie_len, beacon_period,
  1324. cap_info_bitmap, band, bss_desc);
  1325. if (ret)
  1326. goto done;
  1327. ret = mwifiex_check_network_compatibility(priv, bss_desc);
  1328. if (ret)
  1329. goto done;
  1330. /* Update current bss descriptor parameters */
  1331. spin_lock_irqsave(&priv->curr_bcn_buf_lock, flags);
  1332. priv->curr_bss_params.bss_descriptor.bcn_wpa_ie = NULL;
  1333. priv->curr_bss_params.bss_descriptor.wpa_offset = 0;
  1334. priv->curr_bss_params.bss_descriptor.bcn_rsn_ie = NULL;
  1335. priv->curr_bss_params.bss_descriptor.rsn_offset = 0;
  1336. priv->curr_bss_params.bss_descriptor.bcn_wapi_ie = NULL;
  1337. priv->curr_bss_params.bss_descriptor.wapi_offset = 0;
  1338. priv->curr_bss_params.bss_descriptor.bcn_ht_cap = NULL;
  1339. priv->curr_bss_params.bss_descriptor.ht_cap_offset =
  1340. 0;
  1341. priv->curr_bss_params.bss_descriptor.bcn_ht_info = NULL;
  1342. priv->curr_bss_params.bss_descriptor.ht_info_offset =
  1343. 0;
  1344. priv->curr_bss_params.bss_descriptor.bcn_bss_co_2040 =
  1345. NULL;
  1346. priv->curr_bss_params.bss_descriptor.
  1347. bss_co_2040_offset = 0;
  1348. priv->curr_bss_params.bss_descriptor.bcn_ext_cap = NULL;
  1349. priv->curr_bss_params.bss_descriptor.ext_cap_offset = 0;
  1350. priv->curr_bss_params.bss_descriptor.beacon_buf = NULL;
  1351. priv->curr_bss_params.bss_descriptor.beacon_buf_size =
  1352. 0;
  1353. /* Make a copy of current BSSID descriptor */
  1354. memcpy(&priv->curr_bss_params.bss_descriptor, bss_desc,
  1355. sizeof(priv->curr_bss_params.bss_descriptor));
  1356. mwifiex_save_curr_bcn(priv);
  1357. spin_unlock_irqrestore(&priv->curr_bcn_buf_lock, flags);
  1358. done:
  1359. kfree(bss_desc);
  1360. kfree(beacon_ie);
  1361. return 0;
  1362. }
  1363. static void mwifiex_free_bss_priv(struct cfg80211_bss *bss)
  1364. {
  1365. kfree(bss->priv);
  1366. }
  1367. /*
  1368. * This function handles the command response of scan.
  1369. *
  1370. * The response buffer for the scan command has the following
  1371. * memory layout:
  1372. *
  1373. * .-------------------------------------------------------------.
  1374. * | Header (4 * sizeof(t_u16)): Standard command response hdr |
  1375. * .-------------------------------------------------------------.
  1376. * | BufSize (t_u16) : sizeof the BSS Description data |
  1377. * .-------------------------------------------------------------.
  1378. * | NumOfSet (t_u8) : Number of BSS Descs returned |
  1379. * .-------------------------------------------------------------.
  1380. * | BSSDescription data (variable, size given in BufSize) |
  1381. * .-------------------------------------------------------------.
  1382. * | TLV data (variable, size calculated using Header->Size, |
  1383. * | BufSize and sizeof the fixed fields above) |
  1384. * .-------------------------------------------------------------.
  1385. */
  1386. int mwifiex_ret_802_11_scan(struct mwifiex_private *priv,
  1387. struct host_cmd_ds_command *resp)
  1388. {
  1389. int ret = 0;
  1390. struct mwifiex_adapter *adapter = priv->adapter;
  1391. struct cmd_ctrl_node *cmd_node;
  1392. struct host_cmd_ds_802_11_scan_rsp *scan_rsp;
  1393. struct mwifiex_ie_types_data *tlv_data;
  1394. struct mwifiex_ie_types_tsf_timestamp *tsf_tlv;
  1395. u8 *bss_info;
  1396. u32 scan_resp_size;
  1397. u32 bytes_left;
  1398. u32 idx;
  1399. u32 tlv_buf_size;
  1400. struct mwifiex_chan_freq_power *cfp;
  1401. struct mwifiex_ie_types_chan_band_list_param_set *chan_band_tlv;
  1402. struct chan_band_param_set *chan_band;
  1403. u8 is_bgscan_resp;
  1404. unsigned long flags;
  1405. struct cfg80211_bss *bss;
  1406. is_bgscan_resp = (le16_to_cpu(resp->command)
  1407. == HostCmd_CMD_802_11_BG_SCAN_QUERY);
  1408. if (is_bgscan_resp)
  1409. scan_rsp = &resp->params.bg_scan_query_resp.scan_resp;
  1410. else
  1411. scan_rsp = &resp->params.scan_resp;
  1412. if (scan_rsp->number_of_sets > MWIFIEX_MAX_AP) {
  1413. dev_err(adapter->dev, "SCAN_RESP: too many AP returned (%d)\n",
  1414. scan_rsp->number_of_sets);
  1415. ret = -1;
  1416. goto done;
  1417. }
  1418. bytes_left = le16_to_cpu(scan_rsp->bss_descript_size);
  1419. dev_dbg(adapter->dev, "info: SCAN_RESP: bss_descript_size %d\n",
  1420. bytes_left);
  1421. scan_resp_size = le16_to_cpu(resp->size);
  1422. dev_dbg(adapter->dev,
  1423. "info: SCAN_RESP: returned %d APs before parsing\n",
  1424. scan_rsp->number_of_sets);
  1425. bss_info = scan_rsp->bss_desc_and_tlv_buffer;
  1426. /*
  1427. * The size of the TLV buffer is equal to the entire command response
  1428. * size (scan_resp_size) minus the fixed fields (sizeof()'s), the
  1429. * BSS Descriptions (bss_descript_size as bytesLef) and the command
  1430. * response header (S_DS_GEN)
  1431. */
  1432. tlv_buf_size = scan_resp_size - (bytes_left
  1433. + sizeof(scan_rsp->bss_descript_size)
  1434. + sizeof(scan_rsp->number_of_sets)
  1435. + S_DS_GEN);
  1436. tlv_data = (struct mwifiex_ie_types_data *) (scan_rsp->
  1437. bss_desc_and_tlv_buffer +
  1438. bytes_left);
  1439. /* Search the TLV buffer space in the scan response for any valid
  1440. TLVs */
  1441. mwifiex_ret_802_11_scan_get_tlv_ptrs(adapter, tlv_data, tlv_buf_size,
  1442. TLV_TYPE_TSFTIMESTAMP,
  1443. (struct mwifiex_ie_types_data **)
  1444. &tsf_tlv);
  1445. /* Search the TLV buffer space in the scan response for any valid
  1446. TLVs */
  1447. mwifiex_ret_802_11_scan_get_tlv_ptrs(adapter, tlv_data, tlv_buf_size,
  1448. TLV_TYPE_CHANNELBANDLIST,
  1449. (struct mwifiex_ie_types_data **)
  1450. &chan_band_tlv);
  1451. for (idx = 0; idx < scan_rsp->number_of_sets && bytes_left; idx++) {
  1452. u8 bssid[ETH_ALEN];
  1453. s32 rssi;
  1454. const u8 *ie_buf;
  1455. size_t ie_len;
  1456. int channel = -1;
  1457. u64 network_tsf = 0;
  1458. u16 beacon_size = 0;
  1459. u32 curr_bcn_bytes;
  1460. u32 freq;
  1461. u16 beacon_period;
  1462. u16 cap_info_bitmap;
  1463. u8 *current_ptr;
  1464. struct mwifiex_bcn_param *bcn_param;
  1465. if (bytes_left >= sizeof(beacon_size)) {
  1466. /* Extract & convert beacon size from command buffer */
  1467. memcpy(&beacon_size, bss_info, sizeof(beacon_size));
  1468. bytes_left -= sizeof(beacon_size);
  1469. bss_info += sizeof(beacon_size);
  1470. }
  1471. if (!beacon_size || beacon_size > bytes_left) {
  1472. bss_info += bytes_left;
  1473. bytes_left = 0;
  1474. return -1;
  1475. }
  1476. /* Initialize the current working beacon pointer for this BSS
  1477. * iteration */
  1478. current_ptr = bss_info;
  1479. /* Advance the return beacon pointer past the current beacon */
  1480. bss_info += beacon_size;
  1481. bytes_left -= beacon_size;
  1482. curr_bcn_bytes = beacon_size;
  1483. /*
  1484. * First 5 fields are bssid, RSSI, time stamp, beacon interval,
  1485. * and capability information
  1486. */
  1487. if (curr_bcn_bytes < sizeof(struct mwifiex_bcn_param)) {
  1488. dev_err(adapter->dev, "InterpretIE: not enough bytes left\n");
  1489. continue;
  1490. }
  1491. bcn_param = (struct mwifiex_bcn_param *)current_ptr;
  1492. current_ptr += sizeof(*bcn_param);
  1493. curr_bcn_bytes -= sizeof(*bcn_param);
  1494. memcpy(bssid, bcn_param->bssid, ETH_ALEN);
  1495. rssi = (s32) (bcn_param->rssi);
  1496. dev_dbg(adapter->dev, "info: InterpretIE: RSSI=%02X\n",
  1497. rssi);
  1498. beacon_period = le16_to_cpu(bcn_param->beacon_period);
  1499. cap_info_bitmap = le16_to_cpu(bcn_param->cap_info_bitmap);
  1500. dev_dbg(adapter->dev, "info: InterpretIE: capabilities=0x%X\n",
  1501. cap_info_bitmap);
  1502. /* Rest of the current buffer are IE's */
  1503. ie_buf = current_ptr;
  1504. ie_len = curr_bcn_bytes;
  1505. dev_dbg(adapter->dev, "info: InterpretIE: IELength for this AP"
  1506. " = %d\n", curr_bcn_bytes);
  1507. while (curr_bcn_bytes >= sizeof(struct ieee_types_header)) {
  1508. u8 element_id, element_len;
  1509. element_id = *current_ptr;
  1510. element_len = *(current_ptr + 1);
  1511. if (curr_bcn_bytes < element_len +
  1512. sizeof(struct ieee_types_header)) {
  1513. dev_err(priv->adapter->dev, "%s: in processing"
  1514. " IE, bytes left < IE length\n",
  1515. __func__);
  1516. goto done;
  1517. }
  1518. if (element_id == WLAN_EID_DS_PARAMS) {
  1519. channel = *(u8 *) (current_ptr +
  1520. sizeof(struct ieee_types_header));
  1521. break;
  1522. }
  1523. current_ptr += element_len +
  1524. sizeof(struct ieee_types_header);
  1525. curr_bcn_bytes -= element_len +
  1526. sizeof(struct ieee_types_header);
  1527. }
  1528. /*
  1529. * If the TSF TLV was appended to the scan results, save this
  1530. * entry's TSF value in the networkTSF field.The networkTSF is
  1531. * the firmware's TSF value at the time the beacon or probe
  1532. * response was received.
  1533. */
  1534. if (tsf_tlv)
  1535. memcpy(&network_tsf,
  1536. &tsf_tlv->tsf_data[idx * TSF_DATA_SIZE],
  1537. sizeof(network_tsf));
  1538. if (channel != -1) {
  1539. struct ieee80211_channel *chan;
  1540. u8 band;
  1541. band = BAND_G;
  1542. if (chan_band_tlv) {
  1543. chan_band =
  1544. &chan_band_tlv->chan_band_param[idx];
  1545. band = mwifiex_radio_type_to_band(
  1546. chan_band->radio_type
  1547. & (BIT(0) | BIT(1)));
  1548. }
  1549. cfp = mwifiex_get_cfp_by_band_and_channel_from_cfg80211(
  1550. priv, (u8)band, (u16)channel);
  1551. freq = cfp ? cfp->freq : 0;
  1552. chan = ieee80211_get_channel(priv->wdev->wiphy, freq);
  1553. if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
  1554. bss = cfg80211_inform_bss(priv->wdev->wiphy,
  1555. chan, bssid, network_tsf,
  1556. cap_info_bitmap, beacon_period,
  1557. ie_buf, ie_len, rssi, GFP_KERNEL);
  1558. *(u8 *)bss->priv = band;
  1559. bss->free_priv = mwifiex_free_bss_priv;
  1560. if (priv->media_connected && !memcmp(bssid,
  1561. priv->curr_bss_params.bss_descriptor
  1562. .mac_address, ETH_ALEN))
  1563. mwifiex_update_curr_bss_params(priv,
  1564. bssid, rssi, ie_buf,
  1565. ie_len, beacon_period,
  1566. cap_info_bitmap, band);
  1567. }
  1568. } else {
  1569. dev_dbg(adapter->dev, "missing BSS channel IE\n");
  1570. }
  1571. }
  1572. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  1573. if (list_empty(&adapter->scan_pending_q)) {
  1574. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  1575. spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
  1576. adapter->scan_processing = false;
  1577. spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
  1578. /* Need to indicate IOCTL complete */
  1579. if (adapter->curr_cmd->wait_q_enabled) {
  1580. adapter->cmd_wait_q.status = 0;
  1581. mwifiex_complete_cmd(adapter, adapter->curr_cmd);
  1582. }
  1583. if (priv->report_scan_result)
  1584. priv->report_scan_result = false;
  1585. if (priv->scan_pending_on_block) {
  1586. priv->scan_pending_on_block = false;
  1587. up(&priv->async_sem);
  1588. }
  1589. } else {
  1590. /* Get scan command from scan_pending_q and put to
  1591. cmd_pending_q */
  1592. cmd_node = list_first_entry(&adapter->scan_pending_q,
  1593. struct cmd_ctrl_node, list);
  1594. list_del(&cmd_node->list);
  1595. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  1596. mwifiex_insert_cmd_to_pending_q(adapter, cmd_node, true);
  1597. }
  1598. done:
  1599. return ret;
  1600. }
  1601. /*
  1602. * This function prepares command for background scan query.
  1603. *
  1604. * Preparation includes -
  1605. * - Setting command ID and proper size
  1606. * - Setting background scan flush parameter
  1607. * - Ensuring correct endian-ness
  1608. */
  1609. int mwifiex_cmd_802_11_bg_scan_query(struct host_cmd_ds_command *cmd)
  1610. {
  1611. struct host_cmd_ds_802_11_bg_scan_query *bg_query =
  1612. &cmd->params.bg_scan_query;
  1613. cmd->command = cpu_to_le16(HostCmd_CMD_802_11_BG_SCAN_QUERY);
  1614. cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_bg_scan_query)
  1615. + S_DS_GEN);
  1616. bg_query->flush = 1;
  1617. return 0;
  1618. }
  1619. /*
  1620. * This function inserts scan command node to the scan pending queue.
  1621. */
  1622. void
  1623. mwifiex_queue_scan_cmd(struct mwifiex_private *priv,
  1624. struct cmd_ctrl_node *cmd_node)
  1625. {
  1626. struct mwifiex_adapter *adapter = priv->adapter;
  1627. unsigned long flags;
  1628. cmd_node->wait_q_enabled = true;
  1629. cmd_node->condition = &adapter->scan_wait_q_woken;
  1630. spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
  1631. list_add_tail(&cmd_node->list, &adapter->scan_pending_q);
  1632. spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
  1633. }
  1634. /*
  1635. * This function sends a scan command for all available channels to the
  1636. * firmware, filtered on a specific SSID.
  1637. */
  1638. static int mwifiex_scan_specific_ssid(struct mwifiex_private *priv,
  1639. struct mwifiex_802_11_ssid *req_ssid)
  1640. {
  1641. struct mwifiex_adapter *adapter = priv->adapter;
  1642. int ret = 0;
  1643. struct mwifiex_user_scan_cfg *scan_cfg;
  1644. if (!req_ssid)
  1645. return -1;
  1646. if (adapter->scan_processing) {
  1647. dev_dbg(adapter->dev, "cmd: Scan already in process...\n");
  1648. return ret;
  1649. }
  1650. if (priv->scan_block) {
  1651. dev_dbg(adapter->dev,
  1652. "cmd: Scan is blocked during association...\n");
  1653. return ret;
  1654. }
  1655. scan_cfg = kzalloc(sizeof(struct mwifiex_user_scan_cfg), GFP_KERNEL);
  1656. if (!scan_cfg) {
  1657. dev_err(adapter->dev, "failed to alloc scan_cfg\n");
  1658. return -ENOMEM;
  1659. }
  1660. memcpy(scan_cfg->ssid_list[0].ssid, req_ssid->ssid,
  1661. req_ssid->ssid_len);
  1662. ret = mwifiex_scan_networks(priv, scan_cfg);
  1663. kfree(scan_cfg);
  1664. return ret;
  1665. }
  1666. /*
  1667. * Sends IOCTL request to start a scan.
  1668. *
  1669. * This function allocates the IOCTL request buffer, fills it
  1670. * with requisite parameters and calls the IOCTL handler.
  1671. *
  1672. * Scan command can be issued for both normal scan and specific SSID
  1673. * scan, depending upon whether an SSID is provided or not.
  1674. */
  1675. int mwifiex_request_scan(struct mwifiex_private *priv,
  1676. struct mwifiex_802_11_ssid *req_ssid)
  1677. {
  1678. int ret;
  1679. if (down_interruptible(&priv->async_sem)) {
  1680. dev_err(priv->adapter->dev, "%s: acquire semaphore\n",
  1681. __func__);
  1682. return -1;
  1683. }
  1684. priv->scan_pending_on_block = true;
  1685. priv->adapter->scan_wait_q_woken = false;
  1686. if (req_ssid && req_ssid->ssid_len != 0)
  1687. /* Specific SSID scan */
  1688. ret = mwifiex_scan_specific_ssid(priv, req_ssid);
  1689. else
  1690. /* Normal scan */
  1691. ret = mwifiex_scan_networks(priv, NULL);
  1692. if (!ret)
  1693. ret = mwifiex_wait_queue_complete(priv->adapter);
  1694. if (ret == -1) {
  1695. priv->scan_pending_on_block = false;
  1696. up(&priv->async_sem);
  1697. }
  1698. return ret;
  1699. }
  1700. /*
  1701. * This function appends the vendor specific IE TLV to a buffer.
  1702. */
  1703. int
  1704. mwifiex_cmd_append_vsie_tlv(struct mwifiex_private *priv,
  1705. u16 vsie_mask, u8 **buffer)
  1706. {
  1707. int id, ret_len = 0;
  1708. struct mwifiex_ie_types_vendor_param_set *vs_param_set;
  1709. if (!buffer)
  1710. return 0;
  1711. if (!(*buffer))
  1712. return 0;
  1713. /*
  1714. * Traverse through the saved vendor specific IE array and append
  1715. * the selected(scan/assoc/adhoc) IE as TLV to the command
  1716. */
  1717. for (id = 0; id < MWIFIEX_MAX_VSIE_NUM; id++) {
  1718. if (priv->vs_ie[id].mask & vsie_mask) {
  1719. vs_param_set =
  1720. (struct mwifiex_ie_types_vendor_param_set *)
  1721. *buffer;
  1722. vs_param_set->header.type =
  1723. cpu_to_le16(TLV_TYPE_PASSTHROUGH);
  1724. vs_param_set->header.len =
  1725. cpu_to_le16((((u16) priv->vs_ie[id].ie[1])
  1726. & 0x00FF) + 2);
  1727. memcpy(vs_param_set->ie, priv->vs_ie[id].ie,
  1728. le16_to_cpu(vs_param_set->header.len));
  1729. *buffer += le16_to_cpu(vs_param_set->header.len) +
  1730. sizeof(struct mwifiex_ie_types_header);
  1731. ret_len += le16_to_cpu(vs_param_set->header.len) +
  1732. sizeof(struct mwifiex_ie_types_header);
  1733. }
  1734. }
  1735. return ret_len;
  1736. }
  1737. /*
  1738. * This function saves a beacon buffer of the current BSS descriptor.
  1739. *
  1740. * The current beacon buffer is saved so that it can be restored in the
  1741. * following cases that makes the beacon buffer not to contain the current
  1742. * ssid's beacon buffer.
  1743. * - The current ssid was not found somehow in the last scan.
  1744. * - The current ssid was the last entry of the scan table and overloaded.
  1745. */
  1746. void
  1747. mwifiex_save_curr_bcn(struct mwifiex_private *priv)
  1748. {
  1749. struct mwifiex_bssdescriptor *curr_bss =
  1750. &priv->curr_bss_params.bss_descriptor;
  1751. if (!curr_bss->beacon_buf_size)
  1752. return;
  1753. /* allocate beacon buffer at 1st time; or if it's size has changed */
  1754. if (!priv->curr_bcn_buf ||
  1755. priv->curr_bcn_size != curr_bss->beacon_buf_size) {
  1756. priv->curr_bcn_size = curr_bss->beacon_buf_size;
  1757. kfree(priv->curr_bcn_buf);
  1758. priv->curr_bcn_buf = kmalloc(curr_bss->beacon_buf_size,
  1759. GFP_KERNEL);
  1760. if (!priv->curr_bcn_buf) {
  1761. dev_err(priv->adapter->dev,
  1762. "failed to alloc curr_bcn_buf\n");
  1763. return;
  1764. }
  1765. }
  1766. memcpy(priv->curr_bcn_buf, curr_bss->beacon_buf,
  1767. curr_bss->beacon_buf_size);
  1768. dev_dbg(priv->adapter->dev, "info: current beacon saved %d\n",
  1769. priv->curr_bcn_size);
  1770. curr_bss->beacon_buf = priv->curr_bcn_buf;
  1771. /* adjust the pointers in the current BSS descriptor */
  1772. if (curr_bss->bcn_wpa_ie)
  1773. curr_bss->bcn_wpa_ie =
  1774. (struct ieee_types_vendor_specific *)
  1775. (curr_bss->beacon_buf +
  1776. curr_bss->wpa_offset);
  1777. if (curr_bss->bcn_rsn_ie)
  1778. curr_bss->bcn_rsn_ie = (struct ieee_types_generic *)
  1779. (curr_bss->beacon_buf +
  1780. curr_bss->rsn_offset);
  1781. if (curr_bss->bcn_ht_cap)
  1782. curr_bss->bcn_ht_cap = (struct ieee80211_ht_cap *)
  1783. (curr_bss->beacon_buf +
  1784. curr_bss->ht_cap_offset);
  1785. if (curr_bss->bcn_ht_info)
  1786. curr_bss->bcn_ht_info = (struct ieee80211_ht_info *)
  1787. (curr_bss->beacon_buf +
  1788. curr_bss->ht_info_offset);
  1789. if (curr_bss->bcn_bss_co_2040)
  1790. curr_bss->bcn_bss_co_2040 =
  1791. (u8 *) (curr_bss->beacon_buf +
  1792. curr_bss->bss_co_2040_offset);
  1793. if (curr_bss->bcn_ext_cap)
  1794. curr_bss->bcn_ext_cap = (u8 *) (curr_bss->beacon_buf +
  1795. curr_bss->ext_cap_offset);
  1796. }
  1797. /*
  1798. * This function frees the current BSS descriptor beacon buffer.
  1799. */
  1800. void
  1801. mwifiex_free_curr_bcn(struct mwifiex_private *priv)
  1802. {
  1803. kfree(priv->curr_bcn_buf);
  1804. priv->curr_bcn_buf = NULL;
  1805. }