assoc.c 15 KB

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