scan.c 55 KB

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