assoc.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  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. lbs_deb_join("ADHOC_START: WEP enabled, setting privacy on\n");
  597. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  598. } else
  599. lbs_deb_join("ADHOC_START: WEP disabled, setting privacy off\n");
  600. cmd.capability = cpu_to_le16(tmpcap);
  601. /* Only v8 and below support setting probe delay */
  602. if (priv->fwrelease < 0x09000000)
  603. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  604. ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
  605. memcpy(cmd.rates, lbs_bg_rates, ratesize);
  606. /* Copy the ad-hoc creating rates into Current BSS state structure */
  607. memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
  608. memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);
  609. /* Set MSB on basic rates as the firmware requires, but _after_
  610. * copying to current bss rates.
  611. */
  612. lbs_set_basic_rate_flags(cmd.rates, ratesize);
  613. lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
  614. cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);
  615. if (lbs_create_dnld_countryinfo_11d(priv)) {
  616. lbs_deb_join("ADHOC_START: dnld_countryinfo_11d failed\n");
  617. ret = -1;
  618. goto out;
  619. }
  620. lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
  621. assoc_req->channel, assoc_req->band);
  622. priv->adhoccreate = 1;
  623. priv->mode = IW_MODE_ADHOC;
  624. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
  625. if (ret == 0)
  626. ret = lbs_adhoc_post(priv,
  627. (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
  628. out:
  629. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  630. return ret;
  631. }
  632. /**
  633. * @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
  634. *
  635. * @param priv A pointer to struct lbs_private structure
  636. * @return 0 on success, or an error
  637. */
  638. int lbs_adhoc_stop(struct lbs_private *priv)
  639. {
  640. struct cmd_ds_802_11_ad_hoc_stop cmd;
  641. int ret;
  642. lbs_deb_enter(LBS_DEB_JOIN);
  643. memset(&cmd, 0, sizeof (cmd));
  644. cmd.hdr.size = cpu_to_le16 (sizeof (cmd));
  645. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
  646. /* Clean up everything even if there was an error */
  647. lbs_mac_event_disconnected(priv);
  648. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  649. return ret;
  650. }
  651. static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
  652. struct bss_descriptor *match_bss)
  653. {
  654. if (!secinfo->wep_enabled && !secinfo->WPAenabled
  655. && !secinfo->WPA2enabled
  656. && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
  657. && match_bss->rsn_ie[0] != WLAN_EID_RSN
  658. && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  659. return 1;
  660. else
  661. return 0;
  662. }
  663. static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
  664. struct bss_descriptor *match_bss)
  665. {
  666. if (secinfo->wep_enabled && !secinfo->WPAenabled
  667. && !secinfo->WPA2enabled
  668. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  669. return 1;
  670. else
  671. return 0;
  672. }
  673. static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
  674. struct bss_descriptor *match_bss)
  675. {
  676. if (!secinfo->wep_enabled && secinfo->WPAenabled
  677. && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
  678. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  679. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
  680. )
  681. return 1;
  682. else
  683. return 0;
  684. }
  685. static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
  686. struct bss_descriptor *match_bss)
  687. {
  688. if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
  689. (match_bss->rsn_ie[0] == WLAN_EID_RSN)
  690. /* privacy bit may NOT be set in some APs like LinkSys WRT54G
  691. (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
  692. )
  693. return 1;
  694. else
  695. return 0;
  696. }
  697. static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
  698. struct bss_descriptor *match_bss)
  699. {
  700. if (!secinfo->wep_enabled && !secinfo->WPAenabled
  701. && !secinfo->WPA2enabled
  702. && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
  703. && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
  704. && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
  705. return 1;
  706. else
  707. return 0;
  708. }
  709. /**
  710. * @brief Check if a scanned network compatible with the driver settings
  711. *
  712. * WEP WPA WPA2 ad-hoc encrypt Network
  713. * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
  714. * 0 0 0 0 NONE 0 0 0 yes No security
  715. * 1 0 0 0 NONE 1 0 0 yes Static WEP
  716. * 0 1 0 0 x 1x 1 x yes WPA
  717. * 0 0 1 0 x 1x x 1 yes WPA2
  718. * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
  719. * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
  720. *
  721. *
  722. * @param priv A pointer to struct lbs_private
  723. * @param index Index in scantable to check against current driver settings
  724. * @param mode Network mode: Infrastructure or IBSS
  725. *
  726. * @return Index in scantable, or error code if negative
  727. */
  728. static int is_network_compatible(struct lbs_private *priv,
  729. struct bss_descriptor *bss, uint8_t mode)
  730. {
  731. int matched = 0;
  732. lbs_deb_enter(LBS_DEB_SCAN);
  733. if (bss->mode != mode)
  734. goto done;
  735. matched = match_bss_no_security(&priv->secinfo, bss);
  736. if (matched)
  737. goto done;
  738. matched = match_bss_static_wep(&priv->secinfo, bss);
  739. if (matched)
  740. goto done;
  741. matched = match_bss_wpa(&priv->secinfo, bss);
  742. if (matched) {
  743. lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
  744. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
  745. "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  746. priv->secinfo.wep_enabled ? "e" : "d",
  747. priv->secinfo.WPAenabled ? "e" : "d",
  748. priv->secinfo.WPA2enabled ? "e" : "d",
  749. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  750. goto done;
  751. }
  752. matched = match_bss_wpa2(&priv->secinfo, bss);
  753. if (matched) {
  754. lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
  755. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
  756. "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
  757. priv->secinfo.wep_enabled ? "e" : "d",
  758. priv->secinfo.WPAenabled ? "e" : "d",
  759. priv->secinfo.WPA2enabled ? "e" : "d",
  760. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  761. goto done;
  762. }
  763. matched = match_bss_dynamic_wep(&priv->secinfo, bss);
  764. if (matched) {
  765. lbs_deb_scan("is_network_compatible() dynamic WEP: "
  766. "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
  767. bss->wpa_ie[0], bss->rsn_ie[0],
  768. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  769. goto done;
  770. }
  771. /* bss security settings don't match those configured on card */
  772. lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
  773. "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
  774. bss->wpa_ie[0], bss->rsn_ie[0],
  775. priv->secinfo.wep_enabled ? "e" : "d",
  776. priv->secinfo.WPAenabled ? "e" : "d",
  777. priv->secinfo.WPA2enabled ? "e" : "d",
  778. (bss->capability & WLAN_CAPABILITY_PRIVACY));
  779. done:
  780. lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
  781. return matched;
  782. }
  783. /**
  784. * @brief This function finds a specific compatible BSSID in the scan list
  785. *
  786. * Used in association code
  787. *
  788. * @param priv A pointer to struct lbs_private
  789. * @param bssid BSSID to find in the scan list
  790. * @param mode Network mode: Infrastructure or IBSS
  791. *
  792. * @return index in BSSID list, or error return code (< 0)
  793. */
  794. static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
  795. uint8_t *bssid, uint8_t mode)
  796. {
  797. struct bss_descriptor *iter_bss;
  798. struct bss_descriptor *found_bss = NULL;
  799. lbs_deb_enter(LBS_DEB_SCAN);
  800. if (!bssid)
  801. goto out;
  802. lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
  803. /* Look through the scan table for a compatible match. The loop will
  804. * continue past a matched bssid that is not compatible in case there
  805. * is an AP with multiple SSIDs assigned to the same BSSID
  806. */
  807. mutex_lock(&priv->lock);
  808. list_for_each_entry(iter_bss, &priv->network_list, list) {
  809. if (compare_ether_addr(iter_bss->bssid, bssid))
  810. continue; /* bssid doesn't match */
  811. switch (mode) {
  812. case IW_MODE_INFRA:
  813. case IW_MODE_ADHOC:
  814. if (!is_network_compatible(priv, iter_bss, mode))
  815. break;
  816. found_bss = iter_bss;
  817. break;
  818. default:
  819. found_bss = iter_bss;
  820. break;
  821. }
  822. }
  823. mutex_unlock(&priv->lock);
  824. out:
  825. lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
  826. return found_bss;
  827. }
  828. /**
  829. * @brief This function finds ssid in ssid list.
  830. *
  831. * Used in association code
  832. *
  833. * @param priv A pointer to struct lbs_private
  834. * @param ssid SSID to find in the list
  835. * @param bssid BSSID to qualify the SSID selection (if provided)
  836. * @param mode Network mode: Infrastructure or IBSS
  837. *
  838. * @return index in BSSID list
  839. */
  840. static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
  841. uint8_t *ssid, uint8_t ssid_len,
  842. uint8_t *bssid, uint8_t mode,
  843. int channel)
  844. {
  845. u32 bestrssi = 0;
  846. struct bss_descriptor *iter_bss = NULL;
  847. struct bss_descriptor *found_bss = NULL;
  848. struct bss_descriptor *tmp_oldest = NULL;
  849. lbs_deb_enter(LBS_DEB_SCAN);
  850. mutex_lock(&priv->lock);
  851. list_for_each_entry(iter_bss, &priv->network_list, list) {
  852. if (!tmp_oldest ||
  853. (iter_bss->last_scanned < tmp_oldest->last_scanned))
  854. tmp_oldest = iter_bss;
  855. if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
  856. ssid, ssid_len) != 0)
  857. continue; /* ssid doesn't match */
  858. if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
  859. continue; /* bssid doesn't match */
  860. if ((channel > 0) && (iter_bss->channel != channel))
  861. continue; /* channel doesn't match */
  862. switch (mode) {
  863. case IW_MODE_INFRA:
  864. case IW_MODE_ADHOC:
  865. if (!is_network_compatible(priv, iter_bss, mode))
  866. break;
  867. if (bssid) {
  868. /* Found requested BSSID */
  869. found_bss = iter_bss;
  870. goto out;
  871. }
  872. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  873. bestrssi = SCAN_RSSI(iter_bss->rssi);
  874. found_bss = iter_bss;
  875. }
  876. break;
  877. case IW_MODE_AUTO:
  878. default:
  879. if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
  880. bestrssi = SCAN_RSSI(iter_bss->rssi);
  881. found_bss = iter_bss;
  882. }
  883. break;
  884. }
  885. }
  886. out:
  887. mutex_unlock(&priv->lock);
  888. lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
  889. return found_bss;
  890. }
  891. static int assoc_helper_essid(struct lbs_private *priv,
  892. struct assoc_request * assoc_req)
  893. {
  894. int ret = 0;
  895. struct bss_descriptor * bss;
  896. int channel = -1;
  897. DECLARE_SSID_BUF(ssid);
  898. lbs_deb_enter(LBS_DEB_ASSOC);
  899. /* FIXME: take channel into account when picking SSIDs if a channel
  900. * is set.
  901. */
  902. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  903. channel = assoc_req->channel;
  904. lbs_deb_assoc("SSID '%s' requested\n",
  905. print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
  906. if (assoc_req->mode == IW_MODE_INFRA) {
  907. lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
  908. assoc_req->ssid_len);
  909. bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
  910. assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
  911. if (bss != NULL) {
  912. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  913. ret = lbs_try_associate(priv, assoc_req);
  914. } else {
  915. lbs_deb_assoc("SSID not found; cannot associate\n");
  916. }
  917. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  918. /* Scan for the network, do not save previous results. Stale
  919. * scan data will cause us to join a non-existant adhoc network
  920. */
  921. lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
  922. assoc_req->ssid_len);
  923. /* Search for the requested SSID in the scan table */
  924. bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
  925. assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
  926. if (bss != NULL) {
  927. lbs_deb_assoc("SSID found, will join\n");
  928. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  929. lbs_adhoc_join(priv, assoc_req);
  930. } else {
  931. /* else send START command */
  932. lbs_deb_assoc("SSID not found, creating adhoc network\n");
  933. memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
  934. IW_ESSID_MAX_SIZE);
  935. assoc_req->bss.ssid_len = assoc_req->ssid_len;
  936. lbs_adhoc_start(priv, assoc_req);
  937. }
  938. }
  939. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  940. return ret;
  941. }
  942. static int assoc_helper_bssid(struct lbs_private *priv,
  943. struct assoc_request * assoc_req)
  944. {
  945. int ret = 0;
  946. struct bss_descriptor * bss;
  947. lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
  948. /* Search for index position in list for requested MAC */
  949. bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
  950. assoc_req->mode);
  951. if (bss == NULL) {
  952. lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
  953. "cannot associate.\n", assoc_req->bssid);
  954. goto out;
  955. }
  956. memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
  957. if (assoc_req->mode == IW_MODE_INFRA) {
  958. ret = lbs_try_associate(priv, assoc_req);
  959. lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
  960. ret);
  961. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  962. lbs_adhoc_join(priv, assoc_req);
  963. }
  964. out:
  965. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  966. return ret;
  967. }
  968. static int assoc_helper_associate(struct lbs_private *priv,
  969. struct assoc_request * assoc_req)
  970. {
  971. int ret = 0, done = 0;
  972. lbs_deb_enter(LBS_DEB_ASSOC);
  973. /* If we're given and 'any' BSSID, try associating based on SSID */
  974. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  975. if (compare_ether_addr(bssid_any, assoc_req->bssid)
  976. && compare_ether_addr(bssid_off, assoc_req->bssid)) {
  977. ret = assoc_helper_bssid(priv, assoc_req);
  978. done = 1;
  979. }
  980. }
  981. if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  982. ret = assoc_helper_essid(priv, assoc_req);
  983. }
  984. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  985. return ret;
  986. }
  987. static int assoc_helper_mode(struct lbs_private *priv,
  988. struct assoc_request * assoc_req)
  989. {
  990. int ret = 0;
  991. lbs_deb_enter(LBS_DEB_ASSOC);
  992. if (assoc_req->mode == priv->mode)
  993. goto done;
  994. if (assoc_req->mode == IW_MODE_INFRA) {
  995. if (priv->psstate != PS_STATE_FULL_POWER)
  996. lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
  997. priv->psmode = LBS802_11POWERMODECAM;
  998. }
  999. priv->mode = assoc_req->mode;
  1000. ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
  1001. done:
  1002. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1003. return ret;
  1004. }
  1005. static int assoc_helper_channel(struct lbs_private *priv,
  1006. struct assoc_request * assoc_req)
  1007. {
  1008. int ret = 0;
  1009. lbs_deb_enter(LBS_DEB_ASSOC);
  1010. ret = lbs_update_channel(priv);
  1011. if (ret) {
  1012. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  1013. goto done;
  1014. }
  1015. if (assoc_req->channel == priv->curbssparams.channel)
  1016. goto done;
  1017. if (priv->mesh_dev) {
  1018. /* Change mesh channel first; 21.p21 firmware won't let
  1019. you change channel otherwise (even though it'll return
  1020. an error to this */
  1021. lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
  1022. assoc_req->channel);
  1023. }
  1024. lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
  1025. priv->curbssparams.channel, assoc_req->channel);
  1026. ret = lbs_set_channel(priv, assoc_req->channel);
  1027. if (ret < 0)
  1028. lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
  1029. /* FIXME: shouldn't need to grab the channel _again_ after setting
  1030. * it since the firmware is supposed to return the new channel, but
  1031. * whatever... */
  1032. ret = lbs_update_channel(priv);
  1033. if (ret) {
  1034. lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
  1035. goto done;
  1036. }
  1037. if (assoc_req->channel != priv->curbssparams.channel) {
  1038. lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
  1039. assoc_req->channel);
  1040. goto restore_mesh;
  1041. }
  1042. if ( assoc_req->secinfo.wep_enabled
  1043. && (assoc_req->wep_keys[0].len
  1044. || assoc_req->wep_keys[1].len
  1045. || assoc_req->wep_keys[2].len
  1046. || assoc_req->wep_keys[3].len)) {
  1047. /* Make sure WEP keys are re-sent to firmware */
  1048. set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
  1049. }
  1050. /* Must restart/rejoin adhoc networks after channel change */
  1051. set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
  1052. restore_mesh:
  1053. if (priv->mesh_dev)
  1054. lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  1055. priv->curbssparams.channel);
  1056. done:
  1057. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1058. return ret;
  1059. }
  1060. static int assoc_helper_wep_keys(struct lbs_private *priv,
  1061. struct assoc_request *assoc_req)
  1062. {
  1063. int i;
  1064. int ret = 0;
  1065. lbs_deb_enter(LBS_DEB_ASSOC);
  1066. /* Set or remove WEP keys */
  1067. if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
  1068. assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
  1069. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
  1070. else
  1071. ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
  1072. if (ret)
  1073. goto out;
  1074. /* enable/disable the MAC's WEP packet filter */
  1075. if (assoc_req->secinfo.wep_enabled)
  1076. priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
  1077. else
  1078. priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
  1079. lbs_set_mac_control(priv);
  1080. mutex_lock(&priv->lock);
  1081. /* Copy WEP keys into priv wep key fields */
  1082. for (i = 0; i < 4; i++) {
  1083. memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
  1084. sizeof(struct enc_key));
  1085. }
  1086. priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
  1087. mutex_unlock(&priv->lock);
  1088. out:
  1089. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1090. return ret;
  1091. }
  1092. static int assoc_helper_secinfo(struct lbs_private *priv,
  1093. struct assoc_request * assoc_req)
  1094. {
  1095. int ret = 0;
  1096. uint16_t do_wpa;
  1097. uint16_t rsn = 0;
  1098. lbs_deb_enter(LBS_DEB_ASSOC);
  1099. memcpy(&priv->secinfo, &assoc_req->secinfo,
  1100. sizeof(struct lbs_802_11_security));
  1101. lbs_set_mac_control(priv);
  1102. /* If RSN is already enabled, don't try to enable it again, since
  1103. * ENABLE_RSN resets internal state machines and will clobber the
  1104. * 4-way WPA handshake.
  1105. */
  1106. /* Get RSN enabled/disabled */
  1107. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
  1108. if (ret) {
  1109. lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
  1110. goto out;
  1111. }
  1112. /* Don't re-enable RSN if it's already enabled */
  1113. do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
  1114. if (do_wpa == rsn)
  1115. goto out;
  1116. /* Set RSN enabled/disabled */
  1117. ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
  1118. out:
  1119. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1120. return ret;
  1121. }
  1122. static int assoc_helper_wpa_keys(struct lbs_private *priv,
  1123. struct assoc_request * assoc_req)
  1124. {
  1125. int ret = 0;
  1126. unsigned int flags = assoc_req->flags;
  1127. lbs_deb_enter(LBS_DEB_ASSOC);
  1128. /* Work around older firmware bug where WPA unicast and multicast
  1129. * keys must be set independently. Seen in SDIO parts with firmware
  1130. * version 5.0.11p0.
  1131. */
  1132. if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  1133. clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
  1134. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  1135. assoc_req->flags = flags;
  1136. }
  1137. if (ret)
  1138. goto out;
  1139. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  1140. clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);
  1141. ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
  1142. assoc_req->flags = flags;
  1143. }
  1144. out:
  1145. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1146. return ret;
  1147. }
  1148. static int assoc_helper_wpa_ie(struct lbs_private *priv,
  1149. struct assoc_request * assoc_req)
  1150. {
  1151. int ret = 0;
  1152. lbs_deb_enter(LBS_DEB_ASSOC);
  1153. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  1154. memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
  1155. priv->wpa_ie_len = assoc_req->wpa_ie_len;
  1156. } else {
  1157. memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
  1158. priv->wpa_ie_len = 0;
  1159. }
  1160. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1161. return ret;
  1162. }
  1163. static int should_deauth_infrastructure(struct lbs_private *priv,
  1164. struct assoc_request * assoc_req)
  1165. {
  1166. int ret = 0;
  1167. if (priv->connect_status != LBS_CONNECTED)
  1168. return 0;
  1169. lbs_deb_enter(LBS_DEB_ASSOC);
  1170. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  1171. lbs_deb_assoc("Deauthenticating due to new SSID\n");
  1172. ret = 1;
  1173. goto out;
  1174. }
  1175. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  1176. if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
  1177. lbs_deb_assoc("Deauthenticating due to new security\n");
  1178. ret = 1;
  1179. goto out;
  1180. }
  1181. }
  1182. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  1183. lbs_deb_assoc("Deauthenticating due to new BSSID\n");
  1184. ret = 1;
  1185. goto out;
  1186. }
  1187. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  1188. lbs_deb_assoc("Deauthenticating due to channel switch\n");
  1189. ret = 1;
  1190. goto out;
  1191. }
  1192. /* FIXME: deal with 'auto' mode somehow */
  1193. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  1194. if (assoc_req->mode != IW_MODE_INFRA) {
  1195. lbs_deb_assoc("Deauthenticating due to leaving "
  1196. "infra mode\n");
  1197. ret = 1;
  1198. goto out;
  1199. }
  1200. }
  1201. out:
  1202. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  1203. return ret;
  1204. }
  1205. static int should_stop_adhoc(struct lbs_private *priv,
  1206. struct assoc_request * assoc_req)
  1207. {
  1208. lbs_deb_enter(LBS_DEB_ASSOC);
  1209. if (priv->connect_status != LBS_CONNECTED)
  1210. return 0;
  1211. if (lbs_ssid_cmp(priv->curbssparams.ssid,
  1212. priv->curbssparams.ssid_len,
  1213. assoc_req->ssid, assoc_req->ssid_len) != 0)
  1214. return 1;
  1215. /* FIXME: deal with 'auto' mode somehow */
  1216. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  1217. if (assoc_req->mode != IW_MODE_ADHOC)
  1218. return 1;
  1219. }
  1220. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  1221. if (assoc_req->channel != priv->curbssparams.channel)
  1222. return 1;
  1223. }
  1224. lbs_deb_leave(LBS_DEB_ASSOC);
  1225. return 0;
  1226. }
  1227. /**
  1228. * @brief This function finds the best SSID in the Scan List
  1229. *
  1230. * Search the scan table for the best SSID that also matches the current
  1231. * adapter network preference (infrastructure or adhoc)
  1232. *
  1233. * @param priv A pointer to struct lbs_private
  1234. *
  1235. * @return index in BSSID list
  1236. */
  1237. static struct bss_descriptor *lbs_find_best_ssid_in_list(
  1238. struct lbs_private *priv, uint8_t mode)
  1239. {
  1240. uint8_t bestrssi = 0;
  1241. struct bss_descriptor *iter_bss;
  1242. struct bss_descriptor *best_bss = NULL;
  1243. lbs_deb_enter(LBS_DEB_SCAN);
  1244. mutex_lock(&priv->lock);
  1245. list_for_each_entry(iter_bss, &priv->network_list, list) {
  1246. switch (mode) {
  1247. case IW_MODE_INFRA:
  1248. case IW_MODE_ADHOC:
  1249. if (!is_network_compatible(priv, iter_bss, mode))
  1250. break;
  1251. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  1252. break;
  1253. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1254. best_bss = iter_bss;
  1255. break;
  1256. case IW_MODE_AUTO:
  1257. default:
  1258. if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
  1259. break;
  1260. bestrssi = SCAN_RSSI(iter_bss->rssi);
  1261. best_bss = iter_bss;
  1262. break;
  1263. }
  1264. }
  1265. mutex_unlock(&priv->lock);
  1266. lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
  1267. return best_bss;
  1268. }
  1269. /**
  1270. * @brief Find the best AP
  1271. *
  1272. * Used from association worker.
  1273. *
  1274. * @param priv A pointer to struct lbs_private structure
  1275. * @param pSSID A pointer to AP's ssid
  1276. *
  1277. * @return 0--success, otherwise--fail
  1278. */
  1279. static int lbs_find_best_network_ssid(struct lbs_private *priv,
  1280. uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
  1281. uint8_t *out_mode)
  1282. {
  1283. int ret = -1;
  1284. struct bss_descriptor *found;
  1285. lbs_deb_enter(LBS_DEB_SCAN);
  1286. priv->scan_ssid_len = 0;
  1287. lbs_scan_networks(priv, 1);
  1288. if (priv->surpriseremoved)
  1289. goto out;
  1290. found = lbs_find_best_ssid_in_list(priv, preferred_mode);
  1291. if (found && (found->ssid_len > 0)) {
  1292. memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
  1293. *out_ssid_len = found->ssid_len;
  1294. *out_mode = found->mode;
  1295. ret = 0;
  1296. }
  1297. out:
  1298. lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
  1299. return ret;
  1300. }
  1301. void lbs_association_worker(struct work_struct *work)
  1302. {
  1303. struct lbs_private *priv = container_of(work, struct lbs_private,
  1304. assoc_work.work);
  1305. struct assoc_request * assoc_req = NULL;
  1306. int ret = 0;
  1307. int find_any_ssid = 0;
  1308. DECLARE_SSID_BUF(ssid);
  1309. lbs_deb_enter(LBS_DEB_ASSOC);
  1310. mutex_lock(&priv->lock);
  1311. assoc_req = priv->pending_assoc_req;
  1312. priv->pending_assoc_req = NULL;
  1313. priv->in_progress_assoc_req = assoc_req;
  1314. mutex_unlock(&priv->lock);
  1315. if (!assoc_req)
  1316. goto done;
  1317. lbs_deb_assoc(
  1318. "Association Request:\n"
  1319. " flags: 0x%08lx\n"
  1320. " SSID: '%s'\n"
  1321. " chann: %d\n"
  1322. " band: %d\n"
  1323. " mode: %d\n"
  1324. " BSSID: %pM\n"
  1325. " secinfo: %s%s%s\n"
  1326. " auth_mode: %d\n",
  1327. assoc_req->flags,
  1328. print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
  1329. assoc_req->channel, assoc_req->band, assoc_req->mode,
  1330. assoc_req->bssid,
  1331. assoc_req->secinfo.WPAenabled ? " WPA" : "",
  1332. assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
  1333. assoc_req->secinfo.wep_enabled ? " WEP" : "",
  1334. assoc_req->secinfo.auth_mode);
  1335. /* If 'any' SSID was specified, find an SSID to associate with */
  1336. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
  1337. && !assoc_req->ssid_len)
  1338. find_any_ssid = 1;
  1339. /* But don't use 'any' SSID if there's a valid locked BSSID to use */
  1340. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  1341. if (compare_ether_addr(assoc_req->bssid, bssid_any)
  1342. && compare_ether_addr(assoc_req->bssid, bssid_off))
  1343. find_any_ssid = 0;
  1344. }
  1345. if (find_any_ssid) {
  1346. u8 new_mode = assoc_req->mode;
  1347. ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
  1348. &assoc_req->ssid_len, assoc_req->mode, &new_mode);
  1349. if (ret) {
  1350. lbs_deb_assoc("Could not find best network\n");
  1351. ret = -ENETUNREACH;
  1352. goto out;
  1353. }
  1354. /* Ensure we switch to the mode of the AP */
  1355. if (assoc_req->mode == IW_MODE_AUTO) {
  1356. set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
  1357. assoc_req->mode = new_mode;
  1358. }
  1359. }
  1360. /*
  1361. * Check if the attributes being changing require deauthentication
  1362. * from the currently associated infrastructure access point.
  1363. */
  1364. if (priv->mode == IW_MODE_INFRA) {
  1365. if (should_deauth_infrastructure(priv, assoc_req)) {
  1366. ret = lbs_cmd_80211_deauthenticate(priv,
  1367. priv->curbssparams.bssid,
  1368. WLAN_REASON_DEAUTH_LEAVING);
  1369. if (ret) {
  1370. lbs_deb_assoc("Deauthentication due to new "
  1371. "configuration request failed: %d\n",
  1372. ret);
  1373. }
  1374. }
  1375. } else if (priv->mode == IW_MODE_ADHOC) {
  1376. if (should_stop_adhoc(priv, assoc_req)) {
  1377. ret = lbs_adhoc_stop(priv);
  1378. if (ret) {
  1379. lbs_deb_assoc("Teardown of AdHoc network due to "
  1380. "new configuration request failed: %d\n",
  1381. ret);
  1382. }
  1383. }
  1384. }
  1385. /* Send the various configuration bits to the firmware */
  1386. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  1387. ret = assoc_helper_mode(priv, assoc_req);
  1388. if (ret)
  1389. goto out;
  1390. }
  1391. if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
  1392. ret = assoc_helper_channel(priv, assoc_req);
  1393. if (ret)
  1394. goto out;
  1395. }
  1396. if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
  1397. || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
  1398. ret = assoc_helper_wep_keys(priv, assoc_req);
  1399. if (ret)
  1400. goto out;
  1401. }
  1402. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  1403. ret = assoc_helper_secinfo(priv, assoc_req);
  1404. if (ret)
  1405. goto out;
  1406. }
  1407. if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  1408. ret = assoc_helper_wpa_ie(priv, assoc_req);
  1409. if (ret)
  1410. goto out;
  1411. }
  1412. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
  1413. || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  1414. ret = assoc_helper_wpa_keys(priv, assoc_req);
  1415. if (ret)
  1416. goto out;
  1417. }
  1418. /* SSID/BSSID should be the _last_ config option set, because they
  1419. * trigger the association attempt.
  1420. */
  1421. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
  1422. || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  1423. int success = 1;
  1424. ret = assoc_helper_associate(priv, assoc_req);
  1425. if (ret) {
  1426. lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
  1427. ret);
  1428. success = 0;
  1429. }
  1430. if (priv->connect_status != LBS_CONNECTED) {
  1431. lbs_deb_assoc("ASSOC: association unsuccessful, "
  1432. "not connected\n");
  1433. success = 0;
  1434. }
  1435. if (success) {
  1436. lbs_deb_assoc("associated to %pM\n",
  1437. priv->curbssparams.bssid);
  1438. lbs_prepare_and_send_command(priv,
  1439. CMD_802_11_RSSI,
  1440. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  1441. } else {
  1442. ret = -1;
  1443. }
  1444. }
  1445. out:
  1446. if (ret) {
  1447. lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
  1448. ret);
  1449. }
  1450. mutex_lock(&priv->lock);
  1451. priv->in_progress_assoc_req = NULL;
  1452. mutex_unlock(&priv->lock);
  1453. kfree(assoc_req);
  1454. done:
  1455. lbs_deb_leave(LBS_DEB_ASSOC);
  1456. }
  1457. /*
  1458. * Caller MUST hold any necessary locks
  1459. */
  1460. struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
  1461. {
  1462. struct assoc_request * assoc_req;
  1463. lbs_deb_enter(LBS_DEB_ASSOC);
  1464. if (!priv->pending_assoc_req) {
  1465. priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
  1466. GFP_KERNEL);
  1467. if (!priv->pending_assoc_req) {
  1468. lbs_pr_info("Not enough memory to allocate association"
  1469. " request!\n");
  1470. return NULL;
  1471. }
  1472. }
  1473. /* Copy current configuration attributes to the association request,
  1474. * but don't overwrite any that are already set.
  1475. */
  1476. assoc_req = priv->pending_assoc_req;
  1477. if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  1478. memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
  1479. IW_ESSID_MAX_SIZE);
  1480. assoc_req->ssid_len = priv->curbssparams.ssid_len;
  1481. }
  1482. if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  1483. assoc_req->channel = priv->curbssparams.channel;
  1484. if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
  1485. assoc_req->band = priv->curbssparams.band;
  1486. if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
  1487. assoc_req->mode = priv->mode;
  1488. if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  1489. memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
  1490. ETH_ALEN);
  1491. }
  1492. if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
  1493. int i;
  1494. for (i = 0; i < 4; i++) {
  1495. memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
  1496. sizeof(struct enc_key));
  1497. }
  1498. }
  1499. if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
  1500. assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
  1501. if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  1502. memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
  1503. sizeof(struct enc_key));
  1504. }
  1505. if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  1506. memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
  1507. sizeof(struct enc_key));
  1508. }
  1509. if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  1510. memcpy(&assoc_req->secinfo, &priv->secinfo,
  1511. sizeof(struct lbs_802_11_security));
  1512. }
  1513. if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  1514. memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
  1515. MAX_WPA_IE_LEN);
  1516. assoc_req->wpa_ie_len = priv->wpa_ie_len;
  1517. }
  1518. lbs_deb_leave(LBS_DEB_ASSOC);
  1519. return assoc_req;
  1520. }
  1521. /**
  1522. * @brief Deauthenticate from a specific BSS
  1523. *
  1524. * @param priv A pointer to struct lbs_private structure
  1525. * @param bssid The specific BSS to deauthenticate from
  1526. * @param reason The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
  1527. *
  1528. * @return 0 on success, error on failure
  1529. */
  1530. int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
  1531. u16 reason)
  1532. {
  1533. struct cmd_ds_802_11_deauthenticate cmd;
  1534. int ret;
  1535. lbs_deb_enter(LBS_DEB_JOIN);
  1536. memset(&cmd, 0, sizeof(cmd));
  1537. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1538. memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
  1539. cmd.reasoncode = cpu_to_le16(reason);
  1540. ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
  1541. /* Clean up everything even if there was an error; can't assume that
  1542. * we're still authenticated to the AP after trying to deauth.
  1543. */
  1544. lbs_mac_event_disconnected(priv);
  1545. lbs_deb_leave(LBS_DEB_JOIN);
  1546. return ret;
  1547. }