sme.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * SME code for cfg80211's connect emulation.
  3. *
  4. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright (C) 2009 Intel Corporation. All rights reserved.
  6. */
  7. #include <linux/etherdevice.h>
  8. #include <linux/if_arp.h>
  9. #include <linux/slab.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/wireless.h>
  12. #include <net/iw_handler.h>
  13. #include <net/cfg80211.h>
  14. #include <net/rtnetlink.h>
  15. #include "nl80211.h"
  16. #include "reg.h"
  17. struct cfg80211_conn {
  18. struct cfg80211_connect_params params;
  19. /* these are sub-states of the _CONNECTING sme_state */
  20. enum {
  21. CFG80211_CONN_IDLE,
  22. CFG80211_CONN_SCANNING,
  23. CFG80211_CONN_SCAN_AGAIN,
  24. CFG80211_CONN_AUTHENTICATE_NEXT,
  25. CFG80211_CONN_AUTHENTICATING,
  26. CFG80211_CONN_ASSOCIATE_NEXT,
  27. CFG80211_CONN_ASSOCIATING,
  28. CFG80211_CONN_DEAUTH_ASSOC_FAIL,
  29. } state;
  30. u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
  31. u8 *ie;
  32. size_t ie_len;
  33. bool auto_auth, prev_bssid_valid;
  34. };
  35. static bool cfg80211_is_all_idle(void)
  36. {
  37. struct cfg80211_registered_device *rdev;
  38. struct wireless_dev *wdev;
  39. bool is_all_idle = true;
  40. mutex_lock(&cfg80211_mutex);
  41. /*
  42. * All devices must be idle as otherwise if you are actively
  43. * scanning some new beacon hints could be learned and would
  44. * count as new regulatory hints.
  45. */
  46. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  47. cfg80211_lock_rdev(rdev);
  48. list_for_each_entry(wdev, &rdev->netdev_list, list) {
  49. wdev_lock(wdev);
  50. if (wdev->sme_state != CFG80211_SME_IDLE)
  51. is_all_idle = false;
  52. wdev_unlock(wdev);
  53. }
  54. cfg80211_unlock_rdev(rdev);
  55. }
  56. mutex_unlock(&cfg80211_mutex);
  57. return is_all_idle;
  58. }
  59. static void disconnect_work(struct work_struct *work)
  60. {
  61. if (!cfg80211_is_all_idle())
  62. return;
  63. regulatory_hint_disconnect();
  64. }
  65. static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
  66. static int cfg80211_conn_scan(struct wireless_dev *wdev)
  67. {
  68. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  69. struct cfg80211_scan_request *request;
  70. int n_channels, err;
  71. ASSERT_RTNL();
  72. ASSERT_RDEV_LOCK(rdev);
  73. ASSERT_WDEV_LOCK(wdev);
  74. if (rdev->scan_req)
  75. return -EBUSY;
  76. if (wdev->conn->params.channel) {
  77. n_channels = 1;
  78. } else {
  79. enum ieee80211_band band;
  80. n_channels = 0;
  81. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  82. if (!wdev->wiphy->bands[band])
  83. continue;
  84. n_channels += wdev->wiphy->bands[band]->n_channels;
  85. }
  86. }
  87. request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
  88. sizeof(request->channels[0]) * n_channels,
  89. GFP_KERNEL);
  90. if (!request)
  91. return -ENOMEM;
  92. if (wdev->conn->params.channel)
  93. request->channels[0] = wdev->conn->params.channel;
  94. else {
  95. int i = 0, j;
  96. enum ieee80211_band band;
  97. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  98. if (!wdev->wiphy->bands[band])
  99. continue;
  100. for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
  101. i++, j++)
  102. request->channels[i] =
  103. &wdev->wiphy->bands[band]->channels[j];
  104. }
  105. }
  106. request->n_channels = n_channels;
  107. request->ssids = (void *)&request->channels[n_channels];
  108. request->n_ssids = 1;
  109. memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
  110. wdev->conn->params.ssid_len);
  111. request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
  112. request->dev = wdev->netdev;
  113. request->wiphy = &rdev->wiphy;
  114. rdev->scan_req = request;
  115. err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
  116. if (!err) {
  117. wdev->conn->state = CFG80211_CONN_SCANNING;
  118. nl80211_send_scan_start(rdev, wdev->netdev);
  119. dev_hold(wdev->netdev);
  120. } else {
  121. rdev->scan_req = NULL;
  122. kfree(request);
  123. }
  124. return err;
  125. }
  126. static int cfg80211_conn_do_work(struct wireless_dev *wdev)
  127. {
  128. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  129. struct cfg80211_connect_params *params;
  130. const u8 *prev_bssid = NULL;
  131. int err;
  132. ASSERT_WDEV_LOCK(wdev);
  133. if (!wdev->conn)
  134. return 0;
  135. params = &wdev->conn->params;
  136. switch (wdev->conn->state) {
  137. case CFG80211_CONN_SCAN_AGAIN:
  138. return cfg80211_conn_scan(wdev);
  139. case CFG80211_CONN_AUTHENTICATE_NEXT:
  140. BUG_ON(!rdev->ops->auth);
  141. wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
  142. return __cfg80211_mlme_auth(rdev, wdev->netdev,
  143. params->channel, params->auth_type,
  144. params->bssid,
  145. params->ssid, params->ssid_len,
  146. NULL, 0,
  147. params->key, params->key_len,
  148. params->key_idx, false);
  149. case CFG80211_CONN_ASSOCIATE_NEXT:
  150. BUG_ON(!rdev->ops->assoc);
  151. wdev->conn->state = CFG80211_CONN_ASSOCIATING;
  152. if (wdev->conn->prev_bssid_valid)
  153. prev_bssid = wdev->conn->prev_bssid;
  154. err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
  155. params->channel, params->bssid,
  156. prev_bssid,
  157. params->ssid, params->ssid_len,
  158. params->ie, params->ie_len,
  159. false, &params->crypto);
  160. if (err)
  161. __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
  162. NULL, 0,
  163. WLAN_REASON_DEAUTH_LEAVING,
  164. false);
  165. return err;
  166. case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
  167. __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
  168. NULL, 0,
  169. WLAN_REASON_DEAUTH_LEAVING, false);
  170. /* return an error so that we call __cfg80211_connect_result() */
  171. return -EINVAL;
  172. default:
  173. return 0;
  174. }
  175. }
  176. void cfg80211_conn_work(struct work_struct *work)
  177. {
  178. struct cfg80211_registered_device *rdev =
  179. container_of(work, struct cfg80211_registered_device, conn_work);
  180. struct wireless_dev *wdev;
  181. u8 bssid_buf[ETH_ALEN], *bssid = NULL;
  182. rtnl_lock();
  183. cfg80211_lock_rdev(rdev);
  184. mutex_lock(&rdev->devlist_mtx);
  185. list_for_each_entry(wdev, &rdev->netdev_list, list) {
  186. wdev_lock(wdev);
  187. if (!netif_running(wdev->netdev)) {
  188. wdev_unlock(wdev);
  189. continue;
  190. }
  191. if (wdev->sme_state != CFG80211_SME_CONNECTING) {
  192. wdev_unlock(wdev);
  193. continue;
  194. }
  195. if (wdev->conn->params.bssid) {
  196. memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
  197. bssid = bssid_buf;
  198. }
  199. if (cfg80211_conn_do_work(wdev))
  200. __cfg80211_connect_result(
  201. wdev->netdev, bssid,
  202. NULL, 0, NULL, 0,
  203. WLAN_STATUS_UNSPECIFIED_FAILURE,
  204. false, NULL);
  205. wdev_unlock(wdev);
  206. }
  207. mutex_unlock(&rdev->devlist_mtx);
  208. cfg80211_unlock_rdev(rdev);
  209. rtnl_unlock();
  210. }
  211. static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
  212. {
  213. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  214. struct cfg80211_bss *bss;
  215. u16 capa = WLAN_CAPABILITY_ESS;
  216. ASSERT_WDEV_LOCK(wdev);
  217. if (wdev->conn->params.privacy)
  218. capa |= WLAN_CAPABILITY_PRIVACY;
  219. bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
  220. wdev->conn->params.bssid,
  221. wdev->conn->params.ssid,
  222. wdev->conn->params.ssid_len,
  223. WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
  224. capa);
  225. if (!bss)
  226. return NULL;
  227. memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
  228. wdev->conn->params.bssid = wdev->conn->bssid;
  229. wdev->conn->params.channel = bss->channel;
  230. wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
  231. schedule_work(&rdev->conn_work);
  232. return bss;
  233. }
  234. static void __cfg80211_sme_scan_done(struct net_device *dev)
  235. {
  236. struct wireless_dev *wdev = dev->ieee80211_ptr;
  237. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  238. struct cfg80211_bss *bss;
  239. ASSERT_WDEV_LOCK(wdev);
  240. if (wdev->sme_state != CFG80211_SME_CONNECTING)
  241. return;
  242. if (!wdev->conn)
  243. return;
  244. if (wdev->conn->state != CFG80211_CONN_SCANNING &&
  245. wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
  246. return;
  247. bss = cfg80211_get_conn_bss(wdev);
  248. if (bss) {
  249. cfg80211_put_bss(bss);
  250. } else {
  251. /* not found */
  252. if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
  253. schedule_work(&rdev->conn_work);
  254. else
  255. __cfg80211_connect_result(
  256. wdev->netdev,
  257. wdev->conn->params.bssid,
  258. NULL, 0, NULL, 0,
  259. WLAN_STATUS_UNSPECIFIED_FAILURE,
  260. false, NULL);
  261. }
  262. }
  263. void cfg80211_sme_scan_done(struct net_device *dev)
  264. {
  265. struct wireless_dev *wdev = dev->ieee80211_ptr;
  266. mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
  267. wdev_lock(wdev);
  268. __cfg80211_sme_scan_done(dev);
  269. wdev_unlock(wdev);
  270. mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
  271. }
  272. void cfg80211_sme_rx_auth(struct net_device *dev,
  273. const u8 *buf, size_t len)
  274. {
  275. struct wireless_dev *wdev = dev->ieee80211_ptr;
  276. struct wiphy *wiphy = wdev->wiphy;
  277. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  278. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  279. u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
  280. ASSERT_WDEV_LOCK(wdev);
  281. /* should only RX auth frames when connecting */
  282. if (wdev->sme_state != CFG80211_SME_CONNECTING)
  283. return;
  284. if (WARN_ON(!wdev->conn))
  285. return;
  286. if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
  287. wdev->conn->auto_auth &&
  288. wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
  289. /* select automatically between only open, shared, leap */
  290. switch (wdev->conn->params.auth_type) {
  291. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  292. if (wdev->connect_keys)
  293. wdev->conn->params.auth_type =
  294. NL80211_AUTHTYPE_SHARED_KEY;
  295. else
  296. wdev->conn->params.auth_type =
  297. NL80211_AUTHTYPE_NETWORK_EAP;
  298. break;
  299. case NL80211_AUTHTYPE_SHARED_KEY:
  300. wdev->conn->params.auth_type =
  301. NL80211_AUTHTYPE_NETWORK_EAP;
  302. break;
  303. default:
  304. /* huh? */
  305. wdev->conn->params.auth_type =
  306. NL80211_AUTHTYPE_OPEN_SYSTEM;
  307. break;
  308. }
  309. wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
  310. schedule_work(&rdev->conn_work);
  311. } else if (status_code != WLAN_STATUS_SUCCESS) {
  312. __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
  313. status_code, false, NULL);
  314. } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
  315. wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
  316. wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
  317. schedule_work(&rdev->conn_work);
  318. }
  319. }
  320. bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
  321. {
  322. struct wiphy *wiphy = wdev->wiphy;
  323. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  324. if (WARN_ON(!wdev->conn))
  325. return false;
  326. if (!wdev->conn->prev_bssid_valid)
  327. return false;
  328. /*
  329. * Some stupid APs don't accept reassoc, so we
  330. * need to fall back to trying regular assoc.
  331. */
  332. wdev->conn->prev_bssid_valid = false;
  333. wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
  334. schedule_work(&rdev->conn_work);
  335. return true;
  336. }
  337. void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
  338. {
  339. struct wiphy *wiphy = wdev->wiphy;
  340. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  341. wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
  342. schedule_work(&rdev->conn_work);
  343. }
  344. void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  345. const u8 *req_ie, size_t req_ie_len,
  346. const u8 *resp_ie, size_t resp_ie_len,
  347. u16 status, bool wextev,
  348. struct cfg80211_bss *bss)
  349. {
  350. struct wireless_dev *wdev = dev->ieee80211_ptr;
  351. u8 *country_ie;
  352. #ifdef CONFIG_CFG80211_WEXT
  353. union iwreq_data wrqu;
  354. #endif
  355. ASSERT_WDEV_LOCK(wdev);
  356. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
  357. wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
  358. return;
  359. if (wdev->sme_state != CFG80211_SME_CONNECTING)
  360. return;
  361. nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
  362. bssid, req_ie, req_ie_len,
  363. resp_ie, resp_ie_len,
  364. status, GFP_KERNEL);
  365. #ifdef CONFIG_CFG80211_WEXT
  366. if (wextev) {
  367. if (req_ie && status == WLAN_STATUS_SUCCESS) {
  368. memset(&wrqu, 0, sizeof(wrqu));
  369. wrqu.data.length = req_ie_len;
  370. wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
  371. }
  372. if (resp_ie && status == WLAN_STATUS_SUCCESS) {
  373. memset(&wrqu, 0, sizeof(wrqu));
  374. wrqu.data.length = resp_ie_len;
  375. wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
  376. }
  377. memset(&wrqu, 0, sizeof(wrqu));
  378. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  379. if (bssid && status == WLAN_STATUS_SUCCESS) {
  380. memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
  381. memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
  382. wdev->wext.prev_bssid_valid = true;
  383. }
  384. wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
  385. }
  386. #endif
  387. if (wdev->current_bss) {
  388. cfg80211_unhold_bss(wdev->current_bss);
  389. cfg80211_put_bss(&wdev->current_bss->pub);
  390. wdev->current_bss = NULL;
  391. }
  392. if (wdev->conn)
  393. wdev->conn->state = CFG80211_CONN_IDLE;
  394. if (status != WLAN_STATUS_SUCCESS) {
  395. wdev->sme_state = CFG80211_SME_IDLE;
  396. if (wdev->conn)
  397. kfree(wdev->conn->ie);
  398. kfree(wdev->conn);
  399. wdev->conn = NULL;
  400. kfree(wdev->connect_keys);
  401. wdev->connect_keys = NULL;
  402. wdev->ssid_len = 0;
  403. return;
  404. }
  405. if (!bss)
  406. bss = cfg80211_get_bss(wdev->wiphy,
  407. wdev->conn ? wdev->conn->params.channel :
  408. NULL,
  409. bssid,
  410. wdev->ssid, wdev->ssid_len,
  411. WLAN_CAPABILITY_ESS,
  412. WLAN_CAPABILITY_ESS);
  413. if (WARN_ON(!bss))
  414. return;
  415. cfg80211_hold_bss(bss_from_pub(bss));
  416. wdev->current_bss = bss_from_pub(bss);
  417. wdev->sme_state = CFG80211_SME_CONNECTED;
  418. cfg80211_upload_connect_keys(wdev);
  419. country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
  420. if (!country_ie)
  421. return;
  422. /*
  423. * ieee80211_bss_get_ie() ensures we can access:
  424. * - country_ie + 2, the start of the country ie data, and
  425. * - and country_ie[1] which is the IE length
  426. */
  427. regulatory_hint_11d(wdev->wiphy,
  428. bss->channel->band,
  429. country_ie + 2,
  430. country_ie[1]);
  431. }
  432. void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  433. const u8 *req_ie, size_t req_ie_len,
  434. const u8 *resp_ie, size_t resp_ie_len,
  435. u16 status, gfp_t gfp)
  436. {
  437. struct wireless_dev *wdev = dev->ieee80211_ptr;
  438. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  439. struct cfg80211_event *ev;
  440. unsigned long flags;
  441. CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
  442. ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
  443. if (!ev)
  444. return;
  445. ev->type = EVENT_CONNECT_RESULT;
  446. if (bssid)
  447. memcpy(ev->cr.bssid, bssid, ETH_ALEN);
  448. if (req_ie_len) {
  449. ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
  450. ev->cr.req_ie_len = req_ie_len;
  451. memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
  452. }
  453. if (resp_ie_len) {
  454. ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
  455. ev->cr.resp_ie_len = resp_ie_len;
  456. memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
  457. }
  458. ev->cr.status = status;
  459. spin_lock_irqsave(&wdev->event_lock, flags);
  460. list_add_tail(&ev->list, &wdev->event_list);
  461. spin_unlock_irqrestore(&wdev->event_lock, flags);
  462. queue_work(cfg80211_wq, &rdev->event_work);
  463. }
  464. EXPORT_SYMBOL(cfg80211_connect_result);
  465. void __cfg80211_roamed(struct wireless_dev *wdev,
  466. struct ieee80211_channel *channel,
  467. const u8 *bssid,
  468. const u8 *req_ie, size_t req_ie_len,
  469. const u8 *resp_ie, size_t resp_ie_len)
  470. {
  471. struct cfg80211_bss *bss;
  472. #ifdef CONFIG_CFG80211_WEXT
  473. union iwreq_data wrqu;
  474. #endif
  475. ASSERT_WDEV_LOCK(wdev);
  476. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
  477. wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
  478. return;
  479. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  480. return;
  481. /* internal error -- how did we get to CONNECTED w/o BSS? */
  482. if (WARN_ON(!wdev->current_bss)) {
  483. return;
  484. }
  485. cfg80211_unhold_bss(wdev->current_bss);
  486. cfg80211_put_bss(&wdev->current_bss->pub);
  487. wdev->current_bss = NULL;
  488. bss = cfg80211_get_bss(wdev->wiphy, channel, bssid,
  489. wdev->ssid, wdev->ssid_len,
  490. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  491. if (WARN_ON(!bss))
  492. return;
  493. cfg80211_hold_bss(bss_from_pub(bss));
  494. wdev->current_bss = bss_from_pub(bss);
  495. nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
  496. req_ie, req_ie_len, resp_ie, resp_ie_len,
  497. GFP_KERNEL);
  498. #ifdef CONFIG_CFG80211_WEXT
  499. if (req_ie) {
  500. memset(&wrqu, 0, sizeof(wrqu));
  501. wrqu.data.length = req_ie_len;
  502. wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
  503. &wrqu, req_ie);
  504. }
  505. if (resp_ie) {
  506. memset(&wrqu, 0, sizeof(wrqu));
  507. wrqu.data.length = resp_ie_len;
  508. wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
  509. &wrqu, resp_ie);
  510. }
  511. memset(&wrqu, 0, sizeof(wrqu));
  512. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  513. memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
  514. memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
  515. wdev->wext.prev_bssid_valid = true;
  516. wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
  517. #endif
  518. }
  519. void cfg80211_roamed(struct net_device *dev,
  520. struct ieee80211_channel *channel,
  521. const u8 *bssid,
  522. const u8 *req_ie, size_t req_ie_len,
  523. const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
  524. {
  525. struct wireless_dev *wdev = dev->ieee80211_ptr;
  526. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  527. struct cfg80211_event *ev;
  528. unsigned long flags;
  529. CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
  530. ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
  531. if (!ev)
  532. return;
  533. ev->type = EVENT_ROAMED;
  534. ev->rm.channel = channel;
  535. memcpy(ev->rm.bssid, bssid, ETH_ALEN);
  536. ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
  537. ev->rm.req_ie_len = req_ie_len;
  538. memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
  539. ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
  540. ev->rm.resp_ie_len = resp_ie_len;
  541. memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
  542. spin_lock_irqsave(&wdev->event_lock, flags);
  543. list_add_tail(&ev->list, &wdev->event_list);
  544. spin_unlock_irqrestore(&wdev->event_lock, flags);
  545. queue_work(cfg80211_wq, &rdev->event_work);
  546. }
  547. EXPORT_SYMBOL(cfg80211_roamed);
  548. void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
  549. size_t ie_len, u16 reason, bool from_ap)
  550. {
  551. struct wireless_dev *wdev = dev->ieee80211_ptr;
  552. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  553. int i;
  554. #ifdef CONFIG_CFG80211_WEXT
  555. union iwreq_data wrqu;
  556. #endif
  557. ASSERT_WDEV_LOCK(wdev);
  558. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
  559. wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
  560. return;
  561. #ifndef CONFIG_CFG80211_ALLOW_RECONNECT
  562. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  563. return;
  564. #endif
  565. if (wdev->current_bss) {
  566. cfg80211_unhold_bss(wdev->current_bss);
  567. cfg80211_put_bss(&wdev->current_bss->pub);
  568. }
  569. wdev->current_bss = NULL;
  570. wdev->sme_state = CFG80211_SME_IDLE;
  571. wdev->ssid_len = 0;
  572. if (wdev->conn) {
  573. const u8 *bssid;
  574. int ret;
  575. kfree(wdev->conn->ie);
  576. wdev->conn->ie = NULL;
  577. kfree(wdev->conn);
  578. wdev->conn = NULL;
  579. /*
  580. * If this disconnect was due to a disassoc, we
  581. * we might still have an auth BSS around. For
  582. * the userspace SME that's currently expected,
  583. * but for the kernel SME (nl80211 CONNECT or
  584. * wireless extensions) we want to clear up all
  585. * state.
  586. */
  587. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  588. if (!wdev->auth_bsses[i])
  589. continue;
  590. bssid = wdev->auth_bsses[i]->pub.bssid;
  591. ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
  592. WLAN_REASON_DEAUTH_LEAVING,
  593. false);
  594. WARN(ret, "deauth failed: %d\n", ret);
  595. }
  596. }
  597. nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
  598. /*
  599. * Delete all the keys ... pairwise keys can't really
  600. * exist any more anyway, but default keys might.
  601. */
  602. if (rdev->ops->del_key)
  603. for (i = 0; i < 6; i++)
  604. rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
  605. #ifdef CONFIG_CFG80211_WEXT
  606. memset(&wrqu, 0, sizeof(wrqu));
  607. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  608. wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
  609. wdev->wext.connect.ssid_len = 0;
  610. #endif
  611. schedule_work(&cfg80211_disconnect_work);
  612. }
  613. void cfg80211_disconnected(struct net_device *dev, u16 reason,
  614. u8 *ie, size_t ie_len, gfp_t gfp)
  615. {
  616. struct wireless_dev *wdev = dev->ieee80211_ptr;
  617. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  618. struct cfg80211_event *ev;
  619. unsigned long flags;
  620. CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
  621. ev = kzalloc(sizeof(*ev) + ie_len, gfp);
  622. if (!ev)
  623. return;
  624. ev->type = EVENT_DISCONNECTED;
  625. ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
  626. ev->dc.ie_len = ie_len;
  627. memcpy((void *)ev->dc.ie, ie, ie_len);
  628. ev->dc.reason = reason;
  629. spin_lock_irqsave(&wdev->event_lock, flags);
  630. list_add_tail(&ev->list, &wdev->event_list);
  631. spin_unlock_irqrestore(&wdev->event_lock, flags);
  632. queue_work(cfg80211_wq, &rdev->event_work);
  633. }
  634. EXPORT_SYMBOL(cfg80211_disconnected);
  635. int __cfg80211_connect(struct cfg80211_registered_device *rdev,
  636. struct net_device *dev,
  637. struct cfg80211_connect_params *connect,
  638. struct cfg80211_cached_keys *connkeys,
  639. const u8 *prev_bssid)
  640. {
  641. struct wireless_dev *wdev = dev->ieee80211_ptr;
  642. struct cfg80211_bss *bss = NULL;
  643. int err;
  644. ASSERT_WDEV_LOCK(wdev);
  645. #ifndef CONFIG_CFG80211_ALLOW_RECONNECT
  646. if (wdev->sme_state != CFG80211_SME_IDLE)
  647. return -EALREADY;
  648. if (WARN_ON(wdev->connect_keys)) {
  649. #else
  650. if (wdev->connect_keys) {
  651. #endif
  652. kfree(wdev->connect_keys);
  653. wdev->connect_keys = NULL;
  654. }
  655. if (connkeys && connkeys->def >= 0) {
  656. int idx;
  657. u32 cipher;
  658. idx = connkeys->def;
  659. cipher = connkeys->params[idx].cipher;
  660. /* If given a WEP key we may need it for shared key auth */
  661. if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
  662. cipher == WLAN_CIPHER_SUITE_WEP104) {
  663. connect->key_idx = idx;
  664. connect->key = connkeys->params[idx].key;
  665. connect->key_len = connkeys->params[idx].key_len;
  666. /*
  667. * If ciphers are not set (e.g. when going through
  668. * iwconfig), we have to set them appropriately here.
  669. */
  670. if (connect->crypto.cipher_group == 0)
  671. connect->crypto.cipher_group = cipher;
  672. if (connect->crypto.n_ciphers_pairwise == 0) {
  673. connect->crypto.n_ciphers_pairwise = 1;
  674. connect->crypto.ciphers_pairwise[0] = cipher;
  675. }
  676. }
  677. }
  678. if (!rdev->ops->connect) {
  679. if (!rdev->ops->auth || !rdev->ops->assoc)
  680. return -EOPNOTSUPP;
  681. if (WARN_ON(wdev->conn))
  682. return -EINPROGRESS;
  683. wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
  684. if (!wdev->conn)
  685. return -ENOMEM;
  686. /*
  687. * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
  688. */
  689. memcpy(&wdev->conn->params, connect, sizeof(*connect));
  690. if (connect->bssid) {
  691. wdev->conn->params.bssid = wdev->conn->bssid;
  692. memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
  693. }
  694. if (connect->ie) {
  695. wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
  696. GFP_KERNEL);
  697. wdev->conn->params.ie = wdev->conn->ie;
  698. if (!wdev->conn->ie) {
  699. kfree(wdev->conn);
  700. wdev->conn = NULL;
  701. return -ENOMEM;
  702. }
  703. }
  704. if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
  705. wdev->conn->auto_auth = true;
  706. /* start with open system ... should mostly work */
  707. wdev->conn->params.auth_type =
  708. NL80211_AUTHTYPE_OPEN_SYSTEM;
  709. } else {
  710. wdev->conn->auto_auth = false;
  711. }
  712. memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
  713. wdev->ssid_len = connect->ssid_len;
  714. wdev->conn->params.ssid = wdev->ssid;
  715. wdev->conn->params.ssid_len = connect->ssid_len;
  716. /* see if we have the bss already */
  717. bss = cfg80211_get_conn_bss(wdev);
  718. wdev->sme_state = CFG80211_SME_CONNECTING;
  719. wdev->connect_keys = connkeys;
  720. if (prev_bssid) {
  721. memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
  722. wdev->conn->prev_bssid_valid = true;
  723. }
  724. /* we're good if we have a matching bss struct */
  725. if (bss) {
  726. wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
  727. err = cfg80211_conn_do_work(wdev);
  728. cfg80211_put_bss(bss);
  729. } else {
  730. /* otherwise we'll need to scan for the AP first */
  731. err = cfg80211_conn_scan(wdev);
  732. /*
  733. * If we can't scan right now, then we need to scan again
  734. * after the current scan finished, since the parameters
  735. * changed (unless we find a good AP anyway).
  736. */
  737. if (err == -EBUSY) {
  738. err = 0;
  739. wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
  740. }
  741. }
  742. if (err) {
  743. kfree(wdev->conn->ie);
  744. kfree(wdev->conn);
  745. wdev->conn = NULL;
  746. wdev->sme_state = CFG80211_SME_IDLE;
  747. wdev->connect_keys = NULL;
  748. wdev->ssid_len = 0;
  749. }
  750. return err;
  751. } else {
  752. wdev->sme_state = CFG80211_SME_CONNECTING;
  753. wdev->connect_keys = connkeys;
  754. err = rdev->ops->connect(&rdev->wiphy, dev, connect);
  755. if (err) {
  756. wdev->connect_keys = NULL;
  757. wdev->sme_state = CFG80211_SME_IDLE;
  758. return err;
  759. }
  760. memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
  761. wdev->ssid_len = connect->ssid_len;
  762. return 0;
  763. }
  764. }
  765. int cfg80211_connect(struct cfg80211_registered_device *rdev,
  766. struct net_device *dev,
  767. struct cfg80211_connect_params *connect,
  768. struct cfg80211_cached_keys *connkeys)
  769. {
  770. int err;
  771. mutex_lock(&rdev->devlist_mtx);
  772. wdev_lock(dev->ieee80211_ptr);
  773. err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
  774. wdev_unlock(dev->ieee80211_ptr);
  775. mutex_unlock(&rdev->devlist_mtx);
  776. return err;
  777. }
  778. int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  779. struct net_device *dev, u16 reason, bool wextev)
  780. {
  781. struct wireless_dev *wdev = dev->ieee80211_ptr;
  782. int err;
  783. ASSERT_WDEV_LOCK(wdev);
  784. if (wdev->sme_state == CFG80211_SME_IDLE)
  785. return -EINVAL;
  786. kfree(wdev->connect_keys);
  787. wdev->connect_keys = NULL;
  788. if (!rdev->ops->disconnect) {
  789. if (!rdev->ops->deauth)
  790. return -EOPNOTSUPP;
  791. /* was it connected by userspace SME? */
  792. if (!wdev->conn) {
  793. cfg80211_mlme_down(rdev, dev);
  794. return 0;
  795. }
  796. if (wdev->sme_state == CFG80211_SME_CONNECTING &&
  797. (wdev->conn->state == CFG80211_CONN_SCANNING ||
  798. wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
  799. wdev->sme_state = CFG80211_SME_IDLE;
  800. kfree(wdev->conn->ie);
  801. kfree(wdev->conn);
  802. wdev->conn = NULL;
  803. wdev->ssid_len = 0;
  804. return 0;
  805. }
  806. /* wdev->conn->params.bssid must be set if > SCANNING */
  807. err = __cfg80211_mlme_deauth(rdev, dev,
  808. wdev->conn->params.bssid,
  809. NULL, 0, reason, false);
  810. if (err)
  811. return err;
  812. } else {
  813. err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
  814. if (err)
  815. return err;
  816. }
  817. if (wdev->sme_state == CFG80211_SME_CONNECTED)
  818. __cfg80211_disconnected(dev, NULL, 0, 0, false);
  819. else if (wdev->sme_state == CFG80211_SME_CONNECTING)
  820. __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
  821. WLAN_STATUS_UNSPECIFIED_FAILURE,
  822. wextev, NULL);
  823. return 0;
  824. }
  825. int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  826. struct net_device *dev,
  827. u16 reason, bool wextev)
  828. {
  829. int err;
  830. wdev_lock(dev->ieee80211_ptr);
  831. err = __cfg80211_disconnect(rdev, dev, reason, wextev);
  832. wdev_unlock(dev->ieee80211_ptr);
  833. return err;
  834. }
  835. void cfg80211_sme_disassoc(struct net_device *dev, int idx)
  836. {
  837. struct wireless_dev *wdev = dev->ieee80211_ptr;
  838. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  839. u8 bssid[ETH_ALEN];
  840. ASSERT_WDEV_LOCK(wdev);
  841. if (!wdev->conn)
  842. return;
  843. if (wdev->conn->state == CFG80211_CONN_IDLE)
  844. return;
  845. /*
  846. * Ok, so the association was made by this SME -- we don't
  847. * want it any more so deauthenticate too.
  848. */
  849. if (!wdev->auth_bsses[idx])
  850. return;
  851. memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
  852. if (__cfg80211_mlme_deauth(rdev, dev, bssid,
  853. NULL, 0, WLAN_REASON_DEAUTH_LEAVING,
  854. false)) {
  855. /* whatever -- assume gone anyway */
  856. cfg80211_unhold_bss(wdev->auth_bsses[idx]);
  857. cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
  858. wdev->auth_bsses[idx] = NULL;
  859. }
  860. }