scan.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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] != MFIE_TYPE_GENERIC
  65. && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
  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] == MFIE_TYPE_GENERIC)
  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] == MFIE_TYPE_RSN)
  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] != MFIE_TYPE_GENERIC)
  117. && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
  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. struct ieeetypes_fhparamset *pFH;
  787. struct ieeetypes_dsparamset *pDS;
  788. struct ieeetypes_cfparamset *pCF;
  789. struct ieeetypes_ibssparamset *pibss;
  790. u8 *pos, *end;
  791. u8 *pRate;
  792. u8 bytestocopy;
  793. u8 ratesize;
  794. u16 beaconsize;
  795. u8 founddatarateie;
  796. int ret;
  797. struct ieeetypes_countryinfoset *pcountryinfo;
  798. lbs_deb_enter(LBS_DEB_ASSOC);
  799. founddatarateie = 0;
  800. ratesize = 0;
  801. beaconsize = 0;
  802. if (*bytesleft >= sizeof(beaconsize)) {
  803. /* Extract & convert beacon size from the command buffer */
  804. beaconsize = le16_to_cpup((void *)*pbeaconinfo);
  805. *bytesleft -= sizeof(beaconsize);
  806. *pbeaconinfo += sizeof(beaconsize);
  807. }
  808. if (beaconsize == 0 || beaconsize > *bytesleft) {
  809. *pbeaconinfo += *bytesleft;
  810. *bytesleft = 0;
  811. return -1;
  812. }
  813. /* Initialize the current working beacon pointer for this BSS iteration */
  814. pos = *pbeaconinfo;
  815. end = pos + beaconsize;
  816. /* Advance the return beacon pointer past the current beacon */
  817. *pbeaconinfo += beaconsize;
  818. *bytesleft -= beaconsize;
  819. memcpy(bss->bssid, pos, ETH_ALEN);
  820. lbs_deb_scan("process_bss: AP BSSID " MAC_FMT "\n", MAC_ARG(bss->bssid));
  821. pos += ETH_ALEN;
  822. if ((end - pos) < 12) {
  823. lbs_deb_scan("process_bss: Not enough bytes left\n");
  824. return -1;
  825. }
  826. /*
  827. * next 4 fields are RSSI, time stamp, beacon interval,
  828. * and capability information
  829. */
  830. /* RSSI is 1 byte long */
  831. bss->rssi = *pos;
  832. lbs_deb_scan("process_bss: RSSI=%02X\n", *pos);
  833. pos++;
  834. /* time stamp is 8 bytes long */
  835. bss->timestamp = le64_to_cpup((void *) pos);
  836. pos += 8;
  837. /* beacon interval is 2 bytes long */
  838. bss->beaconperiod = le16_to_cpup((void *) pos);
  839. pos += 2;
  840. /* capability information is 2 bytes long */
  841. bss->capability = le16_to_cpup((void *) pos);
  842. lbs_deb_scan("process_bss: capabilities = 0x%4X\n", bss->capability);
  843. pos += 2;
  844. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  845. lbs_deb_scan("process_bss: AP WEP enabled\n");
  846. if (bss->capability & WLAN_CAPABILITY_IBSS)
  847. bss->mode = IW_MODE_ADHOC;
  848. else
  849. bss->mode = IW_MODE_INFRA;
  850. /* rest of the current buffer are IE's */
  851. lbs_deb_scan("process_bss: IE length for this AP = %zd\n", end - pos);
  852. lbs_dbg_hex("process_bss: IE info", pos, end - pos);
  853. /* process variable IE */
  854. while (pos <= end - 2) {
  855. struct ieee80211_info_element * elem =
  856. (struct ieee80211_info_element *) pos;
  857. if (pos + elem->len > end) {
  858. lbs_deb_scan("process_bss: error in processing IE, "
  859. "bytes left < IE length\n");
  860. break;
  861. }
  862. switch (elem->id) {
  863. case MFIE_TYPE_SSID:
  864. bss->ssid_len = elem->len;
  865. memcpy(bss->ssid, elem->data, elem->len);
  866. lbs_deb_scan("ssid '%s', ssid length %u\n",
  867. escape_essid(bss->ssid, bss->ssid_len),
  868. bss->ssid_len);
  869. break;
  870. case MFIE_TYPE_RATES:
  871. memcpy(bss->datarates, elem->data, elem->len);
  872. memmove(bss->libertas_supported_rates, elem->data,
  873. elem->len);
  874. ratesize = elem->len;
  875. founddatarateie = 1;
  876. break;
  877. case MFIE_TYPE_FH_SET:
  878. pFH = (struct ieeetypes_fhparamset *) pos;
  879. memmove(&bss->phyparamset.fhparamset, pFH,
  880. sizeof(struct ieeetypes_fhparamset));
  881. #if 0 /* I think we can store these LE */
  882. bss->phyparamset.fhparamset.dwelltime
  883. = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
  884. #endif
  885. break;
  886. case MFIE_TYPE_DS_SET:
  887. pDS = (struct ieeetypes_dsparamset *) pos;
  888. bss->channel = pDS->currentchan;
  889. memcpy(&bss->phyparamset.dsparamset, pDS,
  890. sizeof(struct ieeetypes_dsparamset));
  891. break;
  892. case MFIE_TYPE_CF_SET:
  893. pCF = (struct ieeetypes_cfparamset *) pos;
  894. memcpy(&bss->ssparamset.cfparamset, pCF,
  895. sizeof(struct ieeetypes_cfparamset));
  896. break;
  897. case MFIE_TYPE_IBSS_SET:
  898. pibss = (struct ieeetypes_ibssparamset *) pos;
  899. bss->atimwindow = le32_to_cpu(pibss->atimwindow);
  900. memmove(&bss->ssparamset.ibssparamset, pibss,
  901. sizeof(struct ieeetypes_ibssparamset));
  902. #if 0
  903. bss->ssparamset.ibssparamset.atimwindow
  904. = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
  905. #endif
  906. break;
  907. case MFIE_TYPE_COUNTRY:
  908. pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
  909. if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
  910. || pcountryinfo->len > 254) {
  911. lbs_deb_scan("process_bss: 11D- Err "
  912. "CountryInfo len =%d min=%zd max=254\n",
  913. pcountryinfo->len,
  914. sizeof(pcountryinfo->countrycode));
  915. ret = -1;
  916. goto done;
  917. }
  918. memcpy(&bss->countryinfo,
  919. pcountryinfo, pcountryinfo->len + 2);
  920. lbs_dbg_hex("process_bss: 11D- CountryInfo:",
  921. (u8 *) pcountryinfo,
  922. (u32) (pcountryinfo->len + 2));
  923. break;
  924. case MFIE_TYPE_RATES_EX:
  925. /* only process extended supported rate if data rate is
  926. * already found. Data rate IE should come before
  927. * extended supported rate IE
  928. */
  929. if (!founddatarateie)
  930. break;
  931. if ((elem->len + ratesize) > WLAN_SUPPORTED_RATES) {
  932. bytestocopy =
  933. (WLAN_SUPPORTED_RATES - ratesize);
  934. } else {
  935. bytestocopy = elem->len;
  936. }
  937. pRate = (u8 *) bss->datarates;
  938. pRate += ratesize;
  939. memmove(pRate, elem->data, bytestocopy);
  940. pRate = (u8 *) bss->libertas_supported_rates;
  941. pRate += ratesize;
  942. memmove(pRate, elem->data, bytestocopy);
  943. break;
  944. case MFIE_TYPE_GENERIC:
  945. if (elem->len >= 4 &&
  946. elem->data[0] == 0x00 &&
  947. elem->data[1] == 0x50 &&
  948. elem->data[2] == 0xf2 &&
  949. elem->data[3] == 0x01) {
  950. bss->wpa_ie_len = min(elem->len + 2,
  951. MAX_WPA_IE_LEN);
  952. memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
  953. lbs_dbg_hex("process_bss: WPA IE", bss->wpa_ie,
  954. elem->len);
  955. } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
  956. elem->data[0] == 0x00 &&
  957. elem->data[1] == 0x50 &&
  958. elem->data[2] == 0x43 &&
  959. elem->data[3] == 0x04) {
  960. bss->mesh = 1;
  961. }
  962. break;
  963. case MFIE_TYPE_RSN:
  964. bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
  965. memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
  966. lbs_dbg_hex("process_bss: RSN_IE", bss->rsn_ie, elem->len);
  967. break;
  968. default:
  969. break;
  970. }
  971. pos += elem->len + 2;
  972. }
  973. /* Timestamp */
  974. bss->last_scanned = jiffies;
  975. ret = 0;
  976. done:
  977. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  978. return ret;
  979. }
  980. /**
  981. * @brief Compare two SSIDs
  982. *
  983. * @param ssid1 A pointer to ssid to compare
  984. * @param ssid2 A pointer to ssid to compare
  985. *
  986. * @return 0--ssid is same, otherwise is different
  987. */
  988. int libertas_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
  989. {
  990. if (ssid1_len != ssid2_len)
  991. return -1;
  992. return memcmp(ssid1, ssid2, ssid1_len);
  993. }
  994. /**
  995. * @brief This function finds a specific compatible BSSID in the scan list
  996. *
  997. * @param adapter A pointer to wlan_adapter
  998. * @param bssid BSSID to find in the scan list
  999. * @param mode Network mode: Infrastructure or IBSS
  1000. *
  1001. * @return index in BSSID list, or error return code (< 0)
  1002. */
  1003. struct bss_descriptor * libertas_find_bssid_in_list(wlan_adapter * adapter,
  1004. u8 * bssid, u8 mode)
  1005. {
  1006. struct bss_descriptor * iter_bss;
  1007. struct bss_descriptor * found_bss = NULL;
  1008. if (!bssid)
  1009. return NULL;
  1010. lbs_dbg_hex("libertas_find_BSSID_in_list: looking for ",
  1011. bssid, ETH_ALEN);
  1012. /* Look through the scan table for a compatible match. The loop will
  1013. * continue past a matched bssid that is not compatible in case there
  1014. * is an AP with multiple SSIDs assigned to the same BSSID
  1015. */
  1016. mutex_lock(&adapter->lock);
  1017. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1018. if (compare_ether_addr(iter_bss->bssid, bssid))
  1019. continue; /* bssid doesn't match */
  1020. switch (mode) {
  1021. case IW_MODE_INFRA:
  1022. case IW_MODE_ADHOC:
  1023. if (!is_network_compatible(adapter, iter_bss, mode))
  1024. break;
  1025. found_bss = iter_bss;
  1026. break;
  1027. default:
  1028. found_bss = iter_bss;
  1029. break;
  1030. }
  1031. }
  1032. mutex_unlock(&adapter->lock);
  1033. return found_bss;
  1034. }
  1035. /**
  1036. * @brief This function finds ssid in ssid list.
  1037. *
  1038. * @param adapter A pointer to wlan_adapter
  1039. * @param ssid SSID to find in the list
  1040. * @param bssid BSSID to qualify the SSID selection (if provided)
  1041. * @param mode Network mode: Infrastructure or IBSS
  1042. *
  1043. * @return index in BSSID list
  1044. */
  1045. struct bss_descriptor * libertas_find_ssid_in_list(wlan_adapter * adapter,
  1046. u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
  1047. int channel)
  1048. {
  1049. u8 bestrssi = 0;
  1050. struct bss_descriptor * iter_bss = NULL;
  1051. struct bss_descriptor * found_bss = NULL;
  1052. struct bss_descriptor * tmp_oldest = NULL;
  1053. mutex_lock(&adapter->lock);
  1054. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1055. if ( !tmp_oldest
  1056. || (iter_bss->last_scanned < tmp_oldest->last_scanned))
  1057. tmp_oldest = iter_bss;
  1058. if (libertas_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
  1059. ssid, ssid_len) != 0)
  1060. continue; /* ssid doesn't match */
  1061. if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
  1062. continue; /* bssid doesn't match */
  1063. if ((channel > 0) && (iter_bss->channel != channel))
  1064. continue; /* channel doesn't match */
  1065. switch (mode) {
  1066. case IW_MODE_INFRA:
  1067. case IW_MODE_ADHOC:
  1068. if (!is_network_compatible(adapter, iter_bss, mode))
  1069. break;
  1070. if (bssid) {
  1071. /* Found requested BSSID */
  1072. found_bss = iter_bss;
  1073. goto out;
  1074. }
  1075. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  1076. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1077. found_bss = iter_bss;
  1078. }
  1079. break;
  1080. case IW_MODE_AUTO:
  1081. default:
  1082. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  1083. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1084. found_bss = iter_bss;
  1085. }
  1086. break;
  1087. }
  1088. }
  1089. out:
  1090. mutex_unlock(&adapter->lock);
  1091. return found_bss;
  1092. }
  1093. /**
  1094. * @brief This function finds the best SSID in the Scan List
  1095. *
  1096. * Search the scan table for the best SSID that also matches the current
  1097. * adapter network preference (infrastructure or adhoc)
  1098. *
  1099. * @param adapter A pointer to wlan_adapter
  1100. *
  1101. * @return index in BSSID list
  1102. */
  1103. struct bss_descriptor * libertas_find_best_ssid_in_list(wlan_adapter * adapter,
  1104. u8 mode)
  1105. {
  1106. u8 bestrssi = 0;
  1107. struct bss_descriptor * iter_bss;
  1108. struct bss_descriptor * best_bss = NULL;
  1109. mutex_lock(&adapter->lock);
  1110. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1111. switch (mode) {
  1112. case IW_MODE_INFRA:
  1113. case IW_MODE_ADHOC:
  1114. if (!is_network_compatible(adapter, iter_bss, mode))
  1115. break;
  1116. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  1117. break;
  1118. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1119. best_bss = iter_bss;
  1120. break;
  1121. case IW_MODE_AUTO:
  1122. default:
  1123. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  1124. break;
  1125. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1126. best_bss = iter_bss;
  1127. break;
  1128. }
  1129. }
  1130. mutex_unlock(&adapter->lock);
  1131. return best_bss;
  1132. }
  1133. /**
  1134. * @brief Find the AP with specific ssid in the scan list
  1135. *
  1136. * @param priv A pointer to wlan_private structure
  1137. * @param pSSID A pointer to AP's ssid
  1138. *
  1139. * @return 0--success, otherwise--fail
  1140. */
  1141. int libertas_find_best_network_ssid(wlan_private * priv,
  1142. u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
  1143. {
  1144. wlan_adapter *adapter = priv->adapter;
  1145. int ret = -1;
  1146. struct bss_descriptor * found;
  1147. lbs_deb_enter(LBS_DEB_ASSOC);
  1148. wlan_scan_networks(priv, NULL, 1);
  1149. if (adapter->surpriseremoved)
  1150. return -1;
  1151. wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
  1152. found = libertas_find_best_ssid_in_list(adapter, preferred_mode);
  1153. if (found && (found->ssid_len > 0)) {
  1154. memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
  1155. *out_ssid_len = found->ssid_len;
  1156. *out_mode = found->mode;
  1157. ret = 0;
  1158. }
  1159. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1160. return ret;
  1161. }
  1162. /**
  1163. * @brief Scan Network
  1164. *
  1165. * @param dev A pointer to net_device structure
  1166. * @param info A pointer to iw_request_info structure
  1167. * @param vwrq A pointer to iw_param structure
  1168. * @param extra A pointer to extra data buf
  1169. *
  1170. * @return 0 --success, otherwise fail
  1171. */
  1172. int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
  1173. struct iw_param *vwrq, char *extra)
  1174. {
  1175. wlan_private *priv = dev->priv;
  1176. wlan_adapter *adapter = priv->adapter;
  1177. lbs_deb_enter(LBS_DEB_SCAN);
  1178. wlan_scan_networks(priv, NULL, 0);
  1179. if (adapter->surpriseremoved)
  1180. return -1;
  1181. lbs_deb_leave(LBS_DEB_SCAN);
  1182. return 0;
  1183. }
  1184. /**
  1185. * @brief Send a scan command for all available channels filtered on a spec
  1186. *
  1187. * @param priv A pointer to wlan_private structure
  1188. * @param prequestedssid A pointer to AP's ssid
  1189. * @param keeppreviousscan Flag used to save/clear scan table before scan
  1190. *
  1191. * @return 0-success, otherwise fail
  1192. */
  1193. int libertas_send_specific_ssid_scan(wlan_private * priv,
  1194. u8 *ssid, u8 ssid_len, u8 clear_ssid)
  1195. {
  1196. wlan_adapter *adapter = priv->adapter;
  1197. struct wlan_ioctl_user_scan_cfg scancfg;
  1198. int ret = 0;
  1199. lbs_deb_enter(LBS_DEB_ASSOC);
  1200. if (!ssid_len)
  1201. goto out;
  1202. memset(&scancfg, 0x00, sizeof(scancfg));
  1203. memcpy(scancfg.ssid, ssid, ssid_len);
  1204. scancfg.ssid_len = ssid_len;
  1205. scancfg.clear_ssid = clear_ssid;
  1206. wlan_scan_networks(priv, &scancfg, 1);
  1207. if (adapter->surpriseremoved)
  1208. return -1;
  1209. wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
  1210. out:
  1211. lbs_deb_leave(LBS_DEB_ASSOC);
  1212. return ret;
  1213. }
  1214. /**
  1215. * @brief scan an AP with specific BSSID
  1216. *
  1217. * @param priv A pointer to wlan_private structure
  1218. * @param bssid A pointer to AP's bssid
  1219. * @param keeppreviousscan Flag used to save/clear scan table before scan
  1220. *
  1221. * @return 0-success, otherwise fail
  1222. */
  1223. int libertas_send_specific_bssid_scan(wlan_private * priv, u8 * bssid, u8 clear_bssid)
  1224. {
  1225. struct wlan_ioctl_user_scan_cfg scancfg;
  1226. lbs_deb_enter(LBS_DEB_ASSOC);
  1227. if (bssid == NULL)
  1228. goto out;
  1229. memset(&scancfg, 0x00, sizeof(scancfg));
  1230. memcpy(scancfg.bssid, bssid, ETH_ALEN);
  1231. scancfg.clear_bssid = clear_bssid;
  1232. wlan_scan_networks(priv, &scancfg, 1);
  1233. if (priv->adapter->surpriseremoved)
  1234. return -1;
  1235. wait_event_interruptible(priv->adapter->cmd_pending,
  1236. !priv->adapter->nr_cmd_pending);
  1237. out:
  1238. lbs_deb_leave(LBS_DEB_ASSOC);
  1239. return 0;
  1240. }
  1241. static inline char *libertas_translate_scan(wlan_private *priv,
  1242. char *start, char *stop,
  1243. struct bss_descriptor *bss)
  1244. {
  1245. wlan_adapter *adapter = priv->adapter;
  1246. struct chan_freq_power *cfp;
  1247. char *current_val; /* For rates */
  1248. struct iw_event iwe; /* Temporary buffer */
  1249. int j;
  1250. #define PERFECT_RSSI ((u8)50)
  1251. #define WORST_RSSI ((u8)0)
  1252. #define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
  1253. u8 rssi;
  1254. cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
  1255. if (!cfp) {
  1256. lbs_deb_scan("Invalid channel number %d\n", bss->channel);
  1257. return NULL;
  1258. }
  1259. /* First entry *MUST* be the AP BSSID */
  1260. iwe.cmd = SIOCGIWAP;
  1261. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1262. memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
  1263. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
  1264. /* SSID */
  1265. iwe.cmd = SIOCGIWESSID;
  1266. iwe.u.data.flags = 1;
  1267. iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
  1268. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  1269. /* Mode */
  1270. iwe.cmd = SIOCGIWMODE;
  1271. iwe.u.mode = bss->mode;
  1272. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
  1273. /* Frequency */
  1274. iwe.cmd = SIOCGIWFREQ;
  1275. iwe.u.freq.m = (long)cfp->freq * 100000;
  1276. iwe.u.freq.e = 1;
  1277. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
  1278. /* Add quality statistics */
  1279. iwe.cmd = IWEVQUAL;
  1280. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
  1281. iwe.u.qual.level = SCAN_RSSI(bss->rssi);
  1282. rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1283. iwe.u.qual.qual =
  1284. (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
  1285. (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
  1286. (RSSI_DIFF * RSSI_DIFF);
  1287. if (iwe.u.qual.qual > 100)
  1288. iwe.u.qual.qual = 100;
  1289. if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
  1290. iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1291. } else {
  1292. iwe.u.qual.noise =
  1293. CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
  1294. }
  1295. /* Locally created ad-hoc BSSs won't have beacons if this is the
  1296. * only station in the adhoc network; so get signal strength
  1297. * from receive statistics.
  1298. */
  1299. if ((adapter->mode == IW_MODE_ADHOC)
  1300. && adapter->adhoccreate
  1301. && !libertas_ssid_cmp(adapter->curbssparams.ssid,
  1302. adapter->curbssparams.ssid_len,
  1303. bss->ssid, bss->ssid_len)) {
  1304. int snr, nf;
  1305. snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  1306. nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  1307. iwe.u.qual.level = CAL_RSSI(snr, nf);
  1308. }
  1309. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
  1310. /* Add encryption capability */
  1311. iwe.cmd = SIOCGIWENCODE;
  1312. if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
  1313. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1314. } else {
  1315. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1316. }
  1317. iwe.u.data.length = 0;
  1318. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  1319. current_val = start + IW_EV_LCP_LEN;
  1320. iwe.cmd = SIOCGIWRATE;
  1321. iwe.u.bitrate.fixed = 0;
  1322. iwe.u.bitrate.disabled = 0;
  1323. iwe.u.bitrate.value = 0;
  1324. for (j = 0; j < sizeof(bss->libertas_supported_rates); j++) {
  1325. u8 rate = bss->libertas_supported_rates[j];
  1326. if (rate == 0)
  1327. break; /* no more rates */
  1328. /* Bit rate given in 500 kb/s units (+ 0x80) */
  1329. iwe.u.bitrate.value = (rate & 0x7f) * 500000;
  1330. current_val = iwe_stream_add_value(start, current_val,
  1331. stop, &iwe, IW_EV_PARAM_LEN);
  1332. }
  1333. if ((bss->mode == IW_MODE_ADHOC)
  1334. && !libertas_ssid_cmp(adapter->curbssparams.ssid,
  1335. adapter->curbssparams.ssid_len,
  1336. bss->ssid, bss->ssid_len)
  1337. && adapter->adhoccreate) {
  1338. iwe.u.bitrate.value = 22 * 500000;
  1339. current_val = iwe_stream_add_value(start, current_val,
  1340. stop, &iwe, IW_EV_PARAM_LEN);
  1341. }
  1342. /* Check if we added any event */
  1343. if((current_val - start) > IW_EV_LCP_LEN)
  1344. start = current_val;
  1345. memset(&iwe, 0, sizeof(iwe));
  1346. if (bss->wpa_ie_len) {
  1347. char buf[MAX_WPA_IE_LEN];
  1348. memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
  1349. iwe.cmd = IWEVGENIE;
  1350. iwe.u.data.length = bss->wpa_ie_len;
  1351. start = iwe_stream_add_point(start, stop, &iwe, buf);
  1352. }
  1353. memset(&iwe, 0, sizeof(iwe));
  1354. if (bss->rsn_ie_len) {
  1355. char buf[MAX_WPA_IE_LEN];
  1356. memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
  1357. iwe.cmd = IWEVGENIE;
  1358. iwe.u.data.length = bss->rsn_ie_len;
  1359. start = iwe_stream_add_point(start, stop, &iwe, buf);
  1360. }
  1361. return start;
  1362. }
  1363. /**
  1364. * @brief Retrieve the scan table entries via wireless tools IOCTL call
  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 libertas_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. wlan_private *priv = dev->priv;
  1378. wlan_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_ASSOC);
  1385. /* If we've got an uncompleted scan, schedule the next part */
  1386. if (!adapter->nr_cmd_pending && adapter->last_scanned_channel)
  1387. wlan_scan_networks(priv, NULL, 0);
  1388. /* Update RSSI if current BSS is a locally created ad-hoc BSS */
  1389. if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
  1390. libertas_prepare_and_send_command(priv, cmd_802_11_rssi, 0,
  1391. cmd_option_waitforrsp, 0, NULL);
  1392. }
  1393. mutex_lock(&adapter->lock);
  1394. list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
  1395. char * next_ev;
  1396. unsigned long stale_time;
  1397. if (stop - ev < SCAN_ITEM_SIZE) {
  1398. err = -E2BIG;
  1399. break;
  1400. }
  1401. /* For mesh device, list only mesh networks */
  1402. if (dev == priv->mesh_dev && !iter_bss->mesh)
  1403. continue;
  1404. /* Prune old an old scan result */
  1405. stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1406. if (time_after(jiffies, stale_time)) {
  1407. list_move_tail (&iter_bss->list,
  1408. &adapter->network_free_list);
  1409. clear_bss_descriptor(iter_bss);
  1410. continue;
  1411. }
  1412. /* Translate to WE format this entry */
  1413. next_ev = libertas_translate_scan(priv, ev, stop, iter_bss);
  1414. if (next_ev == NULL)
  1415. continue;
  1416. ev = next_ev;
  1417. }
  1418. mutex_unlock(&adapter->lock);
  1419. dwrq->length = (ev - extra);
  1420. dwrq->flags = 0;
  1421. lbs_deb_leave(LBS_DEB_ASSOC);
  1422. return err;
  1423. }
  1424. /**
  1425. * @brief Prepare a scan command to be sent to the firmware
  1426. *
  1427. * Use the wlan_scan_cmd_config sent to the command processing module in
  1428. * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
  1429. * struct to send to firmware.
  1430. *
  1431. * The fixed fields specifying the BSS type and BSSID filters as well as a
  1432. * variable number/length of TLVs are sent in the command to firmware.
  1433. *
  1434. * @param priv A pointer to wlan_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 wlan_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. * @sa wlan_scan_create_channel_list
  1443. */
  1444. int libertas_cmd_80211_scan(wlan_private * priv,
  1445. struct cmd_ds_command *cmd, void *pdata_buf)
  1446. {
  1447. struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
  1448. struct wlan_scan_cmd_config *pscancfg;
  1449. lbs_deb_enter(LBS_DEB_ASSOC);
  1450. pscancfg = pdata_buf;
  1451. /* Set fixed field variables in scan command */
  1452. pscan->bsstype = pscancfg->bsstype;
  1453. memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
  1454. memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
  1455. cmd->command = cpu_to_le16(cmd_802_11_scan);
  1456. /* size is equal to the sizeof(fixed portions) + the TLV len + header */
  1457. cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
  1458. + pscancfg->tlvbufferlen + S_DS_GEN);
  1459. lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
  1460. le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
  1461. le16_to_cpu(cmd->seqnum));
  1462. lbs_deb_leave(LBS_DEB_ASSOC);
  1463. return 0;
  1464. }
  1465. static inline int is_same_network(struct bss_descriptor *src,
  1466. struct bss_descriptor *dst)
  1467. {
  1468. /* A network is only a duplicate if the channel, BSSID, and ESSID
  1469. * all match. We treat all <hidden> with the same BSSID and channel
  1470. * as one network */
  1471. return ((src->ssid_len == dst->ssid_len) &&
  1472. (src->channel == dst->channel) &&
  1473. !compare_ether_addr(src->bssid, dst->bssid) &&
  1474. !memcmp(src->ssid, dst->ssid, src->ssid_len));
  1475. }
  1476. /**
  1477. * @brief This function handles the command response of scan
  1478. *
  1479. * The response buffer for the scan command has the following
  1480. * memory layout:
  1481. *
  1482. * .-----------------------------------------------------------.
  1483. * | header (4 * sizeof(u16)): Standard command response hdr |
  1484. * .-----------------------------------------------------------.
  1485. * | bufsize (u16) : sizeof the BSS Description data |
  1486. * .-----------------------------------------------------------.
  1487. * | NumOfSet (u8) : Number of BSS Descs returned |
  1488. * .-----------------------------------------------------------.
  1489. * | BSSDescription data (variable, size given in bufsize) |
  1490. * .-----------------------------------------------------------.
  1491. * | TLV data (variable, size calculated using header->size, |
  1492. * | bufsize and sizeof the fixed fields above) |
  1493. * .-----------------------------------------------------------.
  1494. *
  1495. * @param priv A pointer to wlan_private structure
  1496. * @param resp A pointer to cmd_ds_command
  1497. *
  1498. * @return 0 or -1
  1499. */
  1500. int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
  1501. {
  1502. wlan_adapter *adapter = priv->adapter;
  1503. struct cmd_ds_802_11_scan_rsp *pscan;
  1504. struct mrvlietypes_data *ptlv;
  1505. struct mrvlietypes_tsftimestamp *ptsftlv;
  1506. struct bss_descriptor * iter_bss;
  1507. struct bss_descriptor * safe;
  1508. u8 *pbssinfo;
  1509. u16 scanrespsize;
  1510. int bytesleft;
  1511. int idx;
  1512. int tlvbufsize;
  1513. int ret;
  1514. lbs_deb_enter(LBS_DEB_ASSOC);
  1515. /* Prune old entries from scan table */
  1516. list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
  1517. unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1518. if (time_before(jiffies, stale_time))
  1519. continue;
  1520. list_move_tail (&iter_bss->list, &adapter->network_free_list);
  1521. clear_bss_descriptor(iter_bss);
  1522. }
  1523. pscan = &resp->params.scanresp;
  1524. if (pscan->nr_sets > MAX_NETWORK_COUNT) {
  1525. lbs_deb_scan(
  1526. "SCAN_RESP: too many scan results (%d, max %d)!!\n",
  1527. pscan->nr_sets, MAX_NETWORK_COUNT);
  1528. ret = -1;
  1529. goto done;
  1530. }
  1531. bytesleft = le16_to_cpu(pscan->bssdescriptsize);
  1532. lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
  1533. scanrespsize = le16_to_cpu(resp->size);
  1534. lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
  1535. pscan->nr_sets);
  1536. pbssinfo = pscan->bssdesc_and_tlvbuffer;
  1537. /* The size of the TLV buffer is equal to the entire command response
  1538. * size (scanrespsize) minus the fixed fields (sizeof()'s), the
  1539. * BSS Descriptions (bssdescriptsize as bytesLef) and the command
  1540. * response header (S_DS_GEN)
  1541. */
  1542. tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
  1543. + sizeof(pscan->nr_sets)
  1544. + S_DS_GEN);
  1545. ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);
  1546. /* Search the TLV buffer space in the scan response for any valid TLVs */
  1547. wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);
  1548. /*
  1549. * Process each scan response returned (pscan->nr_sets). Save
  1550. * the information in the newbssentry and then insert into the
  1551. * driver scan table either as an update to an existing entry
  1552. * or as an addition at the end of the table
  1553. */
  1554. for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
  1555. struct bss_descriptor new;
  1556. struct bss_descriptor * found = NULL;
  1557. struct bss_descriptor * oldest = NULL;
  1558. /* Process the data fields and IEs returned for this BSS */
  1559. memset(&new, 0, sizeof (struct bss_descriptor));
  1560. if (libertas_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
  1561. /* error parsing the scan response, skipped */
  1562. lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
  1563. continue;
  1564. }
  1565. /* Try to find this bss in the scan table */
  1566. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1567. if (is_same_network(iter_bss, &new)) {
  1568. found = iter_bss;
  1569. break;
  1570. }
  1571. if ((oldest == NULL) ||
  1572. (iter_bss->last_scanned < oldest->last_scanned))
  1573. oldest = iter_bss;
  1574. }
  1575. if (found) {
  1576. /* found, clear it */
  1577. clear_bss_descriptor(found);
  1578. } else if (!list_empty(&adapter->network_free_list)) {
  1579. /* Pull one from the free list */
  1580. found = list_entry(adapter->network_free_list.next,
  1581. struct bss_descriptor, list);
  1582. list_move_tail(&found->list, &adapter->network_list);
  1583. } else if (oldest) {
  1584. /* If there are no more slots, expire the oldest */
  1585. found = oldest;
  1586. clear_bss_descriptor(found);
  1587. list_move_tail(&found->list, &adapter->network_list);
  1588. } else {
  1589. continue;
  1590. }
  1591. lbs_deb_scan("SCAN_RESP: BSSID = " MAC_FMT "\n",
  1592. new.bssid[0], new.bssid[1], new.bssid[2],
  1593. new.bssid[3], new.bssid[4], new.bssid[5]);
  1594. /*
  1595. * If the TSF TLV was appended to the scan results, save the
  1596. * this entries TSF value in the networktsf field. The
  1597. * networktsf is the firmware's TSF value at the time the
  1598. * beacon or probe response was received.
  1599. */
  1600. if (ptsftlv) {
  1601. new.networktsf = le64_to_cpup(&ptsftlv->tsftable[idx]);
  1602. }
  1603. /* Copy the locally created newbssentry to the scan table */
  1604. memcpy(found, &new, offsetof(struct bss_descriptor, list));
  1605. }
  1606. ret = 0;
  1607. done:
  1608. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1609. return ret;
  1610. }