assoc.c 51 KB

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