scan.c 48 KB

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