scan.c 60 KB

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