scan.c 59 KB

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