scan.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041
  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->dev, 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->dev);
  710. netif_carrier_off(priv->dev);
  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->dev);
  729. netif_wake_queue(priv->dev);
  730. netif_carrier_on(priv->mesh_dev);
  731. netif_wake_queue(priv->mesh_dev);
  732. }
  733. out:
  734. if (scan_cfg)
  735. kfree(scan_cfg);
  736. if (scan_chan_list)
  737. kfree(scan_chan_list);
  738. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  739. return ret;
  740. }
  741. /**
  742. * @brief Inspect the scan response buffer for pointers to expected TLVs
  743. *
  744. * TLVs can be included at the end of the scan response BSS information.
  745. * Parse the data in the buffer for pointers to TLVs that can potentially
  746. * be passed back in the response
  747. *
  748. * @param ptlv Pointer to the start of the TLV buffer to parse
  749. * @param tlvbufsize size of the TLV buffer
  750. * @param ptsftlv Output parameter: Pointer to the TSF TLV if found
  751. *
  752. * @return void
  753. */
  754. static
  755. void wlan_ret_802_11_scan_get_tlv_ptrs(struct mrvlietypes_data * ptlv,
  756. int tlvbufsize,
  757. struct mrvlietypes_tsftimestamp ** ptsftlv)
  758. {
  759. struct mrvlietypes_data *pcurrenttlv;
  760. int tlvbufleft;
  761. u16 tlvtype;
  762. u16 tlvlen;
  763. pcurrenttlv = ptlv;
  764. tlvbufleft = tlvbufsize;
  765. *ptsftlv = NULL;
  766. lbs_deb_scan("SCAN_RESP: tlvbufsize = %d\n", tlvbufsize);
  767. lbs_dbg_hex("SCAN_RESP: TLV Buf", (u8 *) ptlv, tlvbufsize);
  768. while (tlvbufleft >= sizeof(struct mrvlietypesheader)) {
  769. tlvtype = le16_to_cpu(pcurrenttlv->header.type);
  770. tlvlen = le16_to_cpu(pcurrenttlv->header.len);
  771. switch (tlvtype) {
  772. case TLV_TYPE_TSFTIMESTAMP:
  773. *ptsftlv = (struct mrvlietypes_tsftimestamp *) pcurrenttlv;
  774. break;
  775. default:
  776. lbs_deb_scan("SCAN_RESP: Unhandled TLV = %d\n",
  777. tlvtype);
  778. /* Give up, this seems corrupted */
  779. return;
  780. } /* switch */
  781. tlvbufleft -= (sizeof(ptlv->header) + tlvlen);
  782. pcurrenttlv =
  783. (struct mrvlietypes_data *) (pcurrenttlv->Data + tlvlen);
  784. } /* while */
  785. }
  786. /**
  787. * @brief Interpret a BSS scan response returned from the firmware
  788. *
  789. * Parse the various fixed fields and IEs passed back for a a BSS probe
  790. * response or beacon from the scan command. Record information as needed
  791. * in the scan table struct bss_descriptor for that entry.
  792. *
  793. * @param pBSSIDEntry Output parameter: Pointer to the BSS Entry
  794. *
  795. * @return 0 or -1
  796. */
  797. static int InterpretBSSDescriptionWithIE(struct bss_descriptor * pBSSEntry,
  798. u8 ** pbeaconinfo, int *bytesleft)
  799. {
  800. enum ieeetypes_elementid elemID;
  801. struct ieeetypes_fhparamset *pFH;
  802. struct ieeetypes_dsparamset *pDS;
  803. struct ieeetypes_cfparamset *pCF;
  804. struct ieeetypes_ibssparamset *pibss;
  805. struct ieeetypes_capinfo *pcap;
  806. struct WLAN_802_11_FIXED_IEs fixedie;
  807. u8 *pcurrentptr;
  808. u8 *pRate;
  809. u8 elemlen;
  810. u8 bytestocopy;
  811. u8 ratesize;
  812. u16 beaconsize;
  813. u8 founddatarateie;
  814. int bytesleftforcurrentbeacon;
  815. int ret;
  816. struct IE_WPA *pIe;
  817. const u8 oui01[4] = { 0x00, 0x50, 0xf2, 0x01 };
  818. struct ieeetypes_countryinfoset *pcountryinfo;
  819. lbs_deb_enter(LBS_DEB_ASSOC);
  820. founddatarateie = 0;
  821. ratesize = 0;
  822. beaconsize = 0;
  823. if (*bytesleft >= sizeof(beaconsize)) {
  824. /* Extract & convert beacon size from the command buffer */
  825. memcpy(&beaconsize, *pbeaconinfo, sizeof(beaconsize));
  826. beaconsize = le16_to_cpu(beaconsize);
  827. *bytesleft -= sizeof(beaconsize);
  828. *pbeaconinfo += sizeof(beaconsize);
  829. }
  830. if (beaconsize == 0 || beaconsize > *bytesleft) {
  831. *pbeaconinfo += *bytesleft;
  832. *bytesleft = 0;
  833. return -1;
  834. }
  835. /* Initialize the current working beacon pointer for this BSS iteration */
  836. pcurrentptr = *pbeaconinfo;
  837. /* Advance the return beacon pointer past the current beacon */
  838. *pbeaconinfo += beaconsize;
  839. *bytesleft -= beaconsize;
  840. bytesleftforcurrentbeacon = beaconsize;
  841. memcpy(pBSSEntry->macaddress, pcurrentptr, ETH_ALEN);
  842. lbs_deb_scan("InterpretIE: AP MAC Addr-%x:%x:%x:%x:%x:%x\n",
  843. pBSSEntry->macaddress[0], pBSSEntry->macaddress[1],
  844. pBSSEntry->macaddress[2], pBSSEntry->macaddress[3],
  845. pBSSEntry->macaddress[4], pBSSEntry->macaddress[5]);
  846. pcurrentptr += ETH_ALEN;
  847. bytesleftforcurrentbeacon -= ETH_ALEN;
  848. if (bytesleftforcurrentbeacon < 12) {
  849. lbs_deb_scan("InterpretIE: Not enough bytes left\n");
  850. return -1;
  851. }
  852. /*
  853. * next 4 fields are RSSI, time stamp, beacon interval,
  854. * and capability information
  855. */
  856. /* RSSI is 1 byte long */
  857. pBSSEntry->rssi = le32_to_cpu((long)(*pcurrentptr));
  858. lbs_deb_scan("InterpretIE: RSSI=%02X\n", *pcurrentptr);
  859. pcurrentptr += 1;
  860. bytesleftforcurrentbeacon -= 1;
  861. /* time stamp is 8 bytes long */
  862. memcpy(fixedie.timestamp, pcurrentptr, 8);
  863. memcpy(pBSSEntry->timestamp, pcurrentptr, 8);
  864. pcurrentptr += 8;
  865. bytesleftforcurrentbeacon -= 8;
  866. /* beacon interval is 2 bytes long */
  867. memcpy(&fixedie.beaconinterval, pcurrentptr, 2);
  868. pBSSEntry->beaconperiod = le16_to_cpu(fixedie.beaconinterval);
  869. pcurrentptr += 2;
  870. bytesleftforcurrentbeacon -= 2;
  871. /* capability information is 2 bytes long */
  872. memcpy(&fixedie.capabilities, pcurrentptr, 2);
  873. lbs_deb_scan("InterpretIE: fixedie.capabilities=0x%X\n",
  874. fixedie.capabilities);
  875. fixedie.capabilities = le16_to_cpu(fixedie.capabilities);
  876. pcap = (struct ieeetypes_capinfo *) & fixedie.capabilities;
  877. memcpy(&pBSSEntry->cap, pcap, sizeof(struct ieeetypes_capinfo));
  878. pcurrentptr += 2;
  879. bytesleftforcurrentbeacon -= 2;
  880. /* rest of the current buffer are IE's */
  881. lbs_deb_scan("InterpretIE: IElength for this AP = %d\n",
  882. bytesleftforcurrentbeacon);
  883. lbs_dbg_hex("InterpretIE: IE info", (u8 *) pcurrentptr,
  884. bytesleftforcurrentbeacon);
  885. if (pcap->privacy) {
  886. lbs_deb_scan("InterpretIE: AP WEP enabled\n");
  887. pBSSEntry->privacy = wlan802_11privfilter8021xWEP;
  888. } else {
  889. pBSSEntry->privacy = wlan802_11privfilteracceptall;
  890. }
  891. if (pcap->ibss == 1) {
  892. pBSSEntry->mode = IW_MODE_ADHOC;
  893. } else {
  894. pBSSEntry->mode = IW_MODE_INFRA;
  895. }
  896. /* process variable IE */
  897. while (bytesleftforcurrentbeacon >= 2) {
  898. elemID = (enum ieeetypes_elementid) (*((u8 *) pcurrentptr));
  899. elemlen = *((u8 *) pcurrentptr + 1);
  900. if (bytesleftforcurrentbeacon < elemlen) {
  901. lbs_deb_scan("InterpretIE: error in processing IE, "
  902. "bytes left < IE length\n");
  903. bytesleftforcurrentbeacon = 0;
  904. continue;
  905. }
  906. switch (elemID) {
  907. case SSID:
  908. pBSSEntry->ssid.ssidlength = elemlen;
  909. memcpy(pBSSEntry->ssid.ssid, (pcurrentptr + 2),
  910. elemlen);
  911. lbs_deb_scan("ssid '%s'\n", pBSSEntry->ssid.ssid);
  912. break;
  913. case SUPPORTED_RATES:
  914. memcpy(pBSSEntry->datarates, (pcurrentptr + 2),
  915. elemlen);
  916. memmove(pBSSEntry->libertas_supported_rates, (pcurrentptr + 2),
  917. elemlen);
  918. ratesize = elemlen;
  919. founddatarateie = 1;
  920. break;
  921. case EXTRA_IE:
  922. lbs_deb_scan("InterpretIE: EXTRA_IE Found!\n");
  923. pBSSEntry->extra_ie = 1;
  924. break;
  925. case FH_PARAM_SET:
  926. pFH = (struct ieeetypes_fhparamset *) pcurrentptr;
  927. memmove(&pBSSEntry->phyparamset.fhparamset, pFH,
  928. sizeof(struct ieeetypes_fhparamset));
  929. pBSSEntry->phyparamset.fhparamset.dwelltime
  930. =
  931. le16_to_cpu(pBSSEntry->phyparamset.fhparamset.
  932. dwelltime);
  933. break;
  934. case DS_PARAM_SET:
  935. pDS = (struct ieeetypes_dsparamset *) pcurrentptr;
  936. pBSSEntry->channel = pDS->currentchan;
  937. memcpy(&pBSSEntry->phyparamset.dsparamset, pDS,
  938. sizeof(struct ieeetypes_dsparamset));
  939. break;
  940. case CF_PARAM_SET:
  941. pCF = (struct ieeetypes_cfparamset *) pcurrentptr;
  942. memcpy(&pBSSEntry->ssparamset.cfparamset, pCF,
  943. sizeof(struct ieeetypes_cfparamset));
  944. break;
  945. case IBSS_PARAM_SET:
  946. pibss = (struct ieeetypes_ibssparamset *) pcurrentptr;
  947. pBSSEntry->atimwindow =
  948. le32_to_cpu(pibss->atimwindow);
  949. memmove(&pBSSEntry->ssparamset.ibssparamset, pibss,
  950. sizeof(struct ieeetypes_ibssparamset));
  951. pBSSEntry->ssparamset.ibssparamset.atimwindow
  952. =
  953. le16_to_cpu(pBSSEntry->ssparamset.ibssparamset.
  954. atimwindow);
  955. break;
  956. /* Handle Country Info IE */
  957. case COUNTRY_INFO:
  958. pcountryinfo =
  959. (struct ieeetypes_countryinfoset *) pcurrentptr;
  960. if (pcountryinfo->len <
  961. sizeof(pcountryinfo->countrycode)
  962. || pcountryinfo->len > 254) {
  963. lbs_deb_scan("InterpretIE: 11D- Err "
  964. "CountryInfo len =%d min=%zd max=254\n",
  965. pcountryinfo->len,
  966. sizeof(pcountryinfo->countrycode));
  967. ret = -1;
  968. goto done;
  969. }
  970. memcpy(&pBSSEntry->countryinfo,
  971. pcountryinfo, pcountryinfo->len + 2);
  972. lbs_dbg_hex("InterpretIE: 11D- CountryInfo:",
  973. (u8 *) pcountryinfo,
  974. (u32) (pcountryinfo->len + 2));
  975. break;
  976. case EXTENDED_SUPPORTED_RATES:
  977. /*
  978. * only process extended supported rate
  979. * if data rate is already found.
  980. * data rate IE should come before
  981. * extended supported rate IE
  982. */
  983. if (founddatarateie) {
  984. if ((elemlen + ratesize) > WLAN_SUPPORTED_RATES) {
  985. bytestocopy =
  986. (WLAN_SUPPORTED_RATES - ratesize);
  987. } else {
  988. bytestocopy = elemlen;
  989. }
  990. pRate = (u8 *) pBSSEntry->datarates;
  991. pRate += ratesize;
  992. memmove(pRate, (pcurrentptr + 2), bytestocopy);
  993. pRate = (u8 *) pBSSEntry->libertas_supported_rates;
  994. pRate += ratesize;
  995. memmove(pRate, (pcurrentptr + 2), bytestocopy);
  996. }
  997. break;
  998. case VENDOR_SPECIFIC_221:
  999. #define IE_ID_LEN_FIELDS_BYTES 2
  1000. pIe = (struct IE_WPA *)pcurrentptr;
  1001. if (memcmp(pIe->oui, oui01, sizeof(oui01)))
  1002. break;
  1003. pBSSEntry->wpa_ie_len = min_t(size_t,
  1004. elemlen + IE_ID_LEN_FIELDS_BYTES,
  1005. sizeof(pBSSEntry->wpa_ie));
  1006. memcpy(pBSSEntry->wpa_ie, pcurrentptr,
  1007. pBSSEntry->wpa_ie_len);
  1008. lbs_dbg_hex("InterpretIE: Resp WPA_IE",
  1009. pBSSEntry->wpa_ie, elemlen);
  1010. break;
  1011. case WPA2_IE:
  1012. pIe = (struct IE_WPA *)pcurrentptr;
  1013. pBSSEntry->rsn_ie_len = min_t(size_t,
  1014. elemlen + IE_ID_LEN_FIELDS_BYTES,
  1015. sizeof(pBSSEntry->rsn_ie));
  1016. memcpy(pBSSEntry->rsn_ie, pcurrentptr,
  1017. pBSSEntry->rsn_ie_len);
  1018. lbs_dbg_hex("InterpretIE: Resp WPA2_IE",
  1019. pBSSEntry->rsn_ie, elemlen);
  1020. break;
  1021. case TIM:
  1022. break;
  1023. case CHALLENGE_TEXT:
  1024. break;
  1025. }
  1026. pcurrentptr += elemlen + 2;
  1027. /* need to account for IE ID and IE len */
  1028. bytesleftforcurrentbeacon -= (elemlen + 2);
  1029. } /* while (bytesleftforcurrentbeacon > 2) */
  1030. ret = 0;
  1031. done:
  1032. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1033. return ret;
  1034. }
  1035. /**
  1036. * @brief Compare two SSIDs
  1037. *
  1038. * @param ssid1 A pointer to ssid to compare
  1039. * @param ssid2 A pointer to ssid to compare
  1040. *
  1041. * @return 0--ssid is same, otherwise is different
  1042. */
  1043. int libertas_SSID_cmp(struct WLAN_802_11_SSID *ssid1, struct WLAN_802_11_SSID *ssid2)
  1044. {
  1045. if (!ssid1 || !ssid2)
  1046. return -1;
  1047. if (ssid1->ssidlength != ssid2->ssidlength)
  1048. return -1;
  1049. return memcmp(ssid1->ssid, ssid2->ssid, ssid1->ssidlength);
  1050. }
  1051. /**
  1052. * @brief This function finds a specific compatible BSSID in the scan list
  1053. *
  1054. * @param adapter A pointer to wlan_adapter
  1055. * @param bssid BSSID to find in the scan list
  1056. * @param mode Network mode: Infrastructure or IBSS
  1057. *
  1058. * @return index in BSSID list, or error return code (< 0)
  1059. */
  1060. int libertas_find_BSSID_in_list(wlan_adapter * adapter, u8 * bssid, u8 mode)
  1061. {
  1062. int ret = -ENETUNREACH;
  1063. int i;
  1064. if (!bssid)
  1065. return -EFAULT;
  1066. lbs_deb_scan("FindBSSID: Num of BSSIDs = %d\n",
  1067. adapter->numinscantable);
  1068. /* Look through the scan table for a compatible match. The ret return
  1069. * variable will be equal to the index in the scan table (greater
  1070. * than zero) if the network is compatible. The loop will continue
  1071. * past a matched bssid that is not compatible in case there is an
  1072. * AP with multiple SSIDs assigned to the same BSSID
  1073. */
  1074. for (i = 0; ret < 0 && i < adapter->numinscantable; i++) {
  1075. if (!memcmp(adapter->scantable[i].macaddress, bssid, ETH_ALEN)) {
  1076. switch (mode) {
  1077. case IW_MODE_INFRA:
  1078. case IW_MODE_ADHOC:
  1079. ret = is_network_compatible(adapter, i, mode);
  1080. break;
  1081. default:
  1082. ret = i;
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. return ret;
  1088. }
  1089. /**
  1090. * @brief This function finds ssid in ssid list.
  1091. *
  1092. * @param adapter A pointer to wlan_adapter
  1093. * @param ssid SSID to find in the list
  1094. * @param bssid BSSID to qualify the SSID selection (if provided)
  1095. * @param mode Network mode: Infrastructure or IBSS
  1096. *
  1097. * @return index in BSSID list
  1098. */
  1099. int libertas_find_SSID_in_list(wlan_adapter * adapter,
  1100. struct WLAN_802_11_SSID *ssid, u8 * bssid, u8 mode)
  1101. {
  1102. int net = -ENETUNREACH;
  1103. u8 bestrssi = 0;
  1104. int i;
  1105. int j;
  1106. lbs_deb_scan("Num of Entries in Table = %d\n", adapter->numinscantable);
  1107. for (i = 0; i < adapter->numinscantable; i++) {
  1108. if (!libertas_SSID_cmp(&adapter->scantable[i].ssid, ssid) &&
  1109. (!bssid ||
  1110. !memcmp(adapter->scantable[i].
  1111. macaddress, bssid, ETH_ALEN))) {
  1112. switch (mode) {
  1113. case IW_MODE_INFRA:
  1114. case IW_MODE_ADHOC:
  1115. j = is_network_compatible(adapter, i, mode);
  1116. if (j >= 0) {
  1117. if (bssid) {
  1118. return i;
  1119. }
  1120. if (SCAN_RSSI
  1121. (adapter->scantable[i].rssi)
  1122. > bestrssi) {
  1123. bestrssi =
  1124. SCAN_RSSI(adapter->
  1125. scantable[i].
  1126. rssi);
  1127. net = i;
  1128. }
  1129. } else {
  1130. if (net == -ENETUNREACH) {
  1131. net = j;
  1132. }
  1133. }
  1134. break;
  1135. case IW_MODE_AUTO:
  1136. default:
  1137. if (SCAN_RSSI(adapter->scantable[i].rssi)
  1138. > bestrssi) {
  1139. bestrssi =
  1140. SCAN_RSSI(adapter->scantable[i].
  1141. rssi);
  1142. net = i;
  1143. }
  1144. break;
  1145. }
  1146. }
  1147. }
  1148. return net;
  1149. }
  1150. /**
  1151. * @brief This function finds the best SSID in the Scan List
  1152. *
  1153. * Search the scan table for the best SSID that also matches the current
  1154. * adapter network preference (infrastructure or adhoc)
  1155. *
  1156. * @param adapter A pointer to wlan_adapter
  1157. *
  1158. * @return index in BSSID list
  1159. */
  1160. int libertas_find_best_SSID_in_list(wlan_adapter * adapter, u8 mode)
  1161. {
  1162. int bestnet = -ENETUNREACH;
  1163. u8 bestrssi = 0;
  1164. int i;
  1165. lbs_deb_enter(LBS_DEB_ASSOC);
  1166. lbs_deb_scan("Num of BSSIDs = %d\n", adapter->numinscantable);
  1167. for (i = 0; i < adapter->numinscantable; i++) {
  1168. switch (mode) {
  1169. case IW_MODE_INFRA:
  1170. case IW_MODE_ADHOC:
  1171. if (is_network_compatible(adapter, i, mode) >= 0) {
  1172. if (SCAN_RSSI(adapter->scantable[i].rssi) >
  1173. bestrssi) {
  1174. bestrssi =
  1175. SCAN_RSSI(adapter->scantable[i].
  1176. rssi);
  1177. bestnet = i;
  1178. }
  1179. }
  1180. break;
  1181. case IW_MODE_AUTO:
  1182. default:
  1183. if (SCAN_RSSI(adapter->scantable[i].rssi) > bestrssi) {
  1184. bestrssi =
  1185. SCAN_RSSI(adapter->scantable[i].rssi);
  1186. bestnet = i;
  1187. }
  1188. break;
  1189. }
  1190. }
  1191. lbs_deb_leave_args(LBS_DEB_SCAN, "bestnet %d", bestnet);
  1192. return bestnet;
  1193. }
  1194. /**
  1195. * @brief Find the AP with specific ssid in the scan list
  1196. *
  1197. * @param priv A pointer to wlan_private structure
  1198. * @param pSSID A pointer to AP's ssid
  1199. *
  1200. * @return 0--success, otherwise--fail
  1201. */
  1202. int libertas_find_best_network_SSID(wlan_private * priv,
  1203. struct WLAN_802_11_SSID *pSSID,
  1204. u8 preferred_mode, u8 *out_mode)
  1205. {
  1206. wlan_adapter *adapter = priv->adapter;
  1207. int ret = 0;
  1208. struct bss_descriptor *preqbssid;
  1209. int i;
  1210. lbs_deb_enter(LBS_DEB_ASSOC);
  1211. memset(pSSID, 0, sizeof(struct WLAN_802_11_SSID));
  1212. wlan_scan_networks(priv, NULL, 1);
  1213. if (adapter->surpriseremoved)
  1214. return -1;
  1215. wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
  1216. i = libertas_find_best_SSID_in_list(adapter, preferred_mode);
  1217. if (i < 0) {
  1218. ret = -1;
  1219. goto out;
  1220. }
  1221. preqbssid = &adapter->scantable[i];
  1222. memcpy(pSSID, &preqbssid->ssid,
  1223. sizeof(struct WLAN_802_11_SSID));
  1224. *out_mode = preqbssid->mode;
  1225. if (!pSSID->ssidlength) {
  1226. ret = -1;
  1227. }
  1228. out:
  1229. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1230. return ret;
  1231. }
  1232. /**
  1233. * @brief Scan Network
  1234. *
  1235. * @param dev A pointer to net_device structure
  1236. * @param info A pointer to iw_request_info structure
  1237. * @param vwrq A pointer to iw_param structure
  1238. * @param extra A pointer to extra data buf
  1239. *
  1240. * @return 0 --success, otherwise fail
  1241. */
  1242. int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
  1243. struct iw_param *vwrq, char *extra)
  1244. {
  1245. wlan_private *priv = dev->priv;
  1246. wlan_adapter *adapter = priv->adapter;
  1247. lbs_deb_enter(LBS_DEB_SCAN);
  1248. wlan_scan_networks(priv, NULL, 0);
  1249. if (adapter->surpriseremoved)
  1250. return -1;
  1251. lbs_deb_leave(LBS_DEB_SCAN);
  1252. return 0;
  1253. }
  1254. /**
  1255. * @brief Send a scan command for all available channels filtered on a spec
  1256. *
  1257. * @param priv A pointer to wlan_private structure
  1258. * @param prequestedssid A pointer to AP's ssid
  1259. * @param keeppreviousscan Flag used to save/clear scan table before scan
  1260. *
  1261. * @return 0-success, otherwise fail
  1262. */
  1263. int libertas_send_specific_SSID_scan(wlan_private * priv,
  1264. struct WLAN_802_11_SSID *prequestedssid,
  1265. u8 keeppreviousscan)
  1266. {
  1267. wlan_adapter *adapter = priv->adapter;
  1268. struct wlan_ioctl_user_scan_cfg scancfg;
  1269. lbs_deb_enter(LBS_DEB_ASSOC);
  1270. if (prequestedssid == NULL) {
  1271. return -1;
  1272. }
  1273. memset(&scancfg, 0x00, sizeof(scancfg));
  1274. memcpy(scancfg.specificSSID, prequestedssid->ssid,
  1275. prequestedssid->ssidlength);
  1276. scancfg.keeppreviousscan = keeppreviousscan;
  1277. wlan_scan_networks(priv, &scancfg, 1);
  1278. if (adapter->surpriseremoved)
  1279. return -1;
  1280. wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
  1281. lbs_deb_leave(LBS_DEB_ASSOC);
  1282. return 0;
  1283. }
  1284. /**
  1285. * @brief scan an AP with specific BSSID
  1286. *
  1287. * @param priv A pointer to wlan_private structure
  1288. * @param bssid A pointer to AP's bssid
  1289. * @param keeppreviousscan Flag used to save/clear scan table before scan
  1290. *
  1291. * @return 0-success, otherwise fail
  1292. */
  1293. int libertas_send_specific_BSSID_scan(wlan_private * priv, u8 * bssid, u8 keeppreviousscan)
  1294. {
  1295. struct wlan_ioctl_user_scan_cfg scancfg;
  1296. lbs_deb_enter(LBS_DEB_ASSOC);
  1297. if (bssid == NULL) {
  1298. return -1;
  1299. }
  1300. memset(&scancfg, 0x00, sizeof(scancfg));
  1301. memcpy(scancfg.specificBSSID, bssid, sizeof(scancfg.specificBSSID));
  1302. scancfg.keeppreviousscan = keeppreviousscan;
  1303. wlan_scan_networks(priv, &scancfg, 1);
  1304. if (priv->adapter->surpriseremoved)
  1305. return -1;
  1306. wait_event_interruptible(priv->adapter->cmd_pending,
  1307. !priv->adapter->nr_cmd_pending);
  1308. lbs_deb_leave(LBS_DEB_ASSOC);
  1309. return 0;
  1310. }
  1311. /**
  1312. * @brief Retrieve the scan table entries via wireless tools IOCTL call
  1313. *
  1314. * @param dev A pointer to net_device structure
  1315. * @param info A pointer to iw_request_info structure
  1316. * @param dwrq A pointer to iw_point structure
  1317. * @param extra A pointer to extra data buf
  1318. *
  1319. * @return 0 --success, otherwise fail
  1320. */
  1321. int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
  1322. struct iw_point *dwrq, char *extra)
  1323. {
  1324. wlan_private *priv = dev->priv;
  1325. wlan_adapter *adapter = priv->adapter;
  1326. int ret = 0;
  1327. char *current_ev = extra;
  1328. char *end_buf = extra + IW_SCAN_MAX_DATA;
  1329. struct chan_freq_power *cfp;
  1330. struct bss_descriptor *pscantable;
  1331. char *current_val; /* For rates */
  1332. struct iw_event iwe; /* Temporary buffer */
  1333. int i;
  1334. int j;
  1335. int rate;
  1336. #define PERFECT_RSSI ((u8)50)
  1337. #define WORST_RSSI ((u8)0)
  1338. #define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
  1339. u8 rssi;
  1340. u8 buf[16 + 256 * 2];
  1341. u8 *ptr;
  1342. lbs_deb_enter(LBS_DEB_ASSOC);
  1343. /*
  1344. * if there's either commands in the queue or one being
  1345. * processed return -EAGAIN for iwlist to retry later.
  1346. */
  1347. if (adapter->nr_cmd_pending)
  1348. return -EAGAIN;
  1349. if (adapter->last_scanned_channel) {
  1350. wlan_scan_networks(priv, NULL, 0);
  1351. return -EAGAIN;
  1352. }
  1353. if (adapter->connect_status == libertas_connected)
  1354. lbs_deb_scan("current ssid '%s'\n",
  1355. adapter->curbssparams.ssid.ssid);
  1356. lbs_deb_scan("Scan: Get: numinscantable = %d\n",
  1357. adapter->numinscantable);
  1358. /* The old API using SIOCGIWAPLIST had a hard limit of IW_MAX_AP.
  1359. * The new API using SIOCGIWSCAN is only limited by buffer size
  1360. * WE-14 -> WE-16 the buffer is limited to IW_SCAN_MAX_DATA bytes
  1361. * which is 4096.
  1362. */
  1363. for (i = 0; i < adapter->numinscantable; i++) {
  1364. if ((current_ev + MAX_SCAN_CELL_SIZE) >= end_buf) {
  1365. lbs_deb_scan("i=%d break out: current_ev=%p end_buf=%p "
  1366. "MAX_SCAN_CELL_SIZE=%zd\n",
  1367. i, current_ev, end_buf, MAX_SCAN_CELL_SIZE);
  1368. break;
  1369. }
  1370. pscantable = &adapter->scantable[i];
  1371. lbs_deb_scan("i %d, ssid '%s'\n", i, pscantable->ssid.ssid);
  1372. cfp =
  1373. libertas_find_cfp_by_band_and_channel(adapter, 0,
  1374. pscantable->channel);
  1375. if (!cfp) {
  1376. lbs_deb_scan("Invalid channel number %d\n",
  1377. pscantable->channel);
  1378. continue;
  1379. }
  1380. if (!ssid_valid(&adapter->scantable[i].ssid)) {
  1381. continue;
  1382. }
  1383. /* First entry *MUST* be the AP MAC address */
  1384. iwe.cmd = SIOCGIWAP;
  1385. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1386. memcpy(iwe.u.ap_addr.sa_data,
  1387. &adapter->scantable[i].macaddress, ETH_ALEN);
  1388. iwe.len = IW_EV_ADDR_LEN;
  1389. current_ev =
  1390. iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);
  1391. //Add the ESSID
  1392. iwe.u.data.length = adapter->scantable[i].ssid.ssidlength;
  1393. if (iwe.u.data.length > 32) {
  1394. iwe.u.data.length = 32;
  1395. }
  1396. iwe.cmd = SIOCGIWESSID;
  1397. iwe.u.data.flags = 1;
  1398. iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
  1399. current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
  1400. adapter->scantable[i].ssid.
  1401. ssid);
  1402. //Add mode
  1403. iwe.cmd = SIOCGIWMODE;
  1404. iwe.u.mode = adapter->scantable[i].mode;
  1405. iwe.len = IW_EV_UINT_LEN;
  1406. current_ev =
  1407. iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);
  1408. //frequency
  1409. iwe.cmd = SIOCGIWFREQ;
  1410. iwe.u.freq.m = (long)cfp->freq * 100000;
  1411. iwe.u.freq.e = 1;
  1412. iwe.len = IW_EV_FREQ_LEN;
  1413. current_ev =
  1414. iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);
  1415. /* Add quality statistics */
  1416. iwe.cmd = IWEVQUAL;
  1417. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
  1418. iwe.u.qual.level = SCAN_RSSI(adapter->scantable[i].rssi);
  1419. rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1420. iwe.u.qual.qual =
  1421. (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
  1422. (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
  1423. (RSSI_DIFF * RSSI_DIFF);
  1424. if (iwe.u.qual.qual > 100)
  1425. iwe.u.qual.qual = 100;
  1426. else if (iwe.u.qual.qual < 1)
  1427. iwe.u.qual.qual = 0;
  1428. if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
  1429. iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1430. } else {
  1431. iwe.u.qual.noise =
  1432. CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
  1433. }
  1434. if ((adapter->mode == IW_MODE_ADHOC) &&
  1435. !libertas_SSID_cmp(&adapter->curbssparams.ssid,
  1436. &adapter->scantable[i].ssid)
  1437. && adapter->adhoccreate) {
  1438. ret = libertas_prepare_and_send_command(priv,
  1439. cmd_802_11_rssi,
  1440. 0,
  1441. cmd_option_waitforrsp,
  1442. 0, NULL);
  1443. if (!ret) {
  1444. iwe.u.qual.level =
  1445. CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_AVG] /
  1446. AVG_SCALE,
  1447. adapter->NF[TYPE_RXPD][TYPE_AVG] /
  1448. AVG_SCALE);
  1449. }
  1450. }
  1451. iwe.len = IW_EV_QUAL_LEN;
  1452. current_ev =
  1453. iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);
  1454. /* Add encryption capability */
  1455. iwe.cmd = SIOCGIWENCODE;
  1456. if (adapter->scantable[i].privacy) {
  1457. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1458. } else {
  1459. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1460. }
  1461. iwe.u.data.length = 0;
  1462. iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
  1463. current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
  1464. adapter->scantable->ssid.
  1465. ssid);
  1466. current_val = current_ev + IW_EV_LCP_LEN;
  1467. iwe.cmd = SIOCGIWRATE;
  1468. iwe.u.bitrate.fixed = 0;
  1469. iwe.u.bitrate.disabled = 0;
  1470. iwe.u.bitrate.value = 0;
  1471. /* Bit rate given in 500 kb/s units (+ 0x80) */
  1472. for (j = 0; j < sizeof(adapter->scantable[i].libertas_supported_rates);
  1473. j++) {
  1474. if (adapter->scantable[i].libertas_supported_rates[j] == 0) {
  1475. break;
  1476. }
  1477. rate =
  1478. (adapter->scantable[i].libertas_supported_rates[j] & 0x7F) *
  1479. 500000;
  1480. if (rate > iwe.u.bitrate.value) {
  1481. iwe.u.bitrate.value = rate;
  1482. }
  1483. iwe.u.bitrate.value =
  1484. (adapter->scantable[i].libertas_supported_rates[j]
  1485. & 0x7f) * 500000;
  1486. iwe.len = IW_EV_PARAM_LEN;
  1487. current_ev =
  1488. iwe_stream_add_value(current_ev, current_val,
  1489. end_buf, &iwe, iwe.len);
  1490. }
  1491. if ((adapter->scantable[i].mode == IW_MODE_ADHOC)
  1492. && !libertas_SSID_cmp(&adapter->curbssparams.ssid,
  1493. &adapter->scantable[i].ssid)
  1494. && adapter->adhoccreate) {
  1495. iwe.u.bitrate.value = 22 * 500000;
  1496. }
  1497. iwe.len = IW_EV_PARAM_LEN;
  1498. current_ev =
  1499. iwe_stream_add_value(current_ev, current_val, end_buf, &iwe,
  1500. iwe.len);
  1501. /* Add new value to event */
  1502. current_val = current_ev + IW_EV_LCP_LEN;
  1503. if (adapter->scantable[i].rsn_ie[0] == WPA2_IE) {
  1504. memset(&iwe, 0, sizeof(iwe));
  1505. memset(buf, 0, sizeof(buf));
  1506. memcpy(buf, adapter->scantable[i].rsn_ie,
  1507. adapter->scantable[i].rsn_ie_len);
  1508. iwe.cmd = IWEVGENIE;
  1509. iwe.u.data.length = adapter->scantable[i].rsn_ie_len;
  1510. iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
  1511. current_ev = iwe_stream_add_point(current_ev, end_buf,
  1512. &iwe, buf);
  1513. }
  1514. if (adapter->scantable[i].wpa_ie[0] == WPA_IE) {
  1515. memset(&iwe, 0, sizeof(iwe));
  1516. memset(buf, 0, sizeof(buf));
  1517. memcpy(buf, adapter->scantable[i].wpa_ie,
  1518. adapter->scantable[i].wpa_ie_len);
  1519. iwe.cmd = IWEVGENIE;
  1520. iwe.u.data.length = adapter->scantable[i].wpa_ie_len;
  1521. iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
  1522. current_ev = iwe_stream_add_point(current_ev, end_buf,
  1523. &iwe, buf);
  1524. }
  1525. if (adapter->scantable[i].extra_ie != 0) {
  1526. memset(&iwe, 0, sizeof(iwe));
  1527. memset(buf, 0, sizeof(buf));
  1528. ptr = buf;
  1529. ptr += sprintf(ptr, "extra_ie");
  1530. iwe.u.data.length = strlen(buf);
  1531. lbs_deb_scan("iwe.u.data.length %d\n",
  1532. iwe.u.data.length);
  1533. lbs_deb_scan("BUF: %s \n", buf);
  1534. iwe.cmd = IWEVCUSTOM;
  1535. iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
  1536. current_ev =
  1537. iwe_stream_add_point(current_ev, end_buf, &iwe,
  1538. buf);
  1539. }
  1540. current_val = current_ev + IW_EV_LCP_LEN;
  1541. /*
  1542. * Check if we added any event
  1543. */
  1544. if ((current_val - current_ev) > IW_EV_LCP_LEN)
  1545. current_ev = current_val;
  1546. }
  1547. dwrq->length = (current_ev - extra);
  1548. dwrq->flags = 0;
  1549. lbs_deb_leave(LBS_DEB_ASSOC);
  1550. return 0;
  1551. }
  1552. /**
  1553. * @brief Prepare a scan command to be sent to the firmware
  1554. *
  1555. * Use the wlan_scan_cmd_config sent to the command processing module in
  1556. * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
  1557. * struct to send to firmware.
  1558. *
  1559. * The fixed fields specifying the BSS type and BSSID filters as well as a
  1560. * variable number/length of TLVs are sent in the command to firmware.
  1561. *
  1562. * @param priv A pointer to wlan_private structure
  1563. * @param cmd A pointer to cmd_ds_command structure to be sent to
  1564. * firmware with the cmd_DS_801_11_SCAN structure
  1565. * @param pdata_buf Void pointer cast of a wlan_scan_cmd_config struct used
  1566. * to set the fields/TLVs for the command sent to firmware
  1567. *
  1568. * @return 0 or -1
  1569. *
  1570. * @sa wlan_scan_create_channel_list
  1571. */
  1572. int libertas_cmd_80211_scan(wlan_private * priv,
  1573. struct cmd_ds_command *cmd, void *pdata_buf)
  1574. {
  1575. struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
  1576. struct wlan_scan_cmd_config *pscancfg;
  1577. lbs_deb_enter(LBS_DEB_ASSOC);
  1578. pscancfg = pdata_buf;
  1579. /* Set fixed field variables in scan command */
  1580. pscan->bsstype = pscancfg->bsstype;
  1581. memcpy(pscan->BSSID, pscancfg->specificBSSID, sizeof(pscan->BSSID));
  1582. memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
  1583. cmd->command = cpu_to_le16(cmd_802_11_scan);
  1584. /* size is equal to the sizeof(fixed portions) + the TLV len + header */
  1585. cmd->size = cpu_to_le16(sizeof(pscan->bsstype)
  1586. + sizeof(pscan->BSSID)
  1587. + pscancfg->tlvbufferlen + S_DS_GEN);
  1588. lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
  1589. cmd->command, cmd->size, cmd->seqnum);
  1590. lbs_deb_leave(LBS_DEB_ASSOC);
  1591. return 0;
  1592. }
  1593. /**
  1594. * @brief This function handles the command response of scan
  1595. *
  1596. * The response buffer for the scan command has the following
  1597. * memory layout:
  1598. *
  1599. * .-----------------------------------------------------------.
  1600. * | header (4 * sizeof(u16)): Standard command response hdr |
  1601. * .-----------------------------------------------------------.
  1602. * | bufsize (u16) : sizeof the BSS Description data |
  1603. * .-----------------------------------------------------------.
  1604. * | NumOfSet (u8) : Number of BSS Descs returned |
  1605. * .-----------------------------------------------------------.
  1606. * | BSSDescription data (variable, size given in bufsize) |
  1607. * .-----------------------------------------------------------.
  1608. * | TLV data (variable, size calculated using header->size, |
  1609. * | bufsize and sizeof the fixed fields above) |
  1610. * .-----------------------------------------------------------.
  1611. *
  1612. * @param priv A pointer to wlan_private structure
  1613. * @param resp A pointer to cmd_ds_command
  1614. *
  1615. * @return 0 or -1
  1616. */
  1617. int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
  1618. {
  1619. wlan_adapter *adapter = priv->adapter;
  1620. struct cmd_ds_802_11_scan_rsp *pscan;
  1621. struct bss_descriptor newbssentry;
  1622. struct mrvlietypes_data *ptlv;
  1623. struct mrvlietypes_tsftimestamp *ptsftlv;
  1624. u8 *pbssinfo;
  1625. u16 scanrespsize;
  1626. int bytesleft;
  1627. int numintable;
  1628. int bssIdx;
  1629. int idx;
  1630. int tlvbufsize;
  1631. u64 tsfval;
  1632. int ret;
  1633. lbs_deb_enter(LBS_DEB_ASSOC);
  1634. pscan = &resp->params.scanresp;
  1635. if (pscan->nr_sets > MRVDRV_MAX_BSSID_LIST) {
  1636. lbs_deb_scan(
  1637. "SCAN_RESP: Invalid number of AP returned (%d)!!\n",
  1638. pscan->nr_sets);
  1639. ret = -1;
  1640. goto done;
  1641. }
  1642. bytesleft = le16_to_cpu(pscan->bssdescriptsize);
  1643. lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
  1644. scanrespsize = le16_to_cpu(resp->size);
  1645. lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
  1646. pscan->nr_sets);
  1647. numintable = adapter->numinscantable;
  1648. pbssinfo = pscan->bssdesc_and_tlvbuffer;
  1649. /* The size of the TLV buffer is equal to the entire command response
  1650. * size (scanrespsize) minus the fixed fields (sizeof()'s), the
  1651. * BSS Descriptions (bssdescriptsize as bytesLef) and the command
  1652. * response header (S_DS_GEN)
  1653. */
  1654. tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
  1655. + sizeof(pscan->nr_sets)
  1656. + S_DS_GEN);
  1657. ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);
  1658. /* Search the TLV buffer space in the scan response for any valid TLVs */
  1659. wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);
  1660. /*
  1661. * Process each scan response returned (pscan->nr_sets). Save
  1662. * the information in the newbssentry and then insert into the
  1663. * driver scan table either as an update to an existing entry
  1664. * or as an addition at the end of the table
  1665. */
  1666. for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
  1667. /* Zero out the newbssentry we are about to store info in */
  1668. memset(&newbssentry, 0x00, sizeof(newbssentry));
  1669. /* Process the data fields and IEs returned for this BSS */
  1670. if ((InterpretBSSDescriptionWithIE(&newbssentry,
  1671. &pbssinfo,
  1672. &bytesleft) ==
  1673. 0)
  1674. && CHECK_SSID_IS_VALID(&newbssentry.ssid)) {
  1675. lbs_deb_scan(
  1676. "SCAN_RESP: BSSID = %02x:%02x:%02x:%02x:%02x:%02x\n",
  1677. newbssentry.macaddress[0],
  1678. newbssentry.macaddress[1],
  1679. newbssentry.macaddress[2],
  1680. newbssentry.macaddress[3],
  1681. newbssentry.macaddress[4],
  1682. newbssentry.macaddress[5]);
  1683. /*
  1684. * Search the scan table for the same bssid
  1685. */
  1686. for (bssIdx = 0; bssIdx < numintable; bssIdx++) {
  1687. if (memcmp(newbssentry.macaddress,
  1688. adapter->scantable[bssIdx].
  1689. macaddress,
  1690. sizeof(newbssentry.macaddress)) ==
  1691. 0) {
  1692. /*
  1693. * If the SSID matches as well, it is a duplicate of
  1694. * this entry. Keep the bssIdx set to this
  1695. * entry so we replace the old contents in the table
  1696. */
  1697. if ((newbssentry.ssid.ssidlength ==
  1698. adapter->scantable[bssIdx].ssid.
  1699. ssidlength)
  1700. &&
  1701. (memcmp
  1702. (newbssentry.ssid.ssid,
  1703. adapter->scantable[bssIdx].ssid.
  1704. ssid,
  1705. newbssentry.ssid.ssidlength) ==
  1706. 0)) {
  1707. lbs_deb_scan(
  1708. "SCAN_RESP: Duplicate of index: %d\n",
  1709. bssIdx);
  1710. break;
  1711. }
  1712. }
  1713. }
  1714. /*
  1715. * If the bssIdx is equal to the number of entries in the table,
  1716. * the new entry was not a duplicate; append it to the scan
  1717. * table
  1718. */
  1719. if (bssIdx == numintable) {
  1720. /* Range check the bssIdx, keep it limited to the last entry */
  1721. if (bssIdx == MRVDRV_MAX_BSSID_LIST) {
  1722. bssIdx--;
  1723. } else {
  1724. numintable++;
  1725. }
  1726. }
  1727. /*
  1728. * If the TSF TLV was appended to the scan results, save the
  1729. * this entries TSF value in the networktsf field. The
  1730. * networktsf is the firmware's TSF value at the time the
  1731. * beacon or probe response was received.
  1732. */
  1733. if (ptsftlv) {
  1734. memcpy(&tsfval, &ptsftlv->tsftable[idx],
  1735. sizeof(tsfval));
  1736. tsfval = le64_to_cpu(tsfval);
  1737. memcpy(&newbssentry.networktsf,
  1738. &tsfval, sizeof(newbssentry.networktsf));
  1739. }
  1740. /* Copy the locally created newbssentry to the scan table */
  1741. memcpy(&adapter->scantable[bssIdx],
  1742. &newbssentry,
  1743. sizeof(adapter->scantable[bssIdx]));
  1744. } else {
  1745. /* error parsing/interpreting the scan response, skipped */
  1746. lbs_deb_scan("SCAN_RESP: "
  1747. "InterpretBSSDescriptionWithIE returned ERROR\n");
  1748. }
  1749. }
  1750. lbs_deb_scan("SCAN_RESP: Scanned %2d APs, %d valid, %d total\n",
  1751. pscan->nr_sets, numintable - adapter->numinscantable,
  1752. numintable);
  1753. /* Update the total number of BSSIDs in the scan table */
  1754. adapter->numinscantable = numintable;
  1755. ret = 0;
  1756. done:
  1757. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1758. return ret;
  1759. }