scan.c 56 KB

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