join.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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("libertas_join_adhoc_network: CurBss.ssid =%s\n",
  157. adapter->curbssparams.ssid.ssid);
  158. lbs_deb_join("libertas_join_adhoc_network: CurBss.ssid_len =%u\n",
  159. adapter->curbssparams.ssid.ssidlength);
  160. lbs_deb_join("libertas_join_adhoc_network: ssid = '%s'\n",
  161. bss->ssid.ssid);
  162. lbs_deb_join("libertas_join_adhoc_network: ssid len = %u\n",
  163. bss->ssid.ssidlength);
  164. /* check if the requested SSID is already joined */
  165. if (adapter->curbssparams.ssid.ssidlength
  166. && !libertas_SSID_cmp(&bss->ssid, &adapter->curbssparams.ssid)
  167. && (adapter->mode == IW_MODE_ADHOC)) {
  168. lbs_deb_join(
  169. "ADHOC_J_CMD: New ad-hoc SSID is the same as current, "
  170. "not attempting to re-join");
  171. return -1;
  172. }
  173. /*Use shortpreamble only when both creator and card supports
  174. short preamble */
  175. if (!bss->cap.shortpreamble || !adapter->capinfo.shortpreamble) {
  176. lbs_deb_join("AdhocJoin: Long preamble\n");
  177. adapter->preamble = cmd_type_long_preamble;
  178. } else {
  179. lbs_deb_join("AdhocJoin: Short preamble\n");
  180. adapter->preamble = cmd_type_short_preamble;
  181. }
  182. libertas_set_radio_control(priv);
  183. lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
  184. lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);
  185. adapter->adhoccreate = 0;
  186. ret = libertas_prepare_and_send_command(priv, cmd_802_11_ad_hoc_join,
  187. 0, cmd_option_waitforrsp,
  188. OID_802_11_SSID, assoc_req);
  189. return ret;
  190. }
  191. int libertas_stop_adhoc_network(wlan_private * priv)
  192. {
  193. return libertas_prepare_and_send_command(priv, cmd_802_11_ad_hoc_stop,
  194. 0, cmd_option_waitforrsp, 0, NULL);
  195. }
  196. /**
  197. * @brief Send Deauthentication Request
  198. *
  199. * @param priv A pointer to wlan_private structure
  200. * @return 0--success, -1--fail
  201. */
  202. int libertas_send_deauthentication(wlan_private * priv)
  203. {
  204. return libertas_prepare_and_send_command(priv, cmd_802_11_deauthenticate,
  205. 0, cmd_option_waitforrsp, 0, NULL);
  206. }
  207. /**
  208. * @brief This function prepares command of authenticate.
  209. *
  210. * @param priv A pointer to wlan_private structure
  211. * @param cmd A pointer to cmd_ds_command structure
  212. * @param pdata_buf Void cast of pointer to a BSSID to authenticate with
  213. *
  214. * @return 0 or -1
  215. */
  216. int libertas_cmd_80211_authenticate(wlan_private * priv,
  217. struct cmd_ds_command *cmd,
  218. void *pdata_buf)
  219. {
  220. wlan_adapter *adapter = priv->adapter;
  221. struct cmd_ds_802_11_authenticate *pauthenticate = &cmd->params.auth;
  222. int ret = -1;
  223. u8 *bssid = pdata_buf;
  224. lbs_deb_enter(LBS_DEB_JOIN);
  225. cmd->command = cpu_to_le16(cmd_802_11_authenticate);
  226. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
  227. + S_DS_GEN);
  228. /* translate auth mode to 802.11 defined wire value */
  229. switch (adapter->secinfo.auth_mode) {
  230. case IW_AUTH_ALG_OPEN_SYSTEM:
  231. pauthenticate->authtype = 0x00;
  232. break;
  233. case IW_AUTH_ALG_SHARED_KEY:
  234. pauthenticate->authtype = 0x01;
  235. break;
  236. case IW_AUTH_ALG_LEAP:
  237. pauthenticate->authtype = 0x80;
  238. break;
  239. default:
  240. lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
  241. adapter->secinfo.auth_mode);
  242. goto out;
  243. }
  244. memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
  245. lbs_deb_join("AUTH_CMD: BSSID is : " MAC_FMT " auth=0x%X\n",
  246. MAC_ARG(bssid), pauthenticate->authtype);
  247. ret = 0;
  248. out:
  249. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  250. return ret;
  251. }
  252. int libertas_cmd_80211_deauthenticate(wlan_private * priv,
  253. struct cmd_ds_command *cmd)
  254. {
  255. wlan_adapter *adapter = priv->adapter;
  256. struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
  257. lbs_deb_enter(LBS_DEB_JOIN);
  258. cmd->command = cpu_to_le16(cmd_802_11_deauthenticate);
  259. cmd->size =
  260. cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
  261. S_DS_GEN);
  262. /* set AP MAC address */
  263. memmove(dauth->macaddr, adapter->curbssparams.bssid,
  264. 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;
  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 = 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. ssid->header.len = bss->ssid.ssidlength;
  306. memcpy(ssid->ssid, bss->ssid.ssid, ssid->header.len);
  307. pos += sizeof(ssid->header) + ssid->header.len;
  308. ssid->header.len = cpu_to_le16(ssid->header.len);
  309. phy = (struct mrvlietypes_phyparamset *) pos;
  310. phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  311. phy->header.len = sizeof(phy->fh_ds.dsparamset);
  312. memcpy(&phy->fh_ds.dsparamset,
  313. &bss->phyparamset.dsparamset.currentchan,
  314. sizeof(phy->fh_ds.dsparamset));
  315. pos += sizeof(phy->header) + phy->header.len;
  316. phy->header.len = cpu_to_le16(phy->header.len);
  317. ss = (struct mrvlietypes_ssparamset *) pos;
  318. ss->header.type = cpu_to_le16(TLV_TYPE_CF);
  319. ss->header.len = sizeof(ss->cf_ibss.cfparamset);
  320. pos += sizeof(ss->header) + ss->header.len;
  321. ss->header.len = cpu_to_le16(ss->header.len);
  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. rates->header.len = min_t(size_t, strlen(rates->rates), WLAN_SUPPORTED_RATES);
  333. adapter->curbssparams.numofrates = rates->header.len;
  334. pos += sizeof(rates->header) + rates->header.len;
  335. rates->header.len = cpu_to_le16(rates->header.len);
  336. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  337. rsn = (struct mrvlietypes_rsnparamset *) pos;
  338. rsn->header.type = (u16) assoc_req->wpa_ie[0]; /* WPA_IE or WPA2_IE */
  339. rsn->header.type = cpu_to_le16(rsn->header.type);
  340. rsn->header.len = (u16) assoc_req->wpa_ie[1];
  341. memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], rsn->header.len);
  342. lbs_dbg_hex("ASSOC_CMD: RSN IE", (u8 *) rsn,
  343. sizeof(rsn->header) + rsn->header.len);
  344. pos += sizeof(rsn->header) + rsn->header.len;
  345. rsn->header.len = cpu_to_le16(rsn->header.len);
  346. }
  347. /* update curbssparams */
  348. adapter->curbssparams.channel =
  349. (bss->phyparamset.dsparamset.currentchan);
  350. /* Copy the infra. association rates into Current BSS state structure */
  351. memcpy(&adapter->curbssparams.datarates, &rates->rates,
  352. min_t(size_t, sizeof(adapter->curbssparams.datarates), rates->header.len));
  353. lbs_deb_join("ASSOC_CMD: rates->header.len = %d\n", rates->header.len);
  354. /* set IBSS field */
  355. if (bss->mode == IW_MODE_INFRA) {
  356. #define CAPINFO_ESS_MODE 1
  357. passo->capinfo.ess = CAPINFO_ESS_MODE;
  358. }
  359. if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
  360. ret = -1;
  361. goto done;
  362. }
  363. cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
  364. /* set the capability info at last */
  365. memcpy(&tmpcap, &bss->cap, sizeof(passo->capinfo));
  366. tmpcap &= CAPINFO_MASK;
  367. lbs_deb_join("ASSOC_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  368. tmpcap, CAPINFO_MASK);
  369. tmpcap = cpu_to_le16(tmpcap);
  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. u16 tmpcap;
  384. struct assoc_request * assoc_req = pdata_buf;
  385. lbs_deb_enter(LBS_DEB_JOIN);
  386. if (!adapter) {
  387. ret = -1;
  388. goto done;
  389. }
  390. cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_start);
  391. /*
  392. * Fill in the parameters for 2 data structures:
  393. * 1. cmd_ds_802_11_ad_hoc_start command
  394. * 2. adapter->scantable[i]
  395. *
  396. * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
  397. * probe delay, and cap info.
  398. *
  399. * Firmware will fill up beacon period, DTIM, Basic rates
  400. * and operational rates.
  401. */
  402. memset(adhs->SSID, 0, IW_ESSID_MAX_SIZE);
  403. memcpy(adhs->SSID, assoc_req->ssid.ssid, assoc_req->ssid.ssidlength);
  404. lbs_deb_join("ADHOC_S_CMD: SSID = %s\n", adhs->SSID);
  405. /* set the BSS type */
  406. adhs->bsstype = cmd_bss_type_ibss;
  407. adapter->mode = IW_MODE_ADHOC;
  408. adhs->beaconperiod = adapter->beaconperiod;
  409. /* set Physical param set */
  410. #define DS_PARA_IE_ID 3
  411. #define DS_PARA_IE_LEN 1
  412. adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
  413. adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
  414. WARN_ON(!assoc_req->channel);
  415. lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
  416. assoc_req->channel);
  417. adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
  418. /* set IBSS param set */
  419. #define IBSS_PARA_IE_ID 6
  420. #define IBSS_PARA_IE_LEN 2
  421. adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
  422. adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
  423. adhs->ssparamset.ibssparamset.atimwindow = adapter->atimwindow;
  424. /* set capability info */
  425. adhs->cap.ess = 0;
  426. adhs->cap.ibss = 1;
  427. /* probedelay */
  428. adhs->probedelay = cpu_to_le16(cmd_scan_probe_delay_time);
  429. /* set up privacy in adapter->scantable[i] */
  430. if (assoc_req->secinfo.wep_enabled) {
  431. lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
  432. adhs->cap.privacy = AD_HOC_CAP_PRIVACY_ON;
  433. } else {
  434. lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
  435. }
  436. memset(adhs->datarate, 0, sizeof(adhs->datarate));
  437. if (adapter->adhoc_grate_enabled) {
  438. memcpy(adhs->datarate, libertas_adhoc_rates_g,
  439. min(sizeof(adhs->datarate), sizeof(libertas_adhoc_rates_g)));
  440. } else {
  441. memcpy(adhs->datarate, libertas_adhoc_rates_b,
  442. min(sizeof(adhs->datarate), sizeof(libertas_adhoc_rates_b)));
  443. }
  444. /* Find the last non zero */
  445. for (i = 0; i < sizeof(adhs->datarate) && adhs->datarate[i]; i++) ;
  446. adapter->curbssparams.numofrates = i;
  447. /* Copy the ad-hoc creating rates into Current BSS state structure */
  448. memcpy(&adapter->curbssparams.datarates,
  449. &adhs->datarate, adapter->curbssparams.numofrates);
  450. lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
  451. adhs->datarate[0], adhs->datarate[1],
  452. adhs->datarate[2], adhs->datarate[3]);
  453. lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
  454. if (libertas_create_dnld_countryinfo_11d(priv)) {
  455. lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
  456. ret = -1;
  457. goto done;
  458. }
  459. cmd->size =
  460. cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start)
  461. + S_DS_GEN + cmdappendsize);
  462. memcpy(&tmpcap, &adhs->cap, sizeof(u16));
  463. tmpcap = cpu_to_le16(tmpcap);
  464. memcpy(&adhs->cap, &tmpcap, sizeof(u16));
  465. ret = 0;
  466. done:
  467. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  468. return ret;
  469. }
  470. int libertas_cmd_80211_ad_hoc_stop(wlan_private * priv,
  471. struct cmd_ds_command *cmd)
  472. {
  473. cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_stop);
  474. cmd->size = cpu_to_le16(S_DS_GEN);
  475. return 0;
  476. }
  477. int libertas_cmd_80211_ad_hoc_join(wlan_private * priv,
  478. struct cmd_ds_command *cmd, void *pdata_buf)
  479. {
  480. wlan_adapter *adapter = priv->adapter;
  481. struct cmd_ds_802_11_ad_hoc_join *padhocjoin = &cmd->params.adj;
  482. struct assoc_request * assoc_req = pdata_buf;
  483. struct bss_descriptor *bss = &assoc_req->bss;
  484. int cmdappendsize = 0;
  485. int ret = 0;
  486. u8 *card_rates;
  487. int card_rates_size;
  488. u16 tmpcap;
  489. int i;
  490. lbs_deb_enter(LBS_DEB_JOIN);
  491. cmd->command = cpu_to_le16(cmd_802_11_ad_hoc_join);
  492. padhocjoin->bssdescriptor.bsstype = cmd_bss_type_ibss;
  493. padhocjoin->bssdescriptor.beaconperiod = bss->beaconperiod;
  494. memcpy(&padhocjoin->bssdescriptor.BSSID, &bss->bssid, ETH_ALEN);
  495. memcpy(&padhocjoin->bssdescriptor.SSID, &bss->ssid.ssid, bss->ssid.ssidlength);
  496. memcpy(&padhocjoin->bssdescriptor.phyparamset,
  497. &bss->phyparamset, sizeof(union ieeetypes_phyparamset));
  498. memcpy(&padhocjoin->bssdescriptor.ssparamset,
  499. &bss->ssparamset, sizeof(union IEEEtypes_ssparamset));
  500. memcpy(&tmpcap, &bss->cap, sizeof(struct ieeetypes_capinfo));
  501. tmpcap &= CAPINFO_MASK;
  502. lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  503. tmpcap, CAPINFO_MASK);
  504. memcpy(&padhocjoin->bssdescriptor.cap, &tmpcap,
  505. sizeof(struct ieeetypes_capinfo));
  506. /* information on BSSID descriptor passed to FW */
  507. lbs_deb_join(
  508. "ADHOC_J_CMD: BSSID = " MAC_FMT ", SSID = '%s'\n",
  509. MAC_ARG(padhocjoin->bssdescriptor.BSSID),
  510. padhocjoin->bssdescriptor.SSID);
  511. /* failtimeout */
  512. padhocjoin->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  513. /* probedelay */
  514. padhocjoin->probedelay =
  515. cpu_to_le16(cmd_scan_probe_delay_time);
  516. /* Copy Data rates from the rates recorded in scan response */
  517. memset(padhocjoin->bssdescriptor.datarates, 0,
  518. sizeof(padhocjoin->bssdescriptor.datarates));
  519. memcpy(padhocjoin->bssdescriptor.datarates, bss->datarates,
  520. min(sizeof(padhocjoin->bssdescriptor.datarates),
  521. sizeof(bss->datarates)));
  522. card_rates = libertas_supported_rates;
  523. card_rates_size = sizeof(libertas_supported_rates);
  524. adapter->curbssparams.channel = bss->channel;
  525. if (get_common_rates(adapter, padhocjoin->bssdescriptor.datarates,
  526. sizeof(padhocjoin->bssdescriptor.datarates),
  527. card_rates, card_rates_size)) {
  528. lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
  529. ret = -1;
  530. goto done;
  531. }
  532. /* Find the last non zero */
  533. for (i = 0; i < sizeof(padhocjoin->bssdescriptor.datarates)
  534. && padhocjoin->bssdescriptor.datarates[i]; i++) ;
  535. adapter->curbssparams.numofrates = i;
  536. /*
  537. * Copy the adhoc joining rates to Current BSS State structure
  538. */
  539. memcpy(adapter->curbssparams.datarates,
  540. padhocjoin->bssdescriptor.datarates,
  541. adapter->curbssparams.numofrates);
  542. padhocjoin->bssdescriptor.ssparamset.ibssparamset.atimwindow =
  543. cpu_to_le16(bss->atimwindow);
  544. if (assoc_req->secinfo.wep_enabled) {
  545. padhocjoin->bssdescriptor.cap.privacy = AD_HOC_CAP_PRIVACY_ON;
  546. }
  547. if (adapter->psmode == wlan802_11powermodemax_psp) {
  548. /* wake up first */
  549. enum WLAN_802_11_POWER_MODE Localpsmode;
  550. Localpsmode = 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 =
  565. cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join)
  566. + S_DS_GEN + cmdappendsize);
  567. memcpy(&tmpcap, &padhocjoin->bssdescriptor.cap,
  568. sizeof(struct ieeetypes_capinfo));
  569. tmpcap = cpu_to_le16(tmpcap);
  570. memcpy(&padhocjoin->bssdescriptor.cap,
  571. &tmpcap, sizeof(struct ieeetypes_capinfo));
  572. done:
  573. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  574. return ret;
  575. }
  576. int libertas_ret_80211_associate(wlan_private * priv,
  577. struct cmd_ds_command *resp)
  578. {
  579. wlan_adapter *adapter = priv->adapter;
  580. int ret = 0;
  581. union iwreq_data wrqu;
  582. struct ieeetypes_assocrsp *passocrsp;
  583. struct bss_descriptor * bss;
  584. lbs_deb_enter(LBS_DEB_JOIN);
  585. if (!adapter->in_progress_assoc_req) {
  586. lbs_deb_join("ASSOC_RESP: no in-progress association request\n");
  587. ret = -1;
  588. goto done;
  589. }
  590. bss = &adapter->in_progress_assoc_req->bss;
  591. passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
  592. if (passocrsp->statuscode) {
  593. libertas_mac_event_disconnected(priv);
  594. lbs_deb_join("ASSOC_RESP: Association failed, status code = %d\n",
  595. passocrsp->statuscode);
  596. ret = -1;
  597. goto done;
  598. }
  599. lbs_dbg_hex("ASSOC_RESP:", (void *)&resp->params,
  600. le16_to_cpu(resp->size) - S_DS_GEN);
  601. /* Send a Media Connected event, according to the Spec */
  602. adapter->connect_status = libertas_connected;
  603. lbs_deb_join("ASSOC_RESP: %s\n", bss->ssid.ssid);
  604. /* Update current SSID and BSSID */
  605. memcpy(&adapter->curbssparams.ssid,
  606. &bss->ssid, sizeof(struct WLAN_802_11_SSID));
  607. memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  608. lbs_deb_join("ASSOC_RESP: currentpacketfilter is %x\n",
  609. adapter->currentpacketfilter);
  610. adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
  611. adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
  612. memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
  613. memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
  614. adapter->nextSNRNF = 0;
  615. adapter->numSNRNF = 0;
  616. netif_carrier_on(priv->dev);
  617. netif_wake_queue(priv->dev);
  618. netif_carrier_on(priv->mesh_dev);
  619. netif_wake_queue(priv->mesh_dev);
  620. lbs_deb_join("ASSOC_RESP: Associated \n");
  621. memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  622. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  623. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  624. done:
  625. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  626. return ret;
  627. }
  628. int libertas_ret_80211_disassociate(wlan_private * priv,
  629. struct cmd_ds_command *resp)
  630. {
  631. lbs_deb_enter(LBS_DEB_JOIN);
  632. libertas_mac_event_disconnected(priv);
  633. lbs_deb_leave(LBS_DEB_JOIN);
  634. return 0;
  635. }
  636. int libertas_ret_80211_ad_hoc_start(wlan_private * priv,
  637. struct cmd_ds_command *resp)
  638. {
  639. wlan_adapter *adapter = priv->adapter;
  640. int ret = 0;
  641. u16 command = le16_to_cpu(resp->command);
  642. u16 result = le16_to_cpu(resp->result);
  643. struct cmd_ds_802_11_ad_hoc_result *padhocresult;
  644. union iwreq_data wrqu;
  645. struct bss_descriptor *bss;
  646. lbs_deb_enter(LBS_DEB_JOIN);
  647. padhocresult = &resp->params.result;
  648. lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
  649. lbs_deb_join("ADHOC_RESP: command = %x\n", command);
  650. lbs_deb_join("ADHOC_RESP: result = %x\n", result);
  651. if (!adapter->in_progress_assoc_req) {
  652. lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
  653. ret = -1;
  654. goto done;
  655. }
  656. bss = &adapter->in_progress_assoc_req->bss;
  657. /*
  658. * Join result code 0 --> SUCCESS
  659. */
  660. if (result) {
  661. lbs_deb_join("ADHOC_RESP: failed\n");
  662. if (adapter->connect_status == libertas_connected) {
  663. libertas_mac_event_disconnected(priv);
  664. }
  665. ret = -1;
  666. goto done;
  667. }
  668. /*
  669. * Now the join cmd should be successful
  670. * If BSSID has changed use SSID to compare instead of BSSID
  671. */
  672. lbs_deb_join("ADHOC_RESP: %s\n", bss->ssid.ssid);
  673. /* Send a Media Connected event, according to the Spec */
  674. adapter->connect_status = libertas_connected;
  675. if (command == cmd_ret_802_11_ad_hoc_start) {
  676. /* Update the created network descriptor with the new BSSID */
  677. memcpy(bss->bssid, padhocresult->BSSID, ETH_ALEN);
  678. }
  679. /* Set the BSSID from the joined/started descriptor */
  680. memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  681. /* Set the new SSID to current SSID */
  682. memcpy(&adapter->curbssparams.ssid, &bss->ssid,
  683. sizeof(struct WLAN_802_11_SSID));
  684. netif_carrier_on(priv->dev);
  685. netif_wake_queue(priv->dev);
  686. netif_carrier_on(priv->mesh_dev);
  687. netif_wake_queue(priv->mesh_dev);
  688. memset(&wrqu, 0, sizeof(wrqu));
  689. memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  690. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  691. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  692. lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
  693. lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
  694. lbs_deb_join("ADHOC_RESP: BSSID = " MAC_FMT "\n",
  695. MAC_ARG(padhocresult->BSSID));
  696. done:
  697. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  698. return ret;
  699. }
  700. int libertas_ret_80211_ad_hoc_stop(wlan_private * priv,
  701. struct cmd_ds_command *resp)
  702. {
  703. lbs_deb_enter(LBS_DEB_JOIN);
  704. libertas_mac_event_disconnected(priv);
  705. lbs_deb_leave(LBS_DEB_JOIN);
  706. return 0;
  707. }