join.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. DECLARE_MAC_BUF(mac);
  254. lbs_deb_enter(LBS_DEB_JOIN);
  255. cmd->command = cpu_to_le16(CMD_802_11_AUTHENTICATE);
  256. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_authenticate)
  257. + S_DS_GEN);
  258. /* translate auth mode to 802.11 defined wire value */
  259. switch (adapter->secinfo.auth_mode) {
  260. case IW_AUTH_ALG_OPEN_SYSTEM:
  261. pauthenticate->authtype = 0x00;
  262. break;
  263. case IW_AUTH_ALG_SHARED_KEY:
  264. pauthenticate->authtype = 0x01;
  265. break;
  266. case IW_AUTH_ALG_LEAP:
  267. pauthenticate->authtype = 0x80;
  268. break;
  269. default:
  270. lbs_deb_join("AUTH_CMD: invalid auth alg 0x%X\n",
  271. adapter->secinfo.auth_mode);
  272. goto out;
  273. }
  274. memcpy(pauthenticate->macaddr, bssid, ETH_ALEN);
  275. lbs_deb_join("AUTH_CMD: BSSID is : %s auth=0x%X\n",
  276. print_mac(mac, bssid), pauthenticate->authtype);
  277. ret = 0;
  278. out:
  279. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  280. return ret;
  281. }
  282. int libertas_cmd_80211_deauthenticate(wlan_private * priv,
  283. struct cmd_ds_command *cmd)
  284. {
  285. wlan_adapter *adapter = priv->adapter;
  286. struct cmd_ds_802_11_deauthenticate *dauth = &cmd->params.deauth;
  287. lbs_deb_enter(LBS_DEB_JOIN);
  288. cmd->command = cpu_to_le16(CMD_802_11_DEAUTHENTICATE);
  289. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_deauthenticate) +
  290. S_DS_GEN);
  291. /* set AP MAC address */
  292. memmove(dauth->macaddr, adapter->curbssparams.bssid, ETH_ALEN);
  293. /* Reason code 3 = Station is leaving */
  294. #define REASON_CODE_STA_LEAVING 3
  295. dauth->reasoncode = cpu_to_le16(REASON_CODE_STA_LEAVING);
  296. lbs_deb_leave(LBS_DEB_JOIN);
  297. return 0;
  298. }
  299. int libertas_cmd_80211_associate(wlan_private * priv,
  300. struct cmd_ds_command *cmd, void *pdata_buf)
  301. {
  302. wlan_adapter *adapter = priv->adapter;
  303. struct cmd_ds_802_11_associate *passo = &cmd->params.associate;
  304. int ret = 0;
  305. struct assoc_request * assoc_req = pdata_buf;
  306. struct bss_descriptor * bss = &assoc_req->bss;
  307. u8 *pos;
  308. u16 tmpcap, tmplen;
  309. struct mrvlietypes_ssidparamset *ssid;
  310. struct mrvlietypes_phyparamset *phy;
  311. struct mrvlietypes_ssparamset *ss;
  312. struct mrvlietypes_ratesparamset *rates;
  313. struct mrvlietypes_rsnparamset *rsn;
  314. lbs_deb_enter(LBS_DEB_JOIN);
  315. pos = (u8 *) passo;
  316. if (!adapter) {
  317. ret = -1;
  318. goto done;
  319. }
  320. cmd->command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  321. memcpy(passo->peerstaaddr, bss->bssid, sizeof(passo->peerstaaddr));
  322. pos += sizeof(passo->peerstaaddr);
  323. /* set the listen interval */
  324. passo->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  325. pos += sizeof(passo->capability);
  326. pos += sizeof(passo->listeninterval);
  327. pos += sizeof(passo->bcnperiod);
  328. pos += sizeof(passo->dtimperiod);
  329. ssid = (struct mrvlietypes_ssidparamset *) pos;
  330. ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
  331. tmplen = bss->ssid_len;
  332. ssid->header.len = cpu_to_le16(tmplen);
  333. memcpy(ssid->ssid, bss->ssid, tmplen);
  334. pos += sizeof(ssid->header) + tmplen;
  335. phy = (struct mrvlietypes_phyparamset *) pos;
  336. phy->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  337. tmplen = sizeof(phy->fh_ds.dsparamset);
  338. phy->header.len = cpu_to_le16(tmplen);
  339. memcpy(&phy->fh_ds.dsparamset,
  340. &bss->phyparamset.dsparamset.currentchan,
  341. tmplen);
  342. pos += sizeof(phy->header) + tmplen;
  343. ss = (struct mrvlietypes_ssparamset *) pos;
  344. ss->header.type = cpu_to_le16(TLV_TYPE_CF);
  345. tmplen = sizeof(ss->cf_ibss.cfparamset);
  346. ss->header.len = cpu_to_le16(tmplen);
  347. pos += sizeof(ss->header) + tmplen;
  348. rates = (struct mrvlietypes_ratesparamset *) pos;
  349. rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
  350. memcpy(&rates->rates, &bss->rates, MAX_RATES);
  351. tmplen = MAX_RATES;
  352. if (get_common_rates(adapter, rates->rates, &tmplen)) {
  353. ret = -1;
  354. goto done;
  355. }
  356. pos += sizeof(rates->header) + tmplen;
  357. rates->header.len = cpu_to_le16(tmplen);
  358. lbs_deb_join("ASSOC_CMD: num rates = %u\n", tmplen);
  359. /* Copy the infra. association rates into Current BSS state structure */
  360. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  361. memcpy(&adapter->curbssparams.rates, &rates->rates, tmplen);
  362. /* Set MSB on basic rates as the firmware requires, but _after_
  363. * copying to current bss rates.
  364. */
  365. libertas_set_basic_rate_flags(rates->rates, tmplen);
  366. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  367. rsn = (struct mrvlietypes_rsnparamset *) pos;
  368. /* WPA_IE or WPA2_IE */
  369. rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
  370. tmplen = (u16) assoc_req->wpa_ie[1];
  371. rsn->header.len = cpu_to_le16(tmplen);
  372. memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
  373. lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: RSN IE", (u8 *) rsn,
  374. sizeof(rsn->header) + tmplen);
  375. pos += sizeof(rsn->header) + tmplen;
  376. }
  377. /* update curbssparams */
  378. adapter->curbssparams.channel = bss->phyparamset.dsparamset.currentchan;
  379. if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
  380. ret = -1;
  381. goto done;
  382. }
  383. cmd->size = cpu_to_le16((u16) (pos - (u8 *) passo) + S_DS_GEN);
  384. /* set the capability info */
  385. tmpcap = (bss->capability & CAPINFO_MASK);
  386. if (bss->mode == IW_MODE_INFRA)
  387. tmpcap |= WLAN_CAPABILITY_ESS;
  388. passo->capability = cpu_to_le16(tmpcap);
  389. lbs_deb_join("ASSOC_CMD: capability=%4X CAPINFO_MASK=%4X\n",
  390. tmpcap, CAPINFO_MASK);
  391. done:
  392. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  393. return ret;
  394. }
  395. int libertas_cmd_80211_ad_hoc_start(wlan_private * priv,
  396. struct cmd_ds_command *cmd, void *pdata_buf)
  397. {
  398. wlan_adapter *adapter = priv->adapter;
  399. struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
  400. int ret = 0;
  401. int cmdappendsize = 0;
  402. struct assoc_request * assoc_req = pdata_buf;
  403. u16 tmpcap = 0;
  404. size_t ratesize = 0;
  405. lbs_deb_enter(LBS_DEB_JOIN);
  406. if (!adapter) {
  407. ret = -1;
  408. goto done;
  409. }
  410. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
  411. /*
  412. * Fill in the parameters for 2 data structures:
  413. * 1. cmd_ds_802_11_ad_hoc_start command
  414. * 2. adapter->scantable[i]
  415. *
  416. * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
  417. * probe delay, and cap info.
  418. *
  419. * Firmware will fill up beacon period, DTIM, Basic rates
  420. * and operational rates.
  421. */
  422. memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
  423. memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
  424. lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
  425. escape_essid(assoc_req->ssid, assoc_req->ssid_len),
  426. assoc_req->ssid_len);
  427. /* set the BSS type */
  428. adhs->bsstype = CMD_BSS_TYPE_IBSS;
  429. adapter->mode = IW_MODE_ADHOC;
  430. adhs->beaconperiod = cpu_to_le16(MRVDRV_BEACON_INTERVAL);
  431. /* set Physical param set */
  432. #define DS_PARA_IE_ID 3
  433. #define DS_PARA_IE_LEN 1
  434. adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
  435. adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
  436. WARN_ON(!assoc_req->channel);
  437. lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
  438. assoc_req->channel);
  439. adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
  440. /* set IBSS param set */
  441. #define IBSS_PARA_IE_ID 6
  442. #define IBSS_PARA_IE_LEN 2
  443. adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
  444. adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
  445. adhs->ssparamset.ibssparamset.atimwindow = 0;
  446. /* set capability info */
  447. tmpcap = WLAN_CAPABILITY_IBSS;
  448. if (assoc_req->secinfo.wep_enabled) {
  449. lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
  450. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  451. } else {
  452. lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
  453. }
  454. adhs->capability = cpu_to_le16(tmpcap);
  455. /* probedelay */
  456. adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  457. memset(adhs->rates, 0, sizeof(adhs->rates));
  458. ratesize = min(sizeof(adhs->rates), sizeof(libertas_bg_rates));
  459. memcpy(adhs->rates, libertas_bg_rates, ratesize);
  460. /* Copy the ad-hoc creating rates into Current BSS state structure */
  461. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  462. memcpy(&adapter->curbssparams.rates, &adhs->rates, ratesize);
  463. /* Set MSB on basic rates as the firmware requires, but _after_
  464. * copying to current bss rates.
  465. */
  466. libertas_set_basic_rate_flags(adhs->rates, ratesize);
  467. lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
  468. adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
  469. lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
  470. if (libertas_create_dnld_countryinfo_11d(priv)) {
  471. lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
  472. ret = -1;
  473. goto done;
  474. }
  475. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
  476. S_DS_GEN + cmdappendsize);
  477. ret = 0;
  478. done:
  479. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  480. return ret;
  481. }
  482. int libertas_cmd_80211_ad_hoc_stop(wlan_private * priv,
  483. struct cmd_ds_command *cmd)
  484. {
  485. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
  486. cmd->size = cpu_to_le16(S_DS_GEN);
  487. return 0;
  488. }
  489. int libertas_cmd_80211_ad_hoc_join(wlan_private * priv,
  490. struct cmd_ds_command *cmd, void *pdata_buf)
  491. {
  492. wlan_adapter *adapter = priv->adapter;
  493. struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
  494. struct assoc_request * assoc_req = pdata_buf;
  495. struct bss_descriptor *bss = &assoc_req->bss;
  496. int cmdappendsize = 0;
  497. int ret = 0;
  498. u16 ratesize = 0;
  499. DECLARE_MAC_BUF(mac);
  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 = %s, SSID = '%s'\n",
  516. print_mac(mac, join_cmd->bss.bssid),
  517. join_cmd->bss.ssid);
  518. /* failtimeout */
  519. join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  520. /* probedelay */
  521. join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  522. adapter->curbssparams.channel = bss->channel;
  523. /* Copy Data rates from the rates recorded in scan response */
  524. memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
  525. ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
  526. memcpy(join_cmd->bss.rates, bss->rates, ratesize);
  527. if (get_common_rates(adapter, join_cmd->bss.rates, &ratesize)) {
  528. lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
  529. ret = -1;
  530. goto done;
  531. }
  532. /* Copy the ad-hoc creating rates into Current BSS state structure */
  533. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  534. memcpy(&adapter->curbssparams.rates, join_cmd->bss.rates, ratesize);
  535. /* Set MSB on basic rates as the firmware requires, but _after_
  536. * copying to current bss rates.
  537. */
  538. libertas_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
  539. join_cmd->bss.ssparamset.ibssparamset.atimwindow =
  540. cpu_to_le16(bss->atimwindow);
  541. if (assoc_req->secinfo.wep_enabled) {
  542. u16 tmp = le16_to_cpu(join_cmd->bss.capability);
  543. tmp |= WLAN_CAPABILITY_PRIVACY;
  544. join_cmd->bss.capability = cpu_to_le16(tmp);
  545. }
  546. if (adapter->psmode == WLAN802_11POWERMODEMAX_PSP) {
  547. /* wake up first */
  548. __le32 Localpsmode;
  549. Localpsmode = cpu_to_le32(WLAN802_11POWERMODECAM);
  550. ret = libertas_prepare_and_send_command(priv,
  551. CMD_802_11_PS_MODE,
  552. CMD_ACT_SET,
  553. 0, 0, &Localpsmode);
  554. if (ret) {
  555. ret = -1;
  556. goto done;
  557. }
  558. }
  559. if (libertas_parse_dnld_countryinfo_11d(priv, bss)) {
  560. ret = -1;
  561. goto done;
  562. }
  563. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
  564. S_DS_GEN + cmdappendsize);
  565. done:
  566. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  567. return ret;
  568. }
  569. int libertas_ret_80211_associate(wlan_private * priv,
  570. struct cmd_ds_command *resp)
  571. {
  572. wlan_adapter *adapter = priv->adapter;
  573. int ret = 0;
  574. union iwreq_data wrqu;
  575. struct ieeetypes_assocrsp *passocrsp;
  576. struct bss_descriptor * bss;
  577. u16 status_code;
  578. lbs_deb_enter(LBS_DEB_JOIN);
  579. if (!adapter->in_progress_assoc_req) {
  580. lbs_deb_join("ASSOC_RESP: no in-progress association request\n");
  581. ret = -1;
  582. goto done;
  583. }
  584. bss = &adapter->in_progress_assoc_req->bss;
  585. passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
  586. /*
  587. * Older FW versions map the IEEE 802.11 Status Code in the association
  588. * response to the following values returned in passocrsp->statuscode:
  589. *
  590. * IEEE Status Code Marvell Status Code
  591. * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
  592. * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  593. * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  594. * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  595. * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  596. * others -> 0x0003 ASSOC_RESULT_REFUSED
  597. *
  598. * Other response codes:
  599. * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
  600. * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
  601. * association response from the AP)
  602. */
  603. status_code = le16_to_cpu(passocrsp->statuscode);
  604. switch (status_code) {
  605. case 0x00:
  606. lbs_deb_join("ASSOC_RESP: Association succeeded\n");
  607. break;
  608. case 0x01:
  609. lbs_deb_join("ASSOC_RESP: Association failed; invalid "
  610. "parameters (status code %d)\n", status_code);
  611. break;
  612. case 0x02:
  613. lbs_deb_join("ASSOC_RESP: Association failed; internal timer "
  614. "expired while waiting for the AP (status code %d)"
  615. "\n", status_code);
  616. break;
  617. case 0x03:
  618. lbs_deb_join("ASSOC_RESP: Association failed; association "
  619. "was refused by the AP (status code %d)\n",
  620. status_code);
  621. break;
  622. case 0x04:
  623. lbs_deb_join("ASSOC_RESP: Association failed; authentication "
  624. "was refused by the AP (status code %d)\n",
  625. status_code);
  626. break;
  627. default:
  628. lbs_deb_join("ASSOC_RESP: Association failed; reason unknown "
  629. "(status code %d)\n", status_code);
  630. break;
  631. }
  632. if (status_code) {
  633. libertas_mac_event_disconnected(priv);
  634. ret = -1;
  635. goto done;
  636. }
  637. lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_RESP", (void *)&resp->params,
  638. le16_to_cpu(resp->size) - S_DS_GEN);
  639. /* Send a Media Connected event, according to the Spec */
  640. adapter->connect_status = LIBERTAS_CONNECTED;
  641. lbs_deb_join("ASSOC_RESP: assocated to '%s'\n",
  642. escape_essid(bss->ssid, bss->ssid_len));
  643. /* Update current SSID and BSSID */
  644. memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  645. adapter->curbssparams.ssid_len = bss->ssid_len;
  646. memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  647. lbs_deb_join("ASSOC_RESP: currentpacketfilter is %x\n",
  648. adapter->currentpacketfilter);
  649. adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
  650. adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
  651. memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
  652. memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
  653. adapter->nextSNRNF = 0;
  654. adapter->numSNRNF = 0;
  655. netif_carrier_on(priv->dev);
  656. netif_wake_queue(priv->dev);
  657. if (priv->mesh_dev) {
  658. netif_carrier_on(priv->mesh_dev);
  659. netif_wake_queue(priv->mesh_dev);
  660. }
  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. DECLARE_MAC_BUF(mac);
  687. lbs_deb_enter(LBS_DEB_JOIN);
  688. padhocresult = &resp->params.result;
  689. lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
  690. lbs_deb_join("ADHOC_RESP: command = %x\n", command);
  691. lbs_deb_join("ADHOC_RESP: result = %x\n", result);
  692. if (!adapter->in_progress_assoc_req) {
  693. lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
  694. ret = -1;
  695. goto done;
  696. }
  697. bss = &adapter->in_progress_assoc_req->bss;
  698. /*
  699. * Join result code 0 --> SUCCESS
  700. */
  701. if (result) {
  702. lbs_deb_join("ADHOC_RESP: failed\n");
  703. if (adapter->connect_status == LIBERTAS_CONNECTED) {
  704. libertas_mac_event_disconnected(priv);
  705. }
  706. ret = -1;
  707. goto done;
  708. }
  709. /*
  710. * Now the join cmd should be successful
  711. * If BSSID has changed use SSID to compare instead of BSSID
  712. */
  713. lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
  714. escape_essid(bss->ssid, bss->ssid_len));
  715. /* Send a Media Connected event, according to the Spec */
  716. adapter->connect_status = LIBERTAS_CONNECTED;
  717. if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
  718. /* Update the created network descriptor with the new BSSID */
  719. memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
  720. }
  721. /* Set the BSSID from the joined/started descriptor */
  722. memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  723. /* Set the new SSID to current SSID */
  724. memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  725. adapter->curbssparams.ssid_len = bss->ssid_len;
  726. netif_carrier_on(priv->dev);
  727. netif_wake_queue(priv->dev);
  728. if (priv->mesh_dev) {
  729. netif_carrier_on(priv->mesh_dev);
  730. netif_wake_queue(priv->mesh_dev);
  731. }
  732. memset(&wrqu, 0, sizeof(wrqu));
  733. memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  734. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  735. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  736. lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
  737. lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
  738. lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
  739. print_mac(mac, padhocresult->bssid));
  740. done:
  741. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  742. return ret;
  743. }
  744. int libertas_ret_80211_ad_hoc_stop(wlan_private * priv,
  745. struct cmd_ds_command *resp)
  746. {
  747. lbs_deb_enter(LBS_DEB_JOIN);
  748. libertas_mac_event_disconnected(priv);
  749. lbs_deb_leave(LBS_DEB_JOIN);
  750. return 0;
  751. }