mlme.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 wiphy *wiphy = wdev->wiphy;
  650. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  651. struct cfg80211_mgmt_registration *reg, *nreg;
  652. int err = 0;
  653. u16 mgmt_type;
  654. if (!wdev->wiphy->mgmt_stypes)
  655. return -EOPNOTSUPP;
  656. if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
  657. return -EINVAL;
  658. if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
  659. return -EINVAL;
  660. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  661. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
  662. return -EINVAL;
  663. nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
  664. if (!nreg)
  665. return -ENOMEM;
  666. spin_lock_bh(&wdev->mgmt_registrations_lock);
  667. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  668. int mlen = min(match_len, reg->match_len);
  669. if (frame_type != le16_to_cpu(reg->frame_type))
  670. continue;
  671. if (memcmp(reg->match, match_data, mlen) == 0) {
  672. err = -EALREADY;
  673. break;
  674. }
  675. }
  676. if (err) {
  677. kfree(nreg);
  678. goto out;
  679. }
  680. memcpy(nreg->match, match_data, match_len);
  681. nreg->match_len = match_len;
  682. nreg->nlpid = snd_pid;
  683. nreg->frame_type = cpu_to_le16(frame_type);
  684. list_add(&nreg->list, &wdev->mgmt_registrations);
  685. if (rdev->ops->mgmt_frame_register)
  686. rdev->ops->mgmt_frame_register(wiphy, wdev->netdev,
  687. frame_type, true);
  688. out:
  689. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  690. return err;
  691. }
  692. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid)
  693. {
  694. struct wiphy *wiphy = wdev->wiphy;
  695. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  696. struct cfg80211_mgmt_registration *reg, *tmp;
  697. spin_lock_bh(&wdev->mgmt_registrations_lock);
  698. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  699. if (reg->nlpid != nlpid)
  700. continue;
  701. if (rdev->ops->mgmt_frame_register) {
  702. u16 frame_type = le16_to_cpu(reg->frame_type);
  703. rdev->ops->mgmt_frame_register(wiphy, wdev->netdev,
  704. frame_type, false);
  705. }
  706. list_del(&reg->list);
  707. kfree(reg);
  708. }
  709. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  710. }
  711. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
  712. {
  713. struct cfg80211_mgmt_registration *reg, *tmp;
  714. spin_lock_bh(&wdev->mgmt_registrations_lock);
  715. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  716. list_del(&reg->list);
  717. kfree(reg);
  718. }
  719. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  720. }
  721. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  722. struct net_device *dev,
  723. struct ieee80211_channel *chan,
  724. enum nl80211_channel_type channel_type,
  725. bool channel_type_valid,
  726. const u8 *buf, size_t len, u64 *cookie)
  727. {
  728. struct wireless_dev *wdev = dev->ieee80211_ptr;
  729. const struct ieee80211_mgmt *mgmt;
  730. u16 stype;
  731. if (!wdev->wiphy->mgmt_stypes)
  732. return -EOPNOTSUPP;
  733. if (!rdev->ops->mgmt_tx)
  734. return -EOPNOTSUPP;
  735. if (len < 24 + 1)
  736. return -EINVAL;
  737. mgmt = (const struct ieee80211_mgmt *) buf;
  738. if (!ieee80211_is_mgmt(mgmt->frame_control))
  739. return -EINVAL;
  740. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  741. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
  742. return -EINVAL;
  743. if (ieee80211_is_action(mgmt->frame_control) &&
  744. mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
  745. int err = 0;
  746. wdev_lock(wdev);
  747. switch (wdev->iftype) {
  748. case NL80211_IFTYPE_ADHOC:
  749. case NL80211_IFTYPE_STATION:
  750. case NL80211_IFTYPE_P2P_CLIENT:
  751. if (!wdev->current_bss) {
  752. err = -ENOTCONN;
  753. break;
  754. }
  755. if (memcmp(wdev->current_bss->pub.bssid,
  756. mgmt->bssid, ETH_ALEN)) {
  757. err = -ENOTCONN;
  758. break;
  759. }
  760. /*
  761. * check for IBSS DA must be done by driver as
  762. * cfg80211 doesn't track the stations
  763. */
  764. if (wdev->iftype == NL80211_IFTYPE_ADHOC)
  765. break;
  766. /* for station, check that DA is the AP */
  767. if (memcmp(wdev->current_bss->pub.bssid,
  768. mgmt->da, ETH_ALEN)) {
  769. err = -ENOTCONN;
  770. break;
  771. }
  772. break;
  773. case NL80211_IFTYPE_AP:
  774. case NL80211_IFTYPE_P2P_GO:
  775. case NL80211_IFTYPE_AP_VLAN:
  776. if (memcmp(mgmt->bssid, dev->dev_addr, ETH_ALEN))
  777. err = -EINVAL;
  778. break;
  779. default:
  780. err = -EOPNOTSUPP;
  781. break;
  782. }
  783. wdev_unlock(wdev);
  784. if (err)
  785. return err;
  786. }
  787. if (memcmp(mgmt->sa, dev->dev_addr, ETH_ALEN) != 0)
  788. return -EINVAL;
  789. /* Transmit the Action frame as requested by user space */
  790. return rdev->ops->mgmt_tx(&rdev->wiphy, dev, chan, channel_type,
  791. channel_type_valid, buf, len, cookie);
  792. }
  793. bool cfg80211_rx_mgmt(struct net_device *dev, int freq, const u8 *buf,
  794. size_t len, gfp_t gfp)
  795. {
  796. struct wireless_dev *wdev = dev->ieee80211_ptr;
  797. struct wiphy *wiphy = wdev->wiphy;
  798. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  799. struct cfg80211_mgmt_registration *reg;
  800. const struct ieee80211_txrx_stypes *stypes =
  801. &wiphy->mgmt_stypes[wdev->iftype];
  802. struct ieee80211_mgmt *mgmt = (void *)buf;
  803. const u8 *data;
  804. int data_len;
  805. bool result = false;
  806. __le16 ftype = mgmt->frame_control &
  807. cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
  808. u16 stype;
  809. stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
  810. if (!(stypes->rx & BIT(stype)))
  811. return false;
  812. data = buf + ieee80211_hdrlen(mgmt->frame_control);
  813. data_len = len - ieee80211_hdrlen(mgmt->frame_control);
  814. spin_lock_bh(&wdev->mgmt_registrations_lock);
  815. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  816. if (reg->frame_type != ftype)
  817. continue;
  818. if (reg->match_len > data_len)
  819. continue;
  820. if (memcmp(reg->match, data, reg->match_len))
  821. continue;
  822. /* found match! */
  823. /* Indicate the received Action frame to user space */
  824. if (nl80211_send_mgmt(rdev, dev, reg->nlpid, freq,
  825. buf, len, gfp))
  826. continue;
  827. result = true;
  828. break;
  829. }
  830. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  831. return result;
  832. }
  833. EXPORT_SYMBOL(cfg80211_rx_mgmt);
  834. void cfg80211_mgmt_tx_status(struct net_device *dev, u64 cookie,
  835. const u8 *buf, size_t len, bool ack, gfp_t gfp)
  836. {
  837. struct wireless_dev *wdev = dev->ieee80211_ptr;
  838. struct wiphy *wiphy = wdev->wiphy;
  839. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  840. /* Indicate TX status of the Action frame to user space */
  841. nl80211_send_mgmt_tx_status(rdev, dev, cookie, buf, len, ack, gfp);
  842. }
  843. EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
  844. void cfg80211_cqm_rssi_notify(struct net_device *dev,
  845. enum nl80211_cqm_rssi_threshold_event rssi_event,
  846. gfp_t gfp)
  847. {
  848. struct wireless_dev *wdev = dev->ieee80211_ptr;
  849. struct wiphy *wiphy = wdev->wiphy;
  850. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  851. /* Indicate roaming trigger event to user space */
  852. nl80211_send_cqm_rssi_notify(rdev, dev, rssi_event, gfp);
  853. }
  854. EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);