join.c 27 KB

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