assoc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /* Copyright (C) 2006, Red Hat, Inc. */
  2. #include <linux/bitops.h>
  3. #include <net/ieee80211.h>
  4. #include "assoc.h"
  5. #include "join.h"
  6. #include "decl.h"
  7. #include "hostcmd.h"
  8. #include "host.h"
  9. static const u8 bssid_any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  10. static const u8 bssid_off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  11. static int assoc_helper_essid(wlan_private *priv,
  12. struct assoc_request * assoc_req)
  13. {
  14. wlan_adapter *adapter = priv->adapter;
  15. int ret = 0;
  16. int i;
  17. lbs_deb_enter(LBS_DEB_ASSOC);
  18. lbs_deb_assoc("New SSID requested: %s\n", assoc_req->ssid.ssid);
  19. if (assoc_req->mode == IW_MODE_INFRA) {
  20. if (adapter->prescan) {
  21. libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 1);
  22. }
  23. i = libertas_find_SSID_in_list(adapter, &assoc_req->ssid,
  24. NULL, IW_MODE_INFRA);
  25. if (i >= 0) {
  26. lbs_deb_assoc(
  27. "SSID found in scan list ... associating...\n");
  28. ret = wlan_associate(priv, &adapter->scantable[i]);
  29. if (ret == 0) {
  30. memcpy(&assoc_req->bssid,
  31. &adapter->scantable[i].bssid,
  32. ETH_ALEN);
  33. }
  34. } else {
  35. lbs_deb_assoc("SSID '%s' not found; cannot associate\n",
  36. assoc_req->ssid.ssid);
  37. }
  38. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  39. /* Scan for the network, do not save previous results. Stale
  40. * scan data will cause us to join a non-existant adhoc network
  41. */
  42. libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 0);
  43. /* Search for the requested SSID in the scan table */
  44. i = libertas_find_SSID_in_list(adapter, &assoc_req->ssid, NULL,
  45. IW_MODE_ADHOC);
  46. if (i >= 0) {
  47. lbs_deb_assoc("SSID found at %d in List, so join\n", ret);
  48. libertas_join_adhoc_network(priv, &adapter->scantable[i]);
  49. } else {
  50. /* else send START command */
  51. lbs_deb_assoc("SSID not found in list, so creating adhoc"
  52. " with SSID '%s'\n", assoc_req->ssid.ssid);
  53. libertas_start_adhoc_network(priv, &assoc_req->ssid);
  54. }
  55. memcpy(&assoc_req->bssid, &adapter->current_addr, ETH_ALEN);
  56. }
  57. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  58. return ret;
  59. }
  60. static int assoc_helper_bssid(wlan_private *priv,
  61. struct assoc_request * assoc_req)
  62. {
  63. wlan_adapter *adapter = priv->adapter;
  64. int i, ret = 0;
  65. lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID" MAC_FMT "\n",
  66. MAC_ARG(assoc_req->bssid));
  67. /* Search for index position in list for requested MAC */
  68. i = libertas_find_BSSID_in_list(adapter, assoc_req->bssid,
  69. assoc_req->mode);
  70. if (i < 0) {
  71. lbs_deb_assoc("ASSOC: WAP: BSSID " MAC_FMT " not found, "
  72. "cannot associate.\n", MAC_ARG(assoc_req->bssid));
  73. goto out;
  74. }
  75. if (assoc_req->mode == IW_MODE_INFRA) {
  76. ret = wlan_associate(priv, &adapter->scantable[i]);
  77. lbs_deb_assoc("ASSOC: return from wlan_associate(bssd) was %d\n", ret);
  78. } else if (assoc_req->mode == IW_MODE_ADHOC) {
  79. libertas_join_adhoc_network(priv, &adapter->scantable[i]);
  80. }
  81. memcpy(&assoc_req->ssid, &adapter->scantable[i].ssid,
  82. sizeof(struct WLAN_802_11_SSID));
  83. out:
  84. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  85. return ret;
  86. }
  87. static int assoc_helper_associate(wlan_private *priv,
  88. struct assoc_request * assoc_req)
  89. {
  90. int ret = 0, done = 0;
  91. /* If we're given and 'any' BSSID, try associating based on SSID */
  92. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  93. if (memcmp(bssid_any, assoc_req->bssid, ETH_ALEN)
  94. && memcmp(bssid_off, assoc_req->bssid, ETH_ALEN)) {
  95. ret = assoc_helper_bssid(priv, assoc_req);
  96. done = 1;
  97. if (ret) {
  98. lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
  99. }
  100. }
  101. }
  102. if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  103. ret = assoc_helper_essid(priv, assoc_req);
  104. if (ret) {
  105. lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
  106. }
  107. }
  108. return ret;
  109. }
  110. static int assoc_helper_mode(wlan_private *priv,
  111. struct assoc_request * assoc_req)
  112. {
  113. wlan_adapter *adapter = priv->adapter;
  114. int ret = 0;
  115. lbs_deb_enter(LBS_DEB_ASSOC);
  116. if (assoc_req->mode == adapter->mode)
  117. goto done;
  118. if (assoc_req->mode == IW_MODE_INFRA) {
  119. if (adapter->psstate != PS_STATE_FULL_POWER)
  120. libertas_ps_wakeup(priv, cmd_option_waitforrsp);
  121. adapter->psmode = wlan802_11powermodecam;
  122. }
  123. adapter->mode = assoc_req->mode;
  124. ret = libertas_prepare_and_send_command(priv,
  125. cmd_802_11_snmp_mib,
  126. 0, cmd_option_waitforrsp,
  127. OID_802_11_INFRASTRUCTURE_MODE,
  128. (void *) (size_t) assoc_req->mode);
  129. done:
  130. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  131. return ret;
  132. }
  133. static int assoc_helper_wep_keys(wlan_private *priv,
  134. struct assoc_request * assoc_req)
  135. {
  136. wlan_adapter *adapter = priv->adapter;
  137. int i;
  138. int ret = 0;
  139. lbs_deb_enter(LBS_DEB_ASSOC);
  140. /* Set or remove WEP keys */
  141. if ( assoc_req->wep_keys[0].len
  142. || assoc_req->wep_keys[1].len
  143. || assoc_req->wep_keys[2].len
  144. || assoc_req->wep_keys[3].len) {
  145. ret = libertas_prepare_and_send_command(priv,
  146. cmd_802_11_set_wep,
  147. cmd_act_add,
  148. cmd_option_waitforrsp,
  149. 0, assoc_req);
  150. } else {
  151. ret = libertas_prepare_and_send_command(priv,
  152. cmd_802_11_set_wep,
  153. cmd_act_remove,
  154. cmd_option_waitforrsp,
  155. 0, NULL);
  156. }
  157. if (ret)
  158. goto out;
  159. /* enable/disable the MAC's WEP packet filter */
  160. if (assoc_req->secinfo.wep_enabled)
  161. adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
  162. else
  163. adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
  164. ret = libertas_set_mac_packet_filter(priv);
  165. if (ret)
  166. goto out;
  167. mutex_lock(&adapter->lock);
  168. /* Copy WEP keys into adapter wep key fields */
  169. for (i = 0; i < 4; i++) {
  170. memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
  171. sizeof(struct WLAN_802_11_KEY));
  172. }
  173. adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
  174. mutex_unlock(&adapter->lock);
  175. out:
  176. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  177. return ret;
  178. }
  179. static int assoc_helper_secinfo(wlan_private *priv,
  180. struct assoc_request * assoc_req)
  181. {
  182. wlan_adapter *adapter = priv->adapter;
  183. int ret = 0;
  184. lbs_deb_enter(LBS_DEB_ASSOC);
  185. memcpy(&adapter->secinfo, &assoc_req->secinfo,
  186. sizeof(struct wlan_802_11_security));
  187. ret = libertas_set_mac_packet_filter(priv);
  188. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  189. return ret;
  190. }
  191. static int assoc_helper_wpa_keys(wlan_private *priv,
  192. struct assoc_request * assoc_req)
  193. {
  194. int ret = 0;
  195. lbs_deb_enter(LBS_DEB_ASSOC);
  196. /* enable/Disable RSN */
  197. ret = libertas_prepare_and_send_command(priv,
  198. cmd_802_11_enable_rsn,
  199. cmd_act_set,
  200. cmd_option_waitforrsp,
  201. 0, assoc_req);
  202. if (ret)
  203. goto out;
  204. ret = libertas_prepare_and_send_command(priv,
  205. cmd_802_11_key_material,
  206. cmd_act_set,
  207. cmd_option_waitforrsp,
  208. 0, assoc_req);
  209. out:
  210. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  211. return ret;
  212. }
  213. static int assoc_helper_wpa_ie(wlan_private *priv,
  214. struct assoc_request * assoc_req)
  215. {
  216. wlan_adapter *adapter = priv->adapter;
  217. int ret = 0;
  218. lbs_deb_enter(LBS_DEB_ASSOC);
  219. if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
  220. memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
  221. adapter->wpa_ie_len = assoc_req->wpa_ie_len;
  222. } else {
  223. memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
  224. adapter->wpa_ie_len = 0;
  225. }
  226. lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
  227. return ret;
  228. }
  229. static int should_deauth_infrastructure(wlan_adapter *adapter,
  230. struct assoc_request * assoc_req)
  231. {
  232. if (adapter->connect_status != libertas_connected)
  233. return 0;
  234. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  235. lbs_deb_assoc("Deauthenticating due to new SSID in "
  236. " configuration request.\n");
  237. return 1;
  238. }
  239. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  240. if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
  241. lbs_deb_assoc("Deauthenticating due to updated security "
  242. "info in configuration request.\n");
  243. return 1;
  244. }
  245. }
  246. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  247. lbs_deb_assoc("Deauthenticating due to new BSSID in "
  248. " configuration request.\n");
  249. return 1;
  250. }
  251. /* FIXME: deal with 'auto' mode somehow */
  252. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  253. if (assoc_req->mode != IW_MODE_INFRA)
  254. return 1;
  255. }
  256. return 0;
  257. }
  258. static int should_stop_adhoc(wlan_adapter *adapter,
  259. struct assoc_request * assoc_req)
  260. {
  261. if (adapter->connect_status != libertas_connected)
  262. return 0;
  263. if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
  264. return 1;
  265. if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
  266. adapter->curbssparams.ssid.ssidlength))
  267. return 1;
  268. /* FIXME: deal with 'auto' mode somehow */
  269. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  270. if (assoc_req->mode != IW_MODE_ADHOC)
  271. return 1;
  272. }
  273. return 0;
  274. }
  275. void libertas_association_worker(struct work_struct *work)
  276. {
  277. wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
  278. wlan_adapter *adapter = priv->adapter;
  279. struct assoc_request * assoc_req = NULL;
  280. int ret = 0;
  281. int find_any_ssid = 0;
  282. lbs_deb_enter(LBS_DEB_ASSOC);
  283. mutex_lock(&adapter->lock);
  284. assoc_req = adapter->assoc_req;
  285. adapter->assoc_req = NULL;
  286. mutex_unlock(&adapter->lock);
  287. if (!assoc_req)
  288. goto done;
  289. lbs_deb_assoc("ASSOC: starting new association request: flags = 0x%lX\n",
  290. assoc_req->flags);
  291. /* If 'any' SSID was specified, find an SSID to associate with */
  292. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
  293. && !assoc_req->ssid.ssidlength)
  294. find_any_ssid = 1;
  295. /* But don't use 'any' SSID if there's a valid locked BSSID to use */
  296. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  297. if (memcmp(&assoc_req->bssid, bssid_any, ETH_ALEN)
  298. && memcmp(&assoc_req->bssid, bssid_off, ETH_ALEN))
  299. find_any_ssid = 0;
  300. }
  301. if (find_any_ssid) {
  302. u8 new_mode;
  303. ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
  304. assoc_req->mode, &new_mode);
  305. if (ret) {
  306. lbs_deb_assoc("Could not find best network\n");
  307. ret = -ENETUNREACH;
  308. goto out;
  309. }
  310. /* Ensure we switch to the mode of the AP */
  311. if (assoc_req->mode == IW_MODE_AUTO) {
  312. set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
  313. assoc_req->mode = new_mode;
  314. }
  315. }
  316. /*
  317. * Check if the attributes being changing require deauthentication
  318. * from the currently associated infrastructure access point.
  319. */
  320. if (adapter->mode == IW_MODE_INFRA) {
  321. if (should_deauth_infrastructure(adapter, assoc_req)) {
  322. ret = libertas_send_deauthentication(priv);
  323. if (ret) {
  324. lbs_deb_assoc("Deauthentication due to new "
  325. "configuration request failed: %d\n",
  326. ret);
  327. }
  328. }
  329. } else if (adapter->mode == IW_MODE_ADHOC) {
  330. if (should_stop_adhoc(adapter, assoc_req)) {
  331. ret = libertas_stop_adhoc_network(priv);
  332. if (ret) {
  333. lbs_deb_assoc("Teardown of AdHoc network due to "
  334. "new configuration request failed: %d\n",
  335. ret);
  336. }
  337. }
  338. }
  339. /* Send the various configuration bits to the firmware */
  340. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  341. ret = assoc_helper_mode(priv, assoc_req);
  342. if (ret) {
  343. lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
  344. goto out;
  345. }
  346. }
  347. if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
  348. || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
  349. ret = assoc_helper_wep_keys(priv, assoc_req);
  350. if (ret) {
  351. lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
  352. goto out;
  353. }
  354. }
  355. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  356. ret = assoc_helper_secinfo(priv, assoc_req);
  357. if (ret) {
  358. lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
  359. goto out;
  360. }
  361. }
  362. if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  363. ret = assoc_helper_wpa_ie(priv, assoc_req);
  364. if (ret) {
  365. lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
  366. goto out;
  367. }
  368. }
  369. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
  370. || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  371. ret = assoc_helper_wpa_keys(priv, assoc_req);
  372. if (ret) {
  373. lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
  374. goto out;
  375. }
  376. }
  377. /* SSID/BSSID should be the _last_ config option set, because they
  378. * trigger the association attempt.
  379. */
  380. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
  381. || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  382. int success = 1;
  383. ret = assoc_helper_associate(priv, assoc_req);
  384. if (ret) {
  385. lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
  386. ret);
  387. success = 0;
  388. }
  389. if (adapter->connect_status != libertas_connected) {
  390. lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
  391. "not connected.\n");
  392. success = 0;
  393. }
  394. if (success) {
  395. lbs_deb_assoc("ASSOC: association attempt successful. "
  396. "Associated to '%s' (" MAC_FMT ")\n",
  397. assoc_req->ssid.ssid, MAC_ARG(assoc_req->bssid));
  398. libertas_prepare_and_send_command(priv,
  399. cmd_802_11_rssi,
  400. 0, cmd_option_waitforrsp, 0, NULL);
  401. libertas_prepare_and_send_command(priv,
  402. cmd_802_11_get_log,
  403. 0, cmd_option_waitforrsp, 0, NULL);
  404. } else {
  405. ret = -1;
  406. }
  407. }
  408. out:
  409. if (ret) {
  410. lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
  411. ret);
  412. }
  413. kfree(assoc_req);
  414. done:
  415. lbs_deb_leave(LBS_DEB_ASSOC);
  416. }
  417. /*
  418. * Caller MUST hold any necessary locks
  419. */
  420. struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
  421. {
  422. struct assoc_request * assoc_req;
  423. if (!adapter->assoc_req) {
  424. adapter->assoc_req = kzalloc(sizeof(struct assoc_request), GFP_KERNEL);
  425. if (!adapter->assoc_req) {
  426. lbs_pr_info("Not enough memory to allocate association"
  427. " request!\n");
  428. return NULL;
  429. }
  430. }
  431. /* Copy current configuration attributes to the association request,
  432. * but don't overwrite any that are already set.
  433. */
  434. assoc_req = adapter->assoc_req;
  435. if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  436. memcpy(&assoc_req->ssid, adapter->curbssparams.ssid.ssid,
  437. adapter->curbssparams.ssid.ssidlength);
  438. }
  439. if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  440. assoc_req->channel = adapter->curbssparams.channel;
  441. if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
  442. assoc_req->mode = adapter->mode;
  443. if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  444. memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
  445. ETH_ALEN);
  446. }
  447. if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
  448. int i;
  449. for (i = 0; i < 4; i++) {
  450. memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
  451. sizeof(struct WLAN_802_11_KEY));
  452. }
  453. }
  454. if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
  455. assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
  456. if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  457. memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
  458. sizeof(struct WLAN_802_11_KEY));
  459. }
  460. if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  461. memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
  462. sizeof(struct WLAN_802_11_KEY));
  463. }
  464. if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  465. memcpy(&assoc_req->secinfo, &adapter->secinfo,
  466. sizeof(struct wlan_802_11_security));
  467. }
  468. if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  469. memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
  470. MAX_WPA_IE_LEN);
  471. assoc_req->wpa_ie_len = adapter->wpa_ie_len;
  472. }
  473. return assoc_req;
  474. }