join.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 data rate 0x%02x\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_ASSOC);
  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_ASSOC, "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 %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_ASSOC);
  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_assoc("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_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);
  394. done:
  395. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  396. return ret;
  397. }
  398. int lbs_cmd_80211_ad_hoc_start(struct lbs_private *priv,
  399. struct cmd_ds_command *cmd, void *pdata_buf)
  400. {
  401. struct lbs_adapter *adapter = priv->adapter;
  402. struct cmd_ds_802_11_ad_hoc_start *adhs = &cmd->params.ads;
  403. int ret = 0;
  404. int cmdappendsize = 0;
  405. struct assoc_request * assoc_req = pdata_buf;
  406. u16 tmpcap = 0;
  407. size_t ratesize = 0;
  408. lbs_deb_enter(LBS_DEB_JOIN);
  409. if (!adapter) {
  410. ret = -1;
  411. goto done;
  412. }
  413. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_START);
  414. /*
  415. * Fill in the parameters for 2 data structures:
  416. * 1. cmd_ds_802_11_ad_hoc_start command
  417. * 2. adapter->scantable[i]
  418. *
  419. * Driver will fill up SSID, bsstype,IBSS param, Physical Param,
  420. * probe delay, and cap info.
  421. *
  422. * Firmware will fill up beacon period, DTIM, Basic rates
  423. * and operational rates.
  424. */
  425. memset(adhs->ssid, 0, IW_ESSID_MAX_SIZE);
  426. memcpy(adhs->ssid, assoc_req->ssid, assoc_req->ssid_len);
  427. lbs_deb_join("ADHOC_S_CMD: SSID '%s', ssid length %u\n",
  428. escape_essid(assoc_req->ssid, assoc_req->ssid_len),
  429. assoc_req->ssid_len);
  430. /* set the BSS type */
  431. adhs->bsstype = CMD_BSS_TYPE_IBSS;
  432. adapter->mode = IW_MODE_ADHOC;
  433. if (adapter->beacon_period == 0)
  434. adapter->beacon_period = MRVDRV_BEACON_INTERVAL;
  435. adhs->beaconperiod = cpu_to_le16(adapter->beacon_period);
  436. /* set Physical param set */
  437. #define DS_PARA_IE_ID 3
  438. #define DS_PARA_IE_LEN 1
  439. adhs->phyparamset.dsparamset.elementid = DS_PARA_IE_ID;
  440. adhs->phyparamset.dsparamset.len = DS_PARA_IE_LEN;
  441. WARN_ON(!assoc_req->channel);
  442. lbs_deb_join("ADHOC_S_CMD: Creating ADHOC on channel %d\n",
  443. assoc_req->channel);
  444. adhs->phyparamset.dsparamset.currentchan = assoc_req->channel;
  445. /* set IBSS param set */
  446. #define IBSS_PARA_IE_ID 6
  447. #define IBSS_PARA_IE_LEN 2
  448. adhs->ssparamset.ibssparamset.elementid = IBSS_PARA_IE_ID;
  449. adhs->ssparamset.ibssparamset.len = IBSS_PARA_IE_LEN;
  450. adhs->ssparamset.ibssparamset.atimwindow = 0;
  451. /* set capability info */
  452. tmpcap = WLAN_CAPABILITY_IBSS;
  453. if (assoc_req->secinfo.wep_enabled) {
  454. lbs_deb_join("ADHOC_S_CMD: WEP enabled, setting privacy on\n");
  455. tmpcap |= WLAN_CAPABILITY_PRIVACY;
  456. } else {
  457. lbs_deb_join("ADHOC_S_CMD: WEP disabled, setting privacy off\n");
  458. }
  459. adhs->capability = cpu_to_le16(tmpcap);
  460. /* probedelay */
  461. adhs->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  462. memset(adhs->rates, 0, sizeof(adhs->rates));
  463. ratesize = min(sizeof(adhs->rates), sizeof(lbs_bg_rates));
  464. memcpy(adhs->rates, lbs_bg_rates, ratesize);
  465. /* Copy the ad-hoc creating rates into Current BSS state structure */
  466. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  467. memcpy(&adapter->curbssparams.rates, &adhs->rates, ratesize);
  468. /* Set MSB on basic rates as the firmware requires, but _after_
  469. * copying to current bss rates.
  470. */
  471. lbs_set_basic_rate_flags(adhs->rates, ratesize);
  472. lbs_deb_join("ADHOC_S_CMD: rates=%02x %02x %02x %02x \n",
  473. adhs->rates[0], adhs->rates[1], adhs->rates[2], adhs->rates[3]);
  474. lbs_deb_join("ADHOC_S_CMD: AD HOC Start command is ready\n");
  475. if (lbs_create_dnld_countryinfo_11d(priv)) {
  476. lbs_deb_join("ADHOC_S_CMD: dnld_countryinfo_11d failed\n");
  477. ret = -1;
  478. goto done;
  479. }
  480. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_start) +
  481. S_DS_GEN + cmdappendsize);
  482. ret = 0;
  483. done:
  484. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  485. return ret;
  486. }
  487. int lbs_cmd_80211_ad_hoc_stop(struct lbs_private *priv,
  488. struct cmd_ds_command *cmd)
  489. {
  490. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_STOP);
  491. cmd->size = cpu_to_le16(S_DS_GEN);
  492. return 0;
  493. }
  494. int lbs_cmd_80211_ad_hoc_join(struct lbs_private *priv,
  495. struct cmd_ds_command *cmd, void *pdata_buf)
  496. {
  497. struct lbs_adapter *adapter = priv->adapter;
  498. struct cmd_ds_802_11_ad_hoc_join *join_cmd = &cmd->params.adj;
  499. struct assoc_request * assoc_req = pdata_buf;
  500. struct bss_descriptor *bss = &assoc_req->bss;
  501. int cmdappendsize = 0;
  502. int ret = 0;
  503. u16 ratesize = 0;
  504. DECLARE_MAC_BUF(mac);
  505. lbs_deb_enter(LBS_DEB_JOIN);
  506. cmd->command = cpu_to_le16(CMD_802_11_AD_HOC_JOIN);
  507. join_cmd->bss.type = CMD_BSS_TYPE_IBSS;
  508. join_cmd->bss.beaconperiod = cpu_to_le16(bss->beaconperiod);
  509. memcpy(&join_cmd->bss.bssid, &bss->bssid, ETH_ALEN);
  510. memcpy(&join_cmd->bss.ssid, &bss->ssid, bss->ssid_len);
  511. memcpy(&join_cmd->bss.phyparamset, &bss->phyparamset,
  512. sizeof(union ieeetypes_phyparamset));
  513. memcpy(&join_cmd->bss.ssparamset, &bss->ssparamset,
  514. sizeof(union IEEEtypes_ssparamset));
  515. join_cmd->bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  516. lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
  517. bss->capability, CAPINFO_MASK);
  518. /* information on BSSID descriptor passed to FW */
  519. lbs_deb_join(
  520. "ADHOC_J_CMD: BSSID = %s, SSID = '%s'\n",
  521. print_mac(mac, join_cmd->bss.bssid),
  522. join_cmd->bss.ssid);
  523. /* failtimeout */
  524. join_cmd->failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  525. /* probedelay */
  526. join_cmd->probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  527. adapter->curbssparams.channel = bss->channel;
  528. /* Copy Data rates from the rates recorded in scan response */
  529. memset(join_cmd->bss.rates, 0, sizeof(join_cmd->bss.rates));
  530. ratesize = min_t(u16, sizeof(join_cmd->bss.rates), MAX_RATES);
  531. memcpy(join_cmd->bss.rates, bss->rates, ratesize);
  532. if (get_common_rates(adapter, join_cmd->bss.rates, &ratesize)) {
  533. lbs_deb_join("ADHOC_J_CMD: get_common_rates returns error.\n");
  534. ret = -1;
  535. goto done;
  536. }
  537. /* Copy the ad-hoc creating rates into Current BSS state structure */
  538. memset(&adapter->curbssparams.rates, 0, sizeof(adapter->curbssparams.rates));
  539. memcpy(&adapter->curbssparams.rates, join_cmd->bss.rates, ratesize);
  540. /* Set MSB on basic rates as the firmware requires, but _after_
  541. * copying to current bss rates.
  542. */
  543. lbs_set_basic_rate_flags(join_cmd->bss.rates, ratesize);
  544. join_cmd->bss.ssparamset.ibssparamset.atimwindow =
  545. cpu_to_le16(bss->atimwindow);
  546. if (assoc_req->secinfo.wep_enabled) {
  547. u16 tmp = le16_to_cpu(join_cmd->bss.capability);
  548. tmp |= WLAN_CAPABILITY_PRIVACY;
  549. join_cmd->bss.capability = cpu_to_le16(tmp);
  550. }
  551. if (adapter->psmode == LBS802_11POWERMODEMAX_PSP) {
  552. /* wake up first */
  553. __le32 Localpsmode;
  554. Localpsmode = cpu_to_le32(LBS802_11POWERMODECAM);
  555. ret = lbs_prepare_and_send_command(priv,
  556. CMD_802_11_PS_MODE,
  557. CMD_ACT_SET,
  558. 0, 0, &Localpsmode);
  559. if (ret) {
  560. ret = -1;
  561. goto done;
  562. }
  563. }
  564. if (lbs_parse_dnld_countryinfo_11d(priv, bss)) {
  565. ret = -1;
  566. goto done;
  567. }
  568. cmd->size = cpu_to_le16(sizeof(struct cmd_ds_802_11_ad_hoc_join) +
  569. S_DS_GEN + cmdappendsize);
  570. done:
  571. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  572. return ret;
  573. }
  574. int lbs_ret_80211_associate(struct lbs_private *priv,
  575. struct cmd_ds_command *resp)
  576. {
  577. struct lbs_adapter *adapter = priv->adapter;
  578. int ret = 0;
  579. union iwreq_data wrqu;
  580. struct ieeetypes_assocrsp *passocrsp;
  581. struct bss_descriptor * bss;
  582. u16 status_code;
  583. lbs_deb_enter(LBS_DEB_ASSOC);
  584. if (!adapter->in_progress_assoc_req) {
  585. lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
  586. ret = -1;
  587. goto done;
  588. }
  589. bss = &adapter->in_progress_assoc_req->bss;
  590. passocrsp = (struct ieeetypes_assocrsp *) & resp->params;
  591. /*
  592. * Older FW versions map the IEEE 802.11 Status Code in the association
  593. * response to the following values returned in passocrsp->statuscode:
  594. *
  595. * IEEE Status Code Marvell Status Code
  596. * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
  597. * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  598. * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  599. * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  600. * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  601. * others -> 0x0003 ASSOC_RESULT_REFUSED
  602. *
  603. * Other response codes:
  604. * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
  605. * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
  606. * association response from the AP)
  607. */
  608. status_code = le16_to_cpu(passocrsp->statuscode);
  609. switch (status_code) {
  610. case 0x00:
  611. break;
  612. case 0x01:
  613. lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
  614. break;
  615. case 0x02:
  616. lbs_deb_assoc("ASSOC_RESP: internal timer "
  617. "expired while waiting for the AP\n");
  618. break;
  619. case 0x03:
  620. lbs_deb_assoc("ASSOC_RESP: association "
  621. "refused by AP\n");
  622. break;
  623. case 0x04:
  624. lbs_deb_assoc("ASSOC_RESP: authentication "
  625. "refused by AP\n");
  626. break;
  627. default:
  628. lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
  629. " unknown\n", status_code);
  630. break;
  631. }
  632. if (status_code) {
  633. lbs_mac_event_disconnected(priv);
  634. ret = -1;
  635. goto done;
  636. }
  637. lbs_deb_hex(LBS_DEB_ASSOC, "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 = LBS_CONNECTED;
  641. /* Update current SSID and BSSID */
  642. memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  643. adapter->curbssparams.ssid_len = bss->ssid_len;
  644. memcpy(adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  645. lbs_deb_assoc("ASSOC_RESP: currentpacketfilter is 0x%x\n",
  646. adapter->currentpacketfilter);
  647. adapter->SNR[TYPE_RXPD][TYPE_AVG] = 0;
  648. adapter->NF[TYPE_RXPD][TYPE_AVG] = 0;
  649. memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
  650. memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
  651. adapter->nextSNRNF = 0;
  652. adapter->numSNRNF = 0;
  653. netif_carrier_on(priv->dev);
  654. netif_wake_queue(priv->dev);
  655. memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  656. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  657. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  658. done:
  659. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  660. return ret;
  661. }
  662. int lbs_ret_80211_disassociate(struct lbs_private *priv,
  663. struct cmd_ds_command *resp)
  664. {
  665. lbs_deb_enter(LBS_DEB_JOIN);
  666. lbs_mac_event_disconnected(priv);
  667. lbs_deb_leave(LBS_DEB_JOIN);
  668. return 0;
  669. }
  670. int lbs_ret_80211_ad_hoc_start(struct lbs_private *priv,
  671. struct cmd_ds_command *resp)
  672. {
  673. struct lbs_adapter *adapter = priv->adapter;
  674. int ret = 0;
  675. u16 command = le16_to_cpu(resp->command);
  676. u16 result = le16_to_cpu(resp->result);
  677. struct cmd_ds_802_11_ad_hoc_result *padhocresult;
  678. union iwreq_data wrqu;
  679. struct bss_descriptor *bss;
  680. DECLARE_MAC_BUF(mac);
  681. lbs_deb_enter(LBS_DEB_JOIN);
  682. padhocresult = &resp->params.result;
  683. lbs_deb_join("ADHOC_RESP: size = %d\n", le16_to_cpu(resp->size));
  684. lbs_deb_join("ADHOC_RESP: command = %x\n", command);
  685. lbs_deb_join("ADHOC_RESP: result = %x\n", result);
  686. if (!adapter->in_progress_assoc_req) {
  687. lbs_deb_join("ADHOC_RESP: no in-progress association request\n");
  688. ret = -1;
  689. goto done;
  690. }
  691. bss = &adapter->in_progress_assoc_req->bss;
  692. /*
  693. * Join result code 0 --> SUCCESS
  694. */
  695. if (result) {
  696. lbs_deb_join("ADHOC_RESP: failed\n");
  697. if (adapter->connect_status == LBS_CONNECTED) {
  698. lbs_mac_event_disconnected(priv);
  699. }
  700. ret = -1;
  701. goto done;
  702. }
  703. /*
  704. * Now the join cmd should be successful
  705. * If BSSID has changed use SSID to compare instead of BSSID
  706. */
  707. lbs_deb_join("ADHOC_RESP: associated to '%s'\n",
  708. escape_essid(bss->ssid, bss->ssid_len));
  709. /* Send a Media Connected event, according to the Spec */
  710. adapter->connect_status = LBS_CONNECTED;
  711. if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
  712. /* Update the created network descriptor with the new BSSID */
  713. memcpy(bss->bssid, padhocresult->bssid, ETH_ALEN);
  714. }
  715. /* Set the BSSID from the joined/started descriptor */
  716. memcpy(&adapter->curbssparams.bssid, bss->bssid, ETH_ALEN);
  717. /* Set the new SSID to current SSID */
  718. memcpy(&adapter->curbssparams.ssid, &bss->ssid, IW_ESSID_MAX_SIZE);
  719. adapter->curbssparams.ssid_len = bss->ssid_len;
  720. netif_carrier_on(priv->dev);
  721. netif_wake_queue(priv->dev);
  722. memset(&wrqu, 0, sizeof(wrqu));
  723. memcpy(wrqu.ap_addr.sa_data, adapter->curbssparams.bssid, ETH_ALEN);
  724. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  725. wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
  726. lbs_deb_join("ADHOC_RESP: - Joined/Started Ad Hoc\n");
  727. lbs_deb_join("ADHOC_RESP: channel = %d\n", adapter->curbssparams.channel);
  728. lbs_deb_join("ADHOC_RESP: BSSID = %s\n",
  729. print_mac(mac, padhocresult->bssid));
  730. done:
  731. lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
  732. return ret;
  733. }
  734. int lbs_ret_80211_ad_hoc_stop(struct lbs_private *priv,
  735. struct cmd_ds_command *resp)
  736. {
  737. lbs_deb_enter(LBS_DEB_JOIN);
  738. lbs_mac_event_disconnected(priv);
  739. lbs_deb_leave(LBS_DEB_JOIN);
  740. return 0;
  741. }