assoc.c 51 KB

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