scan.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923
  1. /**
  2. * Functions implementing wlan scan IOCTL and firmware command APIs
  3. *
  4. * IOCTL handlers as well as command preperation and response routines
  5. * for sending scan commands to the firmware.
  6. */
  7. #include <linux/ctype.h>
  8. #include <linux/if.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/wireless.h>
  11. #include <linux/etherdevice.h>
  12. #include <net/ieee80211.h>
  13. #include <net/iw_handler.h>
  14. #include "host.h"
  15. #include "decl.h"
  16. #include "dev.h"
  17. #include "scan.h"
  18. //! Approximate amount of data needed to pass a scan result back to iwlist
  19. #define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
  20. + IW_ESSID_MAX_SIZE \
  21. + IW_EV_UINT_LEN \
  22. + IW_EV_FREQ_LEN \
  23. + IW_EV_QUAL_LEN \
  24. + IW_ESSID_MAX_SIZE \
  25. + IW_EV_PARAM_LEN \
  26. + 40) /* 40 for WPAIE */
  27. //! Memory needed to store a max sized channel List TLV for a firmware scan
  28. #define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
  29. + (MRVDRV_MAX_CHANNELS_PER_SCAN \
  30. * sizeof(struct chanscanparamset)))
  31. //! Memory needed to store a max number/size SSID TLV for a firmware scan
  32. #define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
  33. //! Maximum memory needed for a wlan_scan_cmd_config with all TLVs at max
  34. #define MAX_SCAN_CFG_ALLOC (sizeof(struct wlan_scan_cmd_config) \
  35. + sizeof(struct mrvlietypes_numprobes) \
  36. + CHAN_TLV_MAX_SIZE \
  37. + SSID_TLV_MAX_SIZE)
  38. //! The maximum number of channels the firmware can scan per command
  39. #define MRVDRV_MAX_CHANNELS_PER_SCAN 14
  40. /**
  41. * @brief Number of channels to scan per firmware scan command issuance.
  42. *
  43. * Number restricted to prevent hitting the limit on the amount of scan data
  44. * returned in a single firmware scan command.
  45. */
  46. #define MRVDRV_CHANNELS_PER_SCAN_CMD 4
  47. //! Scan time specified in the channel TLV for each channel for passive scans
  48. #define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
  49. //! Scan time specified in the channel TLV for each channel for active scans
  50. #define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
  51. const u8 zeromac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  52. const u8 bcastmac[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  53. static inline void clear_bss_descriptor (struct bss_descriptor * bss)
  54. {
  55. /* Don't blow away ->list, just BSS data */
  56. memset(bss, 0, offsetof(struct bss_descriptor, list));
  57. }
  58. static inline int match_bss_no_security(struct wlan_802_11_security * secinfo,
  59. struct bss_descriptor * match_bss)
  60. {
  61. if ( !secinfo->wep_enabled
  62. && !secinfo->WPAenabled
  63. && !secinfo->WPA2enabled
  64. && match_bss->wpa_ie[0] != WPA_IE
  65. && match_bss->rsn_ie[0] != WPA2_IE
  66. && !match_bss->privacy) {
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. static inline int match_bss_static_wep(struct wlan_802_11_security * secinfo,
  72. struct bss_descriptor * match_bss)
  73. {
  74. if ( secinfo->wep_enabled
  75. && !secinfo->WPAenabled
  76. && !secinfo->WPA2enabled
  77. && match_bss->privacy) {
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static inline int match_bss_wpa(struct wlan_802_11_security * secinfo,
  83. struct bss_descriptor * match_bss)
  84. {
  85. if ( !secinfo->wep_enabled
  86. && secinfo->WPAenabled
  87. && (match_bss->wpa_ie[0] == WPA_IE)
  88. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  89. && bss->privacy */
  90. ) {
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. static inline int match_bss_wpa2(struct wlan_802_11_security * secinfo,
  96. struct bss_descriptor * match_bss)
  97. {
  98. if ( !secinfo->wep_enabled
  99. && secinfo->WPA2enabled
  100. && (match_bss->rsn_ie[0] == WPA2_IE)
  101. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  102. && bss->privacy */
  103. ) {
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. static inline int match_bss_dynamic_wep(struct wlan_802_11_security * secinfo,
  109. struct bss_descriptor * match_bss)
  110. {
  111. if ( !secinfo->wep_enabled
  112. && !secinfo->WPAenabled
  113. && !secinfo->WPA2enabled
  114. && (match_bss->wpa_ie[0] != WPA_IE)
  115. && (match_bss->rsn_ie[0] != WPA2_IE)
  116. && match_bss->privacy) {
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. /**
  122. * @brief Check if a scanned network compatible with the driver settings
  123. *
  124. * WEP WPA WPA2 ad-hoc encrypt Network
  125. * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
  126. * 0 0 0 0 NONE 0 0 0 yes No security
  127. * 1 0 0 0 NONE 1 0 0 yes Static WEP
  128. * 0 1 0 0 x 1x 1 x yes WPA
  129. * 0 0 1 0 x 1x x 1 yes WPA2
  130. * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
  131. * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
  132. *
  133. *
  134. * @param adapter A pointer to wlan_adapter
  135. * @param index Index in scantable to check against current driver settings
  136. * @param mode Network mode: Infrastructure or IBSS
  137. *
  138. * @return Index in scantable, or error code if negative
  139. */
  140. static int is_network_compatible(wlan_adapter * adapter,
  141. struct bss_descriptor * bss, u8 mode)
  142. {
  143. int matched = 0;
  144. lbs_deb_enter(LBS_DEB_ASSOC);
  145. if (bss->mode != mode)
  146. goto done;
  147. if ((matched = match_bss_no_security(&adapter->secinfo, bss))) {
  148. goto done;
  149. } else if ((matched = match_bss_static_wep(&adapter->secinfo, bss))) {
  150. goto done;
  151. } else if ((matched = match_bss_wpa(&adapter->secinfo, bss))) {
  152. lbs_deb_scan(
  153. "is_network_compatible() WPA: wpa_ie=%#x "
  154. "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
  155. "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  156. adapter->secinfo.wep_enabled ? "e" : "d",
  157. adapter->secinfo.WPAenabled ? "e" : "d",
  158. adapter->secinfo.WPA2enabled ? "e" : "d",
  159. bss->privacy);
  160. goto done;
  161. } else if ((matched = match_bss_wpa2(&adapter->secinfo, bss))) {
  162. lbs_deb_scan(
  163. "is_network_compatible() WPA2: wpa_ie=%#x "
  164. "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
  165. "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  166. adapter->secinfo.wep_enabled ? "e" : "d",
  167. adapter->secinfo.WPAenabled ? "e" : "d",
  168. adapter->secinfo.WPA2enabled ? "e" : "d",
  169. bss->privacy);
  170. goto done;
  171. } else if ((matched = match_bss_dynamic_wep(&adapter->secinfo, bss))) {
  172. lbs_deb_scan(
  173. "is_network_compatible() dynamic WEP: "
  174. "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
  175. bss->wpa_ie[0],
  176. bss->rsn_ie[0],
  177. bss->privacy);
  178. goto done;
  179. }
  180. /* bss security settings don't match those configured on card */
  181. lbs_deb_scan(
  182. "is_network_compatible() FAILED: wpa_ie=%#x "
  183. "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
  184. bss->wpa_ie[0], bss->rsn_ie[0],
  185. adapter->secinfo.wep_enabled ? "e" : "d",
  186. adapter->secinfo.WPAenabled ? "e" : "d",
  187. adapter->secinfo.WPA2enabled ? "e" : "d",
  188. bss->privacy);
  189. done:
  190. lbs_deb_leave(LBS_DEB_SCAN);
  191. return matched;
  192. }
  193. /**
  194. * @brief Post process the scan table after a new scan command has completed
  195. *
  196. * Inspect each entry of the scan table and try to find an entry that
  197. * matches our current associated/joined network from the scan. If
  198. * one is found, update the stored copy of the bssdescriptor for our
  199. * current network.
  200. *
  201. * Debug dump the current scan table contents if compiled accordingly.
  202. *
  203. * @param priv A pointer to wlan_private structure
  204. *
  205. * @return void
  206. */
  207. static void wlan_scan_process_results(wlan_private * priv)
  208. {
  209. wlan_adapter *adapter = priv->adapter;
  210. struct bss_descriptor * iter_bss;
  211. if (adapter->connect_status == libertas_connected)
  212. return;
  213. mutex_lock(&adapter->lock);
  214. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  215. lbs_deb_scan("Scan:(%02d) " MAC_FMT ", RSSI[%03d], SSID[%s]\n",
  216. i++, MAC_ARG(iter_bss->bssid), (s32) iter_bss->rssi,
  217. iter_bss->ssid.ssid);
  218. }
  219. mutex_unlock(&adapter->lock);
  220. }
  221. /**
  222. * @brief Create a channel list for the driver to scan based on region info
  223. *
  224. * Use the driver region/band information to construct a comprehensive list
  225. * of channels to scan. This routine is used for any scan that is not
  226. * provided a specific channel list to scan.
  227. *
  228. * @param priv A pointer to wlan_private structure
  229. * @param scanchanlist Output parameter: resulting channel list to scan
  230. * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
  231. * is being sent in the command to firmware. Used to
  232. * increase the number of channels sent in a scan
  233. * command and to disable the firmware channel scan
  234. * filter.
  235. *
  236. * @return void
  237. */
  238. static void wlan_scan_create_channel_list(wlan_private * priv,
  239. struct chanscanparamset * scanchanlist,
  240. u8 filteredscan)
  241. {
  242. wlan_adapter *adapter = priv->adapter;
  243. struct region_channel *scanregion;
  244. struct chan_freq_power *cfp;
  245. int rgnidx;
  246. int chanidx;
  247. int nextchan;
  248. u8 scantype;
  249. chanidx = 0;
  250. /* Set the default scan type to the user specified type, will later
  251. * be changed to passive on a per channel basis if restricted by
  252. * regulatory requirements (11d or 11h)
  253. */
  254. scantype = adapter->scantype;
  255. for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
  256. if (priv->adapter->enable11d &&
  257. adapter->connect_status != libertas_connected) {
  258. /* Scan all the supported chan for the first scan */
  259. if (!adapter->universal_channel[rgnidx].valid)
  260. continue;
  261. scanregion = &adapter->universal_channel[rgnidx];
  262. /* clear the parsed_region_chan for the first scan */
  263. memset(&adapter->parsed_region_chan, 0x00,
  264. sizeof(adapter->parsed_region_chan));
  265. } else {
  266. if (!adapter->region_channel[rgnidx].valid)
  267. continue;
  268. scanregion = &adapter->region_channel[rgnidx];
  269. }
  270. for (nextchan = 0;
  271. nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
  272. cfp = scanregion->CFP + nextchan;
  273. if (priv->adapter->enable11d) {
  274. scantype =
  275. libertas_get_scan_type_11d(cfp->channel,
  276. &adapter->
  277. parsed_region_chan);
  278. }
  279. switch (scanregion->band) {
  280. case BAND_B:
  281. case BAND_G:
  282. default:
  283. scanchanlist[chanidx].radiotype =
  284. cmd_scan_radio_type_bg;
  285. break;
  286. }
  287. if (scantype == cmd_scan_type_passive) {
  288. scanchanlist[chanidx].maxscantime =
  289. cpu_to_le16
  290. (MRVDRV_PASSIVE_SCAN_CHAN_TIME);
  291. scanchanlist[chanidx].chanscanmode.passivescan =
  292. 1;
  293. } else {
  294. scanchanlist[chanidx].maxscantime =
  295. cpu_to_le16
  296. (MRVDRV_ACTIVE_SCAN_CHAN_TIME);
  297. scanchanlist[chanidx].chanscanmode.passivescan =
  298. 0;
  299. }
  300. scanchanlist[chanidx].channumber = cfp->channel;
  301. if (filteredscan) {
  302. scanchanlist[chanidx].chanscanmode.
  303. disablechanfilt = 1;
  304. }
  305. }
  306. }
  307. }
  308. /**
  309. * @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
  310. *
  311. * Application layer or other functions can invoke wlan_scan_networks
  312. * with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
  313. * This structure is used as the basis of one or many wlan_scan_cmd_config
  314. * commands that are sent to the command processing module and sent to
  315. * firmware.
  316. *
  317. * Create a wlan_scan_cmd_config based on the following user supplied
  318. * parameters (if present):
  319. * - SSID filter
  320. * - BSSID filter
  321. * - Number of Probes to be sent
  322. * - channel list
  323. *
  324. * If the SSID or BSSID filter is not present, disable/clear the filter.
  325. * If the number of probes is not set, use the adapter default setting
  326. * Qualify the channel
  327. *
  328. * @param priv A pointer to wlan_private structure
  329. * @param puserscanin NULL or pointer to scan configuration parameters
  330. * @param ppchantlvout Output parameter: Pointer to the start of the
  331. * channel TLV portion of the output scan config
  332. * @param pscanchanlist Output parameter: Pointer to the resulting channel
  333. * list to scan
  334. * @param pmaxchanperscan Output parameter: Number of channels to scan for
  335. * each issuance of the firmware scan command
  336. * @param pfilteredscan Output parameter: Flag indicating whether or not
  337. * a BSSID or SSID filter is being sent in the
  338. * command to firmware. Used to increase the number
  339. * of channels sent in a scan command and to
  340. * disable the firmware channel scan filter.
  341. * @param pscancurrentonly Output parameter: Flag indicating whether or not
  342. * we are only scanning our current active channel
  343. *
  344. * @return resulting scan configuration
  345. */
  346. static struct wlan_scan_cmd_config *
  347. wlan_scan_setup_scan_config(wlan_private * priv,
  348. const struct wlan_ioctl_user_scan_cfg * puserscanin,
  349. struct mrvlietypes_chanlistparamset ** ppchantlvout,
  350. struct chanscanparamset * pscanchanlist,
  351. int *pmaxchanperscan,
  352. u8 * pfilteredscan,
  353. u8 * pscancurrentonly)
  354. {
  355. wlan_adapter *adapter = priv->adapter;
  356. struct mrvlietypes_numprobes *pnumprobestlv;
  357. struct mrvlietypes_ssidparamset *pssidtlv;
  358. struct wlan_scan_cmd_config * pscancfgout = NULL;
  359. u8 *ptlvpos;
  360. u16 numprobes;
  361. int chanidx;
  362. int scantype;
  363. int scandur;
  364. int channel;
  365. int radiotype;
  366. pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
  367. if (pscancfgout == NULL)
  368. goto out;
  369. /* The tlvbufferlen is calculated for each scan command. The TLVs added
  370. * in this routine will be preserved since the routine that sends
  371. * the command will append channelTLVs at *ppchantlvout. The difference
  372. * between the *ppchantlvout and the tlvbuffer start will be used
  373. * to calculate the size of anything we add in this routine.
  374. */
  375. pscancfgout->tlvbufferlen = 0;
  376. /* Running tlv pointer. Assigned to ppchantlvout at end of function
  377. * so later routines know where channels can be added to the command buf
  378. */
  379. ptlvpos = pscancfgout->tlvbuffer;
  380. /*
  381. * Set the initial scan paramters for progressive scanning. If a specific
  382. * BSSID or SSID is used, the number of channels in the scan command
  383. * will be increased to the absolute maximum
  384. */
  385. *pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;
  386. /* Initialize the scan as un-filtered by firmware, set to TRUE below if
  387. * a SSID or BSSID filter is sent in the command
  388. */
  389. *pfilteredscan = 0;
  390. /* Initialize the scan as not being only on the current channel. If
  391. * the channel list is customized, only contains one channel, and
  392. * is the active channel, this is set true and data flow is not halted.
  393. */
  394. *pscancurrentonly = 0;
  395. if (puserscanin) {
  396. /* Set the bss type scan filter, use adapter setting if unset */
  397. pscancfgout->bsstype =
  398. (puserscanin->bsstype ? puserscanin->bsstype : adapter->
  399. scanmode);
  400. /* Set the number of probes to send, use adapter setting if unset */
  401. numprobes = (puserscanin->numprobes ? puserscanin->numprobes :
  402. adapter->scanprobes);
  403. /*
  404. * Set the BSSID filter to the incoming configuration,
  405. * if non-zero. If not set, it will remain disabled (all zeros).
  406. */
  407. memcpy(pscancfgout->bssid, puserscanin->bssid,
  408. sizeof(pscancfgout->bssid));
  409. if (puserscanin->ssid_len) {
  410. pssidtlv =
  411. (struct mrvlietypes_ssidparamset *) pscancfgout->
  412. tlvbuffer;
  413. pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
  414. pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
  415. memcpy(pssidtlv->ssid, puserscanin->ssid,
  416. puserscanin->ssid_len);
  417. ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
  418. }
  419. /*
  420. * The default number of channels sent in the command is low to
  421. * ensure the response buffer from the firmware does not truncate
  422. * scan results. That is not an issue with an SSID or BSSID
  423. * filter applied to the scan results in the firmware.
  424. */
  425. if ( puserscanin->ssid_len
  426. || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
  427. *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
  428. *pfilteredscan = 1;
  429. }
  430. } else {
  431. pscancfgout->bsstype = adapter->scanmode;
  432. numprobes = adapter->scanprobes;
  433. }
  434. /* If the input config or adapter has the number of Probes set, add tlv */
  435. if (numprobes) {
  436. pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
  437. pnumprobestlv->header.type =
  438. cpu_to_le16(TLV_TYPE_NUMPROBES);
  439. pnumprobestlv->header.len = sizeof(pnumprobestlv->numprobes);
  440. pnumprobestlv->numprobes = cpu_to_le16(numprobes);
  441. ptlvpos +=
  442. sizeof(pnumprobestlv->header) + pnumprobestlv->header.len;
  443. pnumprobestlv->header.len =
  444. cpu_to_le16(pnumprobestlv->header.len);
  445. }
  446. /*
  447. * Set the output for the channel TLV to the address in the tlv buffer
  448. * past any TLVs that were added in this fuction (SSID, numprobes).
  449. * channel TLVs will be added past this for each scan command, preserving
  450. * the TLVs that were previously added.
  451. */
  452. *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
  453. if (puserscanin && puserscanin->chanlist[0].channumber) {
  454. lbs_deb_scan("Scan: Using supplied channel list\n");
  455. for (chanidx = 0;
  456. chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
  457. && puserscanin->chanlist[chanidx].channumber; chanidx++) {
  458. channel = puserscanin->chanlist[chanidx].channumber;
  459. (pscanchanlist + chanidx)->channumber = channel;
  460. radiotype = puserscanin->chanlist[chanidx].radiotype;
  461. (pscanchanlist + chanidx)->radiotype = radiotype;
  462. scantype = puserscanin->chanlist[chanidx].scantype;
  463. if (scantype == cmd_scan_type_passive) {
  464. (pscanchanlist +
  465. chanidx)->chanscanmode.passivescan = 1;
  466. } else {
  467. (pscanchanlist +
  468. chanidx)->chanscanmode.passivescan = 0;
  469. }
  470. if (puserscanin->chanlist[chanidx].scantime) {
  471. scandur =
  472. puserscanin->chanlist[chanidx].scantime;
  473. } else {
  474. if (scantype == cmd_scan_type_passive) {
  475. scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
  476. } else {
  477. scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
  478. }
  479. }
  480. (pscanchanlist + chanidx)->minscantime =
  481. cpu_to_le16(scandur);
  482. (pscanchanlist + chanidx)->maxscantime =
  483. cpu_to_le16(scandur);
  484. }
  485. /* Check if we are only scanning the current channel */
  486. if ((chanidx == 1) && (puserscanin->chanlist[0].channumber
  487. ==
  488. priv->adapter->curbssparams.channel)) {
  489. *pscancurrentonly = 1;
  490. lbs_deb_scan("Scan: Scanning current channel only");
  491. }
  492. } else {
  493. lbs_deb_scan("Scan: Creating full region channel list\n");
  494. wlan_scan_create_channel_list(priv, pscanchanlist,
  495. *pfilteredscan);
  496. }
  497. out:
  498. return pscancfgout;
  499. }
  500. /**
  501. * @brief Construct and send multiple scan config commands to the firmware
  502. *
  503. * Previous routines have created a wlan_scan_cmd_config with any requested
  504. * TLVs. This function splits the channel TLV into maxchanperscan lists
  505. * and sends the portion of the channel TLV along with the other TLVs
  506. * to the wlan_cmd routines for execution in the firmware.
  507. *
  508. * @param priv A pointer to wlan_private structure
  509. * @param maxchanperscan Maximum number channels to be included in each
  510. * scan command sent to firmware
  511. * @param filteredscan Flag indicating whether or not a BSSID or SSID
  512. * filter is being used for the firmware command
  513. * scan command sent to firmware
  514. * @param pscancfgout Scan configuration used for this scan.
  515. * @param pchantlvout Pointer in the pscancfgout where the channel TLV
  516. * should start. This is past any other TLVs that
  517. * must be sent down in each firmware command.
  518. * @param pscanchanlist List of channels to scan in maxchanperscan segments
  519. *
  520. * @return 0 or error return otherwise
  521. */
  522. static int wlan_scan_channel_list(wlan_private * priv,
  523. int maxchanperscan,
  524. u8 filteredscan,
  525. struct wlan_scan_cmd_config * pscancfgout,
  526. struct mrvlietypes_chanlistparamset * pchantlvout,
  527. struct chanscanparamset * pscanchanlist,
  528. const struct wlan_ioctl_user_scan_cfg * puserscanin,
  529. int full_scan)
  530. {
  531. struct chanscanparamset *ptmpchan;
  532. struct chanscanparamset *pstartchan;
  533. u8 scanband;
  534. int doneearly;
  535. int tlvidx;
  536. int ret = 0;
  537. int scanned = 0;
  538. union iwreq_data wrqu;
  539. lbs_deb_enter(LBS_DEB_ASSOC);
  540. if (pscancfgout == 0 || pchantlvout == 0 || pscanchanlist == 0) {
  541. lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
  542. pscancfgout, pchantlvout, pscanchanlist);
  543. return -1;
  544. }
  545. pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
  546. /* Set the temp channel struct pointer to the start of the desired list */
  547. ptmpchan = pscanchanlist;
  548. if (priv->adapter->last_scanned_channel && !puserscanin)
  549. ptmpchan += priv->adapter->last_scanned_channel;
  550. /* Loop through the desired channel list, sending a new firmware scan
  551. * commands for each maxchanperscan channels (or for 1,6,11 individually
  552. * if configured accordingly)
  553. */
  554. while (ptmpchan->channumber) {
  555. tlvidx = 0;
  556. pchantlvout->header.len = 0;
  557. scanband = ptmpchan->radiotype;
  558. pstartchan = ptmpchan;
  559. doneearly = 0;
  560. /* Construct the channel TLV for the scan command. Continue to
  561. * insert channel TLVs until:
  562. * - the tlvidx hits the maximum configured per scan command
  563. * - the next channel to insert is 0 (end of desired channel list)
  564. * - doneearly is set (controlling individual scanning of 1,6,11)
  565. */
  566. while (tlvidx < maxchanperscan && ptmpchan->channumber
  567. && !doneearly && scanned < 2) {
  568. lbs_deb_scan(
  569. "Scan: Chan(%3d), Radio(%d), mode(%d,%d), Dur(%d)\n",
  570. ptmpchan->channumber, ptmpchan->radiotype,
  571. ptmpchan->chanscanmode.passivescan,
  572. ptmpchan->chanscanmode.disablechanfilt,
  573. ptmpchan->maxscantime);
  574. /* Copy the current channel TLV to the command being prepared */
  575. memcpy(pchantlvout->chanscanparam + tlvidx,
  576. ptmpchan, sizeof(pchantlvout->chanscanparam));
  577. /* Increment the TLV header length by the size appended */
  578. pchantlvout->header.len +=
  579. sizeof(pchantlvout->chanscanparam);
  580. /*
  581. * The tlv buffer length is set to the number of bytes of the
  582. * between the channel tlv pointer and the start of the
  583. * tlv buffer. This compensates for any TLVs that were appended
  584. * before the channel list.
  585. */
  586. pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
  587. - pscancfgout->tlvbuffer);
  588. /* Add the size of the channel tlv header and the data length */
  589. pscancfgout->tlvbufferlen +=
  590. (sizeof(pchantlvout->header)
  591. + pchantlvout->header.len);
  592. /* Increment the index to the channel tlv we are constructing */
  593. tlvidx++;
  594. doneearly = 0;
  595. /* Stop the loop if the *current* channel is in the 1,6,11 set
  596. * and we are not filtering on a BSSID or SSID.
  597. */
  598. if (!filteredscan && (ptmpchan->channumber == 1
  599. || ptmpchan->channumber == 6
  600. || ptmpchan->channumber == 11)) {
  601. doneearly = 1;
  602. }
  603. /* Increment the tmp pointer to the next channel to be scanned */
  604. ptmpchan++;
  605. scanned++;
  606. /* Stop the loop if the *next* channel is in the 1,6,11 set.
  607. * This will cause it to be the only channel scanned on the next
  608. * interation
  609. */
  610. if (!filteredscan && (ptmpchan->channumber == 1
  611. || ptmpchan->channumber == 6
  612. || ptmpchan->channumber == 11)) {
  613. doneearly = 1;
  614. }
  615. }
  616. /* Send the scan command to the firmware with the specified cfg */
  617. ret = libertas_prepare_and_send_command(priv, cmd_802_11_scan, 0,
  618. 0, 0, pscancfgout);
  619. if (scanned >= 2 && !full_scan) {
  620. ret = 0;
  621. goto done;
  622. }
  623. scanned = 0;
  624. }
  625. done:
  626. priv->adapter->last_scanned_channel = ptmpchan->channumber;
  627. /* Tell userspace the scan table has been updated */
  628. memset(&wrqu, 0, sizeof(union iwreq_data));
  629. wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
  630. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  631. return ret;
  632. }
  633. static void
  634. clear_selected_scan_list_entries(wlan_adapter * adapter,
  635. const struct wlan_ioctl_user_scan_cfg * scan_cfg)
  636. {
  637. struct bss_descriptor * bss;
  638. struct bss_descriptor * safe;
  639. u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
  640. if (!scan_cfg)
  641. return;
  642. if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
  643. clear_ssid_flag = 1;
  644. if (scan_cfg->clear_bssid
  645. && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
  646. && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
  647. clear_bssid_flag = 1;
  648. }
  649. if (!clear_ssid_flag && !clear_bssid_flag)
  650. return;
  651. mutex_lock(&adapter->lock);
  652. list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
  653. u32 clear = 0;
  654. /* Check for an SSID match */
  655. if ( clear_ssid_flag
  656. && (bss->ssid.ssidlength == scan_cfg->ssid_len)
  657. && !memcmp(bss->ssid.ssid, scan_cfg->ssid, bss->ssid.ssidlength))
  658. clear = 1;
  659. /* Check for a BSSID match */
  660. if ( clear_bssid_flag
  661. && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
  662. clear = 1;
  663. if (clear) {
  664. list_move_tail (&bss->list, &adapter->network_free_list);
  665. clear_bss_descriptor(bss);
  666. }
  667. }
  668. mutex_unlock(&adapter->lock);
  669. }
  670. /**
  671. * @brief Internal function used to start a scan based on an input config
  672. *
  673. * Use the input user scan configuration information when provided in
  674. * order to send the appropriate scan commands to firmware to populate or
  675. * update the internal driver scan table
  676. *
  677. * @param priv A pointer to wlan_private structure
  678. * @param puserscanin Pointer to the input configuration for the requested
  679. * scan.
  680. *
  681. * @return 0 or < 0 if error
  682. */
  683. int wlan_scan_networks(wlan_private * priv,
  684. const struct wlan_ioctl_user_scan_cfg * puserscanin,
  685. int full_scan)
  686. {
  687. wlan_adapter * adapter = priv->adapter;
  688. struct mrvlietypes_chanlistparamset *pchantlvout;
  689. struct chanscanparamset * scan_chan_list = NULL;
  690. struct wlan_scan_cmd_config * scan_cfg = NULL;
  691. u8 filteredscan;
  692. u8 scancurrentchanonly;
  693. int maxchanperscan;
  694. int ret;
  695. lbs_deb_enter(LBS_DEB_ASSOC);
  696. scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
  697. WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
  698. if (scan_chan_list == NULL) {
  699. ret = -ENOMEM;
  700. goto out;
  701. }
  702. scan_cfg = wlan_scan_setup_scan_config(priv,
  703. puserscanin,
  704. &pchantlvout,
  705. scan_chan_list,
  706. &maxchanperscan,
  707. &filteredscan,
  708. &scancurrentchanonly);
  709. if (scan_cfg == NULL) {
  710. ret = -ENOMEM;
  711. goto out;
  712. }
  713. clear_selected_scan_list_entries(adapter, puserscanin);
  714. /* Keep the data path active if we are only scanning our current channel */
  715. if (!scancurrentchanonly) {
  716. netif_stop_queue(priv->dev);
  717. netif_carrier_off(priv->dev);
  718. netif_stop_queue(priv->mesh_dev);
  719. netif_carrier_off(priv->mesh_dev);
  720. }
  721. ret = wlan_scan_channel_list(priv,
  722. maxchanperscan,
  723. filteredscan,
  724. scan_cfg,
  725. pchantlvout,
  726. scan_chan_list,
  727. puserscanin,
  728. full_scan);
  729. /* Process the resulting scan table:
  730. * - Remove any bad ssids
  731. * - Update our current BSS information from scan data
  732. */
  733. wlan_scan_process_results(priv);
  734. if (priv->adapter->connect_status == libertas_connected) {
  735. netif_carrier_on(priv->dev);
  736. netif_wake_queue(priv->dev);
  737. netif_carrier_on(priv->mesh_dev);
  738. netif_wake_queue(priv->mesh_dev);
  739. }
  740. out:
  741. if (scan_cfg)
  742. kfree(scan_cfg);
  743. if (scan_chan_list)
  744. kfree(scan_chan_list);
  745. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  746. return ret;
  747. }
  748. /**
  749. * @brief Inspect the scan response buffer for pointers to expected TLVs
  750. *
  751. * TLVs can be included at the end of the scan response BSS information.
  752. * Parse the data in the buffer for pointers to TLVs that can potentially
  753. * be passed back in the response
  754. *
  755. * @param ptlv Pointer to the start of the TLV buffer to parse
  756. * @param tlvbufsize size of the TLV buffer
  757. * @param ptsftlv Output parameter: Pointer to the TSF TLV if found
  758. *
  759. * @return void
  760. */
  761. static
  762. void wlan_ret_802_11_scan_get_tlv_ptrs(struct mrvlietypes_data * ptlv,
  763. int tlvbufsize,
  764. struct mrvlietypes_tsftimestamp ** ptsftlv)
  765. {
  766. struct mrvlietypes_data *pcurrenttlv;
  767. int tlvbufleft;
  768. u16 tlvtype;
  769. u16 tlvlen;
  770. pcurrenttlv = ptlv;
  771. tlvbufleft = tlvbufsize;
  772. *ptsftlv = NULL;
  773. lbs_deb_scan("SCAN_RESP: tlvbufsize = %d\n", tlvbufsize);
  774. lbs_dbg_hex("SCAN_RESP: TLV Buf", (u8 *) ptlv, tlvbufsize);
  775. while (tlvbufleft >= sizeof(struct mrvlietypesheader)) {
  776. tlvtype = le16_to_cpu(pcurrenttlv->header.type);
  777. tlvlen = le16_to_cpu(pcurrenttlv->header.len);
  778. switch (tlvtype) {
  779. case TLV_TYPE_TSFTIMESTAMP:
  780. *ptsftlv = (struct mrvlietypes_tsftimestamp *) pcurrenttlv;
  781. break;
  782. default:
  783. lbs_deb_scan("SCAN_RESP: Unhandled TLV = %d\n",
  784. tlvtype);
  785. /* Give up, this seems corrupted */
  786. return;
  787. } /* switch */
  788. tlvbufleft -= (sizeof(ptlv->header) + tlvlen);
  789. pcurrenttlv =
  790. (struct mrvlietypes_data *) (pcurrenttlv->Data + tlvlen);
  791. } /* while */
  792. }
  793. /**
  794. * @brief Interpret a BSS scan response returned from the firmware
  795. *
  796. * Parse the various fixed fields and IEs passed back for a a BSS probe
  797. * response or beacon from the scan command. Record information as needed
  798. * in the scan table struct bss_descriptor for that entry.
  799. *
  800. * @param bss Output parameter: Pointer to the BSS Entry
  801. *
  802. * @return 0 or -1
  803. */
  804. static int libertas_process_bss(struct bss_descriptor * bss,
  805. u8 ** pbeaconinfo, int *bytesleft)
  806. {
  807. enum ieeetypes_elementid elemID;
  808. struct ieeetypes_fhparamset *pFH;
  809. struct ieeetypes_dsparamset *pDS;
  810. struct ieeetypes_cfparamset *pCF;
  811. struct ieeetypes_ibssparamset *pibss;
  812. struct ieeetypes_capinfo *pcap;
  813. struct WLAN_802_11_FIXED_IEs fixedie;
  814. u8 *pcurrentptr;
  815. u8 *pRate;
  816. u8 elemlen;
  817. u8 bytestocopy;
  818. u8 ratesize;
  819. u16 beaconsize;
  820. u8 founddatarateie;
  821. int bytesleftforcurrentbeacon;
  822. int ret;
  823. struct IE_WPA *pIe;
  824. const u8 oui01[4] = { 0x00, 0x50, 0xf2, 0x01 };
  825. struct ieeetypes_countryinfoset *pcountryinfo;
  826. lbs_deb_enter(LBS_DEB_ASSOC);
  827. founddatarateie = 0;
  828. ratesize = 0;
  829. beaconsize = 0;
  830. if (*bytesleft >= sizeof(beaconsize)) {
  831. /* Extract & convert beacon size from the command buffer */
  832. memcpy(&beaconsize, *pbeaconinfo, sizeof(beaconsize));
  833. beaconsize = le16_to_cpu(beaconsize);
  834. *bytesleft -= sizeof(beaconsize);
  835. *pbeaconinfo += sizeof(beaconsize);
  836. }
  837. if (beaconsize == 0 || beaconsize > *bytesleft) {
  838. *pbeaconinfo += *bytesleft;
  839. *bytesleft = 0;
  840. return -1;
  841. }
  842. /* Initialize the current working beacon pointer for this BSS iteration */
  843. pcurrentptr = *pbeaconinfo;
  844. /* Advance the return beacon pointer past the current beacon */
  845. *pbeaconinfo += beaconsize;
  846. *bytesleft -= beaconsize;
  847. bytesleftforcurrentbeacon = beaconsize;
  848. memcpy(bss->bssid, pcurrentptr, ETH_ALEN);
  849. lbs_deb_scan("process_bss: AP BSSID " MAC_FMT "\n", MAC_ARG(bss->bssid));
  850. pcurrentptr += ETH_ALEN;
  851. bytesleftforcurrentbeacon -= ETH_ALEN;
  852. if (bytesleftforcurrentbeacon < 12) {
  853. lbs_deb_scan("process_bss: Not enough bytes left\n");
  854. return -1;
  855. }
  856. /*
  857. * next 4 fields are RSSI, time stamp, beacon interval,
  858. * and capability information
  859. */
  860. /* RSSI is 1 byte long */
  861. bss->rssi = le32_to_cpu((long)(*pcurrentptr));
  862. lbs_deb_scan("process_bss: RSSI=%02X\n", *pcurrentptr);
  863. pcurrentptr += 1;
  864. bytesleftforcurrentbeacon -= 1;
  865. /* time stamp is 8 bytes long */
  866. memcpy(fixedie.timestamp, pcurrentptr, 8);
  867. memcpy(bss->timestamp, pcurrentptr, 8);
  868. pcurrentptr += 8;
  869. bytesleftforcurrentbeacon -= 8;
  870. /* beacon interval is 2 bytes long */
  871. memcpy(&fixedie.beaconinterval, pcurrentptr, 2);
  872. bss->beaconperiod = le16_to_cpu(fixedie.beaconinterval);
  873. pcurrentptr += 2;
  874. bytesleftforcurrentbeacon -= 2;
  875. /* capability information is 2 bytes long */
  876. memcpy(&fixedie.capabilities, pcurrentptr, 2);
  877. lbs_deb_scan("process_bss: fixedie.capabilities=0x%X\n",
  878. fixedie.capabilities);
  879. fixedie.capabilities = le16_to_cpu(fixedie.capabilities);
  880. pcap = (struct ieeetypes_capinfo *) & fixedie.capabilities;
  881. memcpy(&bss->cap, pcap, sizeof(struct ieeetypes_capinfo));
  882. pcurrentptr += 2;
  883. bytesleftforcurrentbeacon -= 2;
  884. /* rest of the current buffer are IE's */
  885. lbs_deb_scan("process_bss: IE length for this AP = %d\n",
  886. bytesleftforcurrentbeacon);
  887. lbs_dbg_hex("process_bss: IE info", (u8 *) pcurrentptr,
  888. bytesleftforcurrentbeacon);
  889. if (pcap->privacy) {
  890. lbs_deb_scan("process_bss: AP WEP enabled\n");
  891. bss->privacy = wlan802_11privfilter8021xWEP;
  892. } else {
  893. bss->privacy = wlan802_11privfilteracceptall;
  894. }
  895. if (pcap->ibss == 1) {
  896. bss->mode = IW_MODE_ADHOC;
  897. } else {
  898. bss->mode = IW_MODE_INFRA;
  899. }
  900. /* process variable IE */
  901. while (bytesleftforcurrentbeacon >= 2) {
  902. elemID = (enum ieeetypes_elementid) (*((u8 *) pcurrentptr));
  903. elemlen = *((u8 *) pcurrentptr + 1);
  904. if (bytesleftforcurrentbeacon < elemlen) {
  905. lbs_deb_scan("process_bss: error in processing IE, "
  906. "bytes left < IE length\n");
  907. bytesleftforcurrentbeacon = 0;
  908. continue;
  909. }
  910. switch (elemID) {
  911. case SSID:
  912. bss->ssid.ssidlength = elemlen;
  913. memcpy(bss->ssid.ssid, (pcurrentptr + 2), elemlen);
  914. lbs_deb_scan("ssid '%s'\n", bss->ssid.ssid);
  915. break;
  916. case SUPPORTED_RATES:
  917. memcpy(bss->datarates, (pcurrentptr + 2), elemlen);
  918. memmove(bss->libertas_supported_rates, (pcurrentptr + 2),
  919. elemlen);
  920. ratesize = elemlen;
  921. founddatarateie = 1;
  922. break;
  923. case EXTRA_IE:
  924. lbs_deb_scan("process_bss: EXTRA_IE Found!\n");
  925. break;
  926. case FH_PARAM_SET:
  927. pFH = (struct ieeetypes_fhparamset *) pcurrentptr;
  928. memmove(&bss->phyparamset.fhparamset, pFH,
  929. sizeof(struct ieeetypes_fhparamset));
  930. bss->phyparamset.fhparamset.dwelltime
  931. = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
  932. break;
  933. case DS_PARAM_SET:
  934. pDS = (struct ieeetypes_dsparamset *) pcurrentptr;
  935. bss->channel = pDS->currentchan;
  936. memcpy(&bss->phyparamset.dsparamset, pDS,
  937. sizeof(struct ieeetypes_dsparamset));
  938. break;
  939. case CF_PARAM_SET:
  940. pCF = (struct ieeetypes_cfparamset *) pcurrentptr;
  941. memcpy(&bss->ssparamset.cfparamset, pCF,
  942. sizeof(struct ieeetypes_cfparamset));
  943. break;
  944. case IBSS_PARAM_SET:
  945. pibss = (struct ieeetypes_ibssparamset *) pcurrentptr;
  946. bss->atimwindow = le32_to_cpu(pibss->atimwindow);
  947. memmove(&bss->ssparamset.ibssparamset, pibss,
  948. sizeof(struct ieeetypes_ibssparamset));
  949. bss->ssparamset.ibssparamset.atimwindow
  950. = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
  951. break;
  952. /* Handle Country Info IE */
  953. case COUNTRY_INFO:
  954. pcountryinfo = (struct ieeetypes_countryinfoset *) pcurrentptr;
  955. if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
  956. || pcountryinfo->len > 254) {
  957. lbs_deb_scan("process_bss: 11D- Err "
  958. "CountryInfo len =%d min=%zd max=254\n",
  959. pcountryinfo->len,
  960. sizeof(pcountryinfo->countrycode));
  961. ret = -1;
  962. goto done;
  963. }
  964. memcpy(&bss->countryinfo,
  965. pcountryinfo, pcountryinfo->len + 2);
  966. lbs_dbg_hex("process_bss: 11D- CountryInfo:",
  967. (u8 *) pcountryinfo,
  968. (u32) (pcountryinfo->len + 2));
  969. break;
  970. case EXTENDED_SUPPORTED_RATES:
  971. /*
  972. * only process extended supported rate
  973. * if data rate is already found.
  974. * data rate IE should come before
  975. * extended supported rate IE
  976. */
  977. if (founddatarateie) {
  978. if ((elemlen + ratesize) > WLAN_SUPPORTED_RATES) {
  979. bytestocopy =
  980. (WLAN_SUPPORTED_RATES - ratesize);
  981. } else {
  982. bytestocopy = elemlen;
  983. }
  984. pRate = (u8 *) bss->datarates;
  985. pRate += ratesize;
  986. memmove(pRate, (pcurrentptr + 2), bytestocopy);
  987. pRate = (u8 *) bss->libertas_supported_rates;
  988. pRate += ratesize;
  989. memmove(pRate, (pcurrentptr + 2), bytestocopy);
  990. }
  991. break;
  992. case VENDOR_SPECIFIC_221:
  993. #define IE_ID_LEN_FIELDS_BYTES 2
  994. pIe = (struct IE_WPA *)pcurrentptr;
  995. if (memcmp(pIe->oui, oui01, sizeof(oui01)))
  996. break;
  997. bss->wpa_ie_len = min(elemlen + IE_ID_LEN_FIELDS_BYTES,
  998. MAX_WPA_IE_LEN);
  999. memcpy(bss->wpa_ie, pcurrentptr, bss->wpa_ie_len);
  1000. lbs_dbg_hex("process_bss: WPA IE", bss->wpa_ie, elemlen);
  1001. break;
  1002. case WPA2_IE:
  1003. pIe = (struct IE_WPA *)pcurrentptr;
  1004. bss->rsn_ie_len = min(elemlen + IE_ID_LEN_FIELDS_BYTES,
  1005. MAX_WPA_IE_LEN);
  1006. memcpy(bss->rsn_ie, pcurrentptr, bss->rsn_ie_len);
  1007. lbs_dbg_hex("process_bss: RSN_IE", bss->rsn_ie, elemlen);
  1008. break;
  1009. case TIM:
  1010. break;
  1011. case CHALLENGE_TEXT:
  1012. break;
  1013. }
  1014. pcurrentptr += elemlen + 2;
  1015. /* need to account for IE ID and IE len */
  1016. bytesleftforcurrentbeacon -= (elemlen + 2);
  1017. } /* while (bytesleftforcurrentbeacon > 2) */
  1018. /* Timestamp */
  1019. bss->last_scanned = jiffies;
  1020. ret = 0;
  1021. done:
  1022. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1023. return ret;
  1024. }
  1025. /**
  1026. * @brief Compare two SSIDs
  1027. *
  1028. * @param ssid1 A pointer to ssid to compare
  1029. * @param ssid2 A pointer to ssid to compare
  1030. *
  1031. * @return 0--ssid is same, otherwise is different
  1032. */
  1033. int libertas_SSID_cmp(struct WLAN_802_11_SSID *ssid1, struct WLAN_802_11_SSID *ssid2)
  1034. {
  1035. if (!ssid1 || !ssid2)
  1036. return -1;
  1037. if (ssid1->ssidlength != ssid2->ssidlength)
  1038. return -1;
  1039. return memcmp(ssid1->ssid, ssid2->ssid, ssid1->ssidlength);
  1040. }
  1041. /**
  1042. * @brief This function finds a specific compatible BSSID in the scan list
  1043. *
  1044. * @param adapter A pointer to wlan_adapter
  1045. * @param bssid BSSID to find in the scan list
  1046. * @param mode Network mode: Infrastructure or IBSS
  1047. *
  1048. * @return index in BSSID list, or error return code (< 0)
  1049. */
  1050. struct bss_descriptor * libertas_find_BSSID_in_list(wlan_adapter * adapter,
  1051. u8 * bssid, u8 mode)
  1052. {
  1053. struct bss_descriptor * iter_bss;
  1054. struct bss_descriptor * found_bss = NULL;
  1055. if (!bssid)
  1056. return NULL;
  1057. lbs_dbg_hex("libertas_find_BSSID_in_list: looking for ",
  1058. bssid, ETH_ALEN);
  1059. /* Look through the scan table for a compatible match. The loop will
  1060. * continue past a matched bssid that is not compatible in case there
  1061. * is an AP with multiple SSIDs assigned to the same BSSID
  1062. */
  1063. mutex_lock(&adapter->lock);
  1064. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1065. if (compare_ether_addr(iter_bss->bssid, bssid))
  1066. continue; /* bssid doesn't match */
  1067. switch (mode) {
  1068. case IW_MODE_INFRA:
  1069. case IW_MODE_ADHOC:
  1070. if (!is_network_compatible(adapter, iter_bss, mode))
  1071. break;
  1072. found_bss = iter_bss;
  1073. break;
  1074. default:
  1075. found_bss = iter_bss;
  1076. break;
  1077. }
  1078. }
  1079. mutex_unlock(&adapter->lock);
  1080. return found_bss;
  1081. }
  1082. /**
  1083. * @brief This function finds ssid in ssid list.
  1084. *
  1085. * @param adapter A pointer to wlan_adapter
  1086. * @param ssid SSID to find in the list
  1087. * @param bssid BSSID to qualify the SSID selection (if provided)
  1088. * @param mode Network mode: Infrastructure or IBSS
  1089. *
  1090. * @return index in BSSID list
  1091. */
  1092. struct bss_descriptor * libertas_find_SSID_in_list(wlan_adapter * adapter,
  1093. struct WLAN_802_11_SSID *ssid, u8 * bssid, u8 mode,
  1094. int channel)
  1095. {
  1096. u8 bestrssi = 0;
  1097. struct bss_descriptor * iter_bss = NULL;
  1098. struct bss_descriptor * found_bss = NULL;
  1099. struct bss_descriptor * tmp_oldest = NULL;
  1100. mutex_lock(&adapter->lock);
  1101. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1102. if ( !tmp_oldest
  1103. || (iter_bss->last_scanned < tmp_oldest->last_scanned))
  1104. tmp_oldest = iter_bss;
  1105. if (libertas_SSID_cmp(&iter_bss->ssid, ssid) != 0)
  1106. continue; /* ssid doesn't match */
  1107. if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
  1108. continue; /* bssid doesn't match */
  1109. if ((channel > 0) && (iter_bss->channel != channel))
  1110. continue; /* channel doesn't match */
  1111. switch (mode) {
  1112. case IW_MODE_INFRA:
  1113. case IW_MODE_ADHOC:
  1114. if (!is_network_compatible(adapter, iter_bss, mode))
  1115. break;
  1116. if (bssid) {
  1117. /* Found requested BSSID */
  1118. found_bss = iter_bss;
  1119. goto out;
  1120. }
  1121. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  1122. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1123. found_bss = iter_bss;
  1124. }
  1125. break;
  1126. case IW_MODE_AUTO:
  1127. default:
  1128. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  1129. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1130. found_bss = iter_bss;
  1131. }
  1132. break;
  1133. }
  1134. }
  1135. out:
  1136. mutex_unlock(&adapter->lock);
  1137. return found_bss;
  1138. }
  1139. /**
  1140. * @brief This function finds the best SSID in the Scan List
  1141. *
  1142. * Search the scan table for the best SSID that also matches the current
  1143. * adapter network preference (infrastructure or adhoc)
  1144. *
  1145. * @param adapter A pointer to wlan_adapter
  1146. *
  1147. * @return index in BSSID list
  1148. */
  1149. struct bss_descriptor * libertas_find_best_SSID_in_list(wlan_adapter * adapter,
  1150. u8 mode)
  1151. {
  1152. u8 bestrssi = 0;
  1153. struct bss_descriptor * iter_bss;
  1154. struct bss_descriptor * best_bss = NULL;
  1155. mutex_lock(&adapter->lock);
  1156. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1157. switch (mode) {
  1158. case IW_MODE_INFRA:
  1159. case IW_MODE_ADHOC:
  1160. if (!is_network_compatible(adapter, iter_bss, mode))
  1161. break;
  1162. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  1163. break;
  1164. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1165. best_bss = iter_bss;
  1166. break;
  1167. case IW_MODE_AUTO:
  1168. default:
  1169. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  1170. break;
  1171. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1172. best_bss = iter_bss;
  1173. break;
  1174. }
  1175. }
  1176. mutex_unlock(&adapter->lock);
  1177. return best_bss;
  1178. }
  1179. /**
  1180. * @brief Find the AP with specific ssid in the scan list
  1181. *
  1182. * @param priv A pointer to wlan_private structure
  1183. * @param pSSID A pointer to AP's ssid
  1184. *
  1185. * @return 0--success, otherwise--fail
  1186. */
  1187. int libertas_find_best_network_SSID(wlan_private * priv,
  1188. struct WLAN_802_11_SSID *ssid,
  1189. u8 preferred_mode, u8 *out_mode)
  1190. {
  1191. wlan_adapter *adapter = priv->adapter;
  1192. int ret = -1;
  1193. struct bss_descriptor * found;
  1194. lbs_deb_enter(LBS_DEB_ASSOC);
  1195. memset(ssid, 0, sizeof(struct WLAN_802_11_SSID));
  1196. wlan_scan_networks(priv, NULL, 1);
  1197. if (adapter->surpriseremoved)
  1198. return -1;
  1199. wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
  1200. found = libertas_find_best_SSID_in_list(adapter, preferred_mode);
  1201. if (found && (found->ssid.ssidlength > 0)) {
  1202. memcpy(ssid, &found->ssid, sizeof(struct WLAN_802_11_SSID));
  1203. *out_mode = found->mode;
  1204. ret = 0;
  1205. }
  1206. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1207. return ret;
  1208. }
  1209. /**
  1210. * @brief Scan Network
  1211. *
  1212. * @param dev A pointer to net_device structure
  1213. * @param info A pointer to iw_request_info structure
  1214. * @param vwrq A pointer to iw_param structure
  1215. * @param extra A pointer to extra data buf
  1216. *
  1217. * @return 0 --success, otherwise fail
  1218. */
  1219. int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
  1220. struct iw_param *vwrq, char *extra)
  1221. {
  1222. wlan_private *priv = dev->priv;
  1223. wlan_adapter *adapter = priv->adapter;
  1224. lbs_deb_enter(LBS_DEB_SCAN);
  1225. wlan_scan_networks(priv, NULL, 0);
  1226. if (adapter->surpriseremoved)
  1227. return -1;
  1228. lbs_deb_leave(LBS_DEB_SCAN);
  1229. return 0;
  1230. }
  1231. /**
  1232. * @brief Send a scan command for all available channels filtered on a spec
  1233. *
  1234. * @param priv A pointer to wlan_private structure
  1235. * @param prequestedssid A pointer to AP's ssid
  1236. * @param keeppreviousscan Flag used to save/clear scan table before scan
  1237. *
  1238. * @return 0-success, otherwise fail
  1239. */
  1240. int libertas_send_specific_SSID_scan(wlan_private * priv,
  1241. struct WLAN_802_11_SSID *prequestedssid,
  1242. u8 clear_ssid)
  1243. {
  1244. wlan_adapter *adapter = priv->adapter;
  1245. struct wlan_ioctl_user_scan_cfg scancfg;
  1246. int ret = 0;
  1247. lbs_deb_enter(LBS_DEB_ASSOC);
  1248. if (prequestedssid == NULL)
  1249. goto out;
  1250. memset(&scancfg, 0x00, sizeof(scancfg));
  1251. memcpy(scancfg.ssid, prequestedssid->ssid, prequestedssid->ssidlength);
  1252. scancfg.ssid_len = prequestedssid->ssidlength;
  1253. scancfg.clear_ssid = clear_ssid;
  1254. wlan_scan_networks(priv, &scancfg, 1);
  1255. if (adapter->surpriseremoved)
  1256. return -1;
  1257. wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
  1258. out:
  1259. lbs_deb_leave(LBS_DEB_ASSOC);
  1260. return ret;
  1261. }
  1262. /**
  1263. * @brief scan an AP with specific BSSID
  1264. *
  1265. * @param priv A pointer to wlan_private structure
  1266. * @param bssid A pointer to AP's bssid
  1267. * @param keeppreviousscan Flag used to save/clear scan table before scan
  1268. *
  1269. * @return 0-success, otherwise fail
  1270. */
  1271. int libertas_send_specific_BSSID_scan(wlan_private * priv, u8 * bssid, u8 clear_bssid)
  1272. {
  1273. struct wlan_ioctl_user_scan_cfg scancfg;
  1274. lbs_deb_enter(LBS_DEB_ASSOC);
  1275. if (bssid == NULL)
  1276. goto out;
  1277. memset(&scancfg, 0x00, sizeof(scancfg));
  1278. memcpy(scancfg.bssid, bssid, ETH_ALEN);
  1279. scancfg.clear_bssid = clear_bssid;
  1280. wlan_scan_networks(priv, &scancfg, 1);
  1281. if (priv->adapter->surpriseremoved)
  1282. return -1;
  1283. wait_event_interruptible(priv->adapter->cmd_pending,
  1284. !priv->adapter->nr_cmd_pending);
  1285. out:
  1286. lbs_deb_leave(LBS_DEB_ASSOC);
  1287. return 0;
  1288. }
  1289. static inline char *libertas_translate_scan(wlan_private *priv,
  1290. char *start, char *stop,
  1291. struct bss_descriptor *bss)
  1292. {
  1293. wlan_adapter *adapter = priv->adapter;
  1294. struct chan_freq_power *cfp;
  1295. char *current_val; /* For rates */
  1296. struct iw_event iwe; /* Temporary buffer */
  1297. int j;
  1298. #define PERFECT_RSSI ((u8)50)
  1299. #define WORST_RSSI ((u8)0)
  1300. #define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
  1301. u8 rssi;
  1302. cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
  1303. if (!cfp) {
  1304. lbs_deb_scan("Invalid channel number %d\n", bss->channel);
  1305. return NULL;
  1306. }
  1307. /* First entry *MUST* be the AP BSSID */
  1308. iwe.cmd = SIOCGIWAP;
  1309. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1310. memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
  1311. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
  1312. /* SSID */
  1313. iwe.cmd = SIOCGIWESSID;
  1314. iwe.u.data.flags = 1;
  1315. iwe.u.data.length = min(bss->ssid.ssidlength, (u32) IW_ESSID_MAX_SIZE);
  1316. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid.ssid);
  1317. /* Mode */
  1318. iwe.cmd = SIOCGIWMODE;
  1319. iwe.u.mode = bss->mode;
  1320. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
  1321. /* Frequency */
  1322. iwe.cmd = SIOCGIWFREQ;
  1323. iwe.u.freq.m = (long)cfp->freq * 100000;
  1324. iwe.u.freq.e = 1;
  1325. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
  1326. /* Add quality statistics */
  1327. iwe.cmd = IWEVQUAL;
  1328. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
  1329. iwe.u.qual.level = SCAN_RSSI(bss->rssi);
  1330. rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1331. iwe.u.qual.qual =
  1332. (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
  1333. (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
  1334. (RSSI_DIFF * RSSI_DIFF);
  1335. if (iwe.u.qual.qual > 100)
  1336. iwe.u.qual.qual = 100;
  1337. if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
  1338. iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1339. } else {
  1340. iwe.u.qual.noise =
  1341. CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
  1342. }
  1343. /* Locally created ad-hoc BSSs won't have beacons if this is the
  1344. * only station in the adhoc network; so get signal strength
  1345. * from receive statistics.
  1346. */
  1347. if ((adapter->mode == IW_MODE_ADHOC)
  1348. && adapter->adhoccreate
  1349. && !libertas_SSID_cmp(&adapter->curbssparams.ssid, &bss->ssid)) {
  1350. int snr, nf;
  1351. snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  1352. nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  1353. iwe.u.qual.level = CAL_RSSI(snr, nf);
  1354. }
  1355. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
  1356. /* Add encryption capability */
  1357. iwe.cmd = SIOCGIWENCODE;
  1358. if (bss->privacy) {
  1359. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1360. } else {
  1361. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1362. }
  1363. iwe.u.data.length = 0;
  1364. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid.ssid);
  1365. current_val = start + IW_EV_LCP_LEN;
  1366. iwe.cmd = SIOCGIWRATE;
  1367. iwe.u.bitrate.fixed = 0;
  1368. iwe.u.bitrate.disabled = 0;
  1369. iwe.u.bitrate.value = 0;
  1370. for (j = 0; j < sizeof(bss->libertas_supported_rates); j++) {
  1371. u8 rate = bss->libertas_supported_rates[j];
  1372. if (rate == 0)
  1373. break; /* no more rates */
  1374. /* Bit rate given in 500 kb/s units (+ 0x80) */
  1375. iwe.u.bitrate.value = (rate & 0x7f) * 500000;
  1376. current_val = iwe_stream_add_value(start, current_val,
  1377. stop, &iwe, IW_EV_PARAM_LEN);
  1378. }
  1379. if ((bss->mode == IW_MODE_ADHOC)
  1380. && !libertas_SSID_cmp(&adapter->curbssparams.ssid, &bss->ssid)
  1381. && adapter->adhoccreate) {
  1382. iwe.u.bitrate.value = 22 * 500000;
  1383. current_val = iwe_stream_add_value(start, current_val,
  1384. stop, &iwe, IW_EV_PARAM_LEN);
  1385. }
  1386. /* Check if we added any event */
  1387. if((current_val - start) > IW_EV_LCP_LEN)
  1388. start = current_val;
  1389. memset(&iwe, 0, sizeof(iwe));
  1390. if (bss->wpa_ie_len) {
  1391. char buf[MAX_WPA_IE_LEN];
  1392. memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
  1393. iwe.cmd = IWEVGENIE;
  1394. iwe.u.data.length = bss->wpa_ie_len;
  1395. start = iwe_stream_add_point(start, stop, &iwe, buf);
  1396. }
  1397. memset(&iwe, 0, sizeof(iwe));
  1398. if (bss->rsn_ie_len) {
  1399. char buf[MAX_WPA_IE_LEN];
  1400. memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
  1401. iwe.cmd = IWEVGENIE;
  1402. iwe.u.data.length = bss->rsn_ie_len;
  1403. start = iwe_stream_add_point(start, stop, &iwe, buf);
  1404. }
  1405. return start;
  1406. }
  1407. /**
  1408. * @brief Retrieve the scan table entries via wireless tools IOCTL call
  1409. *
  1410. * @param dev A pointer to net_device structure
  1411. * @param info A pointer to iw_request_info structure
  1412. * @param dwrq A pointer to iw_point structure
  1413. * @param extra A pointer to extra data buf
  1414. *
  1415. * @return 0 --success, otherwise fail
  1416. */
  1417. int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
  1418. struct iw_point *dwrq, char *extra)
  1419. {
  1420. #define SCAN_ITEM_SIZE 128
  1421. wlan_private *priv = dev->priv;
  1422. wlan_adapter *adapter = priv->adapter;
  1423. int err = 0;
  1424. char *ev = extra;
  1425. char *stop = ev + dwrq->length;
  1426. struct bss_descriptor * iter_bss;
  1427. struct bss_descriptor * safe;
  1428. lbs_deb_enter(LBS_DEB_ASSOC);
  1429. /* If we've got an uncompleted scan, schedule the next part */
  1430. if (!adapter->nr_cmd_pending && adapter->last_scanned_channel)
  1431. wlan_scan_networks(priv, NULL, 0);
  1432. /* Update RSSI if current BSS is a locally created ad-hoc BSS */
  1433. if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
  1434. libertas_prepare_and_send_command(priv, cmd_802_11_rssi, 0,
  1435. cmd_option_waitforrsp, 0, NULL);
  1436. }
  1437. mutex_lock(&adapter->lock);
  1438. list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
  1439. char * next_ev;
  1440. unsigned long stale_time;
  1441. if (stop - ev < SCAN_ITEM_SIZE) {
  1442. err = -E2BIG;
  1443. break;
  1444. }
  1445. /* Prune old an old scan result */
  1446. stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1447. if (time_after(jiffies, stale_time)) {
  1448. list_move_tail (&iter_bss->list,
  1449. &adapter->network_free_list);
  1450. clear_bss_descriptor(iter_bss);
  1451. continue;
  1452. }
  1453. /* Translate to WE format this entry */
  1454. next_ev = libertas_translate_scan(priv, ev, stop, iter_bss);
  1455. if (next_ev == NULL)
  1456. continue;
  1457. ev = next_ev;
  1458. }
  1459. mutex_unlock(&adapter->lock);
  1460. dwrq->length = (ev - extra);
  1461. dwrq->flags = 0;
  1462. lbs_deb_leave(LBS_DEB_ASSOC);
  1463. return err;
  1464. }
  1465. /**
  1466. * @brief Prepare a scan command to be sent to the firmware
  1467. *
  1468. * Use the wlan_scan_cmd_config sent to the command processing module in
  1469. * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
  1470. * struct to send to firmware.
  1471. *
  1472. * The fixed fields specifying the BSS type and BSSID filters as well as a
  1473. * variable number/length of TLVs are sent in the command to firmware.
  1474. *
  1475. * @param priv A pointer to wlan_private structure
  1476. * @param cmd A pointer to cmd_ds_command structure to be sent to
  1477. * firmware with the cmd_DS_801_11_SCAN structure
  1478. * @param pdata_buf Void pointer cast of a wlan_scan_cmd_config struct used
  1479. * to set the fields/TLVs for the command sent to firmware
  1480. *
  1481. * @return 0 or -1
  1482. *
  1483. * @sa wlan_scan_create_channel_list
  1484. */
  1485. int libertas_cmd_80211_scan(wlan_private * priv,
  1486. struct cmd_ds_command *cmd, void *pdata_buf)
  1487. {
  1488. struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
  1489. struct wlan_scan_cmd_config *pscancfg;
  1490. lbs_deb_enter(LBS_DEB_ASSOC);
  1491. pscancfg = pdata_buf;
  1492. /* Set fixed field variables in scan command */
  1493. pscan->bsstype = pscancfg->bsstype;
  1494. memcpy(pscan->BSSID, pscancfg->bssid, sizeof(pscan->BSSID));
  1495. memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
  1496. cmd->command = cpu_to_le16(cmd_802_11_scan);
  1497. /* size is equal to the sizeof(fixed portions) + the TLV len + header */
  1498. cmd->size = cpu_to_le16(sizeof(pscan->bsstype)
  1499. + sizeof(pscan->BSSID)
  1500. + pscancfg->tlvbufferlen + S_DS_GEN);
  1501. lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
  1502. cmd->command, cmd->size, cmd->seqnum);
  1503. lbs_deb_leave(LBS_DEB_ASSOC);
  1504. return 0;
  1505. }
  1506. static inline int is_same_network(struct bss_descriptor *src,
  1507. struct bss_descriptor *dst)
  1508. {
  1509. /* A network is only a duplicate if the channel, BSSID, and ESSID
  1510. * all match. We treat all <hidden> with the same BSSID and channel
  1511. * as one network */
  1512. return ((src->ssid.ssidlength == dst->ssid.ssidlength) &&
  1513. (src->channel == dst->channel) &&
  1514. !compare_ether_addr(src->bssid, dst->bssid) &&
  1515. !memcmp(src->ssid.ssid, dst->ssid.ssid, src->ssid.ssidlength));
  1516. }
  1517. /**
  1518. * @brief This function handles the command response of scan
  1519. *
  1520. * The response buffer for the scan command has the following
  1521. * memory layout:
  1522. *
  1523. * .-----------------------------------------------------------.
  1524. * | header (4 * sizeof(u16)): Standard command response hdr |
  1525. * .-----------------------------------------------------------.
  1526. * | bufsize (u16) : sizeof the BSS Description data |
  1527. * .-----------------------------------------------------------.
  1528. * | NumOfSet (u8) : Number of BSS Descs returned |
  1529. * .-----------------------------------------------------------.
  1530. * | BSSDescription data (variable, size given in bufsize) |
  1531. * .-----------------------------------------------------------.
  1532. * | TLV data (variable, size calculated using header->size, |
  1533. * | bufsize and sizeof the fixed fields above) |
  1534. * .-----------------------------------------------------------.
  1535. *
  1536. * @param priv A pointer to wlan_private structure
  1537. * @param resp A pointer to cmd_ds_command
  1538. *
  1539. * @return 0 or -1
  1540. */
  1541. int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
  1542. {
  1543. wlan_adapter *adapter = priv->adapter;
  1544. struct cmd_ds_802_11_scan_rsp *pscan;
  1545. struct mrvlietypes_data *ptlv;
  1546. struct mrvlietypes_tsftimestamp *ptsftlv;
  1547. struct bss_descriptor * iter_bss;
  1548. struct bss_descriptor * safe;
  1549. u8 *pbssinfo;
  1550. u16 scanrespsize;
  1551. int bytesleft;
  1552. int idx;
  1553. int tlvbufsize;
  1554. u64 tsfval;
  1555. int ret;
  1556. lbs_deb_enter(LBS_DEB_ASSOC);
  1557. /* Prune old entries from scan table */
  1558. list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
  1559. unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1560. if (time_before(jiffies, stale_time))
  1561. continue;
  1562. list_move_tail (&iter_bss->list, &adapter->network_free_list);
  1563. clear_bss_descriptor(iter_bss);
  1564. }
  1565. pscan = &resp->params.scanresp;
  1566. if (pscan->nr_sets > MAX_NETWORK_COUNT) {
  1567. lbs_deb_scan(
  1568. "SCAN_RESP: too many scan results (%d, max %d)!!\n",
  1569. pscan->nr_sets, MAX_NETWORK_COUNT);
  1570. ret = -1;
  1571. goto done;
  1572. }
  1573. bytesleft = le16_to_cpu(pscan->bssdescriptsize);
  1574. lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
  1575. scanrespsize = le16_to_cpu(resp->size);
  1576. lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
  1577. pscan->nr_sets);
  1578. pbssinfo = pscan->bssdesc_and_tlvbuffer;
  1579. /* The size of the TLV buffer is equal to the entire command response
  1580. * size (scanrespsize) minus the fixed fields (sizeof()'s), the
  1581. * BSS Descriptions (bssdescriptsize as bytesLef) and the command
  1582. * response header (S_DS_GEN)
  1583. */
  1584. tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
  1585. + sizeof(pscan->nr_sets)
  1586. + S_DS_GEN);
  1587. ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);
  1588. /* Search the TLV buffer space in the scan response for any valid TLVs */
  1589. wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);
  1590. /*
  1591. * Process each scan response returned (pscan->nr_sets). Save
  1592. * the information in the newbssentry and then insert into the
  1593. * driver scan table either as an update to an existing entry
  1594. * or as an addition at the end of the table
  1595. */
  1596. for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
  1597. struct bss_descriptor new;
  1598. struct bss_descriptor * found = NULL;
  1599. struct bss_descriptor * iter_bss = NULL;
  1600. struct bss_descriptor * oldest = NULL;
  1601. /* Process the data fields and IEs returned for this BSS */
  1602. memset(&new, 0, sizeof (struct bss_descriptor));
  1603. if (libertas_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
  1604. /* error parsing the scan response, skipped */
  1605. lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
  1606. continue;
  1607. }
  1608. /* Try to find this bss in the scan table */
  1609. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1610. if (is_same_network(iter_bss, &new)) {
  1611. found = iter_bss;
  1612. break;
  1613. }
  1614. if ((oldest == NULL) ||
  1615. (iter_bss->last_scanned < oldest->last_scanned))
  1616. oldest = iter_bss;
  1617. }
  1618. if (found) {
  1619. /* found, clear it */
  1620. clear_bss_descriptor(found);
  1621. } else if (!list_empty(&adapter->network_free_list)) {
  1622. /* Pull one from the free list */
  1623. found = list_entry(adapter->network_free_list.next,
  1624. struct bss_descriptor, list);
  1625. list_move_tail(&found->list, &adapter->network_list);
  1626. } else if (oldest) {
  1627. /* If there are no more slots, expire the oldest */
  1628. found = oldest;
  1629. clear_bss_descriptor(found);
  1630. list_move_tail(&found->list, &adapter->network_list);
  1631. } else {
  1632. continue;
  1633. }
  1634. lbs_deb_scan("SCAN_RESP: BSSID = " MAC_FMT "\n",
  1635. new.bssid[0], new.bssid[1], new.bssid[2],
  1636. new.bssid[3], new.bssid[4], new.bssid[5]);
  1637. /*
  1638. * If the TSF TLV was appended to the scan results, save the
  1639. * this entries TSF value in the networktsf field. The
  1640. * networktsf is the firmware's TSF value at the time the
  1641. * beacon or probe response was received.
  1642. */
  1643. if (ptsftlv) {
  1644. memcpy(&tsfval, &ptsftlv->tsftable[idx], sizeof(tsfval));
  1645. tsfval = le64_to_cpu(tsfval);
  1646. memcpy(&new.networktsf, &tsfval, sizeof(new.networktsf));
  1647. }
  1648. /* Copy the locally created newbssentry to the scan table */
  1649. memcpy(found, &new, offsetof(struct bss_descriptor, list));
  1650. }
  1651. ret = 0;
  1652. done:
  1653. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1654. return ret;
  1655. }