scan.c 60 KB

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