sme.c 29 KB

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