scan.c 60 KB

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