scan.c 57 KB

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