scan.c 57 KB

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