join.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /**
  2. * Functions implementing wlan infrastructure and adhoc join routines,
  3. * IOCTL handlers as well as command preperation and response routines
  4. * for sending adhoc start, adhoc join, and association commands
  5. * to the firmware.
  6. */
  7. #include <linux/netdevice.h>
  8. #include <linux/if_arp.h>
  9. #include <linux/wireless.h>
  10. #include <linux/etherdevice.h>
  11. #include <net/iw_handler.h>
  12. #include "host.h"
  13. #include "decl.h"
  14. #include "join.h"
  15. #include "dev.h"
  16. #include "assoc.h"
  17. /* Supported rates for ad-hoc B mode */
  18. u8 adhoc_rates_b[5] = { 0x02, 0x04, 0x0b, 0x16, 0x00 };
  19. /**
  20. * @brief This function finds common rates between rate1 and card rates.
  21. *
  22. * It will fill common rates in rate1 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 adapter A pointer to wlan_adapter structure
  28. * @param rate1 the buffer which keeps input and output
  29. * @param rate1_size the size of rate1 buffer; new size of buffer on return
  30. *
  31. * @return 0 or -1
  32. */
  33. static int get_common_rates(wlan_adapter * adapter, u8 * rates, u16 *rates_size)
  34. {
  35. u8 *card_rates = libertas_bg_rates;
  36. size_t num_card_rates = sizeof(libertas_bg_rates);
  37. int ret = 0, i, j;
  38. u8 tmp[30];
  39. size_t tmp_size = 0;
  40. /* For each rate in card_rates that exists in rate1, copy to tmp */
  41. for (i = 0; card_rates[i] && (i < num_card_rates); i++) {
  42. for (j = 0; rates[j] && (j < *rates_size); j++) {
  43. if (rates[j] == card_rates[i])
  44. tmp[tmp_size++] = card_rates[i];
  45. }
  46. }
  47. lbs_dbg_hex("rate1 (AP) rates:", rates, *rates_size);
  48. lbs_dbg_hex("rate2 (Card) rates:", card_rates, num_card_rates);
  49. lbs_dbg_hex("Common rates:", tmp, tmp_size);
  50. lbs_deb_join("Tx datarate is currently 0x%X\n", adapter->cur_rate);
  51. if (!adapter->auto_rate) {
  52. for (i = 0; i < tmp_size; i++) {
  53. if (tmp[i] == adapter->cur_rate)
  54. goto done;
  55. }
  56. lbs_pr_alert("Previously set fixed data rate %#x isn't "
  57. "compatible with the network.\n", adapter->cur_rate);
  58. ret = -1;
  59. goto done;
  60. }
  61. ret = 0;
  62. done:
  63. memset(rates, 0, *rates_size);
  64. *rates_size = min_t(int, tmp_size, *rates_size);
  65. memcpy(rates, tmp, *rates_size);
  66. return ret;
  67. }
  68. /**
  69. * @brief Sets the MSB on basic rates as the firmware requires
  70. *
  71. * Scan through an array and set the MSB for basic data rates.
  72. *
  73. * @param rates buffer of data rates
  74. * @param len size of buffer
  75. */
  76. static void libertas_set_basic_rate_flags(u8 * rates, size_t len)
  77. {
  78. int i;
  79. for (i = 0; i < len; i++) {
  80. if (rates[i] == 0x02 || rates[i] == 0x04 ||
  81. rates[i] == 0x0b || rates[i] == 0x16)
  82. rates[i] |= 0x80;
  83. }
  84. }
  85. /**
  86. * @brief Unsets the MSB on basic rates
  87. *
  88. * Scan through an array and unset the MSB for basic data rates.
  89. *
  90. * @param rates buffer of data rates
  91. * @param len size of buffer
  92. */
  93. void libertas_unset_basic_rate_flags(u8 * rates, size_t len)
  94. {
  95. int i;
  96. for (i = 0; i < len; i++)
  97. rates[i] &= 0x7f;
  98. }
  99. int libertas_send_deauth(wlan_private * priv)
  100. {
  101. wlan_adapter *adapter = priv->adapter;
  102. int ret = 0;
  103. if (adapter->mode == IW_MODE_INFRA &&
  104. adapter->connect_status == LIBERTAS_CONNECTED)
  105. ret = libertas_send_deauthentication(priv);
  106. else
  107. ret = -ENOTSUPP;
  108. return ret;
  109. }
  110. /**
  111. * @brief Associate to a specific BSS discovered in a scan
  112. *
  113. * @param priv A pointer to wlan_private structure
  114. * @param pbssdesc Pointer to the BSS descriptor to associate with.
  115. *
  116. * @return 0-success, otherwise fail
  117. */
  118. int wlan_associate(wlan_private * priv, struct assoc_request * assoc_req)
  119. {
  120. wlan_adapter *adapter = priv->adapter;
  121. int ret;
  122. lbs_deb_enter(LBS_DEB_JOIN);
  123. ret = libertas_prepare_and_send_command(priv, CMD_802_11_AUTHENTICATE,
  124. 0, CMD_OPTION_WAITFORRSP,
  125. 0, assoc_req->bss.bssid);
  126. if (ret)
  127. goto done;
  128. /* set preamble to firmware */
  129. if ( (adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  130. && (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
  131. adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
  132. else
  133. adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
  134. libertas_set_radio_control(priv);
  135. ret = libertas_prepare_and_send_command(priv, CMD_802_11_ASSOCIATE,
  136. 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
  137. done:
  138. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  139. return ret;
  140. }
  141. /**
  142. * @brief Start an Adhoc Network
  143. *
  144. * @param priv A pointer to wlan_private structure
  145. * @param adhocssid The ssid of the Adhoc Network
  146. * @return 0--success, -1--fail
  147. */
  148. int libertas_start_adhoc_network(wlan_private * priv, struct assoc_request * assoc_req)
  149. {
  150. wlan_adapter *adapter = priv->adapter;
  151. int ret = 0;
  152. adapter->adhoccreate = 1;
  153. if (adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
  154. lbs_deb_join("AdhocStart: Short preamble\n");
  155. adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
  156. } else {
  157. lbs_deb_join("AdhocStart: Long preamble\n");
  158. adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
  159. }
  160. libertas_set_radio_control(priv);
  161. lbs_deb_join("AdhocStart: channel = %d\n", assoc_req->channel);
  162. lbs_deb_join("AdhocStart: band = %d\n", assoc_req->band);
  163. ret = libertas_prepare_and_send_command(priv, CMD_802_11_AD_HOC_START,
  164. 0, CMD_OPTION_WAITFORRSP, 0, assoc_req);
  165. return ret;
  166. }
  167. /**
  168. * @brief Join an adhoc network found in a previous scan
  169. *
  170. * @param priv A pointer to wlan_private structure
  171. * @param pbssdesc Pointer to a BSS descriptor found in a previous scan
  172. * to attempt to join
  173. *
  174. * @return 0--success, -1--fail
  175. */
  176. int libertas_join_adhoc_network(wlan_private * priv, struct assoc_request * assoc_req)
  177. {
  178. wlan_adapter *adapter = priv->adapter;
  179. struct bss_descriptor * bss = &assoc_req->bss;
  180. int ret = 0;
  181. lbs_deb_join("%s: Current SSID '%s', ssid length %u\n",
  182. __func__,
  183. escape_essid(adapter->curbssparams.ssid,
  184. adapter->curbssparams.ssid_len),
  185. adapter->curbssparams.ssid_len);
  186. lbs_deb_join("%s: requested ssid '%s', ssid length %u\n",
  187. __func__, escape_essid(bss->ssid, bss->ssid_len),
  188. bss->ssid_len);
  189. /* check if the requested SSID is already joined */
  190. if (adapter->curbssparams.ssid_len
  191. && !libertas_ssid_cmp(adapter->curbssparams.ssid,
  192. adapter->curbssparams.ssid_len,
  193. bss->ssid, bss->ssid_len)
  194. && (adapter->mode == IW_MODE_ADHOC)) {
  195. lbs_deb_join(
  196. "ADHOC_J_CMD: New ad-hoc SSID is the same as current, "
  197. "not attempting to re-join");
  198. return -1;
  199. }
  200. /* Use shortpreamble only when both creator and card supports
  201. short preamble */
  202. if ( !(bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  203. || !(adapter->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
  204. lbs_deb_join("AdhocJoin: Long preamble\n");
  205. adapter->preamble = CMD_TYPE_LONG_PREAMBLE;
  206. } else {
  207. lbs_deb_join("AdhocJoin: Short preamble\n");
  208. adapter->preamble = CMD_TYPE_SHORT_PREAMBLE;
  209. }
  210. libertas_set_radio_control(priv);
  211. lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
  212. lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
  213. adapter->adhoccreate = 0;
  214. ret = libertas_prepare_and_send_command(priv, CMD_802_11_AD_HOC_JOIN,
  215. 0, CMD_OPTION_WAITFORRSP,
  216. OID_802_11_SSID, assoc_req);
  217. return ret;
  218. }
  219. int libertas_stop_adhoc_network(wlan_private * priv)
  220. {
  221. return libertas_prepare_and_send_command(priv, CMD_802_11_AD_HOC_STOP,
  222. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  223. }
  224. /**
  225. * @brief Send Deauthentication Request
  226. *
  227. * @param priv A pointer to wlan_private structure
  228. * @return 0--success, -1--fail
  229. */
  230. int libertas_send_deauthentication(wlan_private * priv)
  231. {
  232. return libertas_prepare_and_send_command(priv, CMD_802_11_DEAUTHENTICATE,
  233. 0, CMD_OPTION_WAITFORRSP, 0, NULL);
  234. }
  235. /**
  236. * @brief This function prepares command of authenticate.
  237. *
  238. * @param priv A pointer to wlan_private structure
  239. * @param cmd A pointer to cmd_ds_command structure
  240. * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
  241. *
  242. * @return 0 or -1
  243. */
  244. int libertas_cmd_80211_authenticate(wlan_private * priv,
  245. struct cmd_ds_command *cmd,
  246. void *pdata_buf)
  247. {
  248. wlan_adapter *adapter = priv->adapter;
  249. struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
  250. int ret = -1;
  251. u8 *bssid = pdata_buf;
  252. lbs_deb_enter(LBS_DEB_JOIN);
  253. cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
  254. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
  255. + S_DS_GEN);
  256. /* translate auth mode to 802.11 defined wire value */
  257. switch (adapter->secinfo.auth_mode) {
  258. case IW_AUTH_ALG_OPEN_SYSTEM:
  259. pauthenticate->authtype = 0x00;
  260. break;
  261. case IW_AUTH_ALG_SHARED_KEY:
  262. pauthenticate->authtype = 0x01;
  263. break;
  264. case IW_AUTH_ALG_LEAP:
  265. pauthenticate->authtype = 0x80;
  266. break;
  267. default:
  268. lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
  269. adapter->secinfo.auth_mode);
  270. goto out;
  271. }
  272. memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
  273. lbs_deb_join("AUTH_CMD: BSSID is : " MAC_FMT " auth=0x%X\n",
  274. MAC_ARG(bssid), pauthenticate->authtype);
  275. ret = 0;
  276. out:
  277. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  278. return ret;
  279. }
  280. int libertas_cmd_80211_deauthenticate(wlan_private * priv,
  281. struct cmd_ds_command *cmd)
  282. {
  283. wlan_adapter *adapter = priv->adapter;
  284. struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
  285. lbs_deb_enter(LBS_DEB_JOIN);
  286. cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
  287. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
  288. S_DS_GEN);
  289. /* set AP MAC address */
  290. memmove(dauth->macaddr, adapter->curbssparams.bssid, ETH_ALEN);
  291. /* Reason code 3 = Station is leaving */
  292. #define REASON_CODE_STA_LEAVING 3
  293. dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
  294. lbs_deb_leave(LBS_DEB_JOIN);
  295. return 0;
  296. }
  297. int libertas_cmd_80211_associate(wlan_private * priv,
  298. struct cmd_ds_command *cmd, void *pdata_buf)
  299. {
  300. wlan_adapter *adapter = priv->adapter;
  301. struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
  302. int ret = 0;
  303. struct assoc_request * assoc_req = pdata_buf;
  304. struct bss_descriptor * bss = &assoc_req->bss;
  305. u8 *pos;
  306. u16 tmpcap, tmplen;
  307. struct mrvlietypes_ssidparamset *ssid;
  308. struct mrvlietypes_phyparamset *phy;
  309. struct mrvlietypes_ssparamset *ss;
  310. struct mrvlietypes_ratesparamset *rates;
  311. struct mrvlietypes_rsnparamset *rsn;
  312. lbs_deb_enter(LBS_DEB_JOIN);
  313. pos = (u8 *) passo;
  314. if (!adapter) {
  315. ret = -1;
  316. goto done;
  317. }
  318. cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  319. memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
  320. pos += sizeof(passo->peerstaaddr);
  321. /* set the listen interval */
  322. passo->listeninterval = cpu_to_le16(adapter->listeninterval);
  323. pos += sizeof(passo->capability);
  324. pos += sizeof(passo->listeninterval);
  325. pos += sizeof(passo->bcnperiod);
  326. pos += sizeof(passo->dtimperiod);
  327. ssid = (struct mrvlietypes_ssidparamset *) pos;
  328. ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
  329. tmplen = bss->ssid_len;
  330. ssid->header.len = cpu_to_le16(tmplen);
  331. memcpy(ssid->ssid, bss->ssid, tmplen);
  332. pos += sizeof(ssid->header) + tmplen;
  333. phy = (struct mrvlietypes_phyparamset *) pos;
  334. phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  335. tmplen = sizeof(phy->fh_ds.dsparamset);
  336. phy->header.len = cpu_to_le16(tmplen);
  337. memcpy(&phy->fh_ds.dsparamset,
  338. &bss->phyparamset.dsparamset.currentchan,
  339. tmplen);
  340. pos += sizeof(phy->header) + tmplen;
  341. ss = (struct mrvlietypes_ssparamset *) pos;
  342. ss->header.type = cpu_to_le16(TLV_TYPE_CF);
  343. tmplen = sizeof(ss->cf_ibss.cfparamset);
  344. ss->header.len = cpu_to_le16(tmplen);
  345. pos += sizeof(ss->header) + tmplen;
  346. rates = (struct mrvlietypes_ratesparamset *) pos;
  347. rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
  348. memcpy(&rates->rates, &bss->rates, MAX_RATES);
  349. tmplen = MAX_RATES;
  350. if (get_common_rates(adapter, rates->rates, &tmplen)) {
  351. ret = -1;
  352. goto done;
  353. }
  354. pos += sizeof(rates->header) + tmplen;
  355. rates->header.len = cpu_to_le16(tmplen);
  356. lbs_deb_join("ASSOC_CMD: num rates = %u\n", tmplen);
  357. /* Copy the infra. association rates into Current BSS state structure */
  358. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  359. memcpy(&adapter->curbssparams.rates, &rates->rates, tmplen);
  360. /* Set MSB on basic rates as the firmware requires, but _after_
  361. * copying to current bss rates.
  362. */
  363. libertas_set_basic_rate_flags(rates->rates, tmplen);
  364. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  365. rsn = (struct mrvlietypes_rsnparamset *) pos;
  366. /* WPA_IE or WPA2_IE */
  367. rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
  368. tmplen = (u16) assoc_req->wpa_ie[1];
  369. rsn->header.len = cpu_to_le16(tmplen);
  370. memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
  371. lbs_dbg_hex("ASSOC_CMD: RSN IE", (u8 *) rsn,
  372. sizeof(rsn->header) + tmplen);
  373. pos += sizeof(rsn->header) + tmplen;
  374. }
  375. /* update curbssparams */
  376. adapter->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
  377. if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
  378. ret = -1;
  379. goto done;
  380. }
  381. cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
  382. /* set the capability info */
  383. tmpcap = (bss->capability & CAPINFO_MASK);
  384. if (bss->mode == IW_MODE_INFRA)
  385. tmpcap |= WLAN_CAPABILITY_ESS;
  386. passo->capability = cpu_to_le16(tmpcap);
  387. lbs_deb_join("ASSOC_CMD: capability=%4X CAPINFO_MASK=%4X\n",
  388. tmpcap, CAPINFO_MASK);
  389. done:
  390. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  391. return ret;
  392. }
  393. int libertas_cmd_80211_ad_hoc_start(wlan_private * priv,
  394. struct cmd_ds_command *cmd, void *pdata_buf)
  395. {
  396. wlan_adapter *adapter = priv->adapter;
  397. struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
  398. int ret = 0;
  399. int cmdappendsize = 0;
  400. struct assoc_request * assoc_req = pdata_buf;
  401. u16 tmpcap = 0;
  402. size_t ratesize = 0;
  403. lbs_deb_enter(LBS_DEB_JOIN);
  404. if (!adapter) {
  405. ret = -1;
  406. goto done;
  407. }
  408. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
  409. /*
  410. * Fill in the parameters for 2 data structures:
  411. * 1. cmd_ds_802_11_ad_hoc_start command
  412. * 2. adapter->scantable[i]
  413. *
  414. * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
  415. * probe delay, and cap info.
  416. *
  417. * Firmware will fill up beacon period, DTIM, Basic rates
  418. * and operational rates.
  419. */
  420. memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
  421. memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
  422. lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
  423. escape_essid(assoc_req->ssid, assoc_req->ssid_len),
  424. assoc_req->ssid_len);
  425. /* set the BSS type */
  426. adhs->bsstype = CMD_BSS_TYPE_IBSS;
  427. adapter->mode = IW_MODE_ADHOC;
  428. adhs->beaconperiod = cpu_to_le16(adapter->beaconperiod);
  429. /* set Physical param set */
  430. #define DS_PARA_IE_ID 3
  431. #define DS_PARA_IE_LEN 1
  432. adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
  433. adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
  434. WARN_ON(!assoc_req->channel);
  435. lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
  436. assoc_req->channel);
  437. adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
  438. /* set IBSS param set */
  439. #define IBSS_PARA_IE_ID 6
  440. #define IBSS_PARA_IE_LEN 2
  441. adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
  442. adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
  443. adhs->ssparamset.ibssparamset.atimwindow = cpu_to_le16(adapter->atimwindow);
  444. /* set capability info */
  445. tmpcap = WLAN_CAPABILITY_IBSS;
  446. if (assoc_req->secinfo.wep_enabled) {
  447. lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
  448. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  449. } else {
  450. lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
  451. }
  452. adhs->capability = cpu_to_le16(tmpcap);
  453. /* probedelay */
  454. adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  455. memset(adhs->rates, 0, sizeof(adhs->rates));
  456. if (adapter->adhoc_grate_enabled) {
  457. ratesize = min(sizeof(adhs->rates), sizeof(libertas_bg_rates));
  458. memcpy(adhs->rates, libertas_bg_rates, ratesize);
  459. } else {
  460. ratesize = min(sizeof(adhs->rates), sizeof(adhoc_rates_b));
  461. memcpy(adhs->rates, adhoc_rates_b, ratesize);
  462. }
  463. /* Copy the ad-hoc creating rates into Current BSS state structure */
  464. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  465. memcpy(&adapter->curbssparams.rates, &adhs->rates, ratesize);
  466. /* Set MSB on basic rates as the firmware requires, but _after_
  467. * copying to current bss rates.
  468. */
  469. libertas_set_basic_rate_flags(adhs->rates, ratesize);
  470. lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
  471. adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
  472. lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
  473. if (libertas_create_dnld_countryinfo_11d(priv)) {
  474. lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
  475. ret = -1;
  476. goto done;
  477. }
  478. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
  479. S_DS_GEN + cmdappendsize);
  480. ret = 0;
  481. done:
  482. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  483. return ret;
  484. }
  485. int libertas_cmd_80211_ad_hoc_stop(wlan_private * priv,
  486. struct cmd_ds_command *cmd)
  487. {
  488. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
  489. cmd->size = cpu_to_le16(S_DS_GEN);
  490. return 0;
  491. }
  492. int libertas_cmd_80211_ad_hoc_join(wlan_private * priv,
  493. struct cmd_ds_command *cmd, void *pdata_buf)
  494. {
  495. wlan_adapter *adapter = priv->adapter;
  496. struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
  497. struct assoc_request * assoc_req = pdata_buf;
  498. struct bss_descriptor *bss = &assoc_req->bss;
  499. int cmdappendsize = 0;
  500. int ret = 0;
  501. u16 ratesize = 0;
  502. lbs_deb_enter(LBS_DEB_JOIN);
  503. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
  504. join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
  505. join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
  506. memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
  507. memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
  508. memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
  509. sizeof(union ieeetypes_phyparamset));
  510. memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
  511. sizeof(union IEEEtypes_ssparamset));
  512. join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  513. lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  514. bss->capability, CAPINFO_MASK);
  515. /* information on BSSID descriptor passed to FW */
  516. lbs_deb_join(
  517. "ADHOC_J_CMD: BSSID = " MAC_FMT ", SSID = '%s'\n",
  518. MAC_ARG(join_cmd->bss.bssid), join_cmd->bss.ssid);
  519. /* failtimeout */
  520. join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  521. /* probedelay */
  522. join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  523. adapter->curbssparams.channel = bss->channel;
  524. /* Copy Data rates from the rates recorded in scan response */
  525. memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
  526. ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
  527. memcpy(join_cmd->bss.rates, bss->rates, ratesize);
  528. if (get_common_rates(adapter, join_cmd->bss.rates, &ratesize)) {
  529. lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
  530. ret = -1;
  531. goto done;
  532. }
  533. /* Copy the ad-hoc creating rates into Current BSS state structure */
  534. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  535. memcpy(&adapter->curbssparams.rates, join_cmd->bss.rates, ratesize);
  536. /* Set MSB on basic rates as the firmware requires, but _after_
  537. * copying to current bss rates.
  538. */
  539. libertas_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
  540. join_cmd->bss.ssparamset.ibssparamset.atimwindow =
  541. cpu_to_le16(bss->atimwindow);
  542. if (assoc_req->secinfo.wep_enabled) {
  543. u16 tmp = le16_to_cpu(join_cmd->bss.capability);
  544. tmp |= WLAN_CAPABILITY_PRIVACY;
  545. join_cmd->bss.capability = cpu_to_le16(tmp);
  546. }
  547. if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
  548. /* wake up first */
  549. __le32 Localpsmode;
  550. Localpsmode = cpu_to_le32(WLAN802_11POWERMODECAM);
  551. ret = libertas_prepare_and_send_command(priv,
  552. CMD_802_11_PS_MODE,
  553. CMD_ACT_SET,
  554. 0, 0, &Localpsmode);
  555. if (ret) {
  556. ret = -1;
  557. goto done;
  558. }
  559. }
  560. if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
  561. ret = -1;
  562. goto done;
  563. }
  564. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
  565. S_DS_GEN + cmdappendsize);
  566. done:
  567. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  568. return ret;
  569. }
  570. int libertas_ret_80211_associate(wlan_private * priv,
  571. struct cmd_ds_command *resp)
  572. {
  573. wlan_adapter *adapter = priv->adapter;
  574. int ret = 0;
  575. union iwreq_data wrqu;
  576. struct ieeetypes_assocrsp *passocrsp;
  577. struct bss_descriptor * bss;
  578. lbs_deb_enter(LBS_DEB_JOIN);
  579. if (!adapter->in_progress_assoc_req) {
  580. lbs_deb_join("ASSOC_RESP: no in-progress association request\n");
  581. ret = -1;
  582. goto done;
  583. }
  584. bss = &adapter->in_progress_assoc_req->bss;
  585. passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
  586. if (le16_to_cpu(passocrsp->statuscode)) {
  587. libertas_mac_event_disconnected(priv);
  588. lbs_deb_join("ASSOC_RESP: Association failed, status code = %d\n",
  589. le16_to_cpu(passocrsp->statuscode));
  590. ret = -1;
  591. goto done;
  592. }
  593. lbs_dbg_hex("ASSOC_RESP:", (void *)&resp->params,
  594. le16_to_cpu(resp->size) - S_DS_GEN);
  595. /* Send a Media Connected event, according to the Spec */
  596. adapter->connect_status = LIBERTAS_CONNECTED;
  597. lbs_deb_join("ASSOC_RESP: assocated to '%s'\n",
  598. escape_essid(bss->ssid, bss->ssid_len));
  599. /* Update current SSID and BSSID */
  600. memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  601. adapter->curbssparams.ssid_len = bss->ssid_len;
  602. memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  603. lbs_deb_join("ASSOC_RESP: currentpacketfilter is %x\n",
  604. adapter->currentpacketfilter);
  605. adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
  606. adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
  607. memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
  608. memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
  609. adapter->nextSNRNF = 0;
  610. adapter->numSNRNF = 0;
  611. netif_carrier_on(priv->dev);
  612. netif_wake_queue(priv->dev);
  613. netif_carrier_on(priv->mesh_dev);
  614. netif_wake_queue(priv->mesh_dev);
  615. lbs_deb_join("ASSOC_RESP: Associated \n");
  616. memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  617. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  618. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  619. done:
  620. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  621. return ret;
  622. }
  623. int libertas_ret_80211_disassociate(wlan_private * priv,
  624. struct cmd_ds_command *resp)
  625. {
  626. lbs_deb_enter(LBS_DEB_JOIN);
  627. libertas_mac_event_disconnected(priv);
  628. lbs_deb_leave(LBS_DEB_JOIN);
  629. return 0;
  630. }
  631. int libertas_ret_80211_ad_hoc_start(wlan_private * priv,
  632. struct cmd_ds_command *resp)
  633. {
  634. wlan_adapter *adapter = priv->adapter;
  635. int ret = 0;
  636. u16 command = le16_to_cpu(resp->command);
  637. u16 result = le16_to_cpu(resp->result);
  638. struct cmd_ds_802_11_ad_hoc_result *padhocresult;
  639. union iwreq_data wrqu;
  640. struct bss_descriptor *bss;
  641. lbs_deb_enter(LBS_DEB_JOIN);
  642. padhocresult = &resp->params.result;
  643. lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
  644. lbs_deb_join("ADHOC_RESP: command = %x\n", command);
  645. lbs_deb_join("ADHOC_RESP: result = %x\n", result);
  646. if (!adapter->in_progress_assoc_req) {
  647. lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
  648. ret = -1;
  649. goto done;
  650. }
  651. bss = &adapter->in_progress_assoc_req->bss;
  652. /*
  653. * Join result code 0 --> SUCCESS
  654. */
  655. if (result) {
  656. lbs_deb_join("ADHOC_RESP: failed\n");
  657. if (adapter->connect_status == LIBERTAS_CONNECTED) {
  658. libertas_mac_event_disconnected(priv);
  659. }
  660. ret = -1;
  661. goto done;
  662. }
  663. /*
  664. * Now the join cmd should be successful
  665. * If BSSID has changed use SSID to compare instead of BSSID
  666. */
  667. lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
  668. escape_essid(bss->ssid, bss->ssid_len));
  669. /* Send a Media Connected event, according to the Spec */
  670. adapter->connect_status = LIBERTAS_CONNECTED;
  671. if (command == CMD_RET_802_11_AD_HOC_START) {
  672. /* Update the created network descriptor with the new BSSID */
  673. memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
  674. }
  675. /* Set the BSSID from the joined/started descriptor */
  676. memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  677. /* Set the new SSID to current SSID */
  678. memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  679. adapter->curbssparams.ssid_len = bss->ssid_len;
  680. netif_carrier_on(priv->dev);
  681. netif_wake_queue(priv->dev);
  682. netif_carrier_on(priv->mesh_dev);
  683. netif_wake_queue(priv->mesh_dev);
  684. memset(&wrqu, 0, sizeof(wrqu));
  685. memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  686. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  687. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  688. lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
  689. lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
  690. lbs_deb_join("ADHOC_RESP: BSSID = " MAC_FMT "\n",
  691. MAC_ARG(padhocresult->bssid));
  692. done:
  693. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  694. return ret;
  695. }
  696. int libertas_ret_80211_ad_hoc_stop(wlan_private * priv,
  697. struct cmd_ds_command *resp)
  698. {
  699. lbs_deb_enter(LBS_DEB_JOIN);
  700. libertas_mac_event_disconnected(priv);
  701. lbs_deb_leave(LBS_DEB_JOIN);
  702. return 0;
  703. }