join.c 26 KB

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