scan.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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 adapter A pointer to struct lbs_adapter
  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_adapter *adapter,
  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(&adapter->secinfo, bss))) {
  181. goto done;
  182. } else if ((matched = match_bss_static_wep(&adapter->secinfo, bss))) {
  183. goto done;
  184. } else if ((matched = match_bss_wpa(&adapter->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. adapter->secinfo.wep_enabled ? "e" : "d",
  190. adapter->secinfo.WPAenabled ? "e" : "d",
  191. adapter->secinfo.WPA2enabled ? "e" : "d",
  192. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  193. goto done;
  194. } else if ((matched = match_bss_wpa2(&adapter->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. adapter->secinfo.wep_enabled ? "e" : "d",
  200. adapter->secinfo.WPAenabled ? "e" : "d",
  201. adapter->secinfo.WPA2enabled ? "e" : "d",
  202. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  203. goto done;
  204. } else if ((matched = match_bss_dynamic_wep(&adapter->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. adapter->secinfo.wep_enabled ? "e" : "d",
  218. adapter->secinfo.WPAenabled ? "e" : "d",
  219. adapter->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 lbs_adapter *adapter = priv->adapter;
  262. struct region_channel *scanregion;
  263. struct chan_freq_power *cfp;
  264. int rgnidx;
  265. int chanidx;
  266. int nextchan;
  267. u8 scantype;
  268. chanidx = 0;
  269. /* Set the default scan type to the user specified type, will later
  270. * be changed to passive on a per channel basis if restricted by
  271. * regulatory requirements (11d or 11h)
  272. */
  273. scantype = CMD_SCAN_TYPE_ACTIVE;
  274. for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
  275. if (priv->adapter->enable11d &&
  276. (adapter->connect_status != LBS_CONNECTED) &&
  277. (adapter->mesh_connect_status != LBS_CONNECTED)) {
  278. /* Scan all the supported chan for the first scan */
  279. if (!adapter->universal_channel[rgnidx].valid)
  280. continue;
  281. scanregion = &adapter->universal_channel[rgnidx];
  282. /* clear the parsed_region_chan for the first scan */
  283. memset(&adapter->parsed_region_chan, 0x00,
  284. sizeof(adapter->parsed_region_chan));
  285. } else {
  286. if (!adapter->region_channel[rgnidx].valid)
  287. continue;
  288. scanregion = &adapter->region_channel[rgnidx];
  289. }
  290. for (nextchan = 0;
  291. nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
  292. cfp = scanregion->CFP + nextchan;
  293. if (priv->adapter->enable11d) {
  294. scantype =
  295. lbs_get_scan_type_11d(cfp->channel,
  296. &adapter->
  297. parsed_region_chan);
  298. }
  299. switch (scanregion->band) {
  300. case BAND_B:
  301. case BAND_G:
  302. default:
  303. scanchanlist[chanidx].radiotype =
  304. CMD_SCAN_RADIO_TYPE_BG;
  305. break;
  306. }
  307. if (scantype == CMD_SCAN_TYPE_PASSIVE) {
  308. scanchanlist[chanidx].maxscantime =
  309. cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
  310. scanchanlist[chanidx].chanscanmode.passivescan =
  311. 1;
  312. } else {
  313. scanchanlist[chanidx].maxscantime =
  314. cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
  315. scanchanlist[chanidx].chanscanmode.passivescan =
  316. 0;
  317. }
  318. scanchanlist[chanidx].channumber = cfp->channel;
  319. if (filteredscan) {
  320. scanchanlist[chanidx].chanscanmode.
  321. disablechanfilt = 1;
  322. }
  323. }
  324. }
  325. return chanidx;
  326. }
  327. /*
  328. * Add SSID TLV of the form:
  329. *
  330. * TLV-ID SSID 00 00
  331. * length 06 00
  332. * ssid 4d 4e 54 45 53 54
  333. */
  334. static int lbs_scan_add_ssid_tlv(u8 *tlv,
  335. const struct lbs_ioctl_user_scan_cfg *user_cfg)
  336. {
  337. struct mrvlietypes_ssidparamset *ssid_tlv =
  338. (struct mrvlietypes_ssidparamset *)tlv;
  339. ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
  340. ssid_tlv->header.len = cpu_to_le16(user_cfg->ssid_len);
  341. memcpy(ssid_tlv->ssid, user_cfg->ssid, user_cfg->ssid_len);
  342. return sizeof(ssid_tlv->header) + user_cfg->ssid_len;
  343. }
  344. /*
  345. * Add CHANLIST TLV of the form
  346. *
  347. * TLV-ID CHANLIST 01 01
  348. * length 5b 00
  349. * channel 1 00 01 00 00 00 64 00
  350. * radio type 00
  351. * channel 01
  352. * scan type 00
  353. * min scan time 00 00
  354. * max scan time 64 00
  355. * channel 2 00 02 00 00 00 64 00
  356. * channel 3 00 03 00 00 00 64 00
  357. * channel 4 00 04 00 00 00 64 00
  358. * channel 5 00 05 00 00 00 64 00
  359. * channel 6 00 06 00 00 00 64 00
  360. * channel 7 00 07 00 00 00 64 00
  361. * channel 8 00 08 00 00 00 64 00
  362. * channel 9 00 09 00 00 00 64 00
  363. * channel 10 00 0a 00 00 00 64 00
  364. * channel 11 00 0b 00 00 00 64 00
  365. * channel 12 00 0c 00 00 00 64 00
  366. * channel 13 00 0d 00 00 00 64 00
  367. *
  368. */
  369. static int lbs_scan_add_chanlist_tlv(u8 *tlv,
  370. struct chanscanparamset *chan_list,
  371. int chan_count)
  372. {
  373. size_t size = sizeof(struct chanscanparamset) * chan_count;
  374. struct mrvlietypes_chanlistparamset *chan_tlv =
  375. (struct mrvlietypes_chanlistparamset *) tlv;
  376. chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
  377. memcpy(chan_tlv->chanscanparam, chan_list, size);
  378. chan_tlv->header.len = cpu_to_le16(size);
  379. return sizeof(chan_tlv->header) + size;
  380. }
  381. /*
  382. * Add RATES TLV of the form
  383. *
  384. * TLV-ID RATES 01 00
  385. * length 0e 00
  386. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
  387. *
  388. * The rates are in lbs_bg_rates[], but for the 802.11b
  389. * rates the high bit isn't set.
  390. */
  391. static int lbs_scan_add_rates_tlv(u8 *tlv)
  392. {
  393. int i;
  394. struct mrvlietypes_ratesparamset *rate_tlv =
  395. (struct mrvlietypes_ratesparamset *) tlv;
  396. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  397. tlv += sizeof(rate_tlv->header);
  398. for (i = 0; i < MAX_RATES; i++) {
  399. *tlv = lbs_bg_rates[i];
  400. if (*tlv == 0)
  401. break;
  402. /* This code makes sure that the 802.11b rates (1 MBit/s, 2
  403. MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
  404. Note that the values are MBit/s * 2, to mark them as
  405. basic rates so that the firmware likes it better */
  406. if (*tlv == 0x02 || *tlv == 0x04 ||
  407. *tlv == 0x0b || *tlv == 0x16)
  408. *tlv |= 0x80;
  409. tlv++;
  410. }
  411. rate_tlv->header.len = cpu_to_le16(i);
  412. return sizeof(rate_tlv->header) + i;
  413. }
  414. /*
  415. * Generate the CMD_802_11_SCAN command with the proper tlv
  416. * for a bunch of channels.
  417. */
  418. static int lbs_do_scan(struct lbs_private *priv,
  419. u8 bsstype,
  420. struct chanscanparamset *chan_list,
  421. int chan_count,
  422. const struct lbs_ioctl_user_scan_cfg *user_cfg)
  423. {
  424. int ret = -ENOMEM;
  425. struct lbs_scan_cmd_config *scan_cmd;
  426. u8 *tlv; /* pointer into our current, growing TLV storage area */
  427. lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, "
  428. "chan_count %d",
  429. bsstype, chan_list[0].channumber, chan_count);
  430. /* create the fixed part for scan command */
  431. scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
  432. if (scan_cmd == NULL)
  433. goto out;
  434. tlv = scan_cmd->tlvbuffer;
  435. if (user_cfg)
  436. memcpy(scan_cmd->bssid, user_cfg->bssid, ETH_ALEN);
  437. scan_cmd->bsstype = bsstype;
  438. /* add TLVs */
  439. if (user_cfg && user_cfg->ssid_len)
  440. tlv += lbs_scan_add_ssid_tlv(tlv, user_cfg);
  441. if (chan_list && chan_count)
  442. tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
  443. tlv += lbs_scan_add_rates_tlv(tlv);
  444. /* This is the final data we are about to send */
  445. scan_cmd->tlvbufferlen = tlv - scan_cmd->tlvbuffer;
  446. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd, 1+6);
  447. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
  448. scan_cmd->tlvbufferlen);
  449. ret = lbs_prepare_and_send_command(priv, CMD_802_11_SCAN, 0,
  450. CMD_OPTION_WAITFORRSP, 0, scan_cmd);
  451. out:
  452. kfree(scan_cmd);
  453. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  454. return ret;
  455. }
  456. /**
  457. * @brief Internal function used to start a scan based on an input config
  458. *
  459. * Also used from debugfs
  460. *
  461. * Use the input user scan configuration information when provided in
  462. * order to send the appropriate scan commands to firmware to populate or
  463. * update the internal driver scan table
  464. *
  465. * @param priv A pointer to struct lbs_private structure
  466. * @param puserscanin Pointer to the input configuration for the requested
  467. * scan.
  468. *
  469. * @return 0 or < 0 if error
  470. */
  471. int lbs_scan_networks(struct lbs_private *priv,
  472. const struct lbs_ioctl_user_scan_cfg *user_cfg,
  473. int full_scan)
  474. {
  475. struct lbs_adapter *adapter = priv->adapter;
  476. int ret = -ENOMEM;
  477. struct chanscanparamset *chan_list;
  478. struct chanscanparamset *curr_chans;
  479. int chan_count;
  480. u8 bsstype = CMD_BSS_TYPE_ANY;
  481. int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
  482. int filteredscan = 0;
  483. union iwreq_data wrqu;
  484. #ifdef CONFIG_LIBERTAS_DEBUG
  485. struct bss_descriptor *iter;
  486. int i = 0;
  487. DECLARE_MAC_BUF(mac);
  488. #endif
  489. lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d",
  490. full_scan);
  491. /* Cancel any partial outstanding partial scans if this scan
  492. * is a full scan.
  493. */
  494. if (full_scan && delayed_work_pending(&priv->scan_work))
  495. cancel_delayed_work(&priv->scan_work);
  496. /* Determine same scan parameters */
  497. if (user_cfg) {
  498. if (user_cfg->bsstype)
  499. bsstype = user_cfg->bsstype;
  500. if (compare_ether_addr(user_cfg->bssid, &zeromac[0]) != 0) {
  501. numchannels = MRVDRV_MAX_CHANNELS_PER_SCAN;
  502. filteredscan = 1;
  503. }
  504. }
  505. lbs_deb_scan("numchannels %d, bsstype %d, "
  506. "filteredscan %d\n",
  507. numchannels, bsstype, filteredscan);
  508. /* Create list of channels to scan */
  509. chan_list = kzalloc(sizeof(struct chanscanparamset) *
  510. LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
  511. if (!chan_list) {
  512. lbs_pr_alert("SCAN: chan_list empty\n");
  513. goto out;
  514. }
  515. /* We want to scan all channels */
  516. chan_count = lbs_scan_create_channel_list(priv, chan_list,
  517. filteredscan);
  518. netif_stop_queue(priv->dev);
  519. netif_carrier_off(priv->dev);
  520. if (priv->mesh_dev) {
  521. netif_stop_queue(priv->mesh_dev);
  522. netif_carrier_off(priv->mesh_dev);
  523. }
  524. /* Prepare to continue an interrupted scan */
  525. lbs_deb_scan("chan_count %d, last_scanned_channel %d\n",
  526. chan_count, adapter->last_scanned_channel);
  527. curr_chans = chan_list;
  528. /* advance channel list by already-scanned-channels */
  529. if (adapter->last_scanned_channel > 0) {
  530. curr_chans += adapter->last_scanned_channel;
  531. chan_count -= adapter->last_scanned_channel;
  532. }
  533. /* Send scan command(s)
  534. * numchannels contains the number of channels we should maximally scan
  535. * chan_count is the total number of channels to scan
  536. */
  537. while (chan_count) {
  538. int to_scan = min(numchannels, chan_count);
  539. lbs_deb_scan("scanning %d of %d channels\n",
  540. to_scan, chan_count);
  541. ret = lbs_do_scan(priv, bsstype, curr_chans,
  542. to_scan, user_cfg);
  543. if (ret) {
  544. lbs_pr_err("SCAN_CMD failed\n");
  545. goto out2;
  546. }
  547. curr_chans += to_scan;
  548. chan_count -= to_scan;
  549. /* somehow schedule the next part of the scan */
  550. if (chan_count &&
  551. !full_scan &&
  552. !priv->adapter->surpriseremoved) {
  553. /* -1 marks just that we're currently scanning */
  554. if (adapter->last_scanned_channel < 0)
  555. adapter->last_scanned_channel = to_scan;
  556. else
  557. adapter->last_scanned_channel += to_scan;
  558. cancel_delayed_work(&priv->scan_work);
  559. queue_delayed_work(priv->work_thread, &priv->scan_work,
  560. msecs_to_jiffies(300));
  561. /* skip over GIWSCAN event */
  562. goto out;
  563. }
  564. }
  565. memset(&wrqu, 0, sizeof(union iwreq_data));
  566. wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
  567. #ifdef CONFIG_LIBERTAS_DEBUG
  568. /* Dump the scan table */
  569. mutex_lock(&adapter->lock);
  570. lbs_deb_scan("scan table:\n");
  571. list_for_each_entry(iter, &adapter->network_list, list)
  572. lbs_deb_scan("%02d: BSSID %s, RSSI %d, SSID '%s'\n",
  573. i++, print_mac(mac, iter->bssid), (s32) iter->rssi,
  574. escape_essid(iter->ssid, iter->ssid_len));
  575. mutex_unlock(&adapter->lock);
  576. #endif
  577. out2:
  578. adapter->last_scanned_channel = 0;
  579. out:
  580. if (adapter->connect_status == LBS_CONNECTED) {
  581. netif_carrier_on(priv->dev);
  582. netif_wake_queue(priv->dev);
  583. }
  584. if (priv->mesh_dev && (adapter->mesh_connect_status == LBS_CONNECTED)) {
  585. netif_carrier_on(priv->mesh_dev);
  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((u16 *)*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 = le32_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 adapter A pointer to struct lbs_adapter
  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_adapter *adapter,
  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(&adapter->lock);
  832. list_for_each_entry (iter_bss, &adapter->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(adapter, 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(&adapter->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 adapter A pointer to struct lbs_adapter
  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_adapter *adapter,
  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(&adapter->lock);
  874. list_for_each_entry (iter_bss, &adapter->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(adapter, 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(&adapter->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 adapter A pointer to struct lbs_adapter
  921. *
  922. * @return index in BSSID list
  923. */
  924. static struct bss_descriptor *lbs_find_best_ssid_in_list(
  925. struct lbs_adapter *adapter,
  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(&adapter->lock);
  933. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  934. switch (mode) {
  935. case IW_MODE_INFRA:
  936. case IW_MODE_ADHOC:
  937. if (!is_network_compatible(adapter, 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(&adapter->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. struct lbs_adapter *adapter = priv->adapter;
  971. int ret = -1;
  972. struct bss_descriptor * found;
  973. lbs_deb_enter(LBS_DEB_SCAN);
  974. lbs_scan_networks(priv, NULL, 1);
  975. if (adapter->surpriseremoved)
  976. goto out;
  977. found = lbs_find_best_ssid_in_list(adapter, preferred_mode);
  978. if (found && (found->ssid_len > 0)) {
  979. memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
  980. *out_ssid_len = found->ssid_len;
  981. *out_mode = found->mode;
  982. ret = 0;
  983. }
  984. out:
  985. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  986. return ret;
  987. }
  988. /**
  989. * @brief Send a scan command for all available channels filtered on a spec
  990. *
  991. * Used in association code and from debugfs
  992. *
  993. * @param priv A pointer to struct lbs_private structure
  994. * @param ssid A pointer to the SSID to scan for
  995. * @param ssid_len Length of the SSID
  996. * @param clear_ssid Should existing scan results with this SSID
  997. * be cleared?
  998. *
  999. * @return 0-success, otherwise fail
  1000. */
  1001. int lbs_send_specific_ssid_scan(struct lbs_private *priv,
  1002. u8 *ssid, u8 ssid_len, u8 clear_ssid)
  1003. {
  1004. struct lbs_adapter *adapter = priv->adapter;
  1005. struct lbs_ioctl_user_scan_cfg scancfg;
  1006. int ret = 0;
  1007. lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s', clear %d",
  1008. escape_essid(ssid, ssid_len), clear_ssid);
  1009. if (!ssid_len)
  1010. goto out;
  1011. memset(&scancfg, 0x00, sizeof(scancfg));
  1012. memcpy(scancfg.ssid, ssid, ssid_len);
  1013. scancfg.ssid_len = ssid_len;
  1014. scancfg.clear_ssid = clear_ssid;
  1015. lbs_scan_networks(priv, &scancfg, 1);
  1016. if (adapter->surpriseremoved) {
  1017. ret = -1;
  1018. goto out;
  1019. }
  1020. out:
  1021. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1022. return ret;
  1023. }
  1024. /*********************************************************************/
  1025. /* */
  1026. /* Support for Wireless Extensions */
  1027. /* */
  1028. /*********************************************************************/
  1029. #define MAX_CUSTOM_LEN 64
  1030. static inline char *lbs_translate_scan(struct lbs_private *priv,
  1031. char *start, char *stop,
  1032. struct bss_descriptor *bss)
  1033. {
  1034. struct lbs_adapter *adapter = priv->adapter;
  1035. struct chan_freq_power *cfp;
  1036. char *current_val; /* For rates */
  1037. struct iw_event iwe; /* Temporary buffer */
  1038. int j;
  1039. #define PERFECT_RSSI ((u8)50)
  1040. #define WORST_RSSI ((u8)0)
  1041. #define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
  1042. u8 rssi;
  1043. lbs_deb_enter(LBS_DEB_SCAN);
  1044. cfp = lbs_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
  1045. if (!cfp) {
  1046. lbs_deb_scan("Invalid channel number %d\n", bss->channel);
  1047. start = NULL;
  1048. goto out;
  1049. }
  1050. /* First entry *MUST* be the BSSID */
  1051. iwe.cmd = SIOCGIWAP;
  1052. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1053. memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
  1054. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
  1055. /* SSID */
  1056. iwe.cmd = SIOCGIWESSID;
  1057. iwe.u.data.flags = 1;
  1058. iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
  1059. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  1060. /* Mode */
  1061. iwe.cmd = SIOCGIWMODE;
  1062. iwe.u.mode = bss->mode;
  1063. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
  1064. /* Frequency */
  1065. iwe.cmd = SIOCGIWFREQ;
  1066. iwe.u.freq.m = (long)cfp->freq * 100000;
  1067. iwe.u.freq.e = 1;
  1068. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
  1069. /* Add quality statistics */
  1070. iwe.cmd = IWEVQUAL;
  1071. iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
  1072. iwe.u.qual.level = SCAN_RSSI(bss->rssi);
  1073. rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1074. iwe.u.qual.qual =
  1075. (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
  1076. (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
  1077. (RSSI_DIFF * RSSI_DIFF);
  1078. if (iwe.u.qual.qual > 100)
  1079. iwe.u.qual.qual = 100;
  1080. if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
  1081. iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
  1082. } else {
  1083. iwe.u.qual.noise =
  1084. CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
  1085. }
  1086. /* Locally created ad-hoc BSSs won't have beacons if this is the
  1087. * only station in the adhoc network; so get signal strength
  1088. * from receive statistics.
  1089. */
  1090. if ((adapter->mode == IW_MODE_ADHOC)
  1091. && adapter->adhoccreate
  1092. && !lbs_ssid_cmp(adapter->curbssparams.ssid,
  1093. adapter->curbssparams.ssid_len,
  1094. bss->ssid, bss->ssid_len)) {
  1095. int snr, nf;
  1096. snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  1097. nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
  1098. iwe.u.qual.level = CAL_RSSI(snr, nf);
  1099. }
  1100. start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
  1101. /* Add encryption capability */
  1102. iwe.cmd = SIOCGIWENCODE;
  1103. if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
  1104. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1105. } else {
  1106. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1107. }
  1108. iwe.u.data.length = 0;
  1109. start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
  1110. current_val = start + IW_EV_LCP_LEN;
  1111. iwe.cmd = SIOCGIWRATE;
  1112. iwe.u.bitrate.fixed = 0;
  1113. iwe.u.bitrate.disabled = 0;
  1114. iwe.u.bitrate.value = 0;
  1115. for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
  1116. /* Bit rate given in 500 kb/s units */
  1117. iwe.u.bitrate.value = bss->rates[j] * 500000;
  1118. current_val = iwe_stream_add_value(start, current_val,
  1119. stop, &iwe, IW_EV_PARAM_LEN);
  1120. }
  1121. if ((bss->mode == IW_MODE_ADHOC)
  1122. && !lbs_ssid_cmp(adapter->curbssparams.ssid,
  1123. adapter->curbssparams.ssid_len,
  1124. bss->ssid, bss->ssid_len)
  1125. && adapter->adhoccreate) {
  1126. iwe.u.bitrate.value = 22 * 500000;
  1127. current_val = iwe_stream_add_value(start, current_val,
  1128. stop, &iwe, IW_EV_PARAM_LEN);
  1129. }
  1130. /* Check if we added any event */
  1131. if((current_val - start) > IW_EV_LCP_LEN)
  1132. start = current_val;
  1133. memset(&iwe, 0, sizeof(iwe));
  1134. if (bss->wpa_ie_len) {
  1135. char buf[MAX_WPA_IE_LEN];
  1136. memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
  1137. iwe.cmd = IWEVGENIE;
  1138. iwe.u.data.length = bss->wpa_ie_len;
  1139. start = iwe_stream_add_point(start, stop, &iwe, buf);
  1140. }
  1141. memset(&iwe, 0, sizeof(iwe));
  1142. if (bss->rsn_ie_len) {
  1143. char buf[MAX_WPA_IE_LEN];
  1144. memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
  1145. iwe.cmd = IWEVGENIE;
  1146. iwe.u.data.length = bss->rsn_ie_len;
  1147. start = iwe_stream_add_point(start, stop, &iwe, buf);
  1148. }
  1149. if (bss->mesh) {
  1150. char custom[MAX_CUSTOM_LEN];
  1151. char *p = custom;
  1152. iwe.cmd = IWEVCUSTOM;
  1153. p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
  1154. "mesh-type: olpc");
  1155. iwe.u.data.length = p - custom;
  1156. if (iwe.u.data.length)
  1157. start = iwe_stream_add_point(start, stop, &iwe, custom);
  1158. }
  1159. out:
  1160. lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
  1161. return start;
  1162. }
  1163. /**
  1164. * @brief Handle Scan Network ioctl
  1165. *
  1166. * @param dev A pointer to net_device structure
  1167. * @param info A pointer to iw_request_info structure
  1168. * @param vwrq A pointer to iw_param structure
  1169. * @param extra A pointer to extra data buf
  1170. *
  1171. * @return 0 --success, otherwise fail
  1172. */
  1173. int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
  1174. struct iw_param *wrqu, char *extra)
  1175. {
  1176. struct lbs_private *priv = dev->priv;
  1177. struct lbs_adapter *adapter = priv->adapter;
  1178. lbs_deb_enter(LBS_DEB_SCAN);
  1179. if (!netif_running(dev))
  1180. return -ENETDOWN;
  1181. /* mac80211 does this:
  1182. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1183. if (sdata->type != IEEE80211_IF_TYPE_xxx)
  1184. return -EOPNOTSUPP;
  1185. if (wrqu->data.length == sizeof(struct iw_scan_req) &&
  1186. wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  1187. req = (struct iw_scan_req *)extra;
  1188. ssid = req->essid;
  1189. ssid_len = req->essid_len;
  1190. }
  1191. */
  1192. if (!delayed_work_pending(&priv->scan_work))
  1193. queue_delayed_work(priv->work_thread, &priv->scan_work,
  1194. msecs_to_jiffies(50));
  1195. /* set marker that currently a scan is taking place */
  1196. adapter->last_scanned_channel = -1;
  1197. if (adapter->surpriseremoved)
  1198. return -EIO;
  1199. lbs_deb_leave(LBS_DEB_SCAN);
  1200. return 0;
  1201. }
  1202. /**
  1203. * @brief Handle Retrieve scan table ioctl
  1204. *
  1205. * @param dev A pointer to net_device structure
  1206. * @param info A pointer to iw_request_info structure
  1207. * @param dwrq A pointer to iw_point structure
  1208. * @param extra A pointer to extra data buf
  1209. *
  1210. * @return 0 --success, otherwise fail
  1211. */
  1212. int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
  1213. struct iw_point *dwrq, char *extra)
  1214. {
  1215. #define SCAN_ITEM_SIZE 128
  1216. struct lbs_private *priv = dev->priv;
  1217. struct lbs_adapter *adapter = priv->adapter;
  1218. int err = 0;
  1219. char *ev = extra;
  1220. char *stop = ev + dwrq->length;
  1221. struct bss_descriptor * iter_bss;
  1222. struct bss_descriptor * safe;
  1223. lbs_deb_enter(LBS_DEB_SCAN);
  1224. /* iwlist should wait until the current scan is finished */
  1225. if (adapter->last_scanned_channel)
  1226. return -EAGAIN;
  1227. /* Update RSSI if current BSS is a locally created ad-hoc BSS */
  1228. if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
  1229. lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
  1230. CMD_OPTION_WAITFORRSP, 0, NULL);
  1231. }
  1232. mutex_lock(&adapter->lock);
  1233. list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
  1234. char * next_ev;
  1235. unsigned long stale_time;
  1236. if (stop - ev < SCAN_ITEM_SIZE) {
  1237. err = -E2BIG;
  1238. break;
  1239. }
  1240. /* For mesh device, list only mesh networks */
  1241. if (dev == priv->mesh_dev && !iter_bss->mesh)
  1242. continue;
  1243. /* Prune old an old scan result */
  1244. stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1245. if (time_after(jiffies, stale_time)) {
  1246. list_move_tail (&iter_bss->list,
  1247. &adapter->network_free_list);
  1248. clear_bss_descriptor(iter_bss);
  1249. continue;
  1250. }
  1251. /* Translate to WE format this entry */
  1252. next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
  1253. if (next_ev == NULL)
  1254. continue;
  1255. ev = next_ev;
  1256. }
  1257. mutex_unlock(&adapter->lock);
  1258. dwrq->length = (ev - extra);
  1259. dwrq->flags = 0;
  1260. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", err);
  1261. return err;
  1262. }
  1263. /*********************************************************************/
  1264. /* */
  1265. /* Command execution */
  1266. /* */
  1267. /*********************************************************************/
  1268. /**
  1269. * @brief Prepare a scan command to be sent to the firmware
  1270. *
  1271. * Called via lbs_prepare_and_send_command(priv, CMD_802_11_SCAN, ...)
  1272. * from cmd.c
  1273. *
  1274. * Sends a fixed lenght data part (specifying the BSS type and BSSID filters)
  1275. * as well as a variable number/length of TLVs to the firmware.
  1276. *
  1277. * @param priv A pointer to struct lbs_private structure
  1278. * @param cmd A pointer to cmd_ds_command structure to be sent to
  1279. * firmware with the cmd_DS_801_11_SCAN structure
  1280. * @param pdata_buf Void pointer cast of a lbs_scan_cmd_config struct used
  1281. * to set the fields/TLVs for the command sent to firmware
  1282. *
  1283. * @return 0 or -1
  1284. */
  1285. int lbs_cmd_80211_scan(struct lbs_private *priv,
  1286. struct cmd_ds_command *cmd, void *pdata_buf)
  1287. {
  1288. struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
  1289. struct lbs_scan_cmd_config *pscancfg = pdata_buf;
  1290. lbs_deb_enter(LBS_DEB_SCAN);
  1291. /* Set fixed field variables in scan command */
  1292. pscan->bsstype = pscancfg->bsstype;
  1293. memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
  1294. memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
  1295. /* size is equal to the sizeof(fixed portions) + the TLV len + header */
  1296. cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
  1297. + pscancfg->tlvbufferlen + S_DS_GEN);
  1298. lbs_deb_leave(LBS_DEB_SCAN);
  1299. return 0;
  1300. }
  1301. /**
  1302. * @brief This function handles the command response of scan
  1303. *
  1304. * Called from handle_cmd_response() in cmdrespc.
  1305. *
  1306. * The response buffer for the scan command has the following
  1307. * memory layout:
  1308. *
  1309. * .-----------------------------------------------------------.
  1310. * | header (4 * sizeof(u16)): Standard command response hdr |
  1311. * .-----------------------------------------------------------.
  1312. * | bufsize (u16) : sizeof the BSS Description data |
  1313. * .-----------------------------------------------------------.
  1314. * | NumOfSet (u8) : Number of BSS Descs returned |
  1315. * .-----------------------------------------------------------.
  1316. * | BSSDescription data (variable, size given in bufsize) |
  1317. * .-----------------------------------------------------------.
  1318. * | TLV data (variable, size calculated using header->size, |
  1319. * | bufsize and sizeof the fixed fields above) |
  1320. * .-----------------------------------------------------------.
  1321. *
  1322. * @param priv A pointer to struct lbs_private structure
  1323. * @param resp A pointer to cmd_ds_command
  1324. *
  1325. * @return 0 or -1
  1326. */
  1327. int lbs_ret_80211_scan(struct lbs_private *priv, struct cmd_ds_command *resp)
  1328. {
  1329. struct lbs_adapter *adapter = priv->adapter;
  1330. struct cmd_ds_802_11_scan_rsp *pscan;
  1331. struct bss_descriptor * iter_bss;
  1332. struct bss_descriptor * safe;
  1333. u8 *pbssinfo;
  1334. u16 scanrespsize;
  1335. int bytesleft;
  1336. int idx;
  1337. int tlvbufsize;
  1338. int ret;
  1339. lbs_deb_enter(LBS_DEB_SCAN);
  1340. /* Prune old entries from scan table */
  1341. list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
  1342. unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
  1343. if (time_before(jiffies, stale_time))
  1344. continue;
  1345. list_move_tail (&iter_bss->list, &adapter->network_free_list);
  1346. clear_bss_descriptor(iter_bss);
  1347. }
  1348. pscan = &resp->params.scanresp;
  1349. if (pscan->nr_sets > MAX_NETWORK_COUNT) {
  1350. lbs_deb_scan(
  1351. "SCAN_RESP: too many scan results (%d, max %d)!!\n",
  1352. pscan->nr_sets, MAX_NETWORK_COUNT);
  1353. ret = -1;
  1354. goto done;
  1355. }
  1356. bytesleft = le16_to_cpu(get_unaligned((u16*)&pscan->bssdescriptsize));
  1357. lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
  1358. scanrespsize = le16_to_cpu(get_unaligned((u16*)&resp->size));
  1359. lbs_deb_scan("SCAN_RESP: scan results %d\n",
  1360. pscan->nr_sets);
  1361. pbssinfo = pscan->bssdesc_and_tlvbuffer;
  1362. /* The size of the TLV buffer is equal to the entire command response
  1363. * size (scanrespsize) minus the fixed fields (sizeof()'s), the
  1364. * BSS Descriptions (bssdescriptsize as bytesLef) and the command
  1365. * response header (S_DS_GEN)
  1366. */
  1367. tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
  1368. + sizeof(pscan->nr_sets)
  1369. + S_DS_GEN);
  1370. /*
  1371. * Process each scan response returned (pscan->nr_sets). Save
  1372. * the information in the newbssentry and then insert into the
  1373. * driver scan table either as an update to an existing entry
  1374. * or as an addition at the end of the table
  1375. */
  1376. for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
  1377. struct bss_descriptor new;
  1378. struct bss_descriptor * found = NULL;
  1379. struct bss_descriptor * oldest = NULL;
  1380. DECLARE_MAC_BUF(mac);
  1381. /* Process the data fields and IEs returned for this BSS */
  1382. memset(&new, 0, sizeof (struct bss_descriptor));
  1383. if (lbs_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
  1384. /* error parsing the scan response, skipped */
  1385. lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
  1386. continue;
  1387. }
  1388. /* Try to find this bss in the scan table */
  1389. list_for_each_entry (iter_bss, &adapter->network_list, list) {
  1390. if (is_same_network(iter_bss, &new)) {
  1391. found = iter_bss;
  1392. break;
  1393. }
  1394. if ((oldest == NULL) ||
  1395. (iter_bss->last_scanned < oldest->last_scanned))
  1396. oldest = iter_bss;
  1397. }
  1398. if (found) {
  1399. /* found, clear it */
  1400. clear_bss_descriptor(found);
  1401. } else if (!list_empty(&adapter->network_free_list)) {
  1402. /* Pull one from the free list */
  1403. found = list_entry(adapter->network_free_list.next,
  1404. struct bss_descriptor, list);
  1405. list_move_tail(&found->list, &adapter->network_list);
  1406. } else if (oldest) {
  1407. /* If there are no more slots, expire the oldest */
  1408. found = oldest;
  1409. clear_bss_descriptor(found);
  1410. list_move_tail(&found->list, &adapter->network_list);
  1411. } else {
  1412. continue;
  1413. }
  1414. lbs_deb_scan("SCAN_RESP: BSSID %s\n",
  1415. print_mac(mac, new.bssid));
  1416. /* Copy the locally created newbssentry to the scan table */
  1417. memcpy(found, &new, offsetof(struct bss_descriptor, list));
  1418. }
  1419. ret = 0;
  1420. done:
  1421. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1422. return ret;
  1423. }