scan.c 55 KB

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