scan.c 48 KB

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