join.c 25 KB

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