scan.c 45 KB

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