assoc.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  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 rate1 buffer; new size of buffer on return
  32. *
  33. * @return 0 on success, or -1 on error
  34. */
  35. static int get_common_rates(struct lbs_private *priv,
  36. u8 *rates,
  37. u16 *rates_size)
  38. {
  39. u8 *card_rates = lbs_bg_rates;
  40. size_t num_card_rates = sizeof(lbs_bg_rates);
  41. int ret = 0, i, j;
  42. u8 tmp[30];
  43. size_t tmp_size = 0;
  44. /* For each rate in card_rates that exists in rate1, copy to tmp */
  45. for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
  46. for (j = 0; rates[j] && (j < *rates_size); 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, num_card_rates);
  53. lbs_deb_hex(LBS_DEB_JOIN, "common rates", tmp, tmp_size);
  54. lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);
  55. if (!priv->enablehwauto) {
  56. for (i = 0; i < tmp_size; i++) {
  57. if (tmp[i] == priv->cur_rate)
  58. goto done;
  59. }
  60. lbs_pr_alert("Previously set fixed data rate %#x isn't "
  61. "compatible with the network.\n", priv->cur_rate);
  62. ret = -1;
  63. goto done;
  64. }
  65. ret = 0;
  66. done:
  67. memset(rates, 0, *rates_size);
  68. *rates_size = min_t(int, tmp_size, *rates_size);
  69. memcpy(rates, tmp, *rates_size);
  70. return ret;
  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. DECLARE_MAC_BUF(mac);
  116. lbs_deb_enter(LBS_DEB_JOIN);
  117. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  118. memcpy(cmd.bssid, bssid, ETH_ALEN);
  119. cmd.authtype = iw_auth_to_ieee_auth(auth);
  120. lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
  121. print_mac(mac, bssid), cmd.authtype);
  122. ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
  123. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  124. return ret;
  125. }
  126. static int lbs_assoc_post(struct lbs_private *priv,
  127. struct cmd_ds_802_11_associate_response *resp)
  128. {
  129. int ret = 0;
  130. union iwreq_data wrqu;
  131. struct bss_descriptor *bss;
  132. u16 status_code;
  133. lbs_deb_enter(LBS_DEB_ASSOC);
  134. if (!priv->in_progress_assoc_req) {
  135. lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
  136. ret = -1;
  137. goto done;
  138. }
  139. bss = &priv->in_progress_assoc_req->bss;
  140. /*
  141. * Older FW versions map the IEEE 802.11 Status Code in the association
  142. * response to the following values returned in resp->statuscode:
  143. *
  144. * IEEE Status Code Marvell Status Code
  145. * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
  146. * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  147. * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  148. * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  149. * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  150. * others -> 0x0003 ASSOC_RESULT_REFUSED
  151. *
  152. * Other response codes:
  153. * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
  154. * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
  155. * association response from the AP)
  156. */
  157. status_code = le16_to_cpu(resp->statuscode);
  158. if (priv->fwrelease < 0x09000000) {
  159. switch (status_code) {
  160. case 0x00:
  161. break;
  162. case 0x01:
  163. lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
  164. break;
  165. case 0x02:
  166. lbs_deb_assoc("ASSOC_RESP: internal timer "
  167. "expired while waiting for the AP\n");
  168. break;
  169. case 0x03:
  170. lbs_deb_assoc("ASSOC_RESP: association "
  171. "refused by AP\n");
  172. break;
  173. case 0x04:
  174. lbs_deb_assoc("ASSOC_RESP: authentication "
  175. "refused by AP\n");
  176. break;
  177. default:
  178. lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
  179. " unknown\n", status_code);
  180. break;
  181. }
  182. } else {
  183. /* v9+ returns the AP's association response */
  184. lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
  185. }
  186. if (status_code) {
  187. lbs_mac_event_disconnected(priv);
  188. ret = -1;
  189. goto done;
  190. }
  191. lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
  192. (void *) (resp + sizeof (resp->hdr)),
  193. le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));
  194. /* Send a Media Connected event, according to the Spec */
  195. priv->connect_status = LBS_CONNECTED;
  196. /* Update current SSID and BSSID */
  197. memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  198. priv->curbssparams.ssid_len = bss->ssid_len;
  199. memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
  200. priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
  201. priv->NF[TYPE_RXPD][TYPE_AVG] = 0;
  202. memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
  203. memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
  204. priv->nextSNRNF = 0;
  205. priv->numSNRNF = 0;
  206. netif_carrier_on(priv->dev);
  207. if (!priv->tx_pending_len)
  208. netif_wake_queue(priv->dev);
  209. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
  210. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  211. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  212. done:
  213. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  214. return ret;
  215. }
  216. /**
  217. * @brief This function prepares an association-class command.
  218. *
  219. * @param priv A pointer to struct lbs_private structure
  220. * @param assoc_req The association request describing the BSS to associate
  221. * or reassociate with
  222. * @param command The actual command, either CMD_802_11_ASSOCIATE or
  223. * CMD_802_11_REASSOCIATE
  224. *
  225. * @return 0 or -1
  226. */
  227. static int lbs_associate(struct lbs_private *priv,
  228. struct assoc_request *assoc_req,
  229. u16 command)
  230. {
  231. struct cmd_ds_802_11_associate cmd;
  232. int ret = 0;
  233. struct bss_descriptor *bss = &assoc_req->bss;
  234. u8 *pos = &(cmd.iebuf[0]);
  235. u16 tmpcap, tmplen, tmpauth;
  236. struct mrvl_ie_ssid_param_set *ssid;
  237. struct mrvl_ie_ds_param_set *ds;
  238. struct mrvl_ie_cf_param_set *cf;
  239. struct mrvl_ie_rates_param_set *rates;
  240. struct mrvl_ie_rsn_param_set *rsn;
  241. struct mrvl_ie_auth_type *auth;
  242. lbs_deb_enter(LBS_DEB_ASSOC);
  243. BUG_ON((command != CMD_802_11_ASSOCIATE) &&
  244. (command != CMD_802_11_REASSOCIATE));
  245. memset(&cmd, 0, sizeof(cmd));
  246. cmd.hdr.command = cpu_to_le16(command);
  247. /* Fill in static fields */
  248. memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
  249. cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  250. /* Capability info */
  251. tmpcap = (bss->capability & CAPINFO_MASK);
  252. if (bss->mode == IW_MODE_INFRA)
  253. tmpcap |= WLAN_CAPABILITY_ESS;
  254. cmd.capability = cpu_to_le16(tmpcap);
  255. lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
  256. /* SSID */
  257. ssid = (struct mrvl_ie_ssid_param_set *) pos;
  258. ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
  259. tmplen = bss->ssid_len;
  260. ssid->header.len = cpu_to_le16(tmplen);
  261. memcpy(ssid->ssid, bss->ssid, tmplen);
  262. pos += sizeof(ssid->header) + tmplen;
  263. ds = (struct mrvl_ie_ds_param_set *) pos;
  264. ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  265. ds->header.len = cpu_to_le16(1);
  266. ds->channel = bss->phy.ds.channel;
  267. pos += sizeof(ds->header) + 1;
  268. cf = (struct mrvl_ie_cf_param_set *) pos;
  269. cf->header.type = cpu_to_le16(TLV_TYPE_CF);
  270. tmplen = sizeof(*cf) - sizeof (cf->header);
  271. cf->header.len = cpu_to_le16(tmplen);
  272. /* IE payload should be zeroed, firmware fills it in for us */
  273. pos += sizeof(*cf);
  274. rates = (struct mrvl_ie_rates_param_set *) pos;
  275. rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
  276. memcpy(&rates->rates, &bss->rates, MAX_RATES);
  277. tmplen = MAX_RATES;
  278. if (get_common_rates(priv, rates->rates, &tmplen)) {
  279. ret = -1;
  280. goto done;
  281. }
  282. pos += sizeof(rates->header) + tmplen;
  283. rates->header.len = cpu_to_le16(tmplen);
  284. lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);
  285. /* Copy the infra. association rates into Current BSS state structure */
  286. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  287. memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);
  288. /* Set MSB on basic rates as the firmware requires, but _after_
  289. * copying to current bss rates.
  290. */
  291. lbs_set_basic_rate_flags(rates->rates, tmplen);
  292. /* Firmware v9+ indicate authentication suites as a TLV */
  293. if (priv->fwrelease >= 0x09000000) {
  294. DECLARE_MAC_BUF(mac);
  295. auth = (struct mrvl_ie_auth_type *) pos;
  296. auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
  297. auth->header.len = cpu_to_le16(2);
  298. tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
  299. auth->auth = cpu_to_le16(tmpauth);
  300. pos += sizeof(auth->header) + 2;
  301. lbs_deb_join("AUTH_CMD: BSSID %s, auth 0x%x\n",
  302. print_mac(mac, bss->bssid), priv->secinfo.auth_mode);
  303. }
  304. /* WPA/WPA2 IEs */
  305. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  306. rsn = (struct mrvl_ie_rsn_param_set *) pos;
  307. /* WPA_IE or WPA2_IE */
  308. rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
  309. tmplen = (u16) assoc_req->wpa_ie[1];
  310. rsn->header.len = cpu_to_le16(tmplen);
  311. memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
  312. lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
  313. sizeof(rsn->header) + tmplen);
  314. pos += sizeof(rsn->header) + tmplen;
  315. }
  316. cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
  317. (u16)(pos - (u8 *) &cmd.iebuf));
  318. /* update curbssparams */
  319. priv->curbssparams.channel = bss->phy.ds.channel;
  320. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  321. ret = -1;
  322. goto done;
  323. }
  324. ret = lbs_cmd_with_response(priv, command, &cmd);
  325. if (ret == 0) {
  326. ret = lbs_assoc_post(priv,
  327. (struct cmd_ds_802_11_associate_response *) &cmd);
  328. }
  329. done:
  330. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  331. return ret;
  332. }
  333. /**
  334. * @brief Associate to a specific BSS discovered in a scan
  335. *
  336. * @param priv A pointer to struct lbs_private structure
  337. * @param assoc_req The association request describing the BSS to associate with
  338. *
  339. * @return 0-success, otherwise fail
  340. */
  341. static int lbs_try_associate(struct lbs_private *priv,
  342. struct assoc_request *assoc_req)
  343. {
  344. int ret;
  345. u8 preamble = RADIO_PREAMBLE_LONG;
  346. lbs_deb_enter(LBS_DEB_ASSOC);
  347. /* FW v9 and higher indicate authentication suites as a TLV in the
  348. * association command, not as a separate authentication command.
  349. */
  350. if (priv->fwrelease < 0x09000000) {
  351. ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
  352. priv->secinfo.auth_mode);
  353. if (ret)
  354. goto out;
  355. }
  356. /* Use short preamble only when both the BSS and firmware support it */
  357. if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  358. (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
  359. preamble = RADIO_PREAMBLE_SHORT;
  360. ret = lbs_set_radio(priv, preamble, 1);
  361. if (ret)
  362. goto out;
  363. ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
  364. out:
  365. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  366. return ret;
  367. }
  368. static int lbs_adhoc_post(struct lbs_private *priv,
  369. struct cmd_ds_802_11_ad_hoc_result *resp)
  370. {
  371. int ret = 0;
  372. u16 command = le16_to_cpu(resp->hdr.command);
  373. u16 result = le16_to_cpu(resp->hdr.result);
  374. union iwreq_data wrqu;
  375. struct bss_descriptor *bss;
  376. DECLARE_SSID_BUF(ssid);
  377. lbs_deb_enter(LBS_DEB_JOIN);
  378. if (!priv->in_progress_assoc_req) {
  379. lbs_deb_join("ADHOC_RESP: no in-progress association "
  380. "request\n");
  381. ret = -1;
  382. goto done;
  383. }
  384. bss = &priv->in_progress_assoc_req->bss;
  385. /*
  386. * Join result code 0 --> SUCCESS
  387. */
  388. if (result) {
  389. lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
  390. if (priv->connect_status == LBS_CONNECTED)
  391. lbs_mac_event_disconnected(priv);
  392. ret = -1;
  393. goto done;
  394. }
  395. /* Send a Media Connected event, according to the Spec */
  396. priv->connect_status = LBS_CONNECTED;
  397. if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
  398. /* Update the created network descriptor with the new BSSID */
  399. memcpy(bss->bssid, resp->bssid, ETH_ALEN);
  400. }
  401. /* Set the BSSID from the joined/started descriptor */
  402. memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);
  403. /* Set the new SSID to current SSID */
  404. memcpy(&priv->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  405. priv->curbssparams.ssid_len = bss->ssid_len;
  406. netif_carrier_on(priv->dev);
  407. if (!priv->tx_pending_len)
  408. netif_wake_queue(priv->dev);
  409. memset(&wrqu, 0, sizeof(wrqu));
  410. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
  411. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  412. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  413. lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
  414. print_ssid(ssid, bss->ssid, bss->ssid_len),
  415. priv->curbssparams.bssid,
  416. priv->curbssparams.channel);
  417. done:
  418. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  419. return ret;
  420. }
  421. /**
  422. * @brief Join an adhoc network found in a previous scan
  423. *
  424. * @param priv A pointer to struct lbs_private structure
  425. * @param assoc_req The association request describing the BSS to join
  426. *
  427. * @return 0 on success, error on failure
  428. */
  429. static int lbs_adhoc_join(struct lbs_private *priv,
  430. struct assoc_request *assoc_req)
  431. {
  432. struct cmd_ds_802_11_ad_hoc_join cmd;
  433. struct bss_descriptor *bss = &assoc_req->bss;
  434. u8 preamble = RADIO_PREAMBLE_LONG;
  435. DECLARE_SSID_BUF(ssid);
  436. u16 ratesize = 0;
  437. int ret = 0;
  438. lbs_deb_enter(LBS_DEB_ASSOC);
  439. lbs_deb_join("current SSID '%s', ssid length %u\n",
  440. print_ssid(ssid, priv->curbssparams.ssid,
  441. priv->curbssparams.ssid_len),
  442. priv->curbssparams.ssid_len);
  443. lbs_deb_join("requested ssid '%s', ssid length %u\n",
  444. print_ssid(ssid, bss->ssid, bss->ssid_len),
  445. bss->ssid_len);
  446. /* check if the requested SSID is already joined */
  447. if (priv->curbssparams.ssid_len &&
  448. !lbs_ssid_cmp(priv->curbssparams.ssid,
  449. priv->curbssparams.ssid_len,
  450. bss->ssid, bss->ssid_len) &&
  451. (priv->mode == IW_MODE_ADHOC) &&
  452. (priv->connect_status == LBS_CONNECTED)) {
  453. union iwreq_data wrqu;
  454. lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
  455. "current, not attempting to re-join");
  456. /* Send the re-association event though, because the association
  457. * request really was successful, even if just a null-op.
  458. */
  459. memset(&wrqu, 0, sizeof(wrqu));
  460. memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
  461. ETH_ALEN);
  462. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  463. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  464. goto out;
  465. }
  466. /* Use short preamble only when both the BSS and firmware support it */
  467. if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
  468. (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
  469. lbs_deb_join("AdhocJoin: Short preamble\n");
  470. preamble = RADIO_PREAMBLE_SHORT;
  471. }
  472. ret = lbs_set_radio(priv, preamble, 1);
  473. if (ret)
  474. goto out;
  475. lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
  476. lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
  477. priv->adhoccreate = 0;
  478. priv->curbssparams.channel = bss->channel;
  479. /* Build the join command */
  480. memset(&cmd, 0, sizeof(cmd));
  481. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  482. cmd.bss.type = CMD_BSS_TYPE_IBSS;
  483. cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
  484. memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
  485. memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);
  486. memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
  487. memcpy(&cmd.bss.ibss, &bss->ss.ibss,
  488. sizeof(struct ieee_ie_ibss_param_set));
  489. cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  490. lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  491. bss->capability, CAPINFO_MASK);
  492. /* information on BSSID descriptor passed to FW */
  493. lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
  494. cmd.bss.bssid, cmd.bss.ssid);
  495. /* Only v8 and below support setting these */
  496. if (priv->fwrelease < 0x09000000) {
  497. /* failtimeout */
  498. cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  499. /* probedelay */
  500. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  501. }
  502. /* Copy Data rates from the rates recorded in scan response */
  503. memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
  504. ratesize = min_t(u16, sizeof(cmd.bss.rates), MAX_RATES);
  505. memcpy(cmd.bss.rates, bss->rates, ratesize);
  506. if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
  507. lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
  508. ret = -1;
  509. goto out;
  510. }
  511. /* Copy the ad-hoc creation rates into Current BSS state structure */
  512. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  513. memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);
  514. /* Set MSB on basic rates as the firmware requires, but _after_
  515. * copying to current bss rates.
  516. */
  517. lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);
  518. cmd.bss.ibss.atimwindow = bss->atimwindow;
  519. if (assoc_req->secinfo.wep_enabled) {
  520. u16 tmp = le16_to_cpu(cmd.bss.capability);
  521. tmp |= WLAN_CAPABILITY_PRIVACY;
  522. cmd.bss.capability = cpu_to_le16(tmp);
  523. }
  524. if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
  525. __le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);
  526. /* wake up first */
  527. ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
  528. CMD_ACT_SET, 0, 0,
  529. &local_ps_mode);
  530. if (ret) {
  531. ret = -1;
  532. goto out;
  533. }
  534. }
  535. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  536. ret = -1;
  537. goto out;
  538. }
  539. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
  540. if (ret == 0) {
  541. ret = lbs_adhoc_post(priv,
  542. (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
  543. }
  544. out:
  545. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  546. return ret;
  547. }
  548. /**
  549. * @brief Start an Adhoc Network
  550. *
  551. * @param priv A pointer to struct lbs_private structure
  552. * @param assoc_req The association request describing the BSS to start
  553. *
  554. * @return 0 on success, error on failure
  555. */
  556. static int lbs_adhoc_start(struct lbs_private *priv,
  557. struct assoc_request *assoc_req)
  558. {
  559. struct cmd_ds_802_11_ad_hoc_start cmd;
  560. u8 preamble = RADIO_PREAMBLE_LONG;
  561. size_t ratesize = 0;
  562. u16 tmpcap = 0;
  563. int ret = 0;
  564. DECLARE_SSID_BUF(ssid);
  565. lbs_deb_enter(LBS_DEB_ASSOC);
  566. if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
  567. lbs_deb_join("ADHOC_START: Will use short preamble\n");
  568. preamble = RADIO_PREAMBLE_SHORT;
  569. }
  570. ret = lbs_set_radio(priv, preamble, 1);
  571. if (ret)
  572. goto out;
  573. /* Build the start command */
  574. memset(&cmd, 0, sizeof(cmd));
  575. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  576. memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);
  577. lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
  578. print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
  579. assoc_req->ssid_len);
  580. cmd.bsstype = CMD_BSS_TYPE_IBSS;
  581. if (priv->beacon_period == 0)
  582. priv->beacon_period = MRVDRV_BEACON_INTERVAL;
  583. cmd.beaconperiod = cpu_to_le16(priv->beacon_period);
  584. WARN_ON(!assoc_req->channel);
  585. /* set Physical parameter set */
  586. cmd.ds.header.id = WLAN_EID_DS_PARAMS;
  587. cmd.ds.header.len = 1;
  588. cmd.ds.channel = assoc_req->channel;
  589. /* set IBSS parameter set */
  590. cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  591. cmd.ibss.header.len = 2;
  592. cmd.ibss.atimwindow = cpu_to_le16(0);
  593. /* set capability info */
  594. tmpcap = WLAN_CAPABILITY_IBSS;
  595. if (assoc_req->secinfo.wep_enabled ||
  596. assoc_req->secinfo.WPAenabled ||
  597. assoc_req->secinfo.WPA2enabled) {
  598. lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
  599. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  600. } else
  601. lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
  602. cmd.capability = cpu_to_le16(tmpcap);
  603. /* Only v8 and below support setting probe delay */
  604. if (priv->fwrelease < 0x09000000)
  605. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  606. ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
  607. memcpy(cmd.rates, lbs_bg_rates, ratesize);
  608. /* Copy the ad-hoc creating rates into Current BSS state structure */
  609. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  610. memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
  611. /* Set MSB on basic rates as the firmware requires, but _after_
  612. * copying to current bss rates.
  613. */
  614. lbs_set_basic_rate_flags(cmd.rates, ratesize);
  615. lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
  616. cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
  617. if (lbs_create_dnld_countryinfo_11d(priv)) {
  618. lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
  619. ret = -1;
  620. goto out;
  621. }
  622. lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
  623. assoc_req->channel, assoc_req->band);
  624. priv->adhoccreate = 1;
  625. priv->mode = IW_MODE_ADHOC;
  626. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
  627. if (ret == 0)
  628. ret = lbs_adhoc_post(priv,
  629. (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
  630. out:
  631. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  632. return ret;
  633. }
  634. /**
  635. * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
  636. *
  637. * @param priv A pointer to struct lbs_private structure
  638. * @return 0 on success, or an error
  639. */
  640. int lbs_adhoc_stop(struct lbs_private *priv)
  641. {
  642. struct cmd_ds_802_11_ad_hoc_stop cmd;
  643. int ret;
  644. lbs_deb_enter(LBS_DEB_JOIN);
  645. memset(&cmd, 0, sizeof (cmd));
  646. cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
  647. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
  648. /* Clean up everything even if there was an error */
  649. lbs_mac_event_disconnected(priv);
  650. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  651. return ret;
  652. }
  653. static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
  654. struct bss_descriptor *match_bss)
  655. {
  656. if (!secinfo->wep_enabled && !secinfo->WPAenabled
  657. && !secinfo->WPA2enabled
  658. && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
  659. && match_bss->rsn_ie[0] != WLAN_EID_RSN
  660. && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  661. return 1;
  662. else
  663. return 0;
  664. }
  665. static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
  666. struct bss_descriptor *match_bss)
  667. {
  668. if (secinfo->wep_enabled && !secinfo->WPAenabled
  669. && !secinfo->WPA2enabled
  670. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  671. return 1;
  672. else
  673. return 0;
  674. }
  675. static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
  676. struct bss_descriptor *match_bss)
  677. {
  678. if (!secinfo->wep_enabled && secinfo->WPAenabled
  679. && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
  680. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  681. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
  682. )
  683. return 1;
  684. else
  685. return 0;
  686. }
  687. static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
  688. struct bss_descriptor *match_bss)
  689. {
  690. if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
  691. (match_bss->rsn_ie[0] == WLAN_EID_RSN)
  692. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  693. (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
  694. )
  695. return 1;
  696. else
  697. return 0;
  698. }
  699. static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
  700. struct bss_descriptor *match_bss)
  701. {
  702. if (!secinfo->wep_enabled && !secinfo->WPAenabled
  703. && !secinfo->WPA2enabled
  704. && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
  705. && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
  706. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  707. return 1;
  708. else
  709. return 0;
  710. }
  711. /**
  712. * @brief Check if a scanned network compatible with the driver settings
  713. *
  714. * WEP WPA WPA2 ad-hoc encrypt Network
  715. * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
  716. * 0 0 0 0 NONE 0 0 0 yes No security
  717. * 1 0 0 0 NONE 1 0 0 yes Static WEP
  718. * 0 1 0 0 x 1x 1 x yes WPA
  719. * 0 0 1 0 x 1x x 1 yes WPA2
  720. * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
  721. * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
  722. *
  723. *
  724. * @param priv A pointer to struct lbs_private
  725. * @param index Index in scantable to check against current driver settings
  726. * @param mode Network mode: Infrastructure or IBSS
  727. *
  728. * @return Index in scantable, or error code if negative
  729. */
  730. static int is_network_compatible(struct lbs_private *priv,
  731. struct bss_descriptor *bss, uint8_t mode)
  732. {
  733. int matched = 0;
  734. lbs_deb_enter(LBS_DEB_SCAN);
  735. if (bss->mode != mode)
  736. goto done;
  737. matched = match_bss_no_security(&priv->secinfo, bss);
  738. if (matched)
  739. goto done;
  740. matched = match_bss_static_wep(&priv->secinfo, bss);
  741. if (matched)
  742. goto done;
  743. matched = match_bss_wpa(&priv->secinfo, bss);
  744. if (matched) {
  745. lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
  746. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
  747. "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  748. priv->secinfo.wep_enabled ? "e" : "d",
  749. priv->secinfo.WPAenabled ? "e" : "d",
  750. priv->secinfo.WPA2enabled ? "e" : "d",
  751. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  752. goto done;
  753. }
  754. matched = match_bss_wpa2(&priv->secinfo, bss);
  755. if (matched) {
  756. lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
  757. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
  758. "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  759. priv->secinfo.wep_enabled ? "e" : "d",
  760. priv->secinfo.WPAenabled ? "e" : "d",
  761. priv->secinfo.WPA2enabled ? "e" : "d",
  762. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  763. goto done;
  764. }
  765. matched = match_bss_dynamic_wep(&priv->secinfo, bss);
  766. if (matched) {
  767. lbs_deb_scan("is_network_compatible() dynamic WEP: "
  768. "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
  769. bss->wpa_ie[0], bss->rsn_ie[0],
  770. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  771. goto done;
  772. }
  773. /* bss security settings don't match those configured on card */
  774. lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
  775. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
  776. bss->wpa_ie[0], bss->rsn_ie[0],
  777. priv->secinfo.wep_enabled ? "e" : "d",
  778. priv->secinfo.WPAenabled ? "e" : "d",
  779. priv->secinfo.WPA2enabled ? "e" : "d",
  780. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  781. done:
  782. lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
  783. return matched;
  784. }
  785. /**
  786. * @brief This function finds a specific compatible BSSID in the scan list
  787. *
  788. * Used in association code
  789. *
  790. * @param priv A pointer to struct lbs_private
  791. * @param bssid BSSID to find in the scan list
  792. * @param mode Network mode: Infrastructure or IBSS
  793. *
  794. * @return index in BSSID list, or error return code (< 0)
  795. */
  796. static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
  797. uint8_t *bssid, uint8_t mode)
  798. {
  799. struct bss_descriptor *iter_bss;
  800. struct bss_descriptor *found_bss = NULL;
  801. lbs_deb_enter(LBS_DEB_SCAN);
  802. if (!bssid)
  803. goto out;
  804. lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
  805. /* Look through the scan table for a compatible match. The loop will
  806. * continue past a matched bssid that is not compatible in case there
  807. * is an AP with multiple SSIDs assigned to the same BSSID
  808. */
  809. mutex_lock(&priv->lock);
  810. list_for_each_entry(iter_bss, &priv->network_list, list) {
  811. if (compare_ether_addr(iter_bss->bssid, bssid))
  812. continue; /* bssid doesn't match */
  813. switch (mode) {
  814. case IW_MODE_INFRA:
  815. case IW_MODE_ADHOC:
  816. if (!is_network_compatible(priv, iter_bss, mode))
  817. break;
  818. found_bss = iter_bss;
  819. break;
  820. default:
  821. found_bss = iter_bss;
  822. break;
  823. }
  824. }
  825. mutex_unlock(&priv->lock);
  826. out:
  827. lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
  828. return found_bss;
  829. }
  830. /**
  831. * @brief This function finds ssid in ssid list.
  832. *
  833. * Used in association code
  834. *
  835. * @param priv A pointer to struct lbs_private
  836. * @param ssid SSID to find in the list
  837. * @param bssid BSSID to qualify the SSID selection (if provided)
  838. * @param mode Network mode: Infrastructure or IBSS
  839. *
  840. * @return index in BSSID list
  841. */
  842. static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
  843. uint8_t *ssid, uint8_t ssid_len,
  844. uint8_t *bssid, uint8_t mode,
  845. int channel)
  846. {
  847. u32 bestrssi = 0;
  848. struct bss_descriptor *iter_bss = NULL;
  849. struct bss_descriptor *found_bss = NULL;
  850. struct bss_descriptor *tmp_oldest = NULL;
  851. lbs_deb_enter(LBS_DEB_SCAN);
  852. mutex_lock(&priv->lock);
  853. list_for_each_entry(iter_bss, &priv->network_list, list) {
  854. if (!tmp_oldest ||
  855. (iter_bss->last_scanned < tmp_oldest->last_scanned))
  856. tmp_oldest = iter_bss;
  857. if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
  858. ssid, ssid_len) != 0)
  859. continue; /* ssid doesn't match */
  860. if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
  861. continue; /* bssid doesn't match */
  862. if ((channel > 0) && (iter_bss->channel != channel))
  863. continue; /* channel doesn't match */
  864. switch (mode) {
  865. case IW_MODE_INFRA:
  866. case IW_MODE_ADHOC:
  867. if (!is_network_compatible(priv, iter_bss, mode))
  868. break;
  869. if (bssid) {
  870. /* Found requested BSSID */
  871. found_bss = iter_bss;
  872. goto out;
  873. }
  874. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  875. bestrssi = SCAN_RSSI(iter_bss->rssi);
  876. found_bss = iter_bss;
  877. }
  878. break;
  879. case IW_MODE_AUTO:
  880. default:
  881. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  882. bestrssi = SCAN_RSSI(iter_bss->rssi);
  883. found_bss = iter_bss;
  884. }
  885. break;
  886. }
  887. }
  888. out:
  889. mutex_unlock(&priv->lock);
  890. lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
  891. return found_bss;
  892. }
  893. static int assoc_helper_essid(struct lbs_private *priv,
  894. struct assoc_request * assoc_req)
  895. {
  896. int ret = 0;
  897. struct bss_descriptor * bss;
  898. int channel = -1;
  899. DECLARE_SSID_BUF(ssid);
  900. lbs_deb_enter(LBS_DEB_ASSOC);
  901. /* FIXME: take channel into account when picking SSIDs if a channel
  902. * is set.
  903. */
  904. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  905. channel = assoc_req->channel;
  906. lbs_deb_assoc("SSID '%s' requested\n",
  907. print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
  908. if (assoc_req->mode == IW_MODE_INFRA) {
  909. lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
  910. assoc_req->ssid_len);
  911. bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
  912. assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
  913. if (bss != NULL) {
  914. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  915. ret = lbs_try_associate(priv, assoc_req);
  916. } else {
  917. lbs_deb_assoc("SSID not found; cannot associate\n");
  918. }
  919. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  920. /* Scan for the network, do not save previous results. Stale
  921. * scan data will cause us to join a non-existant adhoc network
  922. */
  923. lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
  924. assoc_req->ssid_len);
  925. /* Search for the requested SSID in the scan table */
  926. bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
  927. assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
  928. if (bss != NULL) {
  929. lbs_deb_assoc("SSID found, will join\n");
  930. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  931. lbs_adhoc_join(priv, assoc_req);
  932. } else {
  933. /* else send START command */
  934. lbs_deb_assoc("SSID not found, creating adhoc network\n");
  935. memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
  936. IW_ESSID_MAX_SIZE);
  937. assoc_req->bss.ssid_len = assoc_req->ssid_len;
  938. lbs_adhoc_start(priv, assoc_req);
  939. }
  940. }
  941. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  942. return ret;
  943. }
  944. static int assoc_helper_bssid(struct lbs_private *priv,
  945. struct assoc_request * assoc_req)
  946. {
  947. int ret = 0;
  948. struct bss_descriptor * bss;
  949. lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
  950. /* Search for index position in list for requested MAC */
  951. bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
  952. assoc_req->mode);
  953. if (bss == NULL) {
  954. lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
  955. "cannot associate.\n", assoc_req->bssid);
  956. goto out;
  957. }
  958. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  959. if (assoc_req->mode == IW_MODE_INFRA) {
  960. ret = lbs_try_associate(priv, assoc_req);
  961. lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
  962. ret);
  963. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  964. lbs_adhoc_join(priv, assoc_req);
  965. }
  966. out:
  967. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  968. return ret;
  969. }
  970. static int assoc_helper_associate(struct lbs_private *priv,
  971. struct assoc_request * assoc_req)
  972. {
  973. int ret = 0, done = 0;
  974. lbs_deb_enter(LBS_DEB_ASSOC);
  975. /* If we're given and 'any' BSSID, try associating based on SSID */
  976. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  977. if (compare_ether_addr(bssid_any, assoc_req->bssid)
  978. && compare_ether_addr(bssid_off, assoc_req->bssid)) {
  979. ret = assoc_helper_bssid(priv, assoc_req);
  980. done = 1;
  981. }
  982. }
  983. if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  984. ret = assoc_helper_essid(priv, assoc_req);
  985. }
  986. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  987. return ret;
  988. }
  989. static int assoc_helper_mode(struct lbs_private *priv,
  990. struct assoc_request * assoc_req)
  991. {
  992. int ret = 0;
  993. lbs_deb_enter(LBS_DEB_ASSOC);
  994. if (assoc_req->mode == priv->mode)
  995. goto done;
  996. if (assoc_req->mode == IW_MODE_INFRA) {
  997. if (priv->psstate != PS_STATE_FULL_POWER)
  998. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  999. priv->psmode = LBS802_11POWERMODECAM;
  1000. }
  1001. priv->mode = assoc_req->mode;
  1002. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
  1003. done:
  1004. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1005. return ret;
  1006. }
  1007. static int assoc_helper_channel(struct lbs_private *priv,
  1008. struct assoc_request * assoc_req)
  1009. {
  1010. int ret = 0;
  1011. lbs_deb_enter(LBS_DEB_ASSOC);
  1012. ret = lbs_update_channel(priv);
  1013. if (ret) {
  1014. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  1015. goto done;
  1016. }
  1017. if (assoc_req->channel == priv->curbssparams.channel)
  1018. goto done;
  1019. if (priv->mesh_dev) {
  1020. /* Change mesh channel first; 21.p21 firmware won't let
  1021. you change channel otherwise (even though it'll return
  1022. an error to this */
  1023. lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
  1024. assoc_req->channel);
  1025. }
  1026. lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
  1027. priv->curbssparams.channel, assoc_req->channel);
  1028. ret = lbs_set_channel(priv, assoc_req->channel);
  1029. if (ret < 0)
  1030. lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
  1031. /* FIXME: shouldn't need to grab the channel _again_ after setting
  1032. * it since the firmware is supposed to return the new channel, but
  1033. * whatever... */
  1034. ret = lbs_update_channel(priv);
  1035. if (ret) {
  1036. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  1037. goto done;
  1038. }
  1039. if (assoc_req->channel != priv->curbssparams.channel) {
  1040. lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
  1041. assoc_req->channel);
  1042. goto restore_mesh;
  1043. }
  1044. if ( assoc_req->secinfo.wep_enabled
  1045. && (assoc_req->wep_keys[0].len
  1046. || assoc_req->wep_keys[1].len
  1047. || assoc_req->wep_keys[2].len
  1048. || assoc_req->wep_keys[3].len)) {
  1049. /* Make sure WEP keys are re-sent to firmware */
  1050. set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
  1051. }
  1052. /* Must restart/rejoin adhoc networks after channel change */
  1053. set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
  1054. restore_mesh:
  1055. if (priv->mesh_dev)
  1056. lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  1057. priv->curbssparams.channel);
  1058. done:
  1059. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1060. return ret;
  1061. }
  1062. static int assoc_helper_wep_keys(struct lbs_private *priv,
  1063. struct assoc_request *assoc_req)
  1064. {
  1065. int i;
  1066. int ret = 0;
  1067. lbs_deb_enter(LBS_DEB_ASSOC);
  1068. /* Set or remove WEP keys */
  1069. if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
  1070. assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
  1071. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
  1072. else
  1073. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
  1074. if (ret)
  1075. goto out;
  1076. /* enable/disable the MAC's WEP packet filter */
  1077. if (assoc_req->secinfo.wep_enabled)
  1078. priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
  1079. else
  1080. priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
  1081. lbs_set_mac_control(priv);
  1082. mutex_lock(&priv->lock);
  1083. /* Copy WEP keys into priv wep key fields */
  1084. for (i = 0; i < 4; i++) {
  1085. memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
  1086. sizeof(struct enc_key));
  1087. }
  1088. priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
  1089. mutex_unlock(&priv->lock);
  1090. out:
  1091. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1092. return ret;
  1093. }
  1094. static int assoc_helper_secinfo(struct lbs_private *priv,
  1095. struct assoc_request * assoc_req)
  1096. {
  1097. int ret = 0;
  1098. uint16_t do_wpa;
  1099. uint16_t rsn = 0;
  1100. lbs_deb_enter(LBS_DEB_ASSOC);
  1101. memcpy(&priv->secinfo, &assoc_req->secinfo,
  1102. sizeof(struct lbs_802_11_security));
  1103. lbs_set_mac_control(priv);
  1104. /* If RSN is already enabled, don't try to enable it again, since
  1105. * ENABLE_RSN resets internal state machines and will clobber the
  1106. * 4-way WPA handshake.
  1107. */
  1108. /* Get RSN enabled/disabled */
  1109. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
  1110. if (ret) {
  1111. lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
  1112. goto out;
  1113. }
  1114. /* Don't re-enable RSN if it's already enabled */
  1115. do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
  1116. if (do_wpa == rsn)
  1117. goto out;
  1118. /* Set RSN enabled/disabled */
  1119. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
  1120. out:
  1121. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1122. return ret;
  1123. }
  1124. static int assoc_helper_wpa_keys(struct lbs_private *priv,
  1125. struct assoc_request * assoc_req)
  1126. {
  1127. int ret = 0;
  1128. unsigned int flags = assoc_req->flags;
  1129. lbs_deb_enter(LBS_DEB_ASSOC);
  1130. /* Work around older firmware bug where WPA unicast and multicast
  1131. * keys must be set independently. Seen in SDIO parts with firmware
  1132. * version 5.0.11p0.
  1133. */
  1134. if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  1135. clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
  1136. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  1137. assoc_req->flags = flags;
  1138. }
  1139. if (ret)
  1140. goto out;
  1141. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  1142. clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
  1143. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  1144. assoc_req->flags = flags;
  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. }