scan.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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 :
  374. adapter->scanprobes);
  375. /*
  376. * Set the BSSID filter to the incoming configuration,
  377. * if non-zero. If not set, it will remain disabled (all zeros).
  378. */
  379. memcpy(pscancfgout->bssid, puserscanin->bssid,
  380. sizeof(pscancfgout->bssid));
  381. if (puserscanin->ssid_len) {
  382. pssidtlv =
  383. (struct mrvlietypes_ssidparamset *) pscancfgout->
  384. tlvbuffer;
  385. pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
  386. pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
  387. memcpy(pssidtlv->ssid, puserscanin->ssid,
  388. puserscanin->ssid_len);
  389. ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
  390. }
  391. /*
  392. * The default number of channels sent in the command is low to
  393. * ensure the response buffer from the firmware does not truncate
  394. * scan results. That is not an issue with an SSID or BSSID
  395. * filter applied to the scan results in the firmware.
  396. */
  397. if ( puserscanin->ssid_len
  398. || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
  399. *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
  400. *pfilteredscan = 1;
  401. }
  402. } else {
  403. pscancfgout->bsstype = adapter->scanmode;
  404. numprobes = adapter->scanprobes;
  405. }
  406. /* If the input config or adapter has the number of Probes set, add tlv */
  407. if (numprobes) {
  408. pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
  409. pnumprobestlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
  410. pnumprobestlv->header.len = cpu_to_le16(2);
  411. pnumprobestlv->numprobes = cpu_to_le16(numprobes);
  412. ptlvpos += sizeof(*pnumprobestlv);
  413. }
  414. /*
  415. * Set the output for the channel TLV to the address in the tlv buffer
  416. * past any TLVs that were added in this fuction (SSID, numprobes).
  417. * channel TLVs will be added past this for each scan command, preserving
  418. * the TLVs that were previously added.
  419. */
  420. *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
  421. if (puserscanin && puserscanin->chanlist[0].channumber) {
  422. lbs_deb_scan("Scan: Using supplied channel list\n");
  423. for (chanidx = 0;
  424. chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
  425. && puserscanin->chanlist[chanidx].channumber; chanidx++) {
  426. channel = puserscanin->chanlist[chanidx].channumber;
  427. (pscanchanlist + chanidx)->channumber = channel;
  428. radiotype = puserscanin->chanlist[chanidx].radiotype;
  429. (pscanchanlist + chanidx)->radiotype = radiotype;
  430. scantype = puserscanin->chanlist[chanidx].scantype;
  431. if (scantype == CMD_SCAN_TYPE_PASSIVE) {
  432. (pscanchanlist +
  433. chanidx)->chanscanmode.passivescan = 1;
  434. } else {
  435. (pscanchanlist +
  436. chanidx)->chanscanmode.passivescan = 0;
  437. }
  438. if (puserscanin->chanlist[chanidx].scantime) {
  439. scandur =
  440. puserscanin->chanlist[chanidx].scantime;
  441. } else {
  442. if (scantype == CMD_SCAN_TYPE_PASSIVE) {
  443. scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
  444. } else {
  445. scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
  446. }
  447. }
  448. (pscanchanlist + chanidx)->minscantime =
  449. cpu_to_le16(scandur);
  450. (pscanchanlist + chanidx)->maxscantime =
  451. cpu_to_le16(scandur);
  452. }
  453. /* Check if we are only scanning the current channel */
  454. if ((chanidx == 1) && (puserscanin->chanlist[0].channumber
  455. ==
  456. priv->adapter->curbssparams.channel)) {
  457. *pscancurrentonly = 1;
  458. lbs_deb_scan("Scan: Scanning current channel only");
  459. }
  460. } else {
  461. lbs_deb_scan("Scan: Creating full region channel list\n");
  462. wlan_scan_create_channel_list(priv, pscanchanlist,
  463. *pfilteredscan);
  464. }
  465. out:
  466. return pscancfgout;
  467. }
  468. /**
  469. * @brief Construct and send multiple scan config commands to the firmware
  470. *
  471. * Previous routines have created a wlan_scan_cmd_config with any requested
  472. * TLVs. This function splits the channel TLV into maxchanperscan lists
  473. * and sends the portion of the channel TLV along with the other TLVs
  474. * to the wlan_cmd routines for execution in the firmware.
  475. *
  476. * @param priv A pointer to wlan_private structure
  477. * @param maxchanperscan Maximum number channels to be included in each
  478. * scan command sent to firmware
  479. * @param filteredscan Flag indicating whether or not a BSSID or SSID
  480. * filter is being used for the firmware command
  481. * scan command sent to firmware
  482. * @param pscancfgout Scan configuration used for this scan.
  483. * @param pchantlvout Pointer in the pscancfgout where the channel TLV
  484. * should start. This is past any other TLVs that
  485. * must be sent down in each firmware command.
  486. * @param pscanchanlist List of channels to scan in maxchanperscan segments
  487. *
  488. * @return 0 or error return otherwise
  489. */
  490. static int wlan_scan_channel_list(wlan_private * priv,
  491. int maxchanperscan,
  492. u8 filteredscan,
  493. struct wlan_scan_cmd_config * pscancfgout,
  494. struct mrvlietypes_chanlistparamset * pchantlvout,
  495. struct chanscanparamset * pscanchanlist,
  496. const struct wlan_ioctl_user_scan_cfg * puserscanin,
  497. int full_scan)
  498. {
  499. struct chanscanparamset *ptmpchan;
  500. struct chanscanparamset *pstartchan;
  501. u8 scanband;
  502. int doneearly;
  503. int tlvidx;
  504. int ret = 0;
  505. int scanned = 0;
  506. union iwreq_data wrqu;
  507. lbs_deb_enter(LBS_DEB_ASSOC);
  508. if (pscancfgout == 0 || pchantlvout == 0 || pscanchanlist == 0) {
  509. lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
  510. pscancfgout, pchantlvout, pscanchanlist);
  511. return -1;
  512. }
  513. pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
  514. /* Set the temp channel struct pointer to the start of the desired list */
  515. ptmpchan = pscanchanlist;
  516. if (priv->adapter->last_scanned_channel && !puserscanin)
  517. ptmpchan += priv->adapter->last_scanned_channel;
  518. /* Loop through the desired channel list, sending a new firmware scan
  519. * commands for each maxchanperscan channels (or for 1,6,11 individually
  520. * if configured accordingly)
  521. */
  522. while (ptmpchan->channumber) {
  523. tlvidx = 0;
  524. pchantlvout->header.len = 0;
  525. scanband = ptmpchan->radiotype;
  526. pstartchan = ptmpchan;
  527. doneearly = 0;
  528. /* Construct the channel TLV for the scan command. Continue to
  529. * insert channel TLVs until:
  530. * - the tlvidx hits the maximum configured per scan command
  531. * - the next channel to insert is 0 (end of desired channel list)
  532. * - doneearly is set (controlling individual scanning of 1,6,11)
  533. */
  534. while (tlvidx < maxchanperscan && ptmpchan->channumber
  535. && !doneearly && scanned < 2) {
  536. lbs_deb_scan(
  537. "Scan: Chan(%3d), Radio(%d), mode(%d,%d), Dur(%d)\n",
  538. ptmpchan->channumber, ptmpchan->radiotype,
  539. ptmpchan->chanscanmode.passivescan,
  540. ptmpchan->chanscanmode.disablechanfilt,
  541. ptmpchan->maxscantime);
  542. /* Copy the current channel TLV to the command being prepared */
  543. memcpy(pchantlvout->chanscanparam + tlvidx,
  544. ptmpchan, sizeof(pchantlvout->chanscanparam));
  545. /* Increment the TLV header length by the size appended */
  546. /* Ew, it would be _so_ nice if we could just declare the
  547. variable little-endian and let GCC handle it for us */
  548. pchantlvout->header.len =
  549. cpu_to_le16(le16_to_cpu(pchantlvout->header.len) +
  550. sizeof(pchantlvout->chanscanparam));
  551. /*
  552. * The tlv buffer length is set to the number of bytes of the
  553. * between the channel tlv pointer and the start of the
  554. * tlv buffer. This compensates for any TLVs that were appended
  555. * before the channel list.
  556. */
  557. pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
  558. - pscancfgout->tlvbuffer);
  559. /* Add the size of the channel tlv header and the data length */
  560. pscancfgout->tlvbufferlen +=
  561. (sizeof(pchantlvout->header)
  562. + le16_to_cpu(pchantlvout->header.len));
  563. /* Increment the index to the channel tlv we are constructing */
  564. tlvidx++;
  565. doneearly = 0;
  566. /* Stop the loop if the *current* channel is in the 1,6,11 set
  567. * and we are not filtering on a BSSID or SSID.
  568. */
  569. if (!filteredscan && (ptmpchan->channumber == 1
  570. || ptmpchan->channumber == 6
  571. || ptmpchan->channumber == 11)) {
  572. doneearly = 1;
  573. }
  574. /* Increment the tmp pointer to the next channel to be scanned */
  575. ptmpchan++;
  576. scanned++;
  577. /* Stop the loop if the *next* channel is in the 1,6,11 set.
  578. * This will cause it to be the only channel scanned on the next
  579. * interation
  580. */
  581. if (!filteredscan && (ptmpchan->channumber == 1
  582. || ptmpchan->channumber == 6
  583. || ptmpchan->channumber == 11)) {
  584. doneearly = 1;
  585. }
  586. }
  587. /* Send the scan command to the firmware with the specified cfg */
  588. ret = libertas_prepare_and_send_command(priv, CMD_802_11_SCAN, 0,
  589. 0, 0, pscancfgout);
  590. if (scanned >= 2 && !full_scan) {
  591. ret = 0;
  592. goto done;
  593. }
  594. scanned = 0;
  595. }
  596. done:
  597. priv->adapter->last_scanned_channel = ptmpchan->channumber;
  598. /* Tell userspace the scan table has been updated */
  599. memset(&wrqu, 0, sizeof(union iwreq_data));
  600. wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
  601. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  602. return ret;
  603. }
  604. static void
  605. clear_selected_scan_list_entries(wlan_adapter * adapter,
  606. const struct wlan_ioctl_user_scan_cfg * scan_cfg)
  607. {
  608. struct bss_descriptor * bss;
  609. struct bss_descriptor * safe;
  610. u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
  611. if (!scan_cfg)
  612. return;
  613. if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
  614. clear_ssid_flag = 1;
  615. if (scan_cfg->clear_bssid
  616. && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
  617. && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
  618. clear_bssid_flag = 1;
  619. }
  620. if (!clear_ssid_flag && !clear_bssid_flag)
  621. return;
  622. mutex_lock(&adapter->lock);
  623. list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
  624. u32 clear = 0;
  625. /* Check for an SSID match */
  626. if ( clear_ssid_flag
  627. && (bss->ssid_len == scan_cfg->ssid_len)
  628. && !memcmp(bss->ssid, scan_cfg->ssid, bss->ssid_len))
  629. clear = 1;
  630. /* Check for a BSSID match */
  631. if ( clear_bssid_flag
  632. && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
  633. clear = 1;
  634. if (clear) {
  635. list_move_tail (&bss->list, &adapter->network_free_list);
  636. clear_bss_descriptor(bss);
  637. }
  638. }
  639. mutex_unlock(&adapter->lock);
  640. }
  641. /**
  642. * @brief Internal function used to start a scan based on an input config
  643. *
  644. * Use the input user scan configuration information when provided in
  645. * order to send the appropriate scan commands to firmware to populate or
  646. * update the internal driver scan table
  647. *
  648. * @param priv A pointer to wlan_private structure
  649. * @param puserscanin Pointer to the input configuration for the requested
  650. * scan.
  651. *
  652. * @return 0 or < 0 if error
  653. */
  654. int wlan_scan_networks(wlan_private * priv,
  655. const struct wlan_ioctl_user_scan_cfg * puserscanin,
  656. int full_scan)
  657. {
  658. wlan_adapter * adapter = priv->adapter;
  659. struct mrvlietypes_chanlistparamset *pchantlvout;
  660. struct chanscanparamset * scan_chan_list = NULL;
  661. struct wlan_scan_cmd_config * scan_cfg = NULL;
  662. u8 filteredscan;
  663. u8 scancurrentchanonly;
  664. int maxchanperscan;
  665. int ret;
  666. #ifdef CONFIG_LIBERTAS_DEBUG
  667. struct bss_descriptor * iter_bss;
  668. int i = 0;
  669. #endif
  670. lbs_deb_enter(LBS_DEB_ASSOC);
  671. scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
  672. WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
  673. if (scan_chan_list == NULL) {
  674. ret = -ENOMEM;
  675. goto out;
  676. }
  677. scan_cfg = wlan_scan_setup_scan_config(priv,
  678. puserscanin,
  679. &pchantlvout,
  680. scan_chan_list,
  681. &maxchanperscan,
  682. &filteredscan,
  683. &scancurrentchanonly);
  684. if (scan_cfg == NULL) {
  685. ret = -ENOMEM;
  686. goto out;
  687. }
  688. clear_selected_scan_list_entries(adapter, puserscanin);
  689. /* Keep the data path active if we are only scanning our current channel */
  690. if (!scancurrentchanonly) {
  691. netif_stop_queue(priv->dev);
  692. netif_carrier_off(priv->dev);
  693. netif_stop_queue(priv->mesh_dev);
  694. netif_carrier_off(priv->mesh_dev);
  695. }
  696. ret = wlan_scan_channel_list(priv,
  697. maxchanperscan,
  698. filteredscan,
  699. scan_cfg,
  700. pchantlvout,
  701. scan_chan_list,
  702. puserscanin,
  703. full_scan);
  704. #ifdef CONFIG_LIBERTAS_DEBUG
  705. /* Dump the scan table */
  706. mutex_lock(&adapter->lock);
  707. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  708. lbs_deb_scan("Scan:(%02d) " MAC_FMT ", RSSI[%03d], SSID[%s]\n",
  709. i++, MAC_ARG(iter_bss->bssid), (s32) iter_bss->rssi,
  710. escape_essid(iter_bss->ssid, iter_bss->ssid_len));
  711. }
  712. mutex_unlock(&adapter->lock);
  713. #endif
  714. if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
  715. netif_carrier_on(priv->dev);
  716. netif_wake_queue(priv->dev);
  717. netif_carrier_on(priv->mesh_dev);
  718. netif_wake_queue(priv->mesh_dev);
  719. }
  720. out:
  721. if (scan_cfg)
  722. kfree(scan_cfg);
  723. if (scan_chan_list)
  724. kfree(scan_chan_list);
  725. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  726. return ret;
  727. }
  728. /**
  729. * @brief Inspect the scan response buffer for pointers to expected TLVs
  730. *
  731. * TLVs can be included at the end of the scan response BSS information.
  732. * Parse the data in the buffer for pointers to TLVs that can potentially
  733. * be passed back in the response
  734. *
  735. * @param ptlv Pointer to the start of the TLV buffer to parse
  736. * @param tlvbufsize size of the TLV buffer
  737. * @param ptsftlv Output parameter: Pointer to the TSF TLV if found
  738. *
  739. * @return void
  740. */
  741. static
  742. void wlan_ret_802_11_scan_get_tlv_ptrs(struct mrvlietypes_data * ptlv,
  743. int tlvbufsize,
  744. struct mrvlietypes_tsftimestamp ** ptsftlv)
  745. {
  746. struct mrvlietypes_data *pcurrenttlv;
  747. int tlvbufleft;
  748. u16 tlvtype;
  749. u16 tlvlen;
  750. pcurrenttlv = ptlv;
  751. tlvbufleft = tlvbufsize;
  752. *ptsftlv = NULL;
  753. lbs_deb_scan("SCAN_RESP: tlvbufsize = %d\n", tlvbufsize);
  754. lbs_dbg_hex("SCAN_RESP: TLV Buf", (u8 *) ptlv, tlvbufsize);
  755. while (tlvbufleft >= sizeof(struct mrvlietypesheader)) {
  756. tlvtype = le16_to_cpu(pcurrenttlv->header.type);
  757. tlvlen = le16_to_cpu(pcurrenttlv->header.len);
  758. switch (tlvtype) {
  759. case TLV_TYPE_TSFTIMESTAMP:
  760. *ptsftlv = (struct mrvlietypes_tsftimestamp *) pcurrenttlv;
  761. break;
  762. default:
  763. lbs_deb_scan("SCAN_RESP: Unhandled TLV = %d\n",
  764. tlvtype);
  765. /* Give up, this seems corrupted */
  766. return;
  767. } /* switch */
  768. tlvbufleft -= (sizeof(ptlv->header) + tlvlen);
  769. pcurrenttlv =
  770. (struct mrvlietypes_data *) (pcurrenttlv->Data + tlvlen);
  771. } /* while */
  772. }
  773. /**
  774. * @brief Interpret a BSS scan response returned from the firmware
  775. *
  776. * Parse the various fixed fields and IEs passed back for a a BSS probe
  777. * response or beacon from the scan command. Record information as needed
  778. * in the scan table struct bss_descriptor for that entry.
  779. *
  780. * @param bss Output parameter: Pointer to the BSS Entry
  781. *
  782. * @return 0 or -1
  783. */
  784. static int libertas_process_bss(struct bss_descriptor * bss,
  785. u8 ** pbeaconinfo, int *bytesleft)
  786. {
  787. struct ieeetypes_fhparamset *pFH;
  788. struct ieeetypes_dsparamset *pDS;
  789. struct ieeetypes_cfparamset *pCF;
  790. struct ieeetypes_ibssparamset *pibss;
  791. struct ieeetypes_countryinfoset *pcountryinfo;
  792. u8 *pos, *end, *p;
  793. u8 n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
  794. u16 beaconsize = 0;
  795. int ret;
  796. lbs_deb_enter(LBS_DEB_ASSOC);
  797. if (*bytesleft >= sizeof(beaconsize)) {
  798. /* Extract & convert beacon size from the command buffer */
  799. beaconsize = le16_to_cpup((void *)*pbeaconinfo);
  800. *bytesleft -= sizeof(beaconsize);
  801. *pbeaconinfo += sizeof(beaconsize);
  802. }
  803. if (beaconsize == 0 || beaconsize > *bytesleft) {
  804. *pbeaconinfo += *bytesleft;
  805. *bytesleft = 0;
  806. return -1;
  807. }
  808. /* Initialize the current working beacon pointer for this BSS iteration */
  809. pos = *pbeaconinfo;
  810. end = pos + beaconsize;
  811. /* Advance the return beacon pointer past the current beacon */
  812. *pbeaconinfo += beaconsize;
  813. *bytesleft -= beaconsize;
  814. memcpy(bss->bssid, pos, ETH_ALEN);
  815. lbs_deb_scan("process_bss: AP BSSID " MAC_FMT "\n", MAC_ARG(bss->bssid));
  816. pos += ETH_ALEN;
  817. if ((end - pos) < 12) {
  818. lbs_deb_scan("process_bss: Not enough bytes left\n");
  819. return -1;
  820. }
  821. /*
  822. * next 4 fields are RSSI, time stamp, beacon interval,
  823. * and capability information
  824. */
  825. /* RSSI is 1 byte long */
  826. bss->rssi = *pos;
  827. lbs_deb_scan("process_bss: RSSI=%02X\n", *pos);
  828. pos++;
  829. /* time stamp is 8 bytes long */
  830. bss->timestamp = le64_to_cpup((void *) pos);
  831. pos += 8;
  832. /* beacon interval is 2 bytes long */
  833. bss->beaconperiod = le16_to_cpup((void *) pos);
  834. pos += 2;
  835. /* capability information is 2 bytes long */
  836. bss->capability = le16_to_cpup((void *) pos);
  837. lbs_deb_scan("process_bss: capabilities = 0x%4X\n", bss->capability);
  838. pos += 2;
  839. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  840. lbs_deb_scan("process_bss: AP WEP enabled\n");
  841. if (bss->capability & WLAN_CAPABILITY_IBSS)
  842. bss->mode = IW_MODE_ADHOC;
  843. else
  844. bss->mode = IW_MODE_INFRA;
  845. /* rest of the current buffer are IE's */
  846. lbs_deb_scan("process_bss: IE length for this AP = %zd\n", end - pos);
  847. lbs_dbg_hex("process_bss: IE info", pos, end - pos);
  848. /* process variable IE */
  849. while (pos <= end - 2) {
  850. struct ieee80211_info_element * elem =
  851. (struct ieee80211_info_element *) pos;
  852. if (pos + elem->len > end) {
  853. lbs_deb_scan("process_bss: error in processing IE, "
  854. "bytes left < IE length\n");
  855. break;
  856. }
  857. switch (elem->id) {
  858. case MFIE_TYPE_SSID:
  859. bss->ssid_len = elem->len;
  860. memcpy(bss->ssid, elem->data, elem->len);
  861. lbs_deb_scan("ssid '%s', ssid length %u\n",
  862. escape_essid(bss->ssid, bss->ssid_len),
  863. bss->ssid_len);
  864. break;
  865. case MFIE_TYPE_RATES:
  866. n_basic_rates = min_t(u8, MAX_RATES, elem->len);
  867. memcpy(bss->rates, elem->data, n_basic_rates);
  868. got_basic_rates = 1;
  869. break;
  870. case MFIE_TYPE_FH_SET:
  871. pFH = (struct ieeetypes_fhparamset *) pos;
  872. memmove(&bss->phyparamset.fhparamset, pFH,
  873. sizeof(struct ieeetypes_fhparamset));
  874. #if 0 /* I think we can store these LE */
  875. bss->phyparamset.fhparamset.dwelltime
  876. = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
  877. #endif
  878. break;
  879. case MFIE_TYPE_DS_SET:
  880. pDS = (struct ieeetypes_dsparamset *) pos;
  881. bss->channel = pDS->currentchan;
  882. memcpy(&bss->phyparamset.dsparamset, pDS,
  883. sizeof(struct ieeetypes_dsparamset));
  884. break;
  885. case MFIE_TYPE_CF_SET:
  886. pCF = (struct ieeetypes_cfparamset *) pos;
  887. memcpy(&bss->ssparamset.cfparamset, pCF,
  888. sizeof(struct ieeetypes_cfparamset));
  889. break;
  890. case MFIE_TYPE_IBSS_SET:
  891. pibss = (struct ieeetypes_ibssparamset *) pos;
  892. bss->atimwindow = le32_to_cpu(pibss->atimwindow);
  893. memmove(&bss->ssparamset.ibssparamset, pibss,
  894. sizeof(struct ieeetypes_ibssparamset));
  895. #if 0
  896. bss->ssparamset.ibssparamset.atimwindow
  897. = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
  898. #endif
  899. break;
  900. case MFIE_TYPE_COUNTRY:
  901. pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
  902. if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
  903. || pcountryinfo->len > 254) {
  904. lbs_deb_scan("process_bss: 11D- Err "
  905. "CountryInfo len =%d min=%zd max=254\n",
  906. pcountryinfo->len,
  907. sizeof(pcountryinfo->countrycode));
  908. ret = -1;
  909. goto done;
  910. }
  911. memcpy(&bss->countryinfo,
  912. pcountryinfo, pcountryinfo->len + 2);
  913. lbs_dbg_hex("process_bss: 11D- CountryInfo:",
  914. (u8 *) pcountryinfo,
  915. (u32) (pcountryinfo->len + 2));
  916. break;
  917. case MFIE_TYPE_RATES_EX:
  918. /* only process extended supported rate if data rate is
  919. * already found. Data rate IE should come before
  920. * extended supported rate IE
  921. */
  922. if (!got_basic_rates)
  923. break;
  924. n_ex_rates = elem->len;
  925. if (n_basic_rates + n_ex_rates > MAX_RATES)
  926. n_ex_rates = MAX_RATES - n_basic_rates;
  927. p = bss->rates + n_basic_rates;
  928. memcpy(p, elem->data, n_ex_rates);
  929. break;
  930. case MFIE_TYPE_GENERIC:
  931. if (elem->len >= 4 &&
  932. elem->data[0] == 0x00 &&
  933. elem->data[1] == 0x50 &&
  934. elem->data[2] == 0xf2 &&
  935. elem->data[3] == 0x01) {
  936. bss->wpa_ie_len = min(elem->len + 2,
  937. MAX_WPA_IE_LEN);
  938. memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
  939. lbs_dbg_hex("process_bss: WPA IE", bss->wpa_ie,
  940. elem->len);
  941. } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
  942. elem->data[0] == 0x00 &&
  943. elem->data[1] == 0x50 &&
  944. elem->data[2] == 0x43 &&
  945. elem->data[3] == 0x04) {
  946. bss->mesh = 1;
  947. }
  948. break;
  949. case MFIE_TYPE_RSN:
  950. bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
  951. memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
  952. lbs_dbg_hex("process_bss: RSN_IE", bss->rsn_ie, elem->len);
  953. break;
  954. default:
  955. break;
  956. }
  957. pos += elem->len + 2;
  958. }
  959. /* Timestamp */
  960. bss->last_scanned = jiffies;
  961. libertas_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
  962. ret = 0;
  963. done:
  964. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  965. return ret;
  966. }
  967. /**
  968. * @brief Compare two SSIDs
  969. *
  970. * @param ssid1 A pointer to ssid to compare
  971. * @param ssid2 A pointer to ssid to compare
  972. *
  973. * @return 0--ssid is same, otherwise is different
  974. */
  975. int libertas_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
  976. {
  977. if (ssid1_len != ssid2_len)
  978. return -1;
  979. return memcmp(ssid1, ssid2, ssid1_len);
  980. }
  981. /**
  982. * @brief This function finds a specific compatible BSSID in the scan list
  983. *
  984. * @param adapter A pointer to wlan_adapter
  985. * @param bssid BSSID to find in the scan list
  986. * @param mode Network mode: Infrastructure or IBSS
  987. *
  988. * @return index in BSSID list, or error return code (< 0)
  989. */
  990. struct bss_descriptor * libertas_find_bssid_in_list(wlan_adapter * adapter,
  991. u8 * bssid, u8 mode)
  992. {
  993. struct bss_descriptor * iter_bss;
  994. struct bss_descriptor * found_bss = NULL;
  995. if (!bssid)
  996. return NULL;
  997. lbs_dbg_hex("libertas_find_BSSID_in_list: looking for ",
  998. bssid, ETH_ALEN);
  999. /* Look through the scan table for a compatible match. The loop will
  1000. * continue past a matched bssid that is not compatible in case there
  1001. * is an AP with multiple SSIDs assigned to the same BSSID
  1002. */
  1003. mutex_lock(&adapter->lock);
  1004. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1005. if (compare_ether_addr(iter_bss->bssid, bssid))
  1006. continue; /* bssid doesn't match */
  1007. switch (mode) {
  1008. case IW_MODE_INFRA:
  1009. case IW_MODE_ADHOC:
  1010. if (!is_network_compatible(adapter, iter_bss, mode))
  1011. break;
  1012. found_bss = iter_bss;
  1013. break;
  1014. default:
  1015. found_bss = iter_bss;
  1016. break;
  1017. }
  1018. }
  1019. mutex_unlock(&adapter->lock);
  1020. return found_bss;
  1021. }
  1022. /**
  1023. * @brief This function finds ssid in ssid list.
  1024. *
  1025. * @param adapter A pointer to wlan_adapter
  1026. * @param ssid SSID to find in the list
  1027. * @param bssid BSSID to qualify the SSID selection (if provided)
  1028. * @param mode Network mode: Infrastructure or IBSS
  1029. *
  1030. * @return index in BSSID list
  1031. */
  1032. struct bss_descriptor * libertas_find_ssid_in_list(wlan_adapter * adapter,
  1033. u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
  1034. int channel)
  1035. {
  1036. u8 bestrssi = 0;
  1037. struct bss_descriptor * iter_bss = NULL;
  1038. struct bss_descriptor * found_bss = NULL;
  1039. struct bss_descriptor * tmp_oldest = NULL;
  1040. mutex_lock(&adapter->lock);
  1041. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1042. if ( !tmp_oldest
  1043. || (iter_bss->last_scanned < tmp_oldest->last_scanned))
  1044. tmp_oldest = iter_bss;
  1045. if (libertas_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
  1046. ssid, ssid_len) != 0)
  1047. continue; /* ssid doesn't match */
  1048. if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
  1049. continue; /* bssid doesn't match */
  1050. if ((channel > 0) && (iter_bss->channel != channel))
  1051. continue; /* channel doesn't match */
  1052. switch (mode) {
  1053. case IW_MODE_INFRA:
  1054. case IW_MODE_ADHOC:
  1055. if (!is_network_compatible(adapter, iter_bss, mode))
  1056. break;
  1057. if (bssid) {
  1058. /* Found requested BSSID */
  1059. found_bss = iter_bss;
  1060. goto out;
  1061. }
  1062. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  1063. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1064. found_bss = iter_bss;
  1065. }
  1066. break;
  1067. case IW_MODE_AUTO:
  1068. default:
  1069. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  1070. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1071. found_bss = iter_bss;
  1072. }
  1073. break;
  1074. }
  1075. }
  1076. out:
  1077. mutex_unlock(&adapter->lock);
  1078. return found_bss;
  1079. }
  1080. /**
  1081. * @brief This function finds the best SSID in the Scan List
  1082. *
  1083. * Search the scan table for the best SSID that also matches the current
  1084. * adapter network preference (infrastructure or adhoc)
  1085. *
  1086. * @param adapter A pointer to wlan_adapter
  1087. *
  1088. * @return index in BSSID list
  1089. */
  1090. struct bss_descriptor * libertas_find_best_ssid_in_list(wlan_adapter * adapter,
  1091. u8 mode)
  1092. {
  1093. u8 bestrssi = 0;
  1094. struct bss_descriptor * iter_bss;
  1095. struct bss_descriptor * best_bss = NULL;
  1096. mutex_lock(&adapter->lock);
  1097. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1098. switch (mode) {
  1099. case IW_MODE_INFRA:
  1100. case IW_MODE_ADHOC:
  1101. if (!is_network_compatible(adapter, iter_bss, mode))
  1102. break;
  1103. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  1104. break;
  1105. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1106. best_bss = iter_bss;
  1107. break;
  1108. case IW_MODE_AUTO:
  1109. default:
  1110. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  1111. break;
  1112. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1113. best_bss = iter_bss;
  1114. break;
  1115. }
  1116. }
  1117. mutex_unlock(&adapter->lock);
  1118. return best_bss;
  1119. }
  1120. /**
  1121. * @brief Find the AP with specific ssid in the scan list
  1122. *
  1123. * @param priv A pointer to wlan_private structure
  1124. * @param pSSID A pointer to AP's ssid
  1125. *
  1126. * @return 0--success, otherwise--fail
  1127. */
  1128. int libertas_find_best_network_ssid(wlan_private * priv,
  1129. u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
  1130. {
  1131. wlan_adapter *adapter = priv->adapter;
  1132. int ret = -1;
  1133. struct bss_descriptor * found;
  1134. lbs_deb_enter(LBS_DEB_ASSOC);
  1135. wlan_scan_networks(priv, NULL, 1);
  1136. if (adapter->surpriseremoved)
  1137. return -1;
  1138. wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
  1139. found = libertas_find_best_ssid_in_list(adapter, preferred_mode);
  1140. if (found && (found->ssid_len > 0)) {
  1141. memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
  1142. *out_ssid_len = found->ssid_len;
  1143. *out_mode = found->mode;
  1144. ret = 0;
  1145. }
  1146. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1147. return ret;
  1148. }
  1149. /**
  1150. * @brief Scan Network
  1151. *
  1152. * @param dev A pointer to net_device structure
  1153. * @param info A pointer to iw_request_info structure
  1154. * @param vwrq A pointer to iw_param structure
  1155. * @param extra A pointer to extra data buf
  1156. *
  1157. * @return 0 --success, otherwise fail
  1158. */
  1159. int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
  1160. struct iw_param *vwrq, char *extra)
  1161. {
  1162. wlan_private *priv = dev->priv;
  1163. wlan_adapter *adapter = priv->adapter;
  1164. lbs_deb_enter(LBS_DEB_SCAN);
  1165. wlan_scan_networks(priv, NULL, 0);
  1166. if (adapter->surpriseremoved)
  1167. return -1;
  1168. lbs_deb_leave(LBS_DEB_SCAN);
  1169. return 0;
  1170. }
  1171. /**
  1172. * @brief Send a scan command for all available channels filtered on a spec
  1173. *
  1174. * @param priv A pointer to wlan_private structure
  1175. * @param prequestedssid A pointer to AP's ssid
  1176. * @param keeppreviousscan Flag used to save/clear scan table before scan
  1177. *
  1178. * @return 0-success, otherwise fail
  1179. */
  1180. int libertas_send_specific_ssid_scan(wlan_private * priv,
  1181. u8 *ssid, u8 ssid_len, u8 clear_ssid)
  1182. {
  1183. wlan_adapter *adapter = priv->adapter;
  1184. struct wlan_ioctl_user_scan_cfg scancfg;
  1185. int ret = 0;
  1186. lbs_deb_enter(LBS_DEB_ASSOC);
  1187. if (!ssid_len)
  1188. goto out;
  1189. memset(&scancfg, 0x00, sizeof(scancfg));
  1190. memcpy(scancfg.ssid, ssid, ssid_len);
  1191. scancfg.ssid_len = ssid_len;
  1192. scancfg.clear_ssid = clear_ssid;
  1193. wlan_scan_networks(priv, &scancfg, 1);
  1194. if (adapter->surpriseremoved)
  1195. return -1;
  1196. wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
  1197. out:
  1198. lbs_deb_leave(LBS_DEB_ASSOC);
  1199. return ret;
  1200. }
  1201. /**
  1202. * @brief scan an AP with specific BSSID
  1203. *
  1204. * @param priv A pointer to wlan_private structure
  1205. * @param bssid A pointer to AP's bssid
  1206. * @param keeppreviousscan Flag used to save/clear scan table before scan
  1207. *
  1208. * @return 0-success, otherwise fail
  1209. */
  1210. int libertas_send_specific_bssid_scan(wlan_private * priv, u8 * bssid, u8 clear_bssid)
  1211. {
  1212. struct wlan_ioctl_user_scan_cfg scancfg;
  1213. lbs_deb_enter(LBS_DEB_ASSOC);
  1214. if (bssid == NULL)
  1215. goto out;
  1216. memset(&scancfg, 0x00, sizeof(scancfg));
  1217. memcpy(scancfg.bssid, bssid, ETH_ALEN);
  1218. scancfg.clear_bssid = clear_bssid;
  1219. wlan_scan_networks(priv, &scancfg, 1);
  1220. if (priv->adapter->surpriseremoved)
  1221. return -1;
  1222. wait_event_interruptible(priv->adapter->cmd_pending,
  1223. !priv->adapter->nr_cmd_pending);
  1224. out:
  1225. lbs_deb_leave(LBS_DEB_ASSOC);
  1226. return 0;
  1227. }
  1228. static inline char *libertas_translate_scan(wlan_private *priv,
  1229. char *start, char *stop,
  1230. struct bss_descriptor *bss)
  1231. {
  1232. wlan_adapter *adapter = priv->adapter;
  1233. struct chan_freq_power *cfp;
  1234. char *current_val; /* For rates */
  1235. struct iw_event iwe; /* Temporary buffer */
  1236. int j;
  1237. #define PERFECT_RSSI ((u8)50)
  1238. #define WORST_RSSI ((u8)0)
  1239. #define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
  1240. u8 rssi;
  1241. cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
  1242. if (!cfp) {
  1243. lbs_deb_scan("Invalid channel number %d\n", bss->channel);
  1244. return NULL;
  1245. }
  1246. /* First entry *MUST* be the AP BSSID */
  1247. iwe.cmd = SIOCGIWAP;
  1248. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1249. memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
  1250. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
  1251. /* SSID */
  1252. iwe.cmd = SIOCGIWESSID;
  1253. iwe.u.data.flags = 1;
  1254. iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
  1255. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  1256. /* Mode */
  1257. iwe.cmd = SIOCGIWMODE;
  1258. iwe.u.mode = bss->mode;
  1259. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
  1260. /* Frequency */
  1261. iwe.cmd = SIOCGIWFREQ;
  1262. iwe.u.freq.m = (long)cfp->freq * 100000;
  1263. iwe.u.freq.e = 1;
  1264. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
  1265. /* Add quality statistics */
  1266. iwe.cmd = IWEVQUAL;
  1267. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
  1268. iwe.u.qual.level = SCAN_RSSI(bss->rssi);
  1269. rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1270. iwe.u.qual.qual =
  1271. (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
  1272. (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
  1273. (RSSI_DIFF * RSSI_DIFF);
  1274. if (iwe.u.qual.qual > 100)
  1275. iwe.u.qual.qual = 100;
  1276. if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
  1277. iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1278. } else {
  1279. iwe.u.qual.noise =
  1280. CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
  1281. }
  1282. /* Locally created ad-hoc BSSs won't have beacons if this is the
  1283. * only station in the adhoc network; so get signal strength
  1284. * from receive statistics.
  1285. */
  1286. if ((adapter->mode == IW_MODE_ADHOC)
  1287. && adapter->adhoccreate
  1288. && !libertas_ssid_cmp(adapter->curbssparams.ssid,
  1289. adapter->curbssparams.ssid_len,
  1290. bss->ssid, bss->ssid_len)) {
  1291. int snr, nf;
  1292. snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  1293. nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  1294. iwe.u.qual.level = CAL_RSSI(snr, nf);
  1295. }
  1296. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
  1297. /* Add encryption capability */
  1298. iwe.cmd = SIOCGIWENCODE;
  1299. if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
  1300. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1301. } else {
  1302. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1303. }
  1304. iwe.u.data.length = 0;
  1305. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  1306. current_val = start + IW_EV_LCP_LEN;
  1307. iwe.cmd = SIOCGIWRATE;
  1308. iwe.u.bitrate.fixed = 0;
  1309. iwe.u.bitrate.disabled = 0;
  1310. iwe.u.bitrate.value = 0;
  1311. for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
  1312. /* Bit rate given in 500 kb/s units */
  1313. iwe.u.bitrate.value = bss->rates[j] * 500000;
  1314. current_val = iwe_stream_add_value(start, current_val,
  1315. stop, &iwe, IW_EV_PARAM_LEN);
  1316. }
  1317. if ((bss->mode == IW_MODE_ADHOC)
  1318. && !libertas_ssid_cmp(adapter->curbssparams.ssid,
  1319. adapter->curbssparams.ssid_len,
  1320. bss->ssid, bss->ssid_len)
  1321. && adapter->adhoccreate) {
  1322. iwe.u.bitrate.value = 22 * 500000;
  1323. current_val = iwe_stream_add_value(start, current_val,
  1324. stop, &iwe, IW_EV_PARAM_LEN);
  1325. }
  1326. /* Check if we added any event */
  1327. if((current_val - start) > IW_EV_LCP_LEN)
  1328. start = current_val;
  1329. memset(&iwe, 0, sizeof(iwe));
  1330. if (bss->wpa_ie_len) {
  1331. char buf[MAX_WPA_IE_LEN];
  1332. memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
  1333. iwe.cmd = IWEVGENIE;
  1334. iwe.u.data.length = bss->wpa_ie_len;
  1335. start = iwe_stream_add_point(start, stop, &iwe, buf);
  1336. }
  1337. memset(&iwe, 0, sizeof(iwe));
  1338. if (bss->rsn_ie_len) {
  1339. char buf[MAX_WPA_IE_LEN];
  1340. memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
  1341. iwe.cmd = IWEVGENIE;
  1342. iwe.u.data.length = bss->rsn_ie_len;
  1343. start = iwe_stream_add_point(start, stop, &iwe, buf);
  1344. }
  1345. return start;
  1346. }
  1347. /**
  1348. * @brief Retrieve the scan table entries via wireless tools IOCTL call
  1349. *
  1350. * @param dev A pointer to net_device structure
  1351. * @param info A pointer to iw_request_info structure
  1352. * @param dwrq A pointer to iw_point structure
  1353. * @param extra A pointer to extra data buf
  1354. *
  1355. * @return 0 --success, otherwise fail
  1356. */
  1357. int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
  1358. struct iw_point *dwrq, char *extra)
  1359. {
  1360. #define SCAN_ITEM_SIZE 128
  1361. wlan_private *priv = dev->priv;
  1362. wlan_adapter *adapter = priv->adapter;
  1363. int err = 0;
  1364. char *ev = extra;
  1365. char *stop = ev + dwrq->length;
  1366. struct bss_descriptor * iter_bss;
  1367. struct bss_descriptor * safe;
  1368. lbs_deb_enter(LBS_DEB_ASSOC);
  1369. /* If we've got an uncompleted scan, schedule the next part */
  1370. if (!adapter->nr_cmd_pending && adapter->last_scanned_channel)
  1371. wlan_scan_networks(priv, NULL, 0);
  1372. /* Update RSSI if current BSS is a locally created ad-hoc BSS */
  1373. if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
  1374. libertas_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  1375. CMD_OPTION_WAITFORRSP, 0, NULL);
  1376. }
  1377. mutex_lock(&adapter->lock);
  1378. list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
  1379. char * next_ev;
  1380. unsigned long stale_time;
  1381. if (stop - ev < SCAN_ITEM_SIZE) {
  1382. err = -E2BIG;
  1383. break;
  1384. }
  1385. /* For mesh device, list only mesh networks */
  1386. if (dev == priv->mesh_dev && !iter_bss->mesh)
  1387. continue;
  1388. /* Prune old an old scan result */
  1389. stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1390. if (time_after(jiffies, stale_time)) {
  1391. list_move_tail (&iter_bss->list,
  1392. &adapter->network_free_list);
  1393. clear_bss_descriptor(iter_bss);
  1394. continue;
  1395. }
  1396. /* Translate to WE format this entry */
  1397. next_ev = libertas_translate_scan(priv, ev, stop, iter_bss);
  1398. if (next_ev == NULL)
  1399. continue;
  1400. ev = next_ev;
  1401. }
  1402. mutex_unlock(&adapter->lock);
  1403. dwrq->length = (ev - extra);
  1404. dwrq->flags = 0;
  1405. lbs_deb_leave(LBS_DEB_ASSOC);
  1406. return err;
  1407. }
  1408. /**
  1409. * @brief Prepare a scan command to be sent to the firmware
  1410. *
  1411. * Use the wlan_scan_cmd_config sent to the command processing module in
  1412. * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
  1413. * struct to send to firmware.
  1414. *
  1415. * The fixed fields specifying the BSS type and BSSID filters as well as a
  1416. * variable number/length of TLVs are sent in the command to firmware.
  1417. *
  1418. * @param priv A pointer to wlan_private structure
  1419. * @param cmd A pointer to cmd_ds_command structure to be sent to
  1420. * firmware with the cmd_DS_801_11_SCAN structure
  1421. * @param pdata_buf Void pointer cast of a wlan_scan_cmd_config struct used
  1422. * to set the fields/TLVs for the command sent to firmware
  1423. *
  1424. * @return 0 or -1
  1425. *
  1426. * @sa wlan_scan_create_channel_list
  1427. */
  1428. int libertas_cmd_80211_scan(wlan_private * priv,
  1429. struct cmd_ds_command *cmd, void *pdata_buf)
  1430. {
  1431. struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
  1432. struct wlan_scan_cmd_config *pscancfg;
  1433. lbs_deb_enter(LBS_DEB_ASSOC);
  1434. pscancfg = pdata_buf;
  1435. /* Set fixed field variables in scan command */
  1436. pscan->bsstype = pscancfg->bsstype;
  1437. memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
  1438. memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
  1439. cmd->command = cpu_to_le16(CMD_802_11_SCAN);
  1440. /* size is equal to the sizeof(fixed portions) + the TLV len + header */
  1441. cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
  1442. + pscancfg->tlvbufferlen + S_DS_GEN);
  1443. lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
  1444. le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
  1445. le16_to_cpu(cmd->seqnum));
  1446. lbs_deb_leave(LBS_DEB_ASSOC);
  1447. return 0;
  1448. }
  1449. static inline int is_same_network(struct bss_descriptor *src,
  1450. struct bss_descriptor *dst)
  1451. {
  1452. /* A network is only a duplicate if the channel, BSSID, and ESSID
  1453. * all match. We treat all <hidden> with the same BSSID and channel
  1454. * as one network */
  1455. return ((src->ssid_len == dst->ssid_len) &&
  1456. (src->channel == dst->channel) &&
  1457. !compare_ether_addr(src->bssid, dst->bssid) &&
  1458. !memcmp(src->ssid, dst->ssid, src->ssid_len));
  1459. }
  1460. /**
  1461. * @brief This function handles the command response of scan
  1462. *
  1463. * The response buffer for the scan command has the following
  1464. * memory layout:
  1465. *
  1466. * .-----------------------------------------------------------.
  1467. * | header (4 * sizeof(u16)): Standard command response hdr |
  1468. * .-----------------------------------------------------------.
  1469. * | bufsize (u16) : sizeof the BSS Description data |
  1470. * .-----------------------------------------------------------.
  1471. * | NumOfSet (u8) : Number of BSS Descs returned |
  1472. * .-----------------------------------------------------------.
  1473. * | BSSDescription data (variable, size given in bufsize) |
  1474. * .-----------------------------------------------------------.
  1475. * | TLV data (variable, size calculated using header->size, |
  1476. * | bufsize and sizeof the fixed fields above) |
  1477. * .-----------------------------------------------------------.
  1478. *
  1479. * @param priv A pointer to wlan_private structure
  1480. * @param resp A pointer to cmd_ds_command
  1481. *
  1482. * @return 0 or -1
  1483. */
  1484. int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
  1485. {
  1486. wlan_adapter *adapter = priv->adapter;
  1487. struct cmd_ds_802_11_scan_rsp *pscan;
  1488. struct mrvlietypes_data *ptlv;
  1489. struct mrvlietypes_tsftimestamp *ptsftlv;
  1490. struct bss_descriptor * iter_bss;
  1491. struct bss_descriptor * safe;
  1492. u8 *pbssinfo;
  1493. u16 scanrespsize;
  1494. int bytesleft;
  1495. int idx;
  1496. int tlvbufsize;
  1497. int ret;
  1498. lbs_deb_enter(LBS_DEB_ASSOC);
  1499. /* Prune old entries from scan table */
  1500. list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
  1501. unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1502. if (time_before(jiffies, stale_time))
  1503. continue;
  1504. list_move_tail (&iter_bss->list, &adapter->network_free_list);
  1505. clear_bss_descriptor(iter_bss);
  1506. }
  1507. pscan = &resp->params.scanresp;
  1508. if (pscan->nr_sets > MAX_NETWORK_COUNT) {
  1509. lbs_deb_scan(
  1510. "SCAN_RESP: too many scan results (%d, max %d)!!\n",
  1511. pscan->nr_sets, MAX_NETWORK_COUNT);
  1512. ret = -1;
  1513. goto done;
  1514. }
  1515. bytesleft = le16_to_cpu(pscan->bssdescriptsize);
  1516. lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
  1517. scanrespsize = le16_to_cpu(resp->size);
  1518. lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
  1519. pscan->nr_sets);
  1520. pbssinfo = pscan->bssdesc_and_tlvbuffer;
  1521. /* The size of the TLV buffer is equal to the entire command response
  1522. * size (scanrespsize) minus the fixed fields (sizeof()'s), the
  1523. * BSS Descriptions (bssdescriptsize as bytesLef) and the command
  1524. * response header (S_DS_GEN)
  1525. */
  1526. tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
  1527. + sizeof(pscan->nr_sets)
  1528. + S_DS_GEN);
  1529. ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);
  1530. /* Search the TLV buffer space in the scan response for any valid TLVs */
  1531. wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);
  1532. /*
  1533. * Process each scan response returned (pscan->nr_sets). Save
  1534. * the information in the newbssentry and then insert into the
  1535. * driver scan table either as an update to an existing entry
  1536. * or as an addition at the end of the table
  1537. */
  1538. for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
  1539. struct bss_descriptor new;
  1540. struct bss_descriptor * found = NULL;
  1541. struct bss_descriptor * oldest = NULL;
  1542. /* Process the data fields and IEs returned for this BSS */
  1543. memset(&new, 0, sizeof (struct bss_descriptor));
  1544. if (libertas_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
  1545. /* error parsing the scan response, skipped */
  1546. lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
  1547. continue;
  1548. }
  1549. /* Try to find this bss in the scan table */
  1550. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1551. if (is_same_network(iter_bss, &new)) {
  1552. found = iter_bss;
  1553. break;
  1554. }
  1555. if ((oldest == NULL) ||
  1556. (iter_bss->last_scanned < oldest->last_scanned))
  1557. oldest = iter_bss;
  1558. }
  1559. if (found) {
  1560. /* found, clear it */
  1561. clear_bss_descriptor(found);
  1562. } else if (!list_empty(&adapter->network_free_list)) {
  1563. /* Pull one from the free list */
  1564. found = list_entry(adapter->network_free_list.next,
  1565. struct bss_descriptor, list);
  1566. list_move_tail(&found->list, &adapter->network_list);
  1567. } else if (oldest) {
  1568. /* If there are no more slots, expire the oldest */
  1569. found = oldest;
  1570. clear_bss_descriptor(found);
  1571. list_move_tail(&found->list, &adapter->network_list);
  1572. } else {
  1573. continue;
  1574. }
  1575. lbs_deb_scan("SCAN_RESP: BSSID = " MAC_FMT "\n",
  1576. new.bssid[0], new.bssid[1], new.bssid[2],
  1577. new.bssid[3], new.bssid[4], new.bssid[5]);
  1578. /*
  1579. * If the TSF TLV was appended to the scan results, save the
  1580. * this entries TSF value in the networktsf field. The
  1581. * networktsf is the firmware's TSF value at the time the
  1582. * beacon or probe response was received.
  1583. */
  1584. if (ptsftlv) {
  1585. new.networktsf = le64_to_cpup(&ptsftlv->tsftable[idx]);
  1586. }
  1587. /* Copy the locally created newbssentry to the scan table */
  1588. memcpy(found, &new, offsetof(struct bss_descriptor, list));
  1589. }
  1590. ret = 0;
  1591. done:
  1592. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1593. return ret;
  1594. }