mlme.c 28 KB

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