scan.c 59 KB

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