assoc.c 15 KB

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