sme.c 27 KB

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