assoc.c 51 KB

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