join.c 26 KB

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