scan.c 57 KB

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