mlme.c 30 KB

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