assoc.c 50 KB

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