assoc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 == wlan802_11infrastructure) {
  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, wlan802_11infrastructure);
  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 == wlan802_11ibss) {
  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. wlan802_11ibss);
  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 == wlan802_11infrastructure) {
  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 == wlan802_11ibss) {
  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->inframode) {
  118. LEAVE();
  119. return 0;
  120. }
  121. if (assoc_req->mode == wlan802_11infrastructure) {
  122. if (adapter->psstate != PS_STATE_FULL_POWER)
  123. libertas_ps_wakeup(priv, cmd_option_waitforrsp);
  124. adapter->psmode = wlan802_11powermodecam;
  125. }
  126. adapter->inframode = 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 *) 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.WEPstatus == wlan802_11WEPenabled)
  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.authmode !=
  243. assoc_req->secinfo.authmode) {
  244. lbs_pr_debug(1, "Deauthenticating due to updated security "
  245. "info in configuration request.\n");
  246. return 1;
  247. }
  248. }
  249. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  250. lbs_pr_debug(1, "Deauthenticating due to new BSSID in "
  251. " configuration request.\n");
  252. return 1;
  253. }
  254. /* FIXME: deal with 'auto' mode somehow */
  255. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  256. if (assoc_req->mode != wlan802_11infrastructure)
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. static int should_stop_adhoc(wlan_adapter *adapter,
  262. struct assoc_request * assoc_req)
  263. {
  264. if (adapter->connect_status != libertas_connected)
  265. return 0;
  266. if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
  267. return 1;
  268. if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
  269. sizeof(struct WLAN_802_11_SSID)))
  270. return 1;
  271. /* FIXME: deal with 'auto' mode somehow */
  272. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  273. if (assoc_req->mode != wlan802_11ibss)
  274. return 1;
  275. }
  276. return 0;
  277. }
  278. void wlan_association_worker(struct work_struct *work)
  279. {
  280. wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
  281. wlan_adapter *adapter = priv->adapter;
  282. struct assoc_request * assoc_req = NULL;
  283. int ret = 0;
  284. int find_any_ssid = 0;
  285. ENTER();
  286. mutex_lock(&adapter->lock);
  287. assoc_req = adapter->assoc_req;
  288. adapter->assoc_req = NULL;
  289. mutex_unlock(&adapter->lock);
  290. if (!assoc_req) {
  291. LEAVE();
  292. return;
  293. }
  294. lbs_pr_debug(1, "ASSOC: starting new association request: flags = 0x%lX\n",
  295. assoc_req->flags);
  296. /* If 'any' SSID was specified, find an SSID to associate with */
  297. if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
  298. && !assoc_req->ssid.ssidlength)
  299. find_any_ssid = 1;
  300. /* But don't use 'any' SSID if there's a valid locked BSSID to use */
  301. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  302. if (memcmp(&assoc_req->bssid, bssid_any, ETH_ALEN)
  303. && memcmp(&assoc_req->bssid, bssid_off, ETH_ALEN))
  304. find_any_ssid = 0;
  305. }
  306. if (find_any_ssid) {
  307. enum WLAN_802_11_NETWORK_INFRASTRUCTURE new_mode;
  308. ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
  309. assoc_req->mode, &new_mode);
  310. if (ret) {
  311. lbs_pr_debug(1, "Could not find best network\n");
  312. ret = -ENETUNREACH;
  313. goto out;
  314. }
  315. /* Ensure we switch to the mode of the AP */
  316. if (assoc_req->mode == wlan802_11autounknown) {
  317. set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
  318. assoc_req->mode = new_mode;
  319. }
  320. }
  321. /*
  322. * Check if the attributes being changing require deauthentication
  323. * from the currently associated infrastructure access point.
  324. */
  325. if (adapter->inframode == wlan802_11infrastructure) {
  326. if (should_deauth_infrastructure(adapter, assoc_req)) {
  327. ret = libertas_send_deauthentication(priv);
  328. if (ret) {
  329. lbs_pr_debug(1, "Deauthentication due to new "
  330. "configuration request failed: %d\n",
  331. ret);
  332. }
  333. }
  334. } else if (adapter->inframode == wlan802_11ibss) {
  335. if (should_stop_adhoc(adapter, assoc_req)) {
  336. ret = libertas_stop_adhoc_network(priv);
  337. if (ret) {
  338. lbs_pr_debug(1, "Teardown of AdHoc network due to "
  339. "new configuration request failed: %d\n",
  340. ret);
  341. }
  342. }
  343. }
  344. /* Send the various configuration bits to the firmware */
  345. if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
  346. ret = assoc_helper_mode(priv, assoc_req);
  347. if (ret) {
  348. lbs_pr_debug(1, "ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
  349. goto out;
  350. }
  351. }
  352. if ( test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
  353. || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
  354. ret = assoc_helper_wep_keys(priv, assoc_req);
  355. if (ret) {
  356. lbs_pr_debug(1, "ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
  357. goto out;
  358. }
  359. }
  360. if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  361. ret = assoc_helper_secinfo(priv, assoc_req);
  362. if (ret) {
  363. lbs_pr_debug(1, "ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
  364. goto out;
  365. }
  366. }
  367. if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  368. ret = assoc_helper_wpa_ie(priv, assoc_req);
  369. if (ret) {
  370. lbs_pr_debug(1, "ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
  371. goto out;
  372. }
  373. }
  374. if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
  375. || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  376. ret = assoc_helper_wpa_keys(priv, assoc_req);
  377. if (ret) {
  378. lbs_pr_debug(1, "ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
  379. goto out;
  380. }
  381. }
  382. /* SSID/BSSID should be the _last_ config option set, because they
  383. * trigger the association attempt.
  384. */
  385. if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
  386. || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  387. int success = 1;
  388. ret = assoc_helper_associate(priv, assoc_req);
  389. if (ret) {
  390. lbs_pr_debug(1, "ASSOC: association attempt unsuccessful: %d\n",
  391. ret);
  392. success = 0;
  393. }
  394. if (adapter->connect_status != libertas_connected) {
  395. lbs_pr_debug(1, "ASSOC: assoication attempt unsuccessful, "
  396. "not connected.\n");
  397. success = 0;
  398. }
  399. if (success) {
  400. lbs_pr_debug(1, "ASSOC: association attempt successful. "
  401. "Associated to '%s' (" MAC_FMT ")\n",
  402. assoc_req->ssid.ssid, MAC_ARG(assoc_req->bssid));
  403. libertas_prepare_and_send_command(priv,
  404. cmd_802_11_rssi,
  405. 0, cmd_option_waitforrsp, 0, NULL);
  406. libertas_prepare_and_send_command(priv,
  407. cmd_802_11_get_log,
  408. 0, cmd_option_waitforrsp, 0, NULL);
  409. } else {
  410. ret = -1;
  411. }
  412. }
  413. out:
  414. if (ret) {
  415. lbs_pr_debug(1, "ASSOC: reconfiguration attempt unsuccessful: %d\n",
  416. ret);
  417. }
  418. kfree(assoc_req);
  419. LEAVE();
  420. }
  421. /*
  422. * Caller MUST hold any necessary locks
  423. */
  424. struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
  425. {
  426. struct assoc_request * assoc_req;
  427. if (!adapter->assoc_req) {
  428. adapter->assoc_req = kzalloc(sizeof(struct assoc_request), GFP_KERNEL);
  429. if (!adapter->assoc_req) {
  430. lbs_pr_info("Not enough memory to allocate association"
  431. " request!\n");
  432. return NULL;
  433. }
  434. }
  435. /* Copy current configuration attributes to the association request,
  436. * but don't overwrite any that are already set.
  437. */
  438. assoc_req = adapter->assoc_req;
  439. if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
  440. memcpy(&assoc_req->ssid, adapter->curbssparams.ssid.ssid,
  441. adapter->curbssparams.ssid.ssidlength);
  442. }
  443. if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
  444. assoc_req->channel = adapter->curbssparams.channel;
  445. if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
  446. assoc_req->mode = adapter->inframode;
  447. if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
  448. memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
  449. ETH_ALEN);
  450. }
  451. if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
  452. int i;
  453. for (i = 0; i < 4; i++) {
  454. memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
  455. sizeof(struct WLAN_802_11_KEY));
  456. }
  457. }
  458. if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
  459. assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;
  460. if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
  461. memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
  462. sizeof(struct WLAN_802_11_KEY));
  463. }
  464. if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
  465. memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
  466. sizeof(struct WLAN_802_11_KEY));
  467. }
  468. if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
  469. memcpy(&assoc_req->secinfo, &adapter->secinfo,
  470. sizeof(struct wlan_802_11_security));
  471. }
  472. if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
  473. memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
  474. MAX_WPA_IE_LEN);
  475. assoc_req->wpa_ie_len = adapter->wpa_ie_len;
  476. }
  477. return assoc_req;
  478. }