scan.c 57 KB

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