scan.c 54 KB

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