mlme.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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, ETH_ALEN) == 0) {
  155. cfg80211_unhold_bss(wdev->authtry_bsses[i]);
  156. cfg80211_put_bss(&wdev->authtry_bsses[i]->pub);
  157. wdev->authtry_bsses[i] = NULL;
  158. found = true;
  159. break;
  160. }
  161. }
  162. if (!found)
  163. return;
  164. nl80211_send_deauth(rdev, dev, buf, len, GFP_KERNEL);
  165. if (wdev->sme_state == CFG80211_SME_CONNECTED && was_current) {
  166. u16 reason_code;
  167. bool from_ap;
  168. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  169. from_ap = memcmp(mgmt->sa, dev->dev_addr, ETH_ALEN) != 0;
  170. __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
  171. } else if (wdev->sme_state == CFG80211_SME_CONNECTING) {
  172. __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
  173. WLAN_STATUS_UNSPECIFIED_FAILURE,
  174. false, NULL);
  175. }
  176. }
  177. EXPORT_SYMBOL(__cfg80211_send_deauth);
  178. void cfg80211_send_deauth(struct net_device *dev, const u8 *buf, size_t len)
  179. {
  180. struct wireless_dev *wdev = dev->ieee80211_ptr;
  181. wdev_lock(wdev);
  182. __cfg80211_send_deauth(dev, buf, len);
  183. wdev_unlock(wdev);
  184. }
  185. EXPORT_SYMBOL(cfg80211_send_deauth);
  186. void __cfg80211_send_disassoc(struct net_device *dev,
  187. const u8 *buf, size_t len)
  188. {
  189. struct wireless_dev *wdev = dev->ieee80211_ptr;
  190. struct wiphy *wiphy = wdev->wiphy;
  191. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  192. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  193. const u8 *bssid = mgmt->bssid;
  194. int i;
  195. u16 reason_code;
  196. bool from_ap;
  197. bool done = false;
  198. ASSERT_WDEV_LOCK(wdev);
  199. nl80211_send_disassoc(rdev, dev, buf, len, GFP_KERNEL);
  200. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  201. return;
  202. if (wdev->current_bss &&
  203. memcmp(wdev->current_bss->pub.bssid, bssid, ETH_ALEN) == 0) {
  204. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  205. if (wdev->authtry_bsses[i] || wdev->auth_bsses[i])
  206. continue;
  207. wdev->auth_bsses[i] = wdev->current_bss;
  208. wdev->current_bss = NULL;
  209. done = true;
  210. cfg80211_sme_disassoc(dev, i);
  211. break;
  212. }
  213. WARN_ON(!done);
  214. } else
  215. WARN_ON(1);
  216. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  217. from_ap = memcmp(mgmt->sa, dev->dev_addr, ETH_ALEN) != 0;
  218. __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
  219. }
  220. EXPORT_SYMBOL(__cfg80211_send_disassoc);
  221. void cfg80211_send_disassoc(struct net_device *dev, const u8 *buf, size_t len)
  222. {
  223. struct wireless_dev *wdev = dev->ieee80211_ptr;
  224. wdev_lock(wdev);
  225. __cfg80211_send_disassoc(dev, buf, len);
  226. wdev_unlock(wdev);
  227. }
  228. EXPORT_SYMBOL(cfg80211_send_disassoc);
  229. static void __cfg80211_auth_remove(struct wireless_dev *wdev, const u8 *addr)
  230. {
  231. int i;
  232. bool done = false;
  233. ASSERT_WDEV_LOCK(wdev);
  234. for (i = 0; addr && i < MAX_AUTH_BSSES; i++) {
  235. if (wdev->authtry_bsses[i] &&
  236. memcmp(wdev->authtry_bsses[i]->pub.bssid,
  237. addr, ETH_ALEN) == 0) {
  238. cfg80211_unhold_bss(wdev->authtry_bsses[i]);
  239. cfg80211_put_bss(&wdev->authtry_bsses[i]->pub);
  240. wdev->authtry_bsses[i] = NULL;
  241. done = true;
  242. break;
  243. }
  244. }
  245. WARN_ON(!done);
  246. }
  247. void __cfg80211_auth_canceled(struct net_device *dev, const u8 *addr)
  248. {
  249. __cfg80211_auth_remove(dev->ieee80211_ptr, addr);
  250. }
  251. EXPORT_SYMBOL(__cfg80211_auth_canceled);
  252. void cfg80211_send_auth_timeout(struct net_device *dev, const u8 *addr)
  253. {
  254. struct wireless_dev *wdev = dev->ieee80211_ptr;
  255. struct wiphy *wiphy = wdev->wiphy;
  256. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  257. wdev_lock(wdev);
  258. nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
  259. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  260. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  261. WLAN_STATUS_UNSPECIFIED_FAILURE,
  262. false, NULL);
  263. __cfg80211_auth_remove(wdev, addr);
  264. wdev_unlock(wdev);
  265. }
  266. EXPORT_SYMBOL(cfg80211_send_auth_timeout);
  267. void cfg80211_send_assoc_timeout(struct net_device *dev, const u8 *addr)
  268. {
  269. struct wireless_dev *wdev = dev->ieee80211_ptr;
  270. struct wiphy *wiphy = wdev->wiphy;
  271. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  272. int i;
  273. bool done = false;
  274. wdev_lock(wdev);
  275. nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
  276. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  277. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  278. WLAN_STATUS_UNSPECIFIED_FAILURE,
  279. false, NULL);
  280. for (i = 0; addr && i < MAX_AUTH_BSSES; i++) {
  281. if (wdev->auth_bsses[i] &&
  282. memcmp(wdev->auth_bsses[i]->pub.bssid,
  283. addr, ETH_ALEN) == 0) {
  284. cfg80211_unhold_bss(wdev->auth_bsses[i]);
  285. cfg80211_put_bss(&wdev->auth_bsses[i]->pub);
  286. wdev->auth_bsses[i] = NULL;
  287. done = true;
  288. break;
  289. }
  290. }
  291. WARN_ON(!done);
  292. wdev_unlock(wdev);
  293. }
  294. EXPORT_SYMBOL(cfg80211_send_assoc_timeout);
  295. void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
  296. enum nl80211_key_type key_type, int key_id,
  297. const u8 *tsc, gfp_t gfp)
  298. {
  299. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  300. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  301. #ifdef CONFIG_CFG80211_WEXT
  302. union iwreq_data wrqu;
  303. char *buf = kmalloc(128, gfp);
  304. if (buf) {
  305. sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
  306. "keyid=%d %scast addr=%pM)", key_id,
  307. key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
  308. addr);
  309. memset(&wrqu, 0, sizeof(wrqu));
  310. wrqu.data.length = strlen(buf);
  311. wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
  312. kfree(buf);
  313. }
  314. #endif
  315. nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
  316. }
  317. EXPORT_SYMBOL(cfg80211_michael_mic_failure);
  318. /* some MLME handling for userspace SME */
  319. int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  320. struct net_device *dev,
  321. struct ieee80211_channel *chan,
  322. enum nl80211_auth_type auth_type,
  323. const u8 *bssid,
  324. const u8 *ssid, int ssid_len,
  325. const u8 *ie, int ie_len,
  326. const u8 *key, int key_len, int key_idx,
  327. bool local_state_change)
  328. {
  329. struct wireless_dev *wdev = dev->ieee80211_ptr;
  330. struct cfg80211_auth_request req;
  331. struct cfg80211_internal_bss *bss;
  332. int i, err, slot = -1, nfree = 0;
  333. ASSERT_WDEV_LOCK(wdev);
  334. if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  335. if (!key || !key_len || key_idx < 0 || key_idx > 4)
  336. return -EINVAL;
  337. if (wdev->current_bss &&
  338. memcmp(bssid, wdev->current_bss->pub.bssid, ETH_ALEN) == 0)
  339. return -EALREADY;
  340. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  341. if (wdev->authtry_bsses[i] &&
  342. memcmp(bssid, wdev->authtry_bsses[i]->pub.bssid,
  343. ETH_ALEN) == 0)
  344. return -EALREADY;
  345. if (wdev->auth_bsses[i] &&
  346. memcmp(bssid, wdev->auth_bsses[i]->pub.bssid,
  347. ETH_ALEN) == 0)
  348. return -EALREADY;
  349. }
  350. memset(&req, 0, sizeof(req));
  351. req.local_state_change = local_state_change;
  352. req.ie = ie;
  353. req.ie_len = ie_len;
  354. req.auth_type = auth_type;
  355. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  356. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  357. req.key = key;
  358. req.key_len = key_len;
  359. req.key_idx = key_idx;
  360. if (!req.bss)
  361. return -ENOENT;
  362. bss = bss_from_pub(req.bss);
  363. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  364. if (!wdev->auth_bsses[i] && !wdev->authtry_bsses[i]) {
  365. slot = i;
  366. nfree++;
  367. }
  368. }
  369. /* we need one free slot for disassoc and one for this auth */
  370. if (nfree < 2) {
  371. err = -ENOSPC;
  372. goto out;
  373. }
  374. if (local_state_change)
  375. wdev->auth_bsses[slot] = bss;
  376. else
  377. wdev->authtry_bsses[slot] = bss;
  378. cfg80211_hold_bss(bss);
  379. err = rdev->ops->auth(&rdev->wiphy, dev, &req);
  380. if (err) {
  381. if (local_state_change)
  382. wdev->auth_bsses[slot] = NULL;
  383. else
  384. wdev->authtry_bsses[slot] = NULL;
  385. cfg80211_unhold_bss(bss);
  386. }
  387. out:
  388. if (err)
  389. cfg80211_put_bss(req.bss);
  390. return err;
  391. }
  392. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  393. struct net_device *dev, struct ieee80211_channel *chan,
  394. enum nl80211_auth_type auth_type, const u8 *bssid,
  395. const u8 *ssid, int ssid_len,
  396. const u8 *ie, int ie_len,
  397. const u8 *key, int key_len, int key_idx,
  398. bool local_state_change)
  399. {
  400. int err;
  401. wdev_lock(dev->ieee80211_ptr);
  402. err = __cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
  403. ssid, ssid_len, ie, ie_len,
  404. key, key_len, key_idx, local_state_change);
  405. wdev_unlock(dev->ieee80211_ptr);
  406. return err;
  407. }
  408. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  409. struct net_device *dev,
  410. struct ieee80211_channel *chan,
  411. const u8 *bssid, const u8 *prev_bssid,
  412. const u8 *ssid, int ssid_len,
  413. const u8 *ie, int ie_len, bool use_mfp,
  414. struct cfg80211_crypto_settings *crypt)
  415. {
  416. struct wireless_dev *wdev = dev->ieee80211_ptr;
  417. struct cfg80211_assoc_request req;
  418. struct cfg80211_internal_bss *bss;
  419. int i, err, slot = -1;
  420. bool was_connected = false;
  421. ASSERT_WDEV_LOCK(wdev);
  422. memset(&req, 0, sizeof(req));
  423. if (wdev->current_bss && prev_bssid &&
  424. memcmp(wdev->current_bss->pub.bssid, prev_bssid, ETH_ALEN) == 0) {
  425. /*
  426. * Trying to reassociate: Allow this to proceed and let the old
  427. * association to be dropped when the new one is completed.
  428. */
  429. if (wdev->sme_state == CFG80211_SME_CONNECTED) {
  430. was_connected = true;
  431. wdev->sme_state = CFG80211_SME_CONNECTING;
  432. }
  433. } else if (wdev->current_bss)
  434. return -EALREADY;
  435. req.ie = ie;
  436. req.ie_len = ie_len;
  437. memcpy(&req.crypto, crypt, sizeof(req.crypto));
  438. req.use_mfp = use_mfp;
  439. req.prev_bssid = prev_bssid;
  440. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  441. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  442. if (!req.bss) {
  443. if (was_connected)
  444. wdev->sme_state = CFG80211_SME_CONNECTED;
  445. return -ENOENT;
  446. }
  447. bss = bss_from_pub(req.bss);
  448. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  449. if (bss == wdev->auth_bsses[i]) {
  450. slot = i;
  451. break;
  452. }
  453. }
  454. if (slot < 0) {
  455. err = -ENOTCONN;
  456. goto out;
  457. }
  458. err = rdev->ops->assoc(&rdev->wiphy, dev, &req);
  459. out:
  460. if (err && was_connected)
  461. wdev->sme_state = CFG80211_SME_CONNECTED;
  462. /* still a reference in wdev->auth_bsses[slot] */
  463. cfg80211_put_bss(req.bss);
  464. return err;
  465. }
  466. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  467. struct net_device *dev,
  468. struct ieee80211_channel *chan,
  469. const u8 *bssid, const u8 *prev_bssid,
  470. const u8 *ssid, int ssid_len,
  471. const u8 *ie, int ie_len, bool use_mfp,
  472. struct cfg80211_crypto_settings *crypt)
  473. {
  474. struct wireless_dev *wdev = dev->ieee80211_ptr;
  475. int err;
  476. wdev_lock(wdev);
  477. err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
  478. ssid, ssid_len, ie, ie_len, use_mfp, crypt);
  479. wdev_unlock(wdev);
  480. return err;
  481. }
  482. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  483. struct net_device *dev, const u8 *bssid,
  484. const u8 *ie, int ie_len, u16 reason,
  485. bool local_state_change)
  486. {
  487. struct wireless_dev *wdev = dev->ieee80211_ptr;
  488. struct cfg80211_deauth_request req;
  489. int i;
  490. ASSERT_WDEV_LOCK(wdev);
  491. memset(&req, 0, sizeof(req));
  492. req.reason_code = reason;
  493. req.local_state_change = local_state_change;
  494. req.ie = ie;
  495. req.ie_len = ie_len;
  496. if (wdev->current_bss &&
  497. memcmp(wdev->current_bss->pub.bssid, bssid, ETH_ALEN) == 0) {
  498. req.bss = &wdev->current_bss->pub;
  499. } else for (i = 0; i < MAX_AUTH_BSSES; i++) {
  500. if (wdev->auth_bsses[i] &&
  501. memcmp(bssid, wdev->auth_bsses[i]->pub.bssid, ETH_ALEN) == 0) {
  502. req.bss = &wdev->auth_bsses[i]->pub;
  503. break;
  504. }
  505. if (wdev->authtry_bsses[i] &&
  506. memcmp(bssid, wdev->authtry_bsses[i]->pub.bssid, ETH_ALEN) == 0) {
  507. req.bss = &wdev->authtry_bsses[i]->pub;
  508. break;
  509. }
  510. }
  511. if (!req.bss)
  512. return -ENOTCONN;
  513. return rdev->ops->deauth(&rdev->wiphy, dev, &req, wdev);
  514. }
  515. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  516. struct net_device *dev, const u8 *bssid,
  517. const u8 *ie, int ie_len, u16 reason,
  518. bool local_state_change)
  519. {
  520. struct wireless_dev *wdev = dev->ieee80211_ptr;
  521. int err;
  522. wdev_lock(wdev);
  523. err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
  524. local_state_change);
  525. wdev_unlock(wdev);
  526. return err;
  527. }
  528. static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  529. struct net_device *dev, const u8 *bssid,
  530. const u8 *ie, int ie_len, u16 reason,
  531. bool local_state_change)
  532. {
  533. struct wireless_dev *wdev = dev->ieee80211_ptr;
  534. struct cfg80211_disassoc_request req;
  535. ASSERT_WDEV_LOCK(wdev);
  536. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  537. return -ENOTCONN;
  538. if (WARN_ON(!wdev->current_bss))
  539. return -ENOTCONN;
  540. memset(&req, 0, sizeof(req));
  541. req.reason_code = reason;
  542. req.local_state_change = local_state_change;
  543. req.ie = ie;
  544. req.ie_len = ie_len;
  545. if (memcmp(wdev->current_bss->pub.bssid, bssid, ETH_ALEN) == 0)
  546. req.bss = &wdev->current_bss->pub;
  547. else
  548. return -ENOTCONN;
  549. return rdev->ops->disassoc(&rdev->wiphy, dev, &req, wdev);
  550. }
  551. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  552. struct net_device *dev, const u8 *bssid,
  553. const u8 *ie, int ie_len, u16 reason,
  554. bool local_state_change)
  555. {
  556. struct wireless_dev *wdev = dev->ieee80211_ptr;
  557. int err;
  558. wdev_lock(wdev);
  559. err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
  560. local_state_change);
  561. wdev_unlock(wdev);
  562. return err;
  563. }
  564. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  565. struct net_device *dev)
  566. {
  567. struct wireless_dev *wdev = dev->ieee80211_ptr;
  568. struct cfg80211_deauth_request req;
  569. int i;
  570. ASSERT_WDEV_LOCK(wdev);
  571. if (!rdev->ops->deauth)
  572. return;
  573. memset(&req, 0, sizeof(req));
  574. req.reason_code = WLAN_REASON_DEAUTH_LEAVING;
  575. req.ie = NULL;
  576. req.ie_len = 0;
  577. if (wdev->current_bss) {
  578. req.bss = &wdev->current_bss->pub;
  579. rdev->ops->deauth(&rdev->wiphy, dev, &req, wdev);
  580. if (wdev->current_bss) {
  581. cfg80211_unhold_bss(wdev->current_bss);
  582. cfg80211_put_bss(&wdev->current_bss->pub);
  583. wdev->current_bss = NULL;
  584. }
  585. }
  586. for (i = 0; i < MAX_AUTH_BSSES; i++) {
  587. if (wdev->auth_bsses[i]) {
  588. req.bss = &wdev->auth_bsses[i]->pub;
  589. rdev->ops->deauth(&rdev->wiphy, dev, &req, wdev);
  590. if (wdev->auth_bsses[i]) {
  591. cfg80211_unhold_bss(wdev->auth_bsses[i]);
  592. cfg80211_put_bss(&wdev->auth_bsses[i]->pub);
  593. wdev->auth_bsses[i] = NULL;
  594. }
  595. }
  596. if (wdev->authtry_bsses[i]) {
  597. req.bss = &wdev->authtry_bsses[i]->pub;
  598. rdev->ops->deauth(&rdev->wiphy, dev, &req, wdev);
  599. if (wdev->authtry_bsses[i]) {
  600. cfg80211_unhold_bss(wdev->authtry_bsses[i]);
  601. cfg80211_put_bss(&wdev->authtry_bsses[i]->pub);
  602. wdev->authtry_bsses[i] = NULL;
  603. }
  604. }
  605. }
  606. }
  607. void cfg80211_ready_on_channel(struct net_device *dev, u64 cookie,
  608. struct ieee80211_channel *chan,
  609. enum nl80211_channel_type channel_type,
  610. unsigned int duration, gfp_t gfp)
  611. {
  612. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  613. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  614. nl80211_send_remain_on_channel(rdev, dev, cookie, chan, channel_type,
  615. duration, gfp);
  616. }
  617. EXPORT_SYMBOL(cfg80211_ready_on_channel);
  618. void cfg80211_remain_on_channel_expired(struct net_device *dev,
  619. u64 cookie,
  620. struct ieee80211_channel *chan,
  621. enum nl80211_channel_type channel_type,
  622. gfp_t gfp)
  623. {
  624. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  625. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  626. nl80211_send_remain_on_channel_cancel(rdev, dev, cookie, chan,
  627. channel_type, gfp);
  628. }
  629. EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
  630. void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
  631. struct station_info *sinfo, gfp_t gfp)
  632. {
  633. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  634. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  635. nl80211_send_sta_event(rdev, dev, mac_addr, sinfo, gfp);
  636. }
  637. EXPORT_SYMBOL(cfg80211_new_sta);
  638. struct cfg80211_mgmt_registration {
  639. struct list_head list;
  640. u32 nlpid;
  641. int match_len;
  642. __le16 frame_type;
  643. u8 match[];
  644. };
  645. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
  646. u16 frame_type, const u8 *match_data,
  647. int match_len)
  648. {
  649. struct cfg80211_mgmt_registration *reg, *nreg;
  650. int err = 0;
  651. u16 mgmt_type;
  652. if (!wdev->wiphy->mgmt_stypes)
  653. return -EOPNOTSUPP;
  654. if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
  655. return -EINVAL;
  656. if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
  657. return -EINVAL;
  658. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  659. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
  660. return -EINVAL;
  661. nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
  662. if (!nreg)
  663. return -ENOMEM;
  664. spin_lock_bh(&wdev->mgmt_registrations_lock);
  665. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  666. int mlen = min(match_len, reg->match_len);
  667. if (frame_type != le16_to_cpu(reg->frame_type))
  668. continue;
  669. if (memcmp(reg->match, match_data, mlen) == 0) {
  670. err = -EALREADY;
  671. break;
  672. }
  673. }
  674. if (err) {
  675. kfree(nreg);
  676. goto out;
  677. }
  678. memcpy(nreg->match, match_data, match_len);
  679. nreg->match_len = match_len;
  680. nreg->nlpid = snd_pid;
  681. nreg->frame_type = cpu_to_le16(frame_type);
  682. list_add(&nreg->list, &wdev->mgmt_registrations);
  683. out:
  684. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  685. return err;
  686. }
  687. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid)
  688. {
  689. struct cfg80211_mgmt_registration *reg, *tmp;
  690. spin_lock_bh(&wdev->mgmt_registrations_lock);
  691. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  692. if (reg->nlpid == nlpid) {
  693. list_del(&reg->list);
  694. kfree(reg);
  695. }
  696. }
  697. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  698. }
  699. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
  700. {
  701. struct cfg80211_mgmt_registration *reg, *tmp;
  702. spin_lock_bh(&wdev->mgmt_registrations_lock);
  703. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  704. list_del(&reg->list);
  705. kfree(reg);
  706. }
  707. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  708. }
  709. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  710. struct net_device *dev,
  711. struct ieee80211_channel *chan,
  712. enum nl80211_channel_type channel_type,
  713. bool channel_type_valid,
  714. const u8 *buf, size_t len, u64 *cookie)
  715. {
  716. struct wireless_dev *wdev = dev->ieee80211_ptr;
  717. const struct ieee80211_mgmt *mgmt;
  718. u16 stype;
  719. if (!wdev->wiphy->mgmt_stypes)
  720. return -EOPNOTSUPP;
  721. if (!rdev->ops->mgmt_tx)
  722. return -EOPNOTSUPP;
  723. if (len < 24 + 1)
  724. return -EINVAL;
  725. mgmt = (const struct ieee80211_mgmt *) buf;
  726. if (!ieee80211_is_mgmt(mgmt->frame_control))
  727. return -EINVAL;
  728. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  729. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
  730. return -EINVAL;
  731. if (ieee80211_is_action(mgmt->frame_control) &&
  732. mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
  733. int err = 0;
  734. wdev_lock(wdev);
  735. switch (wdev->iftype) {
  736. case NL80211_IFTYPE_ADHOC:
  737. case NL80211_IFTYPE_STATION:
  738. case NL80211_IFTYPE_P2P_CLIENT:
  739. if (!wdev->current_bss) {
  740. err = -ENOTCONN;
  741. break;
  742. }
  743. if (memcmp(wdev->current_bss->pub.bssid,
  744. mgmt->bssid, ETH_ALEN)) {
  745. err = -ENOTCONN;
  746. break;
  747. }
  748. /*
  749. * check for IBSS DA must be done by driver as
  750. * cfg80211 doesn't track the stations
  751. */
  752. if (wdev->iftype == NL80211_IFTYPE_ADHOC)
  753. break;
  754. /* for station, check that DA is the AP */
  755. if (memcmp(wdev->current_bss->pub.bssid,
  756. mgmt->da, ETH_ALEN)) {
  757. err = -ENOTCONN;
  758. break;
  759. }
  760. break;
  761. case NL80211_IFTYPE_AP:
  762. case NL80211_IFTYPE_P2P_GO:
  763. case NL80211_IFTYPE_AP_VLAN:
  764. if (memcmp(mgmt->bssid, dev->dev_addr, ETH_ALEN))
  765. err = -EINVAL;
  766. break;
  767. default:
  768. err = -EOPNOTSUPP;
  769. break;
  770. }
  771. wdev_unlock(wdev);
  772. if (err)
  773. return err;
  774. }
  775. if (memcmp(mgmt->sa, dev->dev_addr, ETH_ALEN) != 0)
  776. return -EINVAL;
  777. /* Transmit the Action frame as requested by user space */
  778. return rdev->ops->mgmt_tx(&rdev->wiphy, dev, chan, channel_type,
  779. channel_type_valid, buf, len, cookie);
  780. }
  781. bool cfg80211_rx_mgmt(struct net_device *dev, int freq, const u8 *buf,
  782. size_t len, gfp_t gfp)
  783. {
  784. struct wireless_dev *wdev = dev->ieee80211_ptr;
  785. struct wiphy *wiphy = wdev->wiphy;
  786. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  787. struct cfg80211_mgmt_registration *reg;
  788. const struct ieee80211_txrx_stypes *stypes =
  789. &wiphy->mgmt_stypes[wdev->iftype];
  790. struct ieee80211_mgmt *mgmt = (void *)buf;
  791. const u8 *data;
  792. int data_len;
  793. bool result = false;
  794. __le16 ftype = mgmt->frame_control &
  795. cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
  796. u16 stype;
  797. stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
  798. if (!(stypes->rx & BIT(stype)))
  799. return false;
  800. data = buf + ieee80211_hdrlen(mgmt->frame_control);
  801. data_len = len - ieee80211_hdrlen(mgmt->frame_control);
  802. spin_lock_bh(&wdev->mgmt_registrations_lock);
  803. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  804. if (reg->frame_type != ftype)
  805. continue;
  806. if (reg->match_len > data_len)
  807. continue;
  808. if (memcmp(reg->match, data, reg->match_len))
  809. continue;
  810. /* found match! */
  811. /* Indicate the received Action frame to user space */
  812. if (nl80211_send_mgmt(rdev, dev, reg->nlpid, freq,
  813. buf, len, gfp))
  814. continue;
  815. result = true;
  816. break;
  817. }
  818. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  819. return result;
  820. }
  821. EXPORT_SYMBOL(cfg80211_rx_mgmt);
  822. void cfg80211_mgmt_tx_status(struct net_device *dev, u64 cookie,
  823. const u8 *buf, size_t len, bool ack, gfp_t gfp)
  824. {
  825. struct wireless_dev *wdev = dev->ieee80211_ptr;
  826. struct wiphy *wiphy = wdev->wiphy;
  827. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  828. /* Indicate TX status of the Action frame to user space */
  829. nl80211_send_mgmt_tx_status(rdev, dev, cookie, buf, len, ack, gfp);
  830. }
  831. EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
  832. void cfg80211_cqm_rssi_notify(struct net_device *dev,
  833. enum nl80211_cqm_rssi_threshold_event rssi_event,
  834. gfp_t gfp)
  835. {
  836. struct wireless_dev *wdev = dev->ieee80211_ptr;
  837. struct wiphy *wiphy = wdev->wiphy;
  838. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  839. /* Indicate roaming trigger event to user space */
  840. nl80211_send_cqm_rssi_notify(rdev, dev, rssi_event, gfp);
  841. }
  842. EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);