scan.c 58 KB

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