assoc.c 42 KB

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