assoc.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /* Copyright (C) 2006, Red Hat, Inc. */
  2. #include <linux/etherdevice.h>
  3. #include "assoc.h"
  4. #include "decl.h"
  5. #include "host.h"
  6. #include "scan.h"
  7. #include "cmd.h"
  8. static const u8 bssid_any[ETH_ALEN] __attribute__ ((aligned (2))) =
  9. { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  10. static const u8 bssid_off[ETH_ALEN] __attribute__ ((aligned (2))) =
  11. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  12. /* The firmware needs certain bits masked out of the beacon-derviced capability
  13. * field when associating/joining to BSSs.
  14. */
  15. #define CAPINFO_MASK (~(0xda00))
  16. /**
  17. * @brief Associate to a specific BSS discovered in a scan
  18. *
  19. * @param priv A pointer to struct lbs_private structure
  20. * @param pbssdesc Pointer to the BSS descriptor to associate with.
  21. *
  22. * @return 0-success, otherwise fail
  23. */
  24. static int lbs_associate(struct lbs_private *priv,
  25. struct assoc_request *assoc_req)
  26. {
  27. int ret;
  28. lbs_deb_enter(LBS_DEB_ASSOC);
  29. ret = lbs_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
  30. 0, CMD_OPTION_WAITFORRSP,
  31. 0, assoc_req->bss.bssid);
  32. if (ret)
  33. goto done;
  34. /* set preamble to firmware */
  35. if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  36. (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
  37. priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
  38. else
  39. priv->preamble = CMD_TYPE_LONG_PREAMBLE;
  40. lbs_set_radio_control(priv);
  41. ret = lbs_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
  42. 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
  43. done:
  44. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  45. return ret;
  46. }
  47. /**
  48. * @brief Join an adhoc network found in a previous scan
  49. *
  50. * @param priv A pointer to struct lbs_private structure
  51. * @param pbssdesc Pointer to a BSS descriptor found in a previous scan
  52. * to attempt to join
  53. *
  54. * @return 0--success, -1--fail
  55. */
  56. static int lbs_join_adhoc_network(struct lbs_private *priv,
  57. struct assoc_request *assoc_req)
  58. {
  59. struct bss_descriptor *bss = &assoc_req->bss;
  60. int ret = 0;
  61. lbs_deb_join("current SSID '%s', ssid length %u\n",
  62. escape_essid(priv->curbssparams.ssid,
  63. priv->curbssparams.ssid_len),
  64. priv->curbssparams.ssid_len);
  65. lbs_deb_join("requested ssid '%s', ssid length %u\n",
  66. escape_essid(bss->ssid, bss->ssid_len),
  67. bss->ssid_len);
  68. /* check if the requested SSID is already joined */
  69. if (priv->curbssparams.ssid_len &&
  70. !lbs_ssid_cmp(priv->curbssparams.ssid,
  71. priv->curbssparams.ssid_len,
  72. bss->ssid, bss->ssid_len) &&
  73. (priv->mode == IW_MODE_ADHOC) &&
  74. (priv->connect_status == LBS_CONNECTED)) {
  75. union iwreq_data wrqu;
  76. lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
  77. "current, not attempting to re-join");
  78. /* Send the re-association event though, because the association
  79. * request really was successful, even if just a null-op.
  80. */
  81. memset(&wrqu, 0, sizeof(wrqu));
  82. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
  83. ETH_ALEN);
  84. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  85. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  86. goto out;
  87. }
  88. /* Use shortpreamble only when both creator and card supports
  89. short preamble */
  90. if (!(bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) ||
  91. !(priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
  92. lbs_deb_join("AdhocJoin: Long preamble\n");
  93. priv->preamble = CMD_TYPE_LONG_PREAMBLE;
  94. } else {
  95. lbs_deb_join("AdhocJoin: Short preamble\n");
  96. priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
  97. }
  98. lbs_set_radio_control(priv);
  99. lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
  100. lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
  101. priv->adhoccreate = 0;
  102. ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_JOIN,
  103. 0, CMD_OPTION_WAITFORRSP,
  104. OID_802_11_SSID, assoc_req);
  105. out:
  106. return ret;
  107. }
  108. /**
  109. * @brief Start an Adhoc Network
  110. *
  111. * @param priv A pointer to struct lbs_private structure
  112. * @param adhocssid The ssid of the Adhoc Network
  113. * @return 0--success, -1--fail
  114. */
  115. static int lbs_start_adhoc_network(struct lbs_private *priv,
  116. struct assoc_request *assoc_req)
  117. {
  118. int ret = 0;
  119. priv->adhoccreate = 1;
  120. if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
  121. lbs_deb_join("AdhocStart: Short preamble\n");
  122. priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
  123. } else {
  124. lbs_deb_join("AdhocStart: Long preamble\n");
  125. priv->preamble = CMD_TYPE_LONG_PREAMBLE;
  126. }
  127. lbs_set_radio_control(priv);
  128. lbs_deb_join("AdhocStart: channel = %d\n", assoc_req->channel);
  129. lbs_deb_join("AdhocStart: band = %d\n", assoc_req->band);
  130. ret = lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_START,
  131. 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
  132. return ret;
  133. }
  134. int lbs_stop_adhoc_network(struct lbs_private *priv)
  135. {
  136. return lbs_prepare_and_send_command(priv, CMD_802_11_AD_HOC_STOP,
  137. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  138. }
  139. static inline int match_bss_no_security(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 match_bss_static_wep(struct lbs_802_11_security *secinfo,
  152. struct bss_descriptor *match_bss)
  153. {
  154. if (secinfo->wep_enabled && !secinfo->WPAenabled
  155. && !secinfo->WPA2enabled
  156. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  157. return 1;
  158. else
  159. return 0;
  160. }
  161. static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
  162. struct bss_descriptor *match_bss)
  163. {
  164. if (!secinfo->wep_enabled && secinfo->WPAenabled
  165. && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
  166. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  167. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
  168. )
  169. return 1;
  170. else
  171. return 0;
  172. }
  173. static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
  174. struct bss_descriptor *match_bss)
  175. {
  176. if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
  177. (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
  178. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  179. (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
  180. )
  181. return 1;
  182. else
  183. return 0;
  184. }
  185. static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
  186. struct bss_descriptor *match_bss)
  187. {
  188. if (!secinfo->wep_enabled && !secinfo->WPAenabled
  189. && !secinfo->WPA2enabled
  190. && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
  191. && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
  192. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  193. return 1;
  194. else
  195. return 0;
  196. }
  197. /**
  198. * @brief Check if a scanned network compatible with the driver settings
  199. *
  200. * WEP WPA WPA2 ad-hoc encrypt Network
  201. * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
  202. * 0 0 0 0 NONE 0 0 0 yes No security
  203. * 1 0 0 0 NONE 1 0 0 yes Static WEP
  204. * 0 1 0 0 x 1x 1 x yes WPA
  205. * 0 0 1 0 x 1x x 1 yes WPA2
  206. * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
  207. * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
  208. *
  209. *
  210. * @param priv A pointer to struct lbs_private
  211. * @param index Index in scantable to check against current driver settings
  212. * @param mode Network mode: Infrastructure or IBSS
  213. *
  214. * @return Index in scantable, or error code if negative
  215. */
  216. static int is_network_compatible(struct lbs_private *priv,
  217. struct bss_descriptor *bss, uint8_t mode)
  218. {
  219. int matched = 0;
  220. lbs_deb_enter(LBS_DEB_SCAN);
  221. if (bss->mode != mode)
  222. goto done;
  223. matched = match_bss_no_security(&priv->secinfo, bss);
  224. if (matched)
  225. goto done;
  226. matched = match_bss_static_wep(&priv->secinfo, bss);
  227. if (matched)
  228. goto done;
  229. matched = match_bss_wpa(&priv->secinfo, bss);
  230. if (matched) {
  231. lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
  232. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
  233. "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  234. priv->secinfo.wep_enabled ? "e" : "d",
  235. priv->secinfo.WPAenabled ? "e" : "d",
  236. priv->secinfo.WPA2enabled ? "e" : "d",
  237. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  238. goto done;
  239. }
  240. matched = match_bss_wpa2(&priv->secinfo, bss);
  241. if (matched) {
  242. lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
  243. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
  244. "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  245. priv->secinfo.wep_enabled ? "e" : "d",
  246. priv->secinfo.WPAenabled ? "e" : "d",
  247. priv->secinfo.WPA2enabled ? "e" : "d",
  248. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  249. goto done;
  250. }
  251. matched = match_bss_dynamic_wep(&priv->secinfo, bss);
  252. if (matched) {
  253. lbs_deb_scan("is_network_compatible() dynamic WEP: "
  254. "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
  255. bss->wpa_ie[0], bss->rsn_ie[0],
  256. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  257. goto done;
  258. }
  259. /* bss security settings don't match those configured on card */
  260. lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
  261. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
  262. bss->wpa_ie[0], bss->rsn_ie[0],
  263. priv->secinfo.wep_enabled ? "e" : "d",
  264. priv->secinfo.WPAenabled ? "e" : "d",
  265. priv->secinfo.WPA2enabled ? "e" : "d",
  266. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  267. done:
  268. lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
  269. return matched;
  270. }
  271. /**
  272. * @brief This function finds a specific compatible BSSID in the scan list
  273. *
  274. * Used in association code
  275. *
  276. * @param priv A pointer to struct lbs_private
  277. * @param bssid BSSID to find in the scan list
  278. * @param mode Network mode: Infrastructure or IBSS
  279. *
  280. * @return index in BSSID list, or error return code (< 0)
  281. */
  282. static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
  283. uint8_t *bssid, uint8_t mode)
  284. {
  285. struct bss_descriptor *iter_bss;
  286. struct bss_descriptor *found_bss = NULL;
  287. lbs_deb_enter(LBS_DEB_SCAN);
  288. if (!bssid)
  289. goto out;
  290. lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
  291. /* Look through the scan table for a compatible match. The loop will
  292. * continue past a matched bssid that is not compatible in case there
  293. * is an AP with multiple SSIDs assigned to the same BSSID
  294. */
  295. mutex_lock(&priv->lock);
  296. list_for_each_entry(iter_bss, &priv->network_list, list) {
  297. if (compare_ether_addr(iter_bss->bssid, bssid))
  298. continue; /* bssid doesn't match */
  299. switch (mode) {
  300. case IW_MODE_INFRA:
  301. case IW_MODE_ADHOC:
  302. if (!is_network_compatible(priv, iter_bss, mode))
  303. break;
  304. found_bss = iter_bss;
  305. break;
  306. default:
  307. found_bss = iter_bss;
  308. break;
  309. }
  310. }
  311. mutex_unlock(&priv->lock);
  312. out:
  313. lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
  314. return found_bss;
  315. }
  316. /**
  317. * @brief This function finds ssid in ssid list.
  318. *
  319. * Used in association code
  320. *
  321. * @param priv A pointer to struct lbs_private
  322. * @param ssid SSID to find in the list
  323. * @param bssid BSSID to qualify the SSID selection (if provided)
  324. * @param mode Network mode: Infrastructure or IBSS
  325. *
  326. * @return index in BSSID list
  327. */
  328. static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
  329. uint8_t *ssid, uint8_t ssid_len,
  330. uint8_t *bssid, uint8_t mode,
  331. int channel)
  332. {
  333. u32 bestrssi = 0;
  334. struct bss_descriptor *iter_bss = NULL;
  335. struct bss_descriptor *found_bss = NULL;
  336. struct bss_descriptor *tmp_oldest = NULL;
  337. lbs_deb_enter(LBS_DEB_SCAN);
  338. mutex_lock(&priv->lock);
  339. list_for_each_entry(iter_bss, &priv->network_list, list) {
  340. if (!tmp_oldest ||
  341. (iter_bss->last_scanned < tmp_oldest->last_scanned))
  342. tmp_oldest = iter_bss;
  343. if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
  344. ssid, ssid_len) != 0)
  345. continue; /* ssid doesn't match */
  346. if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
  347. continue; /* bssid doesn't match */
  348. if ((channel > 0) && (iter_bss->channel != channel))
  349. continue; /* channel doesn't match */
  350. switch (mode) {
  351. case IW_MODE_INFRA:
  352. case IW_MODE_ADHOC:
  353. if (!is_network_compatible(priv, iter_bss, mode))
  354. break;
  355. if (bssid) {
  356. /* Found requested BSSID */
  357. found_bss = iter_bss;
  358. goto out;
  359. }
  360. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  361. bestrssi = SCAN_RSSI(iter_bss->rssi);
  362. found_bss = iter_bss;
  363. }
  364. break;
  365. case IW_MODE_AUTO:
  366. default:
  367. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  368. bestrssi = SCAN_RSSI(iter_bss->rssi);
  369. found_bss = iter_bss;
  370. }
  371. break;
  372. }
  373. }
  374. out:
  375. mutex_unlock(&priv->lock);
  376. lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
  377. return found_bss;
  378. }
  379. static int assoc_helper_essid(struct lbs_private *priv,
  380. struct assoc_request * assoc_req)
  381. {
  382. int ret = 0;
  383. struct bss_descriptor * bss;
  384. int channel = -1;
  385. lbs_deb_enter(LBS_DEB_ASSOC);
  386. /* FIXME: take channel into account when picking SSIDs if a channel
  387. * is set.
  388. */
  389. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  390. channel = assoc_req->channel;
  391. lbs_deb_assoc("SSID '%s' requested\n",
  392. escape_essid(assoc_req->ssid, assoc_req->ssid_len));
  393. if (assoc_req->mode == IW_MODE_INFRA) {
  394. lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
  395. assoc_req->ssid_len);
  396. bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
  397. assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
  398. if (bss != NULL) {
  399. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  400. ret = lbs_associate(priv, assoc_req);
  401. } else {
  402. lbs_deb_assoc("SSID not found; cannot associate\n");
  403. }
  404. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  405. /* Scan for the network, do not save previous results. Stale
  406. * scan data will cause us to join a non-existant adhoc network
  407. */
  408. lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
  409. assoc_req->ssid_len);
  410. /* Search for the requested SSID in the scan table */
  411. bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
  412. assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
  413. if (bss != NULL) {
  414. lbs_deb_assoc("SSID found, will join\n");
  415. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  416. lbs_join_adhoc_network(priv, assoc_req);
  417. } else {
  418. /* else send START command */
  419. lbs_deb_assoc("SSID not found, creating adhoc network\n");
  420. memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
  421. IW_ESSID_MAX_SIZE);
  422. assoc_req->bss.ssid_len = assoc_req->ssid_len;
  423. lbs_start_adhoc_network(priv, assoc_req);
  424. }
  425. }
  426. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  427. return ret;
  428. }
  429. static int assoc_helper_bssid(struct lbs_private *priv,
  430. struct assoc_request * assoc_req)
  431. {
  432. int ret = 0;
  433. struct bss_descriptor * bss;
  434. DECLARE_MAC_BUF(mac);
  435. lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
  436. print_mac(mac, assoc_req->bssid));
  437. /* Search for index position in list for requested MAC */
  438. bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
  439. assoc_req->mode);
  440. if (bss == NULL) {
  441. lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
  442. "cannot associate.\n", print_mac(mac, assoc_req->bssid));
  443. goto out;
  444. }
  445. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  446. if (assoc_req->mode == IW_MODE_INFRA) {
  447. ret = lbs_associate(priv, assoc_req);
  448. lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
  449. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  450. lbs_join_adhoc_network(priv, assoc_req);
  451. }
  452. out:
  453. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  454. return ret;
  455. }
  456. static int assoc_helper_associate(struct lbs_private *priv,
  457. struct assoc_request * assoc_req)
  458. {
  459. int ret = 0, done = 0;
  460. lbs_deb_enter(LBS_DEB_ASSOC);
  461. /* If we're given and 'any' BSSID, try associating based on SSID */
  462. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  463. if (compare_ether_addr(bssid_any, assoc_req->bssid)
  464. && compare_ether_addr(bssid_off, assoc_req->bssid)) {
  465. ret = assoc_helper_bssid(priv, assoc_req);
  466. done = 1;
  467. }
  468. }
  469. if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  470. ret = assoc_helper_essid(priv, assoc_req);
  471. }
  472. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  473. return ret;
  474. }
  475. static int assoc_helper_mode(struct lbs_private *priv,
  476. struct assoc_request * assoc_req)
  477. {
  478. int ret = 0;
  479. lbs_deb_enter(LBS_DEB_ASSOC);
  480. if (assoc_req->mode == priv->mode)
  481. goto done;
  482. if (assoc_req->mode == IW_MODE_INFRA) {
  483. if (priv->psstate != PS_STATE_FULL_POWER)
  484. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  485. priv->psmode = LBS802_11POWERMODECAM;
  486. }
  487. priv->mode = assoc_req->mode;
  488. ret = lbs_prepare_and_send_command(priv,
  489. CMD_802_11_SNMP_MIB,
  490. 0, CMD_OPTION_WAITFORRSP,
  491. OID_802_11_INFRASTRUCTURE_MODE,
  492. /* Shoot me now */ (void *) (size_t) assoc_req->mode);
  493. done:
  494. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  495. return ret;
  496. }
  497. static int assoc_helper_channel(struct lbs_private *priv,
  498. struct assoc_request * assoc_req)
  499. {
  500. int ret = 0;
  501. lbs_deb_enter(LBS_DEB_ASSOC);
  502. ret = lbs_update_channel(priv);
  503. if (ret) {
  504. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  505. goto done;
  506. }
  507. if (assoc_req->channel == priv->curbssparams.channel)
  508. goto done;
  509. if (priv->mesh_dev) {
  510. /* Change mesh channel first; 21.p21 firmware won't let
  511. you change channel otherwise (even though it'll return
  512. an error to this */
  513. lbs_mesh_config(priv, 0, assoc_req->channel);
  514. }
  515. lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
  516. priv->curbssparams.channel, assoc_req->channel);
  517. ret = lbs_set_channel(priv, assoc_req->channel);
  518. if (ret < 0)
  519. lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
  520. /* FIXME: shouldn't need to grab the channel _again_ after setting
  521. * it since the firmware is supposed to return the new channel, but
  522. * whatever... */
  523. ret = lbs_update_channel(priv);
  524. if (ret) {
  525. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  526. goto done;
  527. }
  528. if (assoc_req->channel != priv->curbssparams.channel) {
  529. lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
  530. assoc_req->channel);
  531. goto restore_mesh;
  532. }
  533. if ( assoc_req->secinfo.wep_enabled
  534. && (assoc_req->wep_keys[0].len
  535. || assoc_req->wep_keys[1].len
  536. || assoc_req->wep_keys[2].len
  537. || assoc_req->wep_keys[3].len)) {
  538. /* Make sure WEP keys are re-sent to firmware */
  539. set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
  540. }
  541. /* Must restart/rejoin adhoc networks after channel change */
  542. set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
  543. restore_mesh:
  544. if (priv->mesh_dev)
  545. lbs_mesh_config(priv, 1, priv->curbssparams.channel);
  546. done:
  547. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  548. return ret;
  549. }
  550. static int assoc_helper_wep_keys(struct lbs_private *priv,
  551. struct assoc_request *assoc_req)
  552. {
  553. int i;
  554. int ret = 0;
  555. lbs_deb_enter(LBS_DEB_ASSOC);
  556. /* Set or remove WEP keys */
  557. if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
  558. assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
  559. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
  560. else
  561. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
  562. if (ret)
  563. goto out;
  564. /* enable/disable the MAC's WEP packet filter */
  565. if (assoc_req->secinfo.wep_enabled)
  566. priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
  567. else
  568. priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
  569. lbs_set_mac_control(priv);
  570. mutex_lock(&priv->lock);
  571. /* Copy WEP keys into priv wep key fields */
  572. for (i = 0; i < 4; i++) {
  573. memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
  574. sizeof(struct enc_key));
  575. }
  576. priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
  577. mutex_unlock(&priv->lock);
  578. out:
  579. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  580. return ret;
  581. }
  582. static int assoc_helper_secinfo(struct lbs_private *priv,
  583. struct assoc_request * assoc_req)
  584. {
  585. int ret = 0;
  586. uint16_t do_wpa;
  587. uint16_t rsn = 0;
  588. lbs_deb_enter(LBS_DEB_ASSOC);
  589. memcpy(&priv->secinfo, &assoc_req->secinfo,
  590. sizeof(struct lbs_802_11_security));
  591. lbs_set_mac_control(priv);
  592. /* If RSN is already enabled, don't try to enable it again, since
  593. * ENABLE_RSN resets internal state machines and will clobber the
  594. * 4-way WPA handshake.
  595. */
  596. /* Get RSN enabled/disabled */
  597. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
  598. if (ret) {
  599. lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
  600. goto out;
  601. }
  602. /* Don't re-enable RSN if it's already enabled */
  603. do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
  604. if (do_wpa == rsn)
  605. goto out;
  606. /* Set RSN enabled/disabled */
  607. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
  608. out:
  609. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  610. return ret;
  611. }
  612. static int assoc_helper_wpa_keys(struct lbs_private *priv,
  613. struct assoc_request * assoc_req)
  614. {
  615. int ret = 0;
  616. unsigned int flags = assoc_req->flags;
  617. lbs_deb_enter(LBS_DEB_ASSOC);
  618. /* Work around older firmware bug where WPA unicast and multicast
  619. * keys must be set independently. Seen in SDIO parts with firmware
  620. * version 5.0.11p0.
  621. */
  622. if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  623. clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
  624. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  625. assoc_req->flags = flags;
  626. }
  627. if (ret)
  628. goto out;
  629. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  630. clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
  631. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  632. assoc_req->flags = flags;
  633. }
  634. out:
  635. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  636. return ret;
  637. }
  638. static int assoc_helper_wpa_ie(struct lbs_private *priv,
  639. struct assoc_request * assoc_req)
  640. {
  641. int ret = 0;
  642. lbs_deb_enter(LBS_DEB_ASSOC);
  643. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  644. memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
  645. priv->wpa_ie_len = assoc_req->wpa_ie_len;
  646. } else {
  647. memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
  648. priv->wpa_ie_len = 0;
  649. }
  650. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  651. return ret;
  652. }
  653. static int should_deauth_infrastructure(struct lbs_private *priv,
  654. struct assoc_request * assoc_req)
  655. {
  656. int ret = 0;
  657. if (priv->connect_status != LBS_CONNECTED)
  658. return 0;
  659. lbs_deb_enter(LBS_DEB_ASSOC);
  660. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  661. lbs_deb_assoc("Deauthenticating due to new SSID\n");
  662. ret = 1;
  663. goto out;
  664. }
  665. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  666. if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
  667. lbs_deb_assoc("Deauthenticating due to new security\n");
  668. ret = 1;
  669. goto out;
  670. }
  671. }
  672. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  673. lbs_deb_assoc("Deauthenticating due to new BSSID\n");
  674. ret = 1;
  675. goto out;
  676. }
  677. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  678. lbs_deb_assoc("Deauthenticating due to channel switch\n");
  679. ret = 1;
  680. goto out;
  681. }
  682. /* FIXME: deal with 'auto' mode somehow */
  683. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  684. if (assoc_req->mode != IW_MODE_INFRA) {
  685. lbs_deb_assoc("Deauthenticating due to leaving "
  686. "infra mode\n");
  687. ret = 1;
  688. goto out;
  689. }
  690. }
  691. out:
  692. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  693. return ret;
  694. }
  695. static int should_stop_adhoc(struct lbs_private *priv,
  696. struct assoc_request * assoc_req)
  697. {
  698. lbs_deb_enter(LBS_DEB_ASSOC);
  699. if (priv->connect_status != LBS_CONNECTED)
  700. return 0;
  701. if (lbs_ssid_cmp(priv->curbssparams.ssid,
  702. priv->curbssparams.ssid_len,
  703. assoc_req->ssid, assoc_req->ssid_len) != 0)
  704. return 1;
  705. /* FIXME: deal with 'auto' mode somehow */
  706. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  707. if (assoc_req->mode != IW_MODE_ADHOC)
  708. return 1;
  709. }
  710. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  711. if (assoc_req->channel != priv->curbssparams.channel)
  712. return 1;
  713. }
  714. lbs_deb_leave(LBS_DEB_ASSOC);
  715. return 0;
  716. }
  717. /**
  718. * @brief This function finds the best SSID in the Scan List
  719. *
  720. * Search the scan table for the best SSID that also matches the current
  721. * adapter network preference (infrastructure or adhoc)
  722. *
  723. * @param priv A pointer to struct lbs_private
  724. *
  725. * @return index in BSSID list
  726. */
  727. static struct bss_descriptor *lbs_find_best_ssid_in_list(
  728. struct lbs_private *priv, uint8_t mode)
  729. {
  730. uint8_t bestrssi = 0;
  731. struct bss_descriptor *iter_bss;
  732. struct bss_descriptor *best_bss = NULL;
  733. lbs_deb_enter(LBS_DEB_SCAN);
  734. mutex_lock(&priv->lock);
  735. list_for_each_entry(iter_bss, &priv->network_list, list) {
  736. switch (mode) {
  737. case IW_MODE_INFRA:
  738. case IW_MODE_ADHOC:
  739. if (!is_network_compatible(priv, iter_bss, mode))
  740. break;
  741. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  742. break;
  743. bestrssi = SCAN_RSSI(iter_bss->rssi);
  744. best_bss = iter_bss;
  745. break;
  746. case IW_MODE_AUTO:
  747. default:
  748. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  749. break;
  750. bestrssi = SCAN_RSSI(iter_bss->rssi);
  751. best_bss = iter_bss;
  752. break;
  753. }
  754. }
  755. mutex_unlock(&priv->lock);
  756. lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
  757. return best_bss;
  758. }
  759. /**
  760. * @brief Find the best AP
  761. *
  762. * Used from association worker.
  763. *
  764. * @param priv A pointer to struct lbs_private structure
  765. * @param pSSID A pointer to AP's ssid
  766. *
  767. * @return 0--success, otherwise--fail
  768. */
  769. static int lbs_find_best_network_ssid(struct lbs_private *priv,
  770. uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
  771. uint8_t *out_mode)
  772. {
  773. int ret = -1;
  774. struct bss_descriptor *found;
  775. lbs_deb_enter(LBS_DEB_SCAN);
  776. priv->scan_ssid_len = 0;
  777. lbs_scan_networks(priv, 1);
  778. if (priv->surpriseremoved)
  779. goto out;
  780. found = lbs_find_best_ssid_in_list(priv, preferred_mode);
  781. if (found && (found->ssid_len > 0)) {
  782. memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
  783. *out_ssid_len = found->ssid_len;
  784. *out_mode = found->mode;
  785. ret = 0;
  786. }
  787. out:
  788. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  789. return ret;
  790. }
  791. void lbs_association_worker(struct work_struct *work)
  792. {
  793. struct lbs_private *priv = container_of(work, struct lbs_private,
  794. assoc_work.work);
  795. struct assoc_request * assoc_req = NULL;
  796. int ret = 0;
  797. int find_any_ssid = 0;
  798. DECLARE_MAC_BUF(mac);
  799. lbs_deb_enter(LBS_DEB_ASSOC);
  800. mutex_lock(&priv->lock);
  801. assoc_req = priv->pending_assoc_req;
  802. priv->pending_assoc_req = NULL;
  803. priv->in_progress_assoc_req = assoc_req;
  804. mutex_unlock(&priv->lock);
  805. if (!assoc_req)
  806. goto done;
  807. lbs_deb_assoc(
  808. "Association Request:\n"
  809. " flags: 0x%08lx\n"
  810. " SSID: '%s'\n"
  811. " chann: %d\n"
  812. " band: %d\n"
  813. " mode: %d\n"
  814. " BSSID: %s\n"
  815. " secinfo: %s%s%s\n"
  816. " auth_mode: %d\n",
  817. assoc_req->flags,
  818. escape_essid(assoc_req->ssid, assoc_req->ssid_len),
  819. assoc_req->channel, assoc_req->band, assoc_req->mode,
  820. print_mac(mac, assoc_req->bssid),
  821. assoc_req->secinfo.WPAenabled ? " WPA" : "",
  822. assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
  823. assoc_req->secinfo.wep_enabled ? " WEP" : "",
  824. assoc_req->secinfo.auth_mode);
  825. /* If 'any' SSID was specified, find an SSID to associate with */
  826. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
  827. && !assoc_req->ssid_len)
  828. find_any_ssid = 1;
  829. /* But don't use 'any' SSID if there's a valid locked BSSID to use */
  830. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  831. if (compare_ether_addr(assoc_req->bssid, bssid_any)
  832. && compare_ether_addr(assoc_req->bssid, bssid_off))
  833. find_any_ssid = 0;
  834. }
  835. if (find_any_ssid) {
  836. u8 new_mode = assoc_req->mode;
  837. ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
  838. &assoc_req->ssid_len, assoc_req->mode, &new_mode);
  839. if (ret) {
  840. lbs_deb_assoc("Could not find best network\n");
  841. ret = -ENETUNREACH;
  842. goto out;
  843. }
  844. /* Ensure we switch to the mode of the AP */
  845. if (assoc_req->mode == IW_MODE_AUTO) {
  846. set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
  847. assoc_req->mode = new_mode;
  848. }
  849. }
  850. /*
  851. * Check if the attributes being changing require deauthentication
  852. * from the currently associated infrastructure access point.
  853. */
  854. if (priv->mode == IW_MODE_INFRA) {
  855. if (should_deauth_infrastructure(priv, assoc_req)) {
  856. ret = lbs_send_deauthentication(priv);
  857. if (ret) {
  858. lbs_deb_assoc("Deauthentication due to new "
  859. "configuration request failed: %d\n",
  860. ret);
  861. }
  862. }
  863. } else if (priv->mode == IW_MODE_ADHOC) {
  864. if (should_stop_adhoc(priv, assoc_req)) {
  865. ret = lbs_stop_adhoc_network(priv);
  866. if (ret) {
  867. lbs_deb_assoc("Teardown of AdHoc network due to "
  868. "new configuration request failed: %d\n",
  869. ret);
  870. }
  871. }
  872. }
  873. /* Send the various configuration bits to the firmware */
  874. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  875. ret = assoc_helper_mode(priv, assoc_req);
  876. if (ret)
  877. goto out;
  878. }
  879. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  880. ret = assoc_helper_channel(priv, assoc_req);
  881. if (ret)
  882. goto out;
  883. }
  884. if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
  885. || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
  886. ret = assoc_helper_wep_keys(priv, assoc_req);
  887. if (ret)
  888. goto out;
  889. }
  890. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  891. ret = assoc_helper_secinfo(priv, assoc_req);
  892. if (ret)
  893. goto out;
  894. }
  895. if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  896. ret = assoc_helper_wpa_ie(priv, assoc_req);
  897. if (ret)
  898. goto out;
  899. }
  900. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
  901. || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  902. ret = assoc_helper_wpa_keys(priv, assoc_req);
  903. if (ret)
  904. goto out;
  905. }
  906. /* SSID/BSSID should be the _last_ config option set, because they
  907. * trigger the association attempt.
  908. */
  909. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
  910. || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  911. int success = 1;
  912. ret = assoc_helper_associate(priv, assoc_req);
  913. if (ret) {
  914. lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
  915. ret);
  916. success = 0;
  917. }
  918. if (priv->connect_status != LBS_CONNECTED) {
  919. lbs_deb_assoc("ASSOC: association unsuccessful, "
  920. "not connected\n");
  921. success = 0;
  922. }
  923. if (success) {
  924. lbs_deb_assoc("associated to %s\n",
  925. print_mac(mac, priv->curbssparams.bssid));
  926. lbs_prepare_and_send_command(priv,
  927. CMD_802_11_RSSI,
  928. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  929. } else {
  930. ret = -1;
  931. }
  932. }
  933. out:
  934. if (ret) {
  935. lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
  936. ret);
  937. }
  938. mutex_lock(&priv->lock);
  939. priv->in_progress_assoc_req = NULL;
  940. mutex_unlock(&priv->lock);
  941. kfree(assoc_req);
  942. done:
  943. lbs_deb_leave(LBS_DEB_ASSOC);
  944. }
  945. /*
  946. * Caller MUST hold any necessary locks
  947. */
  948. struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
  949. {
  950. struct assoc_request * assoc_req;
  951. lbs_deb_enter(LBS_DEB_ASSOC);
  952. if (!priv->pending_assoc_req) {
  953. priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
  954. GFP_KERNEL);
  955. if (!priv->pending_assoc_req) {
  956. lbs_pr_info("Not enough memory to allocate association"
  957. " request!\n");
  958. return NULL;
  959. }
  960. }
  961. /* Copy current configuration attributes to the association request,
  962. * but don't overwrite any that are already set.
  963. */
  964. assoc_req = priv->pending_assoc_req;
  965. if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  966. memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
  967. IW_ESSID_MAX_SIZE);
  968. assoc_req->ssid_len = priv->curbssparams.ssid_len;
  969. }
  970. if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  971. assoc_req->channel = priv->curbssparams.channel;
  972. if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
  973. assoc_req->band = priv->curbssparams.band;
  974. if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
  975. assoc_req->mode = priv->mode;
  976. if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  977. memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
  978. ETH_ALEN);
  979. }
  980. if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
  981. int i;
  982. for (i = 0; i < 4; i++) {
  983. memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
  984. sizeof(struct enc_key));
  985. }
  986. }
  987. if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
  988. assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
  989. if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  990. memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
  991. sizeof(struct enc_key));
  992. }
  993. if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  994. memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
  995. sizeof(struct enc_key));
  996. }
  997. if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  998. memcpy(&assoc_req->secinfo, &priv->secinfo,
  999. sizeof(struct lbs_802_11_security));
  1000. }
  1001. if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  1002. memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
  1003. MAX_WPA_IE_LEN);
  1004. assoc_req->wpa_ie_len = priv->wpa_ie_len;
  1005. }
  1006. lbs_deb_leave(LBS_DEB_ASSOC);
  1007. return assoc_req;
  1008. }
  1009. /**
  1010. * @brief This function finds common rates between rate1 and card rates.
  1011. *
  1012. * It will fill common rates in rate1 as output if found.
  1013. *
  1014. * NOTE: Setting the MSB of the basic rates need to be taken
  1015. * care, either before or after calling this function
  1016. *
  1017. * @param priv A pointer to struct lbs_private structure
  1018. * @param rate1 the buffer which keeps input and output
  1019. * @param rate1_size the size of rate1 buffer; new size of buffer on return
  1020. *
  1021. * @return 0 or -1
  1022. */
  1023. static int get_common_rates(struct lbs_private *priv,
  1024. u8 *rates,
  1025. u16 *rates_size)
  1026. {
  1027. u8 *card_rates = lbs_bg_rates;
  1028. size_t num_card_rates = sizeof(lbs_bg_rates);
  1029. int ret = 0, i, j;
  1030. u8 tmp[30];
  1031. size_t tmp_size = 0;
  1032. /* For each rate in card_rates that exists in rate1, copy to tmp */
  1033. for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
  1034. for (j = 0; rates[j] && (j < *rates_size); j++) {
  1035. if (rates[j] == card_rates[i])
  1036. tmp[tmp_size++] = card_rates[i];
  1037. }
  1038. }
  1039. lbs_deb_hex(LBS_DEB_JOIN, "AP rates ", rates, *rates_size);
  1040. lbs_deb_hex(LBS_DEB_JOIN, "card rates ", card_rates, num_card_rates);
  1041. lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
  1042. lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
  1043. if (!priv->auto_rate) {
  1044. for (i = 0; i < tmp_size; i++) {
  1045. if (tmp[i] == priv->cur_rate)
  1046. goto done;
  1047. }
  1048. lbs_pr_alert("Previously set fixed data rate %#x isn't "
  1049. "compatible with the network.\n", priv->cur_rate);
  1050. ret = -1;
  1051. goto done;
  1052. }
  1053. ret = 0;
  1054. done:
  1055. memset(rates, 0, *rates_size);
  1056. *rates_size = min_t(int, tmp_size, *rates_size);
  1057. memcpy(rates, tmp, *rates_size);
  1058. return ret;
  1059. }
  1060. /**
  1061. * @brief Sets the MSB on basic rates as the firmware requires
  1062. *
  1063. * Scan through an array and set the MSB for basic data rates.
  1064. *
  1065. * @param rates buffer of data rates
  1066. * @param len size of buffer
  1067. */
  1068. static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
  1069. {
  1070. int i;
  1071. for (i = 0; i < len; i++) {
  1072. if (rates[i] == 0x02 || rates[i] == 0x04 ||
  1073. rates[i] == 0x0b || rates[i] == 0x16)
  1074. rates[i] |= 0x80;
  1075. }
  1076. }
  1077. /**
  1078. * @brief Send Deauthentication Request
  1079. *
  1080. * @param priv A pointer to struct lbs_private structure
  1081. * @return 0--success, -1--fail
  1082. */
  1083. int lbs_send_deauthentication(struct lbs_private *priv)
  1084. {
  1085. return lbs_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
  1086. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  1087. }
  1088. /**
  1089. * @brief This function prepares command of authenticate.
  1090. *
  1091. * @param priv A pointer to struct lbs_private structure
  1092. * @param cmd A pointer to cmd_ds_command structure
  1093. * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
  1094. *
  1095. * @return 0 or -1
  1096. */
  1097. int lbs_cmd_80211_authenticate(struct lbs_private *priv,
  1098. struct cmd_ds_command *cmd,
  1099. void *pdata_buf)
  1100. {
  1101. struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
  1102. int ret = -1;
  1103. u8 *bssid = pdata_buf;
  1104. DECLARE_MAC_BUF(mac);
  1105. lbs_deb_enter(LBS_DEB_JOIN);
  1106. cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
  1107. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
  1108. + S_DS_GEN);
  1109. /* translate auth mode to 802.11 defined wire value */
  1110. switch (priv->secinfo.auth_mode) {
  1111. case IW_AUTH_ALG_OPEN_SYSTEM:
  1112. pauthenticate->authtype = 0x00;
  1113. break;
  1114. case IW_AUTH_ALG_SHARED_KEY:
  1115. pauthenticate->authtype = 0x01;
  1116. break;
  1117. case IW_AUTH_ALG_LEAP:
  1118. pauthenticate->authtype = 0x80;
  1119. break;
  1120. default:
  1121. lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
  1122. priv->secinfo.auth_mode);
  1123. goto out;
  1124. }
  1125. memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
  1126. lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
  1127. print_mac(mac, bssid), pauthenticate->authtype);
  1128. ret = 0;
  1129. out:
  1130. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1131. return ret;
  1132. }
  1133. int lbs_cmd_80211_deauthenticate(struct lbs_private *priv,
  1134. struct cmd_ds_command *cmd)
  1135. {
  1136. struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
  1137. lbs_deb_enter(LBS_DEB_JOIN);
  1138. cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
  1139. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
  1140. S_DS_GEN);
  1141. /* set AP MAC address */
  1142. memmove(dauth->macaddr, priv->curbssparams.bssid, ETH_ALEN);
  1143. /* Reason code 3 = Station is leaving */
  1144. #define REASON_CODE_STA_LEAVING 3
  1145. dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
  1146. lbs_deb_leave(LBS_DEB_JOIN);
  1147. return 0;
  1148. }
  1149. int lbs_cmd_80211_associate(struct lbs_private *priv,
  1150. struct cmd_ds_command *cmd, void *pdata_buf)
  1151. {
  1152. struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
  1153. int ret = 0;
  1154. struct assoc_request *assoc_req = pdata_buf;
  1155. struct bss_descriptor *bss = &assoc_req->bss;
  1156. u8 *pos;
  1157. u16 tmpcap, tmplen;
  1158. struct mrvlietypes_ssidparamset *ssid;
  1159. struct mrvlietypes_phyparamset *phy;
  1160. struct mrvlietypes_ssparamset *ss;
  1161. struct mrvlietypes_ratesparamset *rates;
  1162. struct mrvlietypes_rsnparamset *rsn;
  1163. lbs_deb_enter(LBS_DEB_ASSOC);
  1164. pos = (u8 *) passo;
  1165. if (!priv) {
  1166. ret = -1;
  1167. goto done;
  1168. }
  1169. cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  1170. memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
  1171. pos += sizeof(passo->peerstaaddr);
  1172. /* set the listen interval */
  1173. passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  1174. pos += sizeof(passo->capability);
  1175. pos += sizeof(passo->listeninterval);
  1176. pos += sizeof(passo->bcnperiod);
  1177. pos += sizeof(passo->dtimperiod);
  1178. ssid = (struct mrvlietypes_ssidparamset *) pos;
  1179. ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
  1180. tmplen = bss->ssid_len;
  1181. ssid->header.len = cpu_to_le16(tmplen);
  1182. memcpy(ssid->ssid, bss->ssid, tmplen);
  1183. pos += sizeof(ssid->header) + tmplen;
  1184. phy = (struct mrvlietypes_phyparamset *) pos;
  1185. phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  1186. tmplen = sizeof(phy->fh_ds.dsparamset);
  1187. phy->header.len = cpu_to_le16(tmplen);
  1188. memcpy(&phy->fh_ds.dsparamset,
  1189. &bss->phyparamset.dsparamset.currentchan,
  1190. tmplen);
  1191. pos += sizeof(phy->header) + tmplen;
  1192. ss = (struct mrvlietypes_ssparamset *) pos;
  1193. ss->header.type = cpu_to_le16(TLV_TYPE_CF);
  1194. tmplen = sizeof(ss->cf_ibss.cfparamset);
  1195. ss->header.len = cpu_to_le16(tmplen);
  1196. pos += sizeof(ss->header) + tmplen;
  1197. rates = (struct mrvlietypes_ratesparamset *) pos;
  1198. rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
  1199. memcpy(&rates->rates, &bss->rates, MAX_RATES);
  1200. tmplen = MAX_RATES;
  1201. if (get_common_rates(priv, rates->rates, &tmplen)) {
  1202. ret = -1;
  1203. goto done;
  1204. }
  1205. pos += sizeof(rates->header) + tmplen;
  1206. rates->header.len = cpu_to_le16(tmplen);
  1207. lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
  1208. /* Copy the infra. association rates into Current BSS state structure */
  1209. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  1210. memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
  1211. /* Set MSB on basic rates as the firmware requires, but _after_
  1212. * copying to current bss rates.
  1213. */
  1214. lbs_set_basic_rate_flags(rates->rates, tmplen);
  1215. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  1216. rsn = (struct mrvlietypes_rsnparamset *) pos;
  1217. /* WPA_IE or WPA2_IE */
  1218. rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
  1219. tmplen = (u16) assoc_req->wpa_ie[1];
  1220. rsn->header.len = cpu_to_le16(tmplen);
  1221. memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
  1222. lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
  1223. sizeof(rsn->header) + tmplen);
  1224. pos += sizeof(rsn->header) + tmplen;
  1225. }
  1226. /* update curbssparams */
  1227. priv->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
  1228. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  1229. ret = -1;
  1230. goto done;
  1231. }
  1232. cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
  1233. /* set the capability info */
  1234. tmpcap = (bss->capability & CAPINFO_MASK);
  1235. if (bss->mode == IW_MODE_INFRA)
  1236. tmpcap |= WLAN_CAPABILITY_ESS;
  1237. passo->capability = cpu_to_le16(tmpcap);
  1238. lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
  1239. done:
  1240. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1241. return ret;
  1242. }
  1243. int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
  1244. struct cmd_ds_command *cmd, void *pdata_buf)
  1245. {
  1246. struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
  1247. int ret = 0;
  1248. int cmdappendsize = 0;
  1249. struct assoc_request *assoc_req = pdata_buf;
  1250. u16 tmpcap = 0;
  1251. size_t ratesize = 0;
  1252. lbs_deb_enter(LBS_DEB_JOIN);
  1253. if (!priv) {
  1254. ret = -1;
  1255. goto done;
  1256. }
  1257. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
  1258. /*
  1259. * Fill in the parameters for 2 data structures:
  1260. * 1. cmd_ds_802_11_ad_hoc_start command
  1261. * 2. priv->scantable[i]
  1262. *
  1263. * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
  1264. * probe delay, and cap info.
  1265. *
  1266. * Firmware will fill up beacon period, DTIM, Basic rates
  1267. * and operational rates.
  1268. */
  1269. memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
  1270. memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
  1271. lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
  1272. escape_essid(assoc_req->ssid, assoc_req->ssid_len),
  1273. assoc_req->ssid_len);
  1274. /* set the BSS type */
  1275. adhs->bsstype = CMD_BSS_TYPE_IBSS;
  1276. priv->mode = IW_MODE_ADHOC;
  1277. if (priv->beacon_period == 0)
  1278. priv->beacon_period = MRVDRV_BEACON_INTERVAL;
  1279. adhs->beaconperiod = cpu_to_le16(priv->beacon_period);
  1280. /* set Physical param set */
  1281. #define DS_PARA_IE_ID 3
  1282. #define DS_PARA_IE_LEN 1
  1283. adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
  1284. adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
  1285. WARN_ON(!assoc_req->channel);
  1286. lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
  1287. assoc_req->channel);
  1288. adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
  1289. /* set IBSS param set */
  1290. #define IBSS_PARA_IE_ID 6
  1291. #define IBSS_PARA_IE_LEN 2
  1292. adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
  1293. adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
  1294. adhs->ssparamset.ibssparamset.atimwindow = 0;
  1295. /* set capability info */
  1296. tmpcap = WLAN_CAPABILITY_IBSS;
  1297. if (assoc_req->secinfo.wep_enabled) {
  1298. lbs_deb_join("ADHOC_S_CMD: WEP enabled, "
  1299. "setting privacy on\n");
  1300. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  1301. } else {
  1302. lbs_deb_join("ADHOC_S_CMD: WEP disabled, "
  1303. "setting privacy off\n");
  1304. }
  1305. adhs->capability = cpu_to_le16(tmpcap);
  1306. /* probedelay */
  1307. adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1308. memset(adhs->rates, 0, sizeof(adhs->rates));
  1309. ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
  1310. memcpy(adhs->rates, lbs_bg_rates, ratesize);
  1311. /* Copy the ad-hoc creating rates into Current BSS state structure */
  1312. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  1313. memcpy(&priv->curbssparams.rates, &adhs->rates, ratesize);
  1314. /* Set MSB on basic rates as the firmware requires, but _after_
  1315. * copying to current bss rates.
  1316. */
  1317. lbs_set_basic_rate_flags(adhs->rates, ratesize);
  1318. lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
  1319. adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
  1320. lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
  1321. if (lbs_create_dnld_countryinfo_11d(priv)) {
  1322. lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
  1323. ret = -1;
  1324. goto done;
  1325. }
  1326. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
  1327. S_DS_GEN + cmdappendsize);
  1328. ret = 0;
  1329. done:
  1330. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1331. return ret;
  1332. }
  1333. int lbs_cmd_80211_ad_hoc_stop(struct cmd_ds_command *cmd)
  1334. {
  1335. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
  1336. cmd->size = cpu_to_le16(S_DS_GEN);
  1337. return 0;
  1338. }
  1339. int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
  1340. struct cmd_ds_command *cmd, void *pdata_buf)
  1341. {
  1342. struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
  1343. struct assoc_request *assoc_req = pdata_buf;
  1344. struct bss_descriptor *bss = &assoc_req->bss;
  1345. int cmdappendsize = 0;
  1346. int ret = 0;
  1347. u16 ratesize = 0;
  1348. DECLARE_MAC_BUF(mac);
  1349. lbs_deb_enter(LBS_DEB_JOIN);
  1350. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
  1351. join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
  1352. join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
  1353. memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
  1354. memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
  1355. memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
  1356. sizeof(union ieeetypes_phyparamset));
  1357. memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
  1358. sizeof(union IEEEtypes_ssparamset));
  1359. join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  1360. lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  1361. bss->capability, CAPINFO_MASK);
  1362. /* information on BSSID descriptor passed to FW */
  1363. lbs_deb_join(
  1364. "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
  1365. print_mac(mac, join_cmd->bss.bssid),
  1366. join_cmd->bss.ssid);
  1367. /* failtimeout */
  1368. join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  1369. /* probedelay */
  1370. join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1371. priv->curbssparams.channel = bss->channel;
  1372. /* Copy Data rates from the rates recorded in scan response */
  1373. memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
  1374. ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
  1375. memcpy(join_cmd->bss.rates, bss->rates, ratesize);
  1376. if (get_common_rates(priv, join_cmd->bss.rates, &ratesize)) {
  1377. lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
  1378. ret = -1;
  1379. goto done;
  1380. }
  1381. /* Copy the ad-hoc creating rates into Current BSS state structure */
  1382. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  1383. memcpy(&priv->curbssparams.rates, join_cmd->bss.rates, ratesize);
  1384. /* Set MSB on basic rates as the firmware requires, but _after_
  1385. * copying to current bss rates.
  1386. */
  1387. lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
  1388. join_cmd->bss.ssparamset.ibssparamset.atimwindow =
  1389. cpu_to_le16(bss->atimwindow);
  1390. if (assoc_req->secinfo.wep_enabled) {
  1391. u16 tmp = le16_to_cpu(join_cmd->bss.capability);
  1392. tmp |= WLAN_CAPABILITY_PRIVACY;
  1393. join_cmd->bss.capability = cpu_to_le16(tmp);
  1394. }
  1395. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  1396. /* wake up first */
  1397. __le32 Localpsmode;
  1398. Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
  1399. ret = lbs_prepare_and_send_command(priv,
  1400. CMD_802_11_PS_MODE,
  1401. CMD_ACT_SET,
  1402. 0, 0, &Localpsmode);
  1403. if (ret) {
  1404. ret = -1;
  1405. goto done;
  1406. }
  1407. }
  1408. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  1409. ret = -1;
  1410. goto done;
  1411. }
  1412. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
  1413. S_DS_GEN + cmdappendsize);
  1414. done:
  1415. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1416. return ret;
  1417. }
  1418. int lbs_ret_80211_associate(struct lbs_private *priv,
  1419. struct cmd_ds_command *resp)
  1420. {
  1421. int ret = 0;
  1422. union iwreq_data wrqu;
  1423. struct ieeetypes_assocrsp *passocrsp;
  1424. struct bss_descriptor *bss;
  1425. u16 status_code;
  1426. lbs_deb_enter(LBS_DEB_ASSOC);
  1427. if (!priv->in_progress_assoc_req) {
  1428. lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
  1429. ret = -1;
  1430. goto done;
  1431. }
  1432. bss = &priv->in_progress_assoc_req->bss;
  1433. passocrsp = (struct ieeetypes_assocrsp *) &resp->params;
  1434. /*
  1435. * Older FW versions map the IEEE 802.11 Status Code in the association
  1436. * response to the following values returned in passocrsp->statuscode:
  1437. *
  1438. * IEEE Status Code Marvell Status Code
  1439. * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
  1440. * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1441. * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1442. * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1443. * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  1444. * others -> 0x0003 ASSOC_RESULT_REFUSED
  1445. *
  1446. * Other response codes:
  1447. * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
  1448. * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
  1449. * association response from the AP)
  1450. */
  1451. status_code = le16_to_cpu(passocrsp->statuscode);
  1452. switch (status_code) {
  1453. case 0x00:
  1454. break;
  1455. case 0x01:
  1456. lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
  1457. break;
  1458. case 0x02:
  1459. lbs_deb_assoc("ASSOC_RESP: internal timer "
  1460. "expired while waiting for the AP\n");
  1461. break;
  1462. case 0x03:
  1463. lbs_deb_assoc("ASSOC_RESP: association "
  1464. "refused by AP\n");
  1465. break;
  1466. case 0x04:
  1467. lbs_deb_assoc("ASSOC_RESP: authentication "
  1468. "refused by AP\n");
  1469. break;
  1470. default:
  1471. lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
  1472. " unknown\n", status_code);
  1473. break;
  1474. }
  1475. if (status_code) {
  1476. lbs_mac_event_disconnected(priv);
  1477. ret = -1;
  1478. goto done;
  1479. }
  1480. lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP", (void *)&resp->params,
  1481. le16_to_cpu(resp->size) - S_DS_GEN);
  1482. /* Send a Media Connected event, according to the Spec */
  1483. priv->connect_status = LBS_CONNECTED;
  1484. /* Update current SSID and BSSID */
  1485. memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  1486. priv->curbssparams.ssid_len = bss->ssid_len;
  1487. memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
  1488. priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
  1489. priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
  1490. memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
  1491. memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
  1492. priv->nextSNRNF = 0;
  1493. priv->numSNRNF = 0;
  1494. netif_carrier_on(priv->dev);
  1495. if (!priv->tx_pending_len)
  1496. netif_wake_queue(priv->dev);
  1497. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
  1498. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1499. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1500. done:
  1501. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1502. return ret;
  1503. }
  1504. int lbs_ret_80211_disassociate(struct lbs_private *priv)
  1505. {
  1506. lbs_deb_enter(LBS_DEB_JOIN);
  1507. lbs_mac_event_disconnected(priv);
  1508. lbs_deb_leave(LBS_DEB_JOIN);
  1509. return 0;
  1510. }
  1511. int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
  1512. struct cmd_ds_command *resp)
  1513. {
  1514. int ret = 0;
  1515. u16 command = le16_to_cpu(resp->command);
  1516. u16 result = le16_to_cpu(resp->result);
  1517. struct cmd_ds_802_11_ad_hoc_result *padhocresult;
  1518. union iwreq_data wrqu;
  1519. struct bss_descriptor *bss;
  1520. DECLARE_MAC_BUF(mac);
  1521. lbs_deb_enter(LBS_DEB_JOIN);
  1522. padhocresult = &resp->params.result;
  1523. lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
  1524. lbs_deb_join("ADHOC_RESP: command = %x\n", command);
  1525. lbs_deb_join("ADHOC_RESP: result = %x\n", result);
  1526. if (!priv->in_progress_assoc_req) {
  1527. lbs_deb_join("ADHOC_RESP: no in-progress association "
  1528. "request\n");
  1529. ret = -1;
  1530. goto done;
  1531. }
  1532. bss = &priv->in_progress_assoc_req->bss;
  1533. /*
  1534. * Join result code 0 --> SUCCESS
  1535. */
  1536. if (result) {
  1537. lbs_deb_join("ADHOC_RESP: failed\n");
  1538. if (priv->connect_status == LBS_CONNECTED)
  1539. lbs_mac_event_disconnected(priv);
  1540. ret = -1;
  1541. goto done;
  1542. }
  1543. /*
  1544. * Now the join cmd should be successful
  1545. * If BSSID has changed use SSID to compare instead of BSSID
  1546. */
  1547. lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
  1548. escape_essid(bss->ssid, bss->ssid_len));
  1549. /* Send a Media Connected event, according to the Spec */
  1550. priv->connect_status = LBS_CONNECTED;
  1551. if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
  1552. /* Update the created network descriptor with the new BSSID */
  1553. memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
  1554. }
  1555. /* Set the BSSID from the joined/started descriptor */
  1556. memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
  1557. /* Set the new SSID to current SSID */
  1558. memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  1559. priv->curbssparams.ssid_len = bss->ssid_len;
  1560. netif_carrier_on(priv->dev);
  1561. if (!priv->tx_pending_len)
  1562. netif_wake_queue(priv->dev);
  1563. memset(&wrqu, 0, sizeof(wrqu));
  1564. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
  1565. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  1566. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  1567. lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
  1568. lbs_deb_join("ADHOC_RESP: channel = %d\n", priv->curbssparams.channel);
  1569. lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
  1570. print_mac(mac, padhocresult->bssid));
  1571. done:
  1572. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  1573. return ret;
  1574. }
  1575. int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv)
  1576. {
  1577. lbs_deb_enter(LBS_DEB_JOIN);
  1578. lbs_mac_event_disconnected(priv);
  1579. lbs_deb_leave(LBS_DEB_JOIN);
  1580. return 0;
  1581. }