mlme.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /*
  2. * cfg80211 MLME SAP interface
  3. *
  4. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/nl80211.h>
  10. #include <linux/slab.h>
  11. #include <linux/wireless.h>
  12. #include <net/cfg80211.h>
  13. #include <net/iw_handler.h>
  14. #include "core.h"
  15. #include "nl80211.h"
  16. void cfg80211_send_rx_auth(struct net_device *dev, const u8 *buf, size_t len)
  17. {
  18. struct wireless_dev *wdev = dev->ieee80211_ptr;
  19. struct wiphy *wiphy = wdev->wiphy;
  20. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  21. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  22. u8 *bssid = mgmt->bssid;
  23. int i;
  24. u16 status = le16_to_cpu(mgmt->u.auth.status_code);
  25. bool done = false;
  26. wdev_lock(wdev);
  27. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  28. if (wdev->authtry_bsses[i] &&
  29. memcmp(wdev->authtry_bsses[i]->pub.bssid, bssid,
  30. ETH_ALEN) == 0) {
  31. if (status == WLAN_STATUS_SUCCESS) {
  32. wdev->auth_bsses[i] = wdev->authtry_bsses[i];
  33. } else {
  34. cfg80211_unhold_bss(wdev->authtry_bsses[i]);
  35. cfg80211_put_bss(&wdev->authtry_bsses[i]->pub);
  36. }
  37. wdev->authtry_bsses[i] = NULL;
  38. done = true;
  39. break;
  40. }
  41. }
  42. if (done) {
  43. nl80211_send_rx_auth(rdev, dev, buf, len, GFP_KERNEL);
  44. cfg80211_sme_rx_auth(dev, buf, len);
  45. }
  46. wdev_unlock(wdev);
  47. }
  48. EXPORT_SYMBOL(cfg80211_send_rx_auth);
  49. void cfg80211_send_rx_assoc(struct net_device *dev, const u8 *buf, size_t len)
  50. {
  51. u16 status_code;
  52. struct wireless_dev *wdev = dev->ieee80211_ptr;
  53. struct wiphy *wiphy = wdev->wiphy;
  54. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  55. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  56. u8 *ie = mgmt->u.assoc_resp.variable;
  57. int i, ieoffs = offsetof(struct ieee80211_mgmt, u.assoc_resp.variable);
  58. struct cfg80211_internal_bss *bss = NULL;
  59. wdev_lock(wdev);
  60. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  61. /*
  62. * This is a bit of a hack, we don't notify userspace of
  63. * a (re-)association reply if we tried to send a reassoc
  64. * and got a reject -- we only try again with an assoc
  65. * frame instead of reassoc.
  66. */
  67. if (status_code != WLAN_STATUS_SUCCESS && wdev->conn &&
  68. cfg80211_sme_failed_reassoc(wdev))
  69. goto out;
  70. nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL);
  71. if (status_code == WLAN_STATUS_SUCCESS) {
  72. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  73. if (!wdev->auth_bsses[i])
  74. continue;
  75. if (memcmp(wdev->auth_bsses[i]->pub.bssid, mgmt->bssid,
  76. ETH_ALEN) == 0) {
  77. bss = wdev->auth_bsses[i];
  78. wdev->auth_bsses[i] = NULL;
  79. /* additional reference to drop hold */
  80. cfg80211_ref_bss(bss);
  81. break;
  82. }
  83. }
  84. /*
  85. * We might be coming here because the driver reported
  86. * a successful association at the same time as the
  87. * user requested a deauth. In that case, we will have
  88. * removed the BSS from the auth_bsses list due to the
  89. * deauth request when the assoc response makes it. If
  90. * the two code paths acquire the lock the other way
  91. * around, that's just the standard situation of a
  92. * deauth being requested while connected.
  93. */
  94. if (!bss)
  95. goto out;
  96. } else if (wdev->conn) {
  97. cfg80211_sme_failed_assoc(wdev);
  98. /*
  99. * do not call connect_result() now because the
  100. * sme will schedule work that does it later.
  101. */
  102. goto out;
  103. }
  104. if (!wdev->conn && wdev->sme_state == CFG80211_SME_IDLE) {
  105. /*
  106. * This is for the userspace SME, the CONNECTING
  107. * state will be changed to CONNECTED by
  108. * __cfg80211_connect_result() below.
  109. */
  110. wdev->sme_state = CFG80211_SME_CONNECTING;
  111. }
  112. /* this consumes one bss reference (unless bss is NULL) */
  113. __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, ie, len - ieoffs,
  114. status_code,
  115. status_code == WLAN_STATUS_SUCCESS,
  116. bss ? &bss->pub : NULL);
  117. /* drop hold now, and also reference acquired above */
  118. if (bss) {
  119. cfg80211_unhold_bss(bss);
  120. cfg80211_put_bss(&bss->pub);
  121. }
  122. out:
  123. wdev_unlock(wdev);
  124. }
  125. EXPORT_SYMBOL(cfg80211_send_rx_assoc);
  126. void __cfg80211_send_deauth(struct net_device *dev,
  127. const u8 *buf, size_t len)
  128. {
  129. struct wireless_dev *wdev = dev->ieee80211_ptr;
  130. struct wiphy *wiphy = wdev->wiphy;
  131. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  132. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  133. const u8 *bssid = mgmt->bssid;
  134. int i;
  135. bool found = false, was_current = false;
  136. ASSERT_WDEV_LOCK(wdev);
  137. if (wdev->current_bss &&
  138. memcmp(wdev->current_bss->pub.bssid, bssid, ETH_ALEN) == 0) {
  139. cfg80211_unhold_bss(wdev->current_bss);
  140. cfg80211_put_bss(&wdev->current_bss->pub);
  141. wdev->current_bss = NULL;
  142. found = true;
  143. was_current = true;
  144. } else for (i = 0; i < MAX_AUTH_BSSES; i++) {
  145. if (wdev->auth_bsses[i] &&
  146. memcmp(wdev->auth_bsses[i]->pub.bssid, bssid, ETH_ALEN) == 0) {
  147. cfg80211_unhold_bss(wdev->auth_bsses[i]);
  148. cfg80211_put_bss(&wdev->auth_bsses[i]->pub);
  149. wdev->auth_bsses[i] = NULL;
  150. found = true;
  151. break;
  152. }
  153. if (wdev->authtry_bsses[i] &&
  154. memcmp(wdev->authtry_bsses[i]->pub.bssid, bssid,
  155. ETH_ALEN) == 0 &&
  156. memcmp(mgmt->sa, dev->dev_addr, ETH_ALEN) == 0) {
  157. cfg80211_unhold_bss(wdev->authtry_bsses[i]);
  158. cfg80211_put_bss(&wdev->authtry_bsses[i]->pub);
  159. wdev->authtry_bsses[i] = NULL;
  160. found = true;
  161. break;
  162. }
  163. }
  164. if (!found)
  165. return;
  166. nl80211_send_deauth(rdev, dev, buf, len, GFP_KERNEL);
  167. if (wdev->sme_state == CFG80211_SME_CONNECTED && was_current) {
  168. u16 reason_code;
  169. bool from_ap;
  170. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  171. from_ap = memcmp(mgmt->sa, dev->dev_addr, ETH_ALEN) != 0;
  172. __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
  173. } else if (wdev->sme_state == CFG80211_SME_CONNECTING) {
  174. __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
  175. WLAN_STATUS_UNSPECIFIED_FAILURE,
  176. false, NULL);
  177. }
  178. }
  179. EXPORT_SYMBOL(__cfg80211_send_deauth);
  180. void cfg80211_send_deauth(struct net_device *dev, const u8 *buf, size_t len)
  181. {
  182. struct wireless_dev *wdev = dev->ieee80211_ptr;
  183. wdev_lock(wdev);
  184. __cfg80211_send_deauth(dev, buf, len);
  185. wdev_unlock(wdev);
  186. }
  187. EXPORT_SYMBOL(cfg80211_send_deauth);
  188. void __cfg80211_send_disassoc(struct net_device *dev,
  189. const u8 *buf, size_t len)
  190. {
  191. struct wireless_dev *wdev = dev->ieee80211_ptr;
  192. struct wiphy *wiphy = wdev->wiphy;
  193. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  194. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  195. const u8 *bssid = mgmt->bssid;
  196. int i;
  197. u16 reason_code;
  198. bool from_ap;
  199. bool done = false;
  200. ASSERT_WDEV_LOCK(wdev);
  201. nl80211_send_disassoc(rdev, dev, buf, len, GFP_KERNEL);
  202. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  203. return;
  204. if (wdev->current_bss &&
  205. memcmp(wdev->current_bss->pub.bssid, bssid, ETH_ALEN) == 0) {
  206. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  207. if (wdev->authtry_bsses[i] || wdev->auth_bsses[i])
  208. continue;
  209. wdev->auth_bsses[i] = wdev->current_bss;
  210. wdev->current_bss = NULL;
  211. done = true;
  212. cfg80211_sme_disassoc(dev, i);
  213. break;
  214. }
  215. WARN_ON(!done);
  216. } else
  217. WARN_ON(1);
  218. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  219. from_ap = memcmp(mgmt->sa, dev->dev_addr, ETH_ALEN) != 0;
  220. __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
  221. }
  222. EXPORT_SYMBOL(__cfg80211_send_disassoc);
  223. void cfg80211_send_disassoc(struct net_device *dev, const u8 *buf, size_t len)
  224. {
  225. struct wireless_dev *wdev = dev->ieee80211_ptr;
  226. wdev_lock(wdev);
  227. __cfg80211_send_disassoc(dev, buf, len);
  228. wdev_unlock(wdev);
  229. }
  230. EXPORT_SYMBOL(cfg80211_send_disassoc);
  231. void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
  232. size_t len)
  233. {
  234. struct wireless_dev *wdev = dev->ieee80211_ptr;
  235. struct wiphy *wiphy = wdev->wiphy;
  236. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  237. nl80211_send_unprot_deauth(rdev, dev, buf, len, GFP_ATOMIC);
  238. }
  239. EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
  240. void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
  241. size_t len)
  242. {
  243. struct wireless_dev *wdev = dev->ieee80211_ptr;
  244. struct wiphy *wiphy = wdev->wiphy;
  245. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  246. nl80211_send_unprot_disassoc(rdev, dev, buf, len, GFP_ATOMIC);
  247. }
  248. EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
  249. static void __cfg80211_auth_remove(struct wireless_dev *wdev, const u8 *addr)
  250. {
  251. int i;
  252. bool done = false;
  253. ASSERT_WDEV_LOCK(wdev);
  254. for (i = 0; addr && i < MAX_AUTH_BSSES; i++) {
  255. if (wdev->authtry_bsses[i] &&
  256. memcmp(wdev->authtry_bsses[i]->pub.bssid,
  257. addr, ETH_ALEN) == 0) {
  258. cfg80211_unhold_bss(wdev->authtry_bsses[i]);
  259. cfg80211_put_bss(&wdev->authtry_bsses[i]->pub);
  260. wdev->authtry_bsses[i] = NULL;
  261. done = true;
  262. break;
  263. }
  264. }
  265. WARN_ON(!done);
  266. }
  267. void __cfg80211_auth_canceled(struct net_device *dev, const u8 *addr)
  268. {
  269. __cfg80211_auth_remove(dev->ieee80211_ptr, addr);
  270. }
  271. EXPORT_SYMBOL(__cfg80211_auth_canceled);
  272. void cfg80211_send_auth_timeout(struct net_device *dev, const u8 *addr)
  273. {
  274. struct wireless_dev *wdev = dev->ieee80211_ptr;
  275. struct wiphy *wiphy = wdev->wiphy;
  276. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  277. wdev_lock(wdev);
  278. nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
  279. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  280. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  281. WLAN_STATUS_UNSPECIFIED_FAILURE,
  282. false, NULL);
  283. __cfg80211_auth_remove(wdev, addr);
  284. wdev_unlock(wdev);
  285. }
  286. EXPORT_SYMBOL(cfg80211_send_auth_timeout);
  287. void cfg80211_send_assoc_timeout(struct net_device *dev, const u8 *addr)
  288. {
  289. struct wireless_dev *wdev = dev->ieee80211_ptr;
  290. struct wiphy *wiphy = wdev->wiphy;
  291. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  292. int i;
  293. bool done = false;
  294. wdev_lock(wdev);
  295. nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
  296. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  297. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  298. WLAN_STATUS_UNSPECIFIED_FAILURE,
  299. false, NULL);
  300. for (i = 0; addr && i < MAX_AUTH_BSSES; i++) {
  301. if (wdev->auth_bsses[i] &&
  302. memcmp(wdev->auth_bsses[i]->pub.bssid,
  303. addr, ETH_ALEN) == 0) {
  304. cfg80211_unhold_bss(wdev->auth_bsses[i]);
  305. cfg80211_put_bss(&wdev->auth_bsses[i]->pub);
  306. wdev->auth_bsses[i] = NULL;
  307. done = true;
  308. break;
  309. }
  310. }
  311. WARN_ON(!done);
  312. wdev_unlock(wdev);
  313. }
  314. EXPORT_SYMBOL(cfg80211_send_assoc_timeout);
  315. void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
  316. enum nl80211_key_type key_type, int key_id,
  317. const u8 *tsc, gfp_t gfp)
  318. {
  319. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  320. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  321. #ifdef CONFIG_CFG80211_WEXT
  322. union iwreq_data wrqu;
  323. char *buf = kmalloc(128, gfp);
  324. if (buf) {
  325. sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
  326. "keyid=%d %scast addr=%pM)", key_id,
  327. key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
  328. addr);
  329. memset(&wrqu, 0, sizeof(wrqu));
  330. wrqu.data.length = strlen(buf);
  331. wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
  332. kfree(buf);
  333. }
  334. #endif
  335. nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
  336. }
  337. EXPORT_SYMBOL(cfg80211_michael_mic_failure);
  338. /* some MLME handling for userspace SME */
  339. int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  340. struct net_device *dev,
  341. struct ieee80211_channel *chan,
  342. enum nl80211_auth_type auth_type,
  343. const u8 *bssid,
  344. const u8 *ssid, int ssid_len,
  345. const u8 *ie, int ie_len,
  346. const u8 *key, int key_len, int key_idx,
  347. bool local_state_change)
  348. {
  349. struct wireless_dev *wdev = dev->ieee80211_ptr;
  350. struct cfg80211_auth_request req;
  351. struct cfg80211_internal_bss *bss;
  352. int i, err, slot = -1, nfree = 0;
  353. ASSERT_WDEV_LOCK(wdev);
  354. if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  355. if (!key || !key_len || key_idx < 0 || key_idx > 4)
  356. return -EINVAL;
  357. if (wdev->current_bss &&
  358. memcmp(bssid, wdev->current_bss->pub.bssid, ETH_ALEN) == 0)
  359. return -EALREADY;
  360. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  361. if (wdev->authtry_bsses[i] &&
  362. memcmp(bssid, wdev->authtry_bsses[i]->pub.bssid,
  363. ETH_ALEN) == 0)
  364. return -EALREADY;
  365. if (wdev->auth_bsses[i] &&
  366. memcmp(bssid, wdev->auth_bsses[i]->pub.bssid,
  367. ETH_ALEN) == 0)
  368. return -EALREADY;
  369. }
  370. memset(&req, 0, sizeof(req));
  371. req.local_state_change = local_state_change;
  372. req.ie = ie;
  373. req.ie_len = ie_len;
  374. req.auth_type = auth_type;
  375. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  376. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  377. req.key = key;
  378. req.key_len = key_len;
  379. req.key_idx = key_idx;
  380. if (!req.bss)
  381. return -ENOENT;
  382. bss = bss_from_pub(req.bss);
  383. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  384. if (!wdev->auth_bsses[i] && !wdev->authtry_bsses[i]) {
  385. slot = i;
  386. nfree++;
  387. }
  388. }
  389. /* we need one free slot for disassoc and one for this auth */
  390. if (nfree < 2) {
  391. err = -ENOSPC;
  392. goto out;
  393. }
  394. if (local_state_change)
  395. wdev->auth_bsses[slot] = bss;
  396. else
  397. wdev->authtry_bsses[slot] = bss;
  398. cfg80211_hold_bss(bss);
  399. err = rdev->ops->auth(&rdev->wiphy, dev, &req);
  400. if (err) {
  401. if (local_state_change)
  402. wdev->auth_bsses[slot] = NULL;
  403. else
  404. wdev->authtry_bsses[slot] = NULL;
  405. cfg80211_unhold_bss(bss);
  406. }
  407. out:
  408. if (err)
  409. cfg80211_put_bss(req.bss);
  410. return err;
  411. }
  412. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  413. struct net_device *dev, struct ieee80211_channel *chan,
  414. enum nl80211_auth_type auth_type, const u8 *bssid,
  415. const u8 *ssid, int ssid_len,
  416. const u8 *ie, int ie_len,
  417. const u8 *key, int key_len, int key_idx,
  418. bool local_state_change)
  419. {
  420. int err;
  421. wdev_lock(dev->ieee80211_ptr);
  422. err = __cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
  423. ssid, ssid_len, ie, ie_len,
  424. key, key_len, key_idx, local_state_change);
  425. wdev_unlock(dev->ieee80211_ptr);
  426. return err;
  427. }
  428. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  429. struct net_device *dev,
  430. struct ieee80211_channel *chan,
  431. const u8 *bssid, const u8 *prev_bssid,
  432. const u8 *ssid, int ssid_len,
  433. const u8 *ie, int ie_len, bool use_mfp,
  434. struct cfg80211_crypto_settings *crypt)
  435. {
  436. struct wireless_dev *wdev = dev->ieee80211_ptr;
  437. struct cfg80211_assoc_request req;
  438. struct cfg80211_internal_bss *bss;
  439. int i, err, slot = -1;
  440. bool was_connected = false;
  441. ASSERT_WDEV_LOCK(wdev);
  442. memset(&req, 0, sizeof(req));
  443. if (wdev->current_bss && prev_bssid &&
  444. memcmp(wdev->current_bss->pub.bssid, prev_bssid, ETH_ALEN) == 0) {
  445. /*
  446. * Trying to reassociate: Allow this to proceed and let the old
  447. * association to be dropped when the new one is completed.
  448. */
  449. if (wdev->sme_state == CFG80211_SME_CONNECTED) {
  450. was_connected = true;
  451. wdev->sme_state = CFG80211_SME_CONNECTING;
  452. }
  453. } else if (wdev->current_bss)
  454. return -EALREADY;
  455. req.ie = ie;
  456. req.ie_len = ie_len;
  457. memcpy(&req.crypto, crypt, sizeof(req.crypto));
  458. req.use_mfp = use_mfp;
  459. req.prev_bssid = prev_bssid;
  460. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  461. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  462. if (!req.bss) {
  463. if (was_connected)
  464. wdev->sme_state = CFG80211_SME_CONNECTED;
  465. return -ENOENT;
  466. }
  467. bss = bss_from_pub(req.bss);
  468. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  469. if (bss == wdev->auth_bsses[i]) {
  470. slot = i;
  471. break;
  472. }
  473. }
  474. if (slot < 0) {
  475. err = -ENOTCONN;
  476. goto out;
  477. }
  478. err = rdev->ops->assoc(&rdev->wiphy, dev, &req);
  479. out:
  480. if (err && was_connected)
  481. wdev->sme_state = CFG80211_SME_CONNECTED;
  482. /* still a reference in wdev->auth_bsses[slot] */
  483. cfg80211_put_bss(req.bss);
  484. return err;
  485. }
  486. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  487. struct net_device *dev,
  488. struct ieee80211_channel *chan,
  489. const u8 *bssid, const u8 *prev_bssid,
  490. const u8 *ssid, int ssid_len,
  491. const u8 *ie, int ie_len, bool use_mfp,
  492. struct cfg80211_crypto_settings *crypt)
  493. {
  494. struct wireless_dev *wdev = dev->ieee80211_ptr;
  495. int err;
  496. wdev_lock(wdev);
  497. err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
  498. ssid, ssid_len, ie, ie_len, use_mfp, crypt);
  499. wdev_unlock(wdev);
  500. return err;
  501. }
  502. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  503. struct net_device *dev, const u8 *bssid,
  504. const u8 *ie, int ie_len, u16 reason,
  505. bool local_state_change)
  506. {
  507. struct wireless_dev *wdev = dev->ieee80211_ptr;
  508. struct cfg80211_deauth_request req;
  509. int i;
  510. ASSERT_WDEV_LOCK(wdev);
  511. memset(&req, 0, sizeof(req));
  512. req.reason_code = reason;
  513. req.local_state_change = local_state_change;
  514. req.ie = ie;
  515. req.ie_len = ie_len;
  516. if (wdev->current_bss &&
  517. memcmp(wdev->current_bss->pub.bssid, bssid, ETH_ALEN) == 0) {
  518. req.bss = &wdev->current_bss->pub;
  519. } else for (i = 0; i < MAX_AUTH_BSSES; i++) {
  520. if (wdev->auth_bsses[i] &&
  521. memcmp(bssid, wdev->auth_bsses[i]->pub.bssid, ETH_ALEN) == 0) {
  522. req.bss = &wdev->auth_bsses[i]->pub;
  523. break;
  524. }
  525. if (wdev->authtry_bsses[i] &&
  526. memcmp(bssid, wdev->authtry_bsses[i]->pub.bssid, ETH_ALEN) == 0) {
  527. req.bss = &wdev->authtry_bsses[i]->pub;
  528. break;
  529. }
  530. }
  531. if (!req.bss)
  532. return -ENOTCONN;
  533. return rdev->ops->deauth(&rdev->wiphy, dev, &req, wdev);
  534. }
  535. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  536. struct net_device *dev, const u8 *bssid,
  537. const u8 *ie, int ie_len, u16 reason,
  538. bool local_state_change)
  539. {
  540. struct wireless_dev *wdev = dev->ieee80211_ptr;
  541. int err;
  542. wdev_lock(wdev);
  543. err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
  544. local_state_change);
  545. wdev_unlock(wdev);
  546. return err;
  547. }
  548. static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  549. struct net_device *dev, const u8 *bssid,
  550. const u8 *ie, int ie_len, u16 reason,
  551. bool local_state_change)
  552. {
  553. struct wireless_dev *wdev = dev->ieee80211_ptr;
  554. struct cfg80211_disassoc_request req;
  555. ASSERT_WDEV_LOCK(wdev);
  556. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  557. return -ENOTCONN;
  558. if (WARN_ON(!wdev->current_bss))
  559. return -ENOTCONN;
  560. memset(&req, 0, sizeof(req));
  561. req.reason_code = reason;
  562. req.local_state_change = local_state_change;
  563. req.ie = ie;
  564. req.ie_len = ie_len;
  565. if (memcmp(wdev->current_bss->pub.bssid, bssid, ETH_ALEN) == 0)
  566. req.bss = &wdev->current_bss->pub;
  567. else
  568. return -ENOTCONN;
  569. return rdev->ops->disassoc(&rdev->wiphy, dev, &req, wdev);
  570. }
  571. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  572. struct net_device *dev, const u8 *bssid,
  573. const u8 *ie, int ie_len, u16 reason,
  574. bool local_state_change)
  575. {
  576. struct wireless_dev *wdev = dev->ieee80211_ptr;
  577. int err;
  578. wdev_lock(wdev);
  579. err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
  580. local_state_change);
  581. wdev_unlock(wdev);
  582. return err;
  583. }
  584. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  585. struct net_device *dev)
  586. {
  587. struct wireless_dev *wdev = dev->ieee80211_ptr;
  588. struct cfg80211_deauth_request req;
  589. int i;
  590. ASSERT_WDEV_LOCK(wdev);
  591. if (!rdev->ops->deauth)
  592. return;
  593. memset(&req, 0, sizeof(req));
  594. req.reason_code = WLAN_REASON_DEAUTH_LEAVING;
  595. req.ie = NULL;
  596. req.ie_len = 0;
  597. if (wdev->current_bss) {
  598. req.bss = &wdev->current_bss->pub;
  599. rdev->ops->deauth(&rdev->wiphy, dev, &req, wdev);
  600. if (wdev->current_bss) {
  601. cfg80211_unhold_bss(wdev->current_bss);
  602. cfg80211_put_bss(&wdev->current_bss->pub);
  603. wdev->current_bss = NULL;
  604. }
  605. }
  606. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  607. if (wdev->auth_bsses[i]) {
  608. req.bss = &wdev->auth_bsses[i]->pub;
  609. rdev->ops->deauth(&rdev->wiphy, dev, &req, wdev);
  610. if (wdev->auth_bsses[i]) {
  611. cfg80211_unhold_bss(wdev->auth_bsses[i]);
  612. cfg80211_put_bss(&wdev->auth_bsses[i]->pub);
  613. wdev->auth_bsses[i] = NULL;
  614. }
  615. }
  616. if (wdev->authtry_bsses[i]) {
  617. req.bss = &wdev->authtry_bsses[i]->pub;
  618. rdev->ops->deauth(&rdev->wiphy, dev, &req, wdev);
  619. if (wdev->authtry_bsses[i]) {
  620. cfg80211_unhold_bss(wdev->authtry_bsses[i]);
  621. cfg80211_put_bss(&wdev->authtry_bsses[i]->pub);
  622. wdev->authtry_bsses[i] = NULL;
  623. }
  624. }
  625. }
  626. }
  627. void cfg80211_ready_on_channel(struct net_device *dev, u64 cookie,
  628. struct ieee80211_channel *chan,
  629. enum nl80211_channel_type channel_type,
  630. unsigned int duration, gfp_t gfp)
  631. {
  632. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  633. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  634. nl80211_send_remain_on_channel(rdev, dev, cookie, chan, channel_type,
  635. duration, gfp);
  636. }
  637. EXPORT_SYMBOL(cfg80211_ready_on_channel);
  638. void cfg80211_remain_on_channel_expired(struct net_device *dev,
  639. u64 cookie,
  640. struct ieee80211_channel *chan,
  641. enum nl80211_channel_type channel_type,
  642. gfp_t gfp)
  643. {
  644. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  645. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  646. nl80211_send_remain_on_channel_cancel(rdev, dev, cookie, chan,
  647. channel_type, gfp);
  648. }
  649. EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
  650. void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
  651. struct station_info *sinfo, gfp_t gfp)
  652. {
  653. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  654. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  655. nl80211_send_sta_event(rdev, dev, mac_addr, sinfo, gfp);
  656. }
  657. EXPORT_SYMBOL(cfg80211_new_sta);
  658. void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
  659. {
  660. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  661. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  662. nl80211_send_sta_del_event(rdev, dev, mac_addr, gfp);
  663. }
  664. EXPORT_SYMBOL(cfg80211_del_sta);
  665. struct cfg80211_mgmt_registration {
  666. struct list_head list;
  667. u32 nlpid;
  668. int match_len;
  669. __le16 frame_type;
  670. u8 match[];
  671. };
  672. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
  673. u16 frame_type, const u8 *match_data,
  674. int match_len)
  675. {
  676. struct wiphy *wiphy = wdev->wiphy;
  677. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  678. struct cfg80211_mgmt_registration *reg, *nreg;
  679. int err = 0;
  680. u16 mgmt_type;
  681. if (!wdev->wiphy->mgmt_stypes)
  682. return -EOPNOTSUPP;
  683. if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
  684. return -EINVAL;
  685. if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
  686. return -EINVAL;
  687. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  688. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
  689. return -EINVAL;
  690. nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
  691. if (!nreg)
  692. return -ENOMEM;
  693. spin_lock_bh(&wdev->mgmt_registrations_lock);
  694. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  695. int mlen = min(match_len, reg->match_len);
  696. if (frame_type != le16_to_cpu(reg->frame_type))
  697. continue;
  698. if (memcmp(reg->match, match_data, mlen) == 0) {
  699. err = -EALREADY;
  700. break;
  701. }
  702. }
  703. if (err) {
  704. kfree(nreg);
  705. goto out;
  706. }
  707. memcpy(nreg->match, match_data, match_len);
  708. nreg->match_len = match_len;
  709. nreg->nlpid = snd_pid;
  710. nreg->frame_type = cpu_to_le16(frame_type);
  711. list_add(&nreg->list, &wdev->mgmt_registrations);
  712. if (rdev->ops->mgmt_frame_register)
  713. rdev->ops->mgmt_frame_register(wiphy, wdev->netdev,
  714. frame_type, true);
  715. out:
  716. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  717. return err;
  718. }
  719. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid)
  720. {
  721. struct wiphy *wiphy = wdev->wiphy;
  722. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  723. struct cfg80211_mgmt_registration *reg, *tmp;
  724. spin_lock_bh(&wdev->mgmt_registrations_lock);
  725. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  726. if (reg->nlpid != nlpid)
  727. continue;
  728. if (rdev->ops->mgmt_frame_register) {
  729. u16 frame_type = le16_to_cpu(reg->frame_type);
  730. rdev->ops->mgmt_frame_register(wiphy, wdev->netdev,
  731. frame_type, false);
  732. }
  733. list_del(&reg->list);
  734. kfree(reg);
  735. }
  736. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  737. }
  738. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
  739. {
  740. struct cfg80211_mgmt_registration *reg, *tmp;
  741. spin_lock_bh(&wdev->mgmt_registrations_lock);
  742. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  743. list_del(&reg->list);
  744. kfree(reg);
  745. }
  746. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  747. }
  748. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  749. struct net_device *dev,
  750. struct ieee80211_channel *chan, bool offchan,
  751. enum nl80211_channel_type channel_type,
  752. bool channel_type_valid, unsigned int wait,
  753. const u8 *buf, size_t len, u64 *cookie)
  754. {
  755. struct wireless_dev *wdev = dev->ieee80211_ptr;
  756. const struct ieee80211_mgmt *mgmt;
  757. u16 stype;
  758. if (!wdev->wiphy->mgmt_stypes)
  759. return -EOPNOTSUPP;
  760. if (!rdev->ops->mgmt_tx)
  761. return -EOPNOTSUPP;
  762. if (len < 24 + 1)
  763. return -EINVAL;
  764. mgmt = (const struct ieee80211_mgmt *) buf;
  765. if (!ieee80211_is_mgmt(mgmt->frame_control))
  766. return -EINVAL;
  767. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  768. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
  769. return -EINVAL;
  770. if (ieee80211_is_action(mgmt->frame_control) &&
  771. mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
  772. int err = 0;
  773. wdev_lock(wdev);
  774. switch (wdev->iftype) {
  775. case NL80211_IFTYPE_ADHOC:
  776. case NL80211_IFTYPE_STATION:
  777. case NL80211_IFTYPE_P2P_CLIENT:
  778. if (!wdev->current_bss) {
  779. err = -ENOTCONN;
  780. break;
  781. }
  782. if (memcmp(wdev->current_bss->pub.bssid,
  783. mgmt->bssid, ETH_ALEN)) {
  784. err = -ENOTCONN;
  785. break;
  786. }
  787. /*
  788. * check for IBSS DA must be done by driver as
  789. * cfg80211 doesn't track the stations
  790. */
  791. if (wdev->iftype == NL80211_IFTYPE_ADHOC)
  792. break;
  793. /* for station, check that DA is the AP */
  794. if (memcmp(wdev->current_bss->pub.bssid,
  795. mgmt->da, ETH_ALEN)) {
  796. err = -ENOTCONN;
  797. break;
  798. }
  799. break;
  800. case NL80211_IFTYPE_AP:
  801. case NL80211_IFTYPE_P2P_GO:
  802. case NL80211_IFTYPE_AP_VLAN:
  803. if (memcmp(mgmt->bssid, dev->dev_addr, ETH_ALEN))
  804. err = -EINVAL;
  805. break;
  806. case NL80211_IFTYPE_MESH_POINT:
  807. if (memcmp(mgmt->sa, mgmt->bssid, ETH_ALEN)) {
  808. err = -EINVAL;
  809. break;
  810. }
  811. /*
  812. * check for mesh DA must be done by driver as
  813. * cfg80211 doesn't track the stations
  814. */
  815. break;
  816. default:
  817. err = -EOPNOTSUPP;
  818. break;
  819. }
  820. wdev_unlock(wdev);
  821. if (err)
  822. return err;
  823. }
  824. if (memcmp(mgmt->sa, dev->dev_addr, ETH_ALEN) != 0)
  825. return -EINVAL;
  826. /* Transmit the Action frame as requested by user space */
  827. return rdev->ops->mgmt_tx(&rdev->wiphy, dev, chan, offchan,
  828. channel_type, channel_type_valid,
  829. wait, buf, len, cookie);
  830. }
  831. bool cfg80211_rx_mgmt(struct net_device *dev, int freq, const u8 *buf,
  832. size_t len, gfp_t gfp)
  833. {
  834. struct wireless_dev *wdev = dev->ieee80211_ptr;
  835. struct wiphy *wiphy = wdev->wiphy;
  836. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  837. struct cfg80211_mgmt_registration *reg;
  838. const struct ieee80211_txrx_stypes *stypes =
  839. &wiphy->mgmt_stypes[wdev->iftype];
  840. struct ieee80211_mgmt *mgmt = (void *)buf;
  841. const u8 *data;
  842. int data_len;
  843. bool result = false;
  844. __le16 ftype = mgmt->frame_control &
  845. cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
  846. u16 stype;
  847. stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
  848. if (!(stypes->rx & BIT(stype)))
  849. return false;
  850. data = buf + ieee80211_hdrlen(mgmt->frame_control);
  851. data_len = len - ieee80211_hdrlen(mgmt->frame_control);
  852. spin_lock_bh(&wdev->mgmt_registrations_lock);
  853. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  854. if (reg->frame_type != ftype)
  855. continue;
  856. if (reg->match_len > data_len)
  857. continue;
  858. if (memcmp(reg->match, data, reg->match_len))
  859. continue;
  860. /* found match! */
  861. /* Indicate the received Action frame to user space */
  862. if (nl80211_send_mgmt(rdev, dev, reg->nlpid, freq,
  863. buf, len, gfp))
  864. continue;
  865. result = true;
  866. break;
  867. }
  868. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  869. return result;
  870. }
  871. EXPORT_SYMBOL(cfg80211_rx_mgmt);
  872. void cfg80211_mgmt_tx_status(struct net_device *dev, u64 cookie,
  873. const u8 *buf, size_t len, bool ack, gfp_t gfp)
  874. {
  875. struct wireless_dev *wdev = dev->ieee80211_ptr;
  876. struct wiphy *wiphy = wdev->wiphy;
  877. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  878. /* Indicate TX status of the Action frame to user space */
  879. nl80211_send_mgmt_tx_status(rdev, dev, cookie, buf, len, ack, gfp);
  880. }
  881. EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
  882. void cfg80211_cqm_rssi_notify(struct net_device *dev,
  883. enum nl80211_cqm_rssi_threshold_event rssi_event,
  884. gfp_t gfp)
  885. {
  886. struct wireless_dev *wdev = dev->ieee80211_ptr;
  887. struct wiphy *wiphy = wdev->wiphy;
  888. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  889. /* Indicate roaming trigger event to user space */
  890. nl80211_send_cqm_rssi_notify(rdev, dev, rssi_event, gfp);
  891. }
  892. EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
  893. void cfg80211_cqm_pktloss_notify(struct net_device *dev,
  894. const u8 *peer, u32 num_packets, gfp_t gfp)
  895. {
  896. struct wireless_dev *wdev = dev->ieee80211_ptr;
  897. struct wiphy *wiphy = wdev->wiphy;
  898. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  899. /* Indicate roaming trigger event to user space */
  900. nl80211_send_cqm_pktloss_notify(rdev, dev, peer, num_packets, gfp);
  901. }
  902. EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
  903. void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
  904. const u8 *replay_ctr, gfp_t gfp)
  905. {
  906. struct wireless_dev *wdev = dev->ieee80211_ptr;
  907. struct wiphy *wiphy = wdev->wiphy;
  908. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  909. nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
  910. }
  911. EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);