join.c 24 KB

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