mlme.c 27 KB

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