sme.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. 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. u16 capa = WLAN_CAPABILITY_ESS;
  232. ASSERT_WDEV_LOCK(wdev);
  233. if (wdev->conn->params.privacy)
  234. capa |= WLAN_CAPABILITY_PRIVACY;
  235. bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
  236. wdev->conn->params.bssid,
  237. wdev->conn->params.ssid,
  238. wdev->conn->params.ssid_len,
  239. WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
  240. capa);
  241. if (!bss)
  242. return NULL;
  243. memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
  244. wdev->conn->params.bssid = wdev->conn->bssid;
  245. wdev->conn->params.channel = bss->channel;
  246. wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
  247. schedule_work(&rdev->conn_work);
  248. return bss;
  249. }
  250. static void __cfg80211_sme_scan_done(struct net_device *dev)
  251. {
  252. struct wireless_dev *wdev = dev->ieee80211_ptr;
  253. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  254. struct cfg80211_bss *bss;
  255. ASSERT_WDEV_LOCK(wdev);
  256. if (!wdev->conn)
  257. return;
  258. if (wdev->conn->state != CFG80211_CONN_SCANNING &&
  259. wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
  260. return;
  261. bss = cfg80211_get_conn_bss(wdev);
  262. if (bss)
  263. cfg80211_put_bss(&rdev->wiphy, bss);
  264. else
  265. schedule_work(&rdev->conn_work);
  266. }
  267. void cfg80211_sme_scan_done(struct net_device *dev)
  268. {
  269. struct wireless_dev *wdev = dev->ieee80211_ptr;
  270. wdev_lock(wdev);
  271. __cfg80211_sme_scan_done(dev);
  272. wdev_unlock(wdev);
  273. }
  274. void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
  275. {
  276. struct wiphy *wiphy = wdev->wiphy;
  277. struct cfg80211_registered_device *rdev = wiphy_to_rdev(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. if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
  282. return;
  283. if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
  284. wdev->conn->auto_auth &&
  285. wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
  286. /* select automatically between only open, shared, leap */
  287. switch (wdev->conn->params.auth_type) {
  288. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  289. if (wdev->connect_keys)
  290. wdev->conn->params.auth_type =
  291. NL80211_AUTHTYPE_SHARED_KEY;
  292. else
  293. wdev->conn->params.auth_type =
  294. NL80211_AUTHTYPE_NETWORK_EAP;
  295. break;
  296. case NL80211_AUTHTYPE_SHARED_KEY:
  297. wdev->conn->params.auth_type =
  298. NL80211_AUTHTYPE_NETWORK_EAP;
  299. break;
  300. default:
  301. /* huh? */
  302. wdev->conn->params.auth_type =
  303. NL80211_AUTHTYPE_OPEN_SYSTEM;
  304. break;
  305. }
  306. wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
  307. schedule_work(&rdev->conn_work);
  308. } else if (status_code != WLAN_STATUS_SUCCESS) {
  309. __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
  310. NULL, 0, NULL, 0,
  311. status_code, false, NULL);
  312. } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
  313. wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
  314. schedule_work(&rdev->conn_work);
  315. }
  316. }
  317. bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
  318. {
  319. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  320. if (!wdev->conn)
  321. return false;
  322. if (status == WLAN_STATUS_SUCCESS) {
  323. wdev->conn->state = CFG80211_CONN_CONNECTED;
  324. return false;
  325. }
  326. if (wdev->conn->prev_bssid_valid) {
  327. /*
  328. * Some stupid APs don't accept reassoc, so we
  329. * need to fall back to trying regular assoc;
  330. * return true so no event is sent to userspace.
  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. wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
  338. schedule_work(&rdev->conn_work);
  339. return false;
  340. }
  341. void cfg80211_sme_deauth(struct wireless_dev *wdev)
  342. {
  343. cfg80211_sme_free(wdev);
  344. }
  345. void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
  346. {
  347. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  348. if (!wdev->conn)
  349. return;
  350. wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
  351. schedule_work(&rdev->conn_work);
  352. }
  353. void cfg80211_sme_disassoc(struct wireless_dev *wdev)
  354. {
  355. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  356. if (!wdev->conn)
  357. return;
  358. wdev->conn->state = CFG80211_CONN_DEAUTH;
  359. schedule_work(&rdev->conn_work);
  360. }
  361. void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
  362. {
  363. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  364. if (!wdev->conn)
  365. return;
  366. wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
  367. schedule_work(&rdev->conn_work);
  368. }
  369. static int cfg80211_sme_connect(struct wireless_dev *wdev,
  370. struct cfg80211_connect_params *connect,
  371. const u8 *prev_bssid)
  372. {
  373. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  374. struct cfg80211_bss *bss;
  375. int err;
  376. if (!rdev->ops->auth || !rdev->ops->assoc)
  377. return -EOPNOTSUPP;
  378. if (wdev->current_bss)
  379. return -EALREADY;
  380. if (WARN_ON(wdev->conn))
  381. return -EINPROGRESS;
  382. wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
  383. if (!wdev->conn)
  384. return -ENOMEM;
  385. /*
  386. * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
  387. */
  388. memcpy(&wdev->conn->params, connect, sizeof(*connect));
  389. if (connect->bssid) {
  390. wdev->conn->params.bssid = wdev->conn->bssid;
  391. memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
  392. }
  393. if (connect->ie) {
  394. wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
  395. GFP_KERNEL);
  396. wdev->conn->params.ie = wdev->conn->ie;
  397. if (!wdev->conn->ie) {
  398. kfree(wdev->conn);
  399. wdev->conn = NULL;
  400. return -ENOMEM;
  401. }
  402. }
  403. if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
  404. wdev->conn->auto_auth = true;
  405. /* start with open system ... should mostly work */
  406. wdev->conn->params.auth_type =
  407. NL80211_AUTHTYPE_OPEN_SYSTEM;
  408. } else {
  409. wdev->conn->auto_auth = false;
  410. }
  411. wdev->conn->params.ssid = wdev->ssid;
  412. wdev->conn->params.ssid_len = wdev->ssid_len;
  413. /* see if we have the bss already */
  414. bss = cfg80211_get_conn_bss(wdev);
  415. if (prev_bssid) {
  416. memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
  417. wdev->conn->prev_bssid_valid = true;
  418. }
  419. /* we're good if we have a matching bss struct */
  420. if (bss) {
  421. err = cfg80211_conn_do_work(wdev);
  422. cfg80211_put_bss(wdev->wiphy, bss);
  423. } else {
  424. /* otherwise we'll need to scan for the AP first */
  425. err = cfg80211_conn_scan(wdev);
  426. /*
  427. * If we can't scan right now, then we need to scan again
  428. * after the current scan finished, since the parameters
  429. * changed (unless we find a good AP anyway).
  430. */
  431. if (err == -EBUSY) {
  432. err = 0;
  433. wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
  434. }
  435. }
  436. if (err)
  437. cfg80211_sme_free(wdev);
  438. return err;
  439. }
  440. static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
  441. {
  442. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  443. int err;
  444. if (!wdev->conn)
  445. return 0;
  446. if (!rdev->ops->deauth)
  447. return -EOPNOTSUPP;
  448. if (wdev->conn->state == CFG80211_CONN_SCANNING ||
  449. wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
  450. err = 0;
  451. goto out;
  452. }
  453. /* wdev->conn->params.bssid must be set if > SCANNING */
  454. err = cfg80211_mlme_deauth(rdev, wdev->netdev,
  455. wdev->conn->params.bssid,
  456. NULL, 0, reason, false);
  457. out:
  458. cfg80211_sme_free(wdev);
  459. return err;
  460. }
  461. /*
  462. * code shared for in-device and software SME
  463. */
  464. static bool cfg80211_is_all_idle(void)
  465. {
  466. struct cfg80211_registered_device *rdev;
  467. struct wireless_dev *wdev;
  468. bool is_all_idle = true;
  469. /*
  470. * All devices must be idle as otherwise if you are actively
  471. * scanning some new beacon hints could be learned and would
  472. * count as new regulatory hints.
  473. */
  474. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  475. list_for_each_entry(wdev, &rdev->wdev_list, list) {
  476. wdev_lock(wdev);
  477. if (wdev->conn || wdev->current_bss)
  478. is_all_idle = false;
  479. wdev_unlock(wdev);
  480. }
  481. }
  482. return is_all_idle;
  483. }
  484. static void disconnect_work(struct work_struct *work)
  485. {
  486. rtnl_lock();
  487. if (cfg80211_is_all_idle())
  488. regulatory_hint_disconnect();
  489. rtnl_unlock();
  490. }
  491. static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
  492. /*
  493. * API calls for drivers implementing connect/disconnect and
  494. * SME event handling
  495. */
  496. /* This method must consume bss one way or another */
  497. void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  498. const u8 *req_ie, size_t req_ie_len,
  499. const u8 *resp_ie, size_t resp_ie_len,
  500. u16 status, bool wextev,
  501. struct cfg80211_bss *bss)
  502. {
  503. struct wireless_dev *wdev = dev->ieee80211_ptr;
  504. const u8 *country_ie;
  505. #ifdef CONFIG_CFG80211_WEXT
  506. union iwreq_data wrqu;
  507. #endif
  508. ASSERT_WDEV_LOCK(wdev);
  509. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
  510. wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
  511. cfg80211_put_bss(wdev->wiphy, bss);
  512. return;
  513. }
  514. nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
  515. bssid, req_ie, req_ie_len,
  516. resp_ie, resp_ie_len,
  517. status, GFP_KERNEL);
  518. #ifdef CONFIG_CFG80211_WEXT
  519. if (wextev) {
  520. if (req_ie && status == WLAN_STATUS_SUCCESS) {
  521. memset(&wrqu, 0, sizeof(wrqu));
  522. wrqu.data.length = req_ie_len;
  523. wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
  524. }
  525. if (resp_ie && status == WLAN_STATUS_SUCCESS) {
  526. memset(&wrqu, 0, sizeof(wrqu));
  527. wrqu.data.length = resp_ie_len;
  528. wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
  529. }
  530. memset(&wrqu, 0, sizeof(wrqu));
  531. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  532. if (bssid && status == WLAN_STATUS_SUCCESS) {
  533. memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
  534. memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
  535. wdev->wext.prev_bssid_valid = true;
  536. }
  537. wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
  538. }
  539. #endif
  540. if (!bss && (status == WLAN_STATUS_SUCCESS)) {
  541. WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
  542. bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
  543. wdev->ssid, wdev->ssid_len,
  544. WLAN_CAPABILITY_ESS,
  545. WLAN_CAPABILITY_ESS);
  546. if (bss)
  547. cfg80211_hold_bss(bss_from_pub(bss));
  548. }
  549. if (wdev->current_bss) {
  550. cfg80211_unhold_bss(wdev->current_bss);
  551. cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
  552. wdev->current_bss = NULL;
  553. }
  554. if (status != WLAN_STATUS_SUCCESS) {
  555. kzfree(wdev->connect_keys);
  556. wdev->connect_keys = NULL;
  557. wdev->ssid_len = 0;
  558. if (bss) {
  559. cfg80211_unhold_bss(bss_from_pub(bss));
  560. cfg80211_put_bss(wdev->wiphy, bss);
  561. }
  562. cfg80211_sme_free(wdev);
  563. return;
  564. }
  565. if (WARN_ON(!bss))
  566. return;
  567. wdev->current_bss = bss_from_pub(bss);
  568. cfg80211_upload_connect_keys(wdev);
  569. rcu_read_lock();
  570. country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
  571. if (!country_ie) {
  572. rcu_read_unlock();
  573. return;
  574. }
  575. country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
  576. rcu_read_unlock();
  577. if (!country_ie)
  578. return;
  579. /*
  580. * ieee80211_bss_get_ie() ensures we can access:
  581. * - country_ie + 2, the start of the country ie data, and
  582. * - and country_ie[1] which is the IE length
  583. */
  584. regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
  585. country_ie + 2, country_ie[1]);
  586. kfree(country_ie);
  587. }
  588. void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
  589. const u8 *req_ie, size_t req_ie_len,
  590. const u8 *resp_ie, size_t resp_ie_len,
  591. u16 status, gfp_t gfp)
  592. {
  593. struct wireless_dev *wdev = dev->ieee80211_ptr;
  594. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  595. struct cfg80211_event *ev;
  596. unsigned long flags;
  597. ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
  598. if (!ev)
  599. return;
  600. ev->type = EVENT_CONNECT_RESULT;
  601. if (bssid)
  602. memcpy(ev->cr.bssid, bssid, ETH_ALEN);
  603. if (req_ie_len) {
  604. ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
  605. ev->cr.req_ie_len = req_ie_len;
  606. memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
  607. }
  608. if (resp_ie_len) {
  609. ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
  610. ev->cr.resp_ie_len = resp_ie_len;
  611. memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
  612. }
  613. ev->cr.status = status;
  614. spin_lock_irqsave(&wdev->event_lock, flags);
  615. list_add_tail(&ev->list, &wdev->event_list);
  616. spin_unlock_irqrestore(&wdev->event_lock, flags);
  617. queue_work(cfg80211_wq, &rdev->event_work);
  618. }
  619. EXPORT_SYMBOL(cfg80211_connect_result);
  620. /* Consumes bss object one way or another */
  621. void __cfg80211_roamed(struct wireless_dev *wdev,
  622. struct cfg80211_bss *bss,
  623. const u8 *req_ie, size_t req_ie_len,
  624. const u8 *resp_ie, size_t resp_ie_len)
  625. {
  626. #ifdef CONFIG_CFG80211_WEXT
  627. union iwreq_data wrqu;
  628. #endif
  629. ASSERT_WDEV_LOCK(wdev);
  630. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
  631. wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
  632. goto out;
  633. if (WARN_ON(!wdev->current_bss))
  634. goto out;
  635. cfg80211_unhold_bss(wdev->current_bss);
  636. cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
  637. wdev->current_bss = NULL;
  638. cfg80211_hold_bss(bss_from_pub(bss));
  639. wdev->current_bss = bss_from_pub(bss);
  640. nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
  641. wdev->netdev, bss->bssid,
  642. req_ie, req_ie_len, resp_ie, resp_ie_len,
  643. GFP_KERNEL);
  644. #ifdef CONFIG_CFG80211_WEXT
  645. if (req_ie) {
  646. memset(&wrqu, 0, sizeof(wrqu));
  647. wrqu.data.length = req_ie_len;
  648. wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
  649. &wrqu, req_ie);
  650. }
  651. if (resp_ie) {
  652. memset(&wrqu, 0, sizeof(wrqu));
  653. wrqu.data.length = resp_ie_len;
  654. wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
  655. &wrqu, resp_ie);
  656. }
  657. memset(&wrqu, 0, sizeof(wrqu));
  658. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  659. memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
  660. memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
  661. wdev->wext.prev_bssid_valid = true;
  662. wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
  663. #endif
  664. return;
  665. out:
  666. cfg80211_put_bss(wdev->wiphy, bss);
  667. }
  668. void cfg80211_roamed(struct net_device *dev,
  669. struct ieee80211_channel *channel,
  670. const u8 *bssid,
  671. const u8 *req_ie, size_t req_ie_len,
  672. const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
  673. {
  674. struct wireless_dev *wdev = dev->ieee80211_ptr;
  675. struct cfg80211_bss *bss;
  676. bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
  677. wdev->ssid_len, WLAN_CAPABILITY_ESS,
  678. WLAN_CAPABILITY_ESS);
  679. if (WARN_ON(!bss))
  680. return;
  681. cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
  682. resp_ie_len, gfp);
  683. }
  684. EXPORT_SYMBOL(cfg80211_roamed);
  685. /* Consumes bss object one way or another */
  686. void cfg80211_roamed_bss(struct net_device *dev,
  687. struct cfg80211_bss *bss, const u8 *req_ie,
  688. size_t req_ie_len, const u8 *resp_ie,
  689. size_t resp_ie_len, gfp_t gfp)
  690. {
  691. struct wireless_dev *wdev = dev->ieee80211_ptr;
  692. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  693. struct cfg80211_event *ev;
  694. unsigned long flags;
  695. if (WARN_ON(!bss))
  696. return;
  697. ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
  698. if (!ev) {
  699. cfg80211_put_bss(wdev->wiphy, bss);
  700. return;
  701. }
  702. ev->type = EVENT_ROAMED;
  703. ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
  704. ev->rm.req_ie_len = req_ie_len;
  705. memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
  706. ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
  707. ev->rm.resp_ie_len = resp_ie_len;
  708. memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
  709. ev->rm.bss = bss;
  710. spin_lock_irqsave(&wdev->event_lock, flags);
  711. list_add_tail(&ev->list, &wdev->event_list);
  712. spin_unlock_irqrestore(&wdev->event_lock, flags);
  713. queue_work(cfg80211_wq, &rdev->event_work);
  714. }
  715. EXPORT_SYMBOL(cfg80211_roamed_bss);
  716. void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
  717. size_t ie_len, u16 reason, bool from_ap)
  718. {
  719. struct wireless_dev *wdev = dev->ieee80211_ptr;
  720. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  721. int i;
  722. #ifdef CONFIG_CFG80211_WEXT
  723. union iwreq_data wrqu;
  724. #endif
  725. ASSERT_WDEV_LOCK(wdev);
  726. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
  727. wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
  728. return;
  729. if (wdev->current_bss) {
  730. cfg80211_unhold_bss(wdev->current_bss);
  731. cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
  732. }
  733. wdev->current_bss = NULL;
  734. wdev->ssid_len = 0;
  735. nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
  736. /*
  737. * Delete all the keys ... pairwise keys can't really
  738. * exist any more anyway, but default keys might.
  739. */
  740. if (rdev->ops->del_key)
  741. for (i = 0; i < 6; i++)
  742. rdev_del_key(rdev, dev, i, false, NULL);
  743. rdev_set_qos_map(rdev, dev, NULL);
  744. #ifdef CONFIG_CFG80211_WEXT
  745. memset(&wrqu, 0, sizeof(wrqu));
  746. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  747. wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
  748. wdev->wext.connect.ssid_len = 0;
  749. #endif
  750. schedule_work(&cfg80211_disconnect_work);
  751. }
  752. void cfg80211_disconnected(struct net_device *dev, u16 reason,
  753. const u8 *ie, size_t ie_len, gfp_t gfp)
  754. {
  755. struct wireless_dev *wdev = dev->ieee80211_ptr;
  756. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  757. struct cfg80211_event *ev;
  758. unsigned long flags;
  759. ev = kzalloc(sizeof(*ev) + ie_len, gfp);
  760. if (!ev)
  761. return;
  762. ev->type = EVENT_DISCONNECTED;
  763. ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
  764. ev->dc.ie_len = ie_len;
  765. memcpy((void *)ev->dc.ie, ie, ie_len);
  766. ev->dc.reason = reason;
  767. spin_lock_irqsave(&wdev->event_lock, flags);
  768. list_add_tail(&ev->list, &wdev->event_list);
  769. spin_unlock_irqrestore(&wdev->event_lock, flags);
  770. queue_work(cfg80211_wq, &rdev->event_work);
  771. }
  772. EXPORT_SYMBOL(cfg80211_disconnected);
  773. /*
  774. * API calls for nl80211/wext compatibility code
  775. */
  776. int cfg80211_connect(struct cfg80211_registered_device *rdev,
  777. struct net_device *dev,
  778. struct cfg80211_connect_params *connect,
  779. struct cfg80211_cached_keys *connkeys,
  780. const u8 *prev_bssid)
  781. {
  782. struct wireless_dev *wdev = dev->ieee80211_ptr;
  783. int err;
  784. ASSERT_WDEV_LOCK(wdev);
  785. if (WARN_ON(wdev->connect_keys)) {
  786. kzfree(wdev->connect_keys);
  787. wdev->connect_keys = NULL;
  788. }
  789. cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
  790. rdev->wiphy.ht_capa_mod_mask);
  791. if (connkeys && connkeys->def >= 0) {
  792. int idx;
  793. u32 cipher;
  794. idx = connkeys->def;
  795. cipher = connkeys->params[idx].cipher;
  796. /* If given a WEP key we may need it for shared key auth */
  797. if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
  798. cipher == WLAN_CIPHER_SUITE_WEP104) {
  799. connect->key_idx = idx;
  800. connect->key = connkeys->params[idx].key;
  801. connect->key_len = connkeys->params[idx].key_len;
  802. /*
  803. * If ciphers are not set (e.g. when going through
  804. * iwconfig), we have to set them appropriately here.
  805. */
  806. if (connect->crypto.cipher_group == 0)
  807. connect->crypto.cipher_group = cipher;
  808. if (connect->crypto.n_ciphers_pairwise == 0) {
  809. connect->crypto.n_ciphers_pairwise = 1;
  810. connect->crypto.ciphers_pairwise[0] = cipher;
  811. }
  812. }
  813. }
  814. wdev->connect_keys = connkeys;
  815. memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
  816. wdev->ssid_len = connect->ssid_len;
  817. if (!rdev->ops->connect)
  818. err = cfg80211_sme_connect(wdev, connect, prev_bssid);
  819. else
  820. err = rdev_connect(rdev, dev, connect);
  821. if (err) {
  822. wdev->connect_keys = NULL;
  823. wdev->ssid_len = 0;
  824. return err;
  825. }
  826. return 0;
  827. }
  828. int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
  829. struct net_device *dev, u16 reason, bool wextev)
  830. {
  831. struct wireless_dev *wdev = dev->ieee80211_ptr;
  832. int err = 0;
  833. ASSERT_WDEV_LOCK(wdev);
  834. kzfree(wdev->connect_keys);
  835. wdev->connect_keys = NULL;
  836. if (wdev->conn)
  837. err = cfg80211_sme_disconnect(wdev, reason);
  838. else if (!rdev->ops->disconnect)
  839. cfg80211_mlme_down(rdev, dev);
  840. else if (wdev->current_bss)
  841. err = rdev_disconnect(rdev, dev, reason);
  842. return err;
  843. }