mlme.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. compare_ether_addr(wdev->current_bss->pub.bssid, bssid) == 0) {
  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 = compare_ether_addr(mgmt->sa, dev->dev_addr) != 0;
  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. compare_ether_addr(wdev->current_bss->pub.bssid, bssid) == 0) {
  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 = compare_ether_addr(mgmt->sa, dev->dev_addr) != 0;
  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. compare_ether_addr(bssid, wdev->current_bss->pub.bssid) == 0)
  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 = rdev->ops->auth(&rdev->wiphy, dev, &req);
  252. cfg80211_put_bss(req.bss);
  253. return err;
  254. }
  255. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  256. struct net_device *dev, struct ieee80211_channel *chan,
  257. enum nl80211_auth_type auth_type, const u8 *bssid,
  258. const u8 *ssid, int ssid_len,
  259. const u8 *ie, int ie_len,
  260. const u8 *key, int key_len, int key_idx)
  261. {
  262. int err;
  263. wdev_lock(dev->ieee80211_ptr);
  264. err = __cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
  265. ssid, ssid_len, ie, ie_len,
  266. key, key_len, key_idx);
  267. wdev_unlock(dev->ieee80211_ptr);
  268. return err;
  269. }
  270. /* Do a logical ht_capa &= ht_capa_mask. */
  271. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  272. const struct ieee80211_ht_cap *ht_capa_mask)
  273. {
  274. int i;
  275. u8 *p1, *p2;
  276. if (!ht_capa_mask) {
  277. memset(ht_capa, 0, sizeof(*ht_capa));
  278. return;
  279. }
  280. p1 = (u8*)(ht_capa);
  281. p2 = (u8*)(ht_capa_mask);
  282. for (i = 0; i<sizeof(*ht_capa); i++)
  283. p1[i] &= p2[i];
  284. }
  285. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  286. struct net_device *dev,
  287. struct ieee80211_channel *chan,
  288. const u8 *bssid, const u8 *prev_bssid,
  289. const u8 *ssid, int ssid_len,
  290. const u8 *ie, int ie_len, bool use_mfp,
  291. struct cfg80211_crypto_settings *crypt,
  292. u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
  293. struct ieee80211_ht_cap *ht_capa_mask)
  294. {
  295. struct wireless_dev *wdev = dev->ieee80211_ptr;
  296. struct cfg80211_assoc_request req;
  297. int err;
  298. bool was_connected = false;
  299. ASSERT_WDEV_LOCK(wdev);
  300. memset(&req, 0, sizeof(req));
  301. if (wdev->current_bss && prev_bssid &&
  302. compare_ether_addr(wdev->current_bss->pub.bssid, prev_bssid) == 0) {
  303. /*
  304. * Trying to reassociate: Allow this to proceed and let the old
  305. * association to be dropped when the new one is completed.
  306. */
  307. if (wdev->sme_state == CFG80211_SME_CONNECTED) {
  308. was_connected = true;
  309. wdev->sme_state = CFG80211_SME_CONNECTING;
  310. }
  311. } else if (wdev->current_bss)
  312. return -EALREADY;
  313. req.ie = ie;
  314. req.ie_len = ie_len;
  315. memcpy(&req.crypto, crypt, sizeof(req.crypto));
  316. req.use_mfp = use_mfp;
  317. req.prev_bssid = prev_bssid;
  318. req.flags = assoc_flags;
  319. if (ht_capa)
  320. memcpy(&req.ht_capa, ht_capa, sizeof(req.ht_capa));
  321. if (ht_capa_mask)
  322. memcpy(&req.ht_capa_mask, ht_capa_mask,
  323. sizeof(req.ht_capa_mask));
  324. cfg80211_oper_and_ht_capa(&req.ht_capa_mask,
  325. rdev->wiphy.ht_capa_mod_mask);
  326. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  327. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  328. if (!req.bss) {
  329. if (was_connected)
  330. wdev->sme_state = CFG80211_SME_CONNECTED;
  331. return -ENOENT;
  332. }
  333. err = rdev->ops->assoc(&rdev->wiphy, dev, &req);
  334. if (err) {
  335. if (was_connected)
  336. wdev->sme_state = CFG80211_SME_CONNECTED;
  337. cfg80211_put_bss(req.bss);
  338. }
  339. return err;
  340. }
  341. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  342. struct net_device *dev,
  343. struct ieee80211_channel *chan,
  344. const u8 *bssid, const u8 *prev_bssid,
  345. const u8 *ssid, int ssid_len,
  346. const u8 *ie, int ie_len, bool use_mfp,
  347. struct cfg80211_crypto_settings *crypt,
  348. u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
  349. struct ieee80211_ht_cap *ht_capa_mask)
  350. {
  351. struct wireless_dev *wdev = dev->ieee80211_ptr;
  352. int err;
  353. wdev_lock(wdev);
  354. err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
  355. ssid, ssid_len, ie, ie_len, use_mfp, crypt,
  356. assoc_flags, ht_capa, ht_capa_mask);
  357. wdev_unlock(wdev);
  358. return err;
  359. }
  360. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  361. struct net_device *dev, const u8 *bssid,
  362. const u8 *ie, int ie_len, u16 reason,
  363. bool local_state_change)
  364. {
  365. struct wireless_dev *wdev = dev->ieee80211_ptr;
  366. struct cfg80211_deauth_request req = {
  367. .bssid = bssid,
  368. .reason_code = reason,
  369. .ie = ie,
  370. .ie_len = ie_len,
  371. };
  372. ASSERT_WDEV_LOCK(wdev);
  373. if (local_state_change) {
  374. if (wdev->current_bss &&
  375. compare_ether_addr(wdev->current_bss->pub.bssid, bssid)
  376. == 0) {
  377. cfg80211_unhold_bss(wdev->current_bss);
  378. cfg80211_put_bss(&wdev->current_bss->pub);
  379. wdev->current_bss = NULL;
  380. }
  381. return 0;
  382. }
  383. return rdev->ops->deauth(&rdev->wiphy, dev, &req);
  384. }
  385. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  386. struct net_device *dev, const u8 *bssid,
  387. const u8 *ie, int ie_len, u16 reason,
  388. bool local_state_change)
  389. {
  390. struct wireless_dev *wdev = dev->ieee80211_ptr;
  391. int err;
  392. wdev_lock(wdev);
  393. err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
  394. local_state_change);
  395. wdev_unlock(wdev);
  396. return err;
  397. }
  398. static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  399. struct net_device *dev, const u8 *bssid,
  400. const u8 *ie, int ie_len, u16 reason,
  401. bool local_state_change)
  402. {
  403. struct wireless_dev *wdev = dev->ieee80211_ptr;
  404. struct cfg80211_disassoc_request req;
  405. ASSERT_WDEV_LOCK(wdev);
  406. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  407. return -ENOTCONN;
  408. if (WARN_ON(!wdev->current_bss))
  409. return -ENOTCONN;
  410. memset(&req, 0, sizeof(req));
  411. req.reason_code = reason;
  412. req.local_state_change = local_state_change;
  413. req.ie = ie;
  414. req.ie_len = ie_len;
  415. if (compare_ether_addr(wdev->current_bss->pub.bssid, bssid) == 0)
  416. req.bss = &wdev->current_bss->pub;
  417. else
  418. return -ENOTCONN;
  419. return rdev->ops->disassoc(&rdev->wiphy, dev, &req);
  420. }
  421. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  422. struct net_device *dev, const u8 *bssid,
  423. const u8 *ie, int ie_len, u16 reason,
  424. bool local_state_change)
  425. {
  426. struct wireless_dev *wdev = dev->ieee80211_ptr;
  427. int err;
  428. wdev_lock(wdev);
  429. err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
  430. local_state_change);
  431. wdev_unlock(wdev);
  432. return err;
  433. }
  434. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  435. struct net_device *dev)
  436. {
  437. struct wireless_dev *wdev = dev->ieee80211_ptr;
  438. struct cfg80211_deauth_request req;
  439. u8 bssid[ETH_ALEN];
  440. ASSERT_WDEV_LOCK(wdev);
  441. if (!rdev->ops->deauth)
  442. return;
  443. memset(&req, 0, sizeof(req));
  444. req.reason_code = WLAN_REASON_DEAUTH_LEAVING;
  445. req.ie = NULL;
  446. req.ie_len = 0;
  447. if (!wdev->current_bss)
  448. return;
  449. memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
  450. req.bssid = bssid;
  451. rdev->ops->deauth(&rdev->wiphy, dev, &req);
  452. if (wdev->current_bss) {
  453. cfg80211_unhold_bss(wdev->current_bss);
  454. cfg80211_put_bss(&wdev->current_bss->pub);
  455. wdev->current_bss = NULL;
  456. }
  457. }
  458. void cfg80211_ready_on_channel(struct net_device *dev, u64 cookie,
  459. struct ieee80211_channel *chan,
  460. enum nl80211_channel_type channel_type,
  461. unsigned int duration, gfp_t gfp)
  462. {
  463. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  464. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  465. nl80211_send_remain_on_channel(rdev, dev, cookie, chan, channel_type,
  466. duration, gfp);
  467. }
  468. EXPORT_SYMBOL(cfg80211_ready_on_channel);
  469. void cfg80211_remain_on_channel_expired(struct net_device *dev,
  470. u64 cookie,
  471. struct ieee80211_channel *chan,
  472. enum nl80211_channel_type channel_type,
  473. gfp_t gfp)
  474. {
  475. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  476. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  477. nl80211_send_remain_on_channel_cancel(rdev, dev, cookie, chan,
  478. channel_type, gfp);
  479. }
  480. EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
  481. void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
  482. struct station_info *sinfo, gfp_t gfp)
  483. {
  484. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  485. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  486. nl80211_send_sta_event(rdev, dev, mac_addr, sinfo, gfp);
  487. }
  488. EXPORT_SYMBOL(cfg80211_new_sta);
  489. void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, 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_del_event(rdev, dev, mac_addr, gfp);
  494. }
  495. EXPORT_SYMBOL(cfg80211_del_sta);
  496. struct cfg80211_mgmt_registration {
  497. struct list_head list;
  498. u32 nlpid;
  499. int match_len;
  500. __le16 frame_type;
  501. u8 match[];
  502. };
  503. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
  504. u16 frame_type, const u8 *match_data,
  505. int match_len)
  506. {
  507. struct wiphy *wiphy = wdev->wiphy;
  508. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  509. struct cfg80211_mgmt_registration *reg, *nreg;
  510. int err = 0;
  511. u16 mgmt_type;
  512. if (!wdev->wiphy->mgmt_stypes)
  513. return -EOPNOTSUPP;
  514. if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
  515. return -EINVAL;
  516. if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
  517. return -EINVAL;
  518. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  519. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
  520. return -EINVAL;
  521. nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
  522. if (!nreg)
  523. return -ENOMEM;
  524. spin_lock_bh(&wdev->mgmt_registrations_lock);
  525. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  526. int mlen = min(match_len, reg->match_len);
  527. if (frame_type != le16_to_cpu(reg->frame_type))
  528. continue;
  529. if (memcmp(reg->match, match_data, mlen) == 0) {
  530. err = -EALREADY;
  531. break;
  532. }
  533. }
  534. if (err) {
  535. kfree(nreg);
  536. goto out;
  537. }
  538. memcpy(nreg->match, match_data, match_len);
  539. nreg->match_len = match_len;
  540. nreg->nlpid = snd_pid;
  541. nreg->frame_type = cpu_to_le16(frame_type);
  542. list_add(&nreg->list, &wdev->mgmt_registrations);
  543. if (rdev->ops->mgmt_frame_register)
  544. rdev->ops->mgmt_frame_register(wiphy, wdev->netdev,
  545. frame_type, true);
  546. out:
  547. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  548. return err;
  549. }
  550. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid)
  551. {
  552. struct wiphy *wiphy = wdev->wiphy;
  553. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  554. struct cfg80211_mgmt_registration *reg, *tmp;
  555. spin_lock_bh(&wdev->mgmt_registrations_lock);
  556. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  557. if (reg->nlpid != nlpid)
  558. continue;
  559. if (rdev->ops->mgmt_frame_register) {
  560. u16 frame_type = le16_to_cpu(reg->frame_type);
  561. rdev->ops->mgmt_frame_register(wiphy, wdev->netdev,
  562. frame_type, false);
  563. }
  564. list_del(&reg->list);
  565. kfree(reg);
  566. }
  567. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  568. if (nlpid == wdev->ap_unexpected_nlpid)
  569. wdev->ap_unexpected_nlpid = 0;
  570. }
  571. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
  572. {
  573. struct cfg80211_mgmt_registration *reg, *tmp;
  574. spin_lock_bh(&wdev->mgmt_registrations_lock);
  575. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  576. list_del(&reg->list);
  577. kfree(reg);
  578. }
  579. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  580. }
  581. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  582. struct net_device *dev,
  583. struct ieee80211_channel *chan, bool offchan,
  584. enum nl80211_channel_type channel_type,
  585. bool channel_type_valid, unsigned int wait,
  586. const u8 *buf, size_t len, bool no_cck,
  587. bool dont_wait_for_ack, u64 *cookie)
  588. {
  589. struct wireless_dev *wdev = dev->ieee80211_ptr;
  590. const struct ieee80211_mgmt *mgmt;
  591. u16 stype;
  592. if (!wdev->wiphy->mgmt_stypes)
  593. return -EOPNOTSUPP;
  594. if (!rdev->ops->mgmt_tx)
  595. return -EOPNOTSUPP;
  596. if (len < 24 + 1)
  597. return -EINVAL;
  598. mgmt = (const struct ieee80211_mgmt *) buf;
  599. if (!ieee80211_is_mgmt(mgmt->frame_control))
  600. return -EINVAL;
  601. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  602. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
  603. return -EINVAL;
  604. if (ieee80211_is_action(mgmt->frame_control) &&
  605. mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
  606. int err = 0;
  607. wdev_lock(wdev);
  608. switch (wdev->iftype) {
  609. case NL80211_IFTYPE_ADHOC:
  610. case NL80211_IFTYPE_STATION:
  611. case NL80211_IFTYPE_P2P_CLIENT:
  612. if (!wdev->current_bss) {
  613. err = -ENOTCONN;
  614. break;
  615. }
  616. if (compare_ether_addr(wdev->current_bss->pub.bssid,
  617. mgmt->bssid)) {
  618. err = -ENOTCONN;
  619. break;
  620. }
  621. /*
  622. * check for IBSS DA must be done by driver as
  623. * cfg80211 doesn't track the stations
  624. */
  625. if (wdev->iftype == NL80211_IFTYPE_ADHOC)
  626. break;
  627. /* for station, check that DA is the AP */
  628. if (compare_ether_addr(wdev->current_bss->pub.bssid,
  629. mgmt->da)) {
  630. err = -ENOTCONN;
  631. break;
  632. }
  633. break;
  634. case NL80211_IFTYPE_AP:
  635. case NL80211_IFTYPE_P2P_GO:
  636. case NL80211_IFTYPE_AP_VLAN:
  637. if (compare_ether_addr(mgmt->bssid, dev->dev_addr))
  638. err = -EINVAL;
  639. break;
  640. case NL80211_IFTYPE_MESH_POINT:
  641. if (compare_ether_addr(mgmt->sa, mgmt->bssid)) {
  642. err = -EINVAL;
  643. break;
  644. }
  645. /*
  646. * check for mesh DA must be done by driver as
  647. * cfg80211 doesn't track the stations
  648. */
  649. break;
  650. default:
  651. err = -EOPNOTSUPP;
  652. break;
  653. }
  654. wdev_unlock(wdev);
  655. if (err)
  656. return err;
  657. }
  658. if (compare_ether_addr(mgmt->sa, dev->dev_addr) != 0)
  659. return -EINVAL;
  660. /* Transmit the Action frame as requested by user space */
  661. return rdev->ops->mgmt_tx(&rdev->wiphy, dev, chan, offchan,
  662. channel_type, channel_type_valid,
  663. wait, buf, len, no_cck, dont_wait_for_ack,
  664. cookie);
  665. }
  666. bool cfg80211_rx_mgmt(struct net_device *dev, int freq, int sig_mbm,
  667. const u8 *buf, size_t len, gfp_t gfp)
  668. {
  669. struct wireless_dev *wdev = dev->ieee80211_ptr;
  670. struct wiphy *wiphy = wdev->wiphy;
  671. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  672. struct cfg80211_mgmt_registration *reg;
  673. const struct ieee80211_txrx_stypes *stypes =
  674. &wiphy->mgmt_stypes[wdev->iftype];
  675. struct ieee80211_mgmt *mgmt = (void *)buf;
  676. const u8 *data;
  677. int data_len;
  678. bool result = false;
  679. __le16 ftype = mgmt->frame_control &
  680. cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
  681. u16 stype;
  682. stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
  683. if (!(stypes->rx & BIT(stype)))
  684. return false;
  685. data = buf + ieee80211_hdrlen(mgmt->frame_control);
  686. data_len = len - ieee80211_hdrlen(mgmt->frame_control);
  687. spin_lock_bh(&wdev->mgmt_registrations_lock);
  688. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  689. if (reg->frame_type != ftype)
  690. continue;
  691. if (reg->match_len > data_len)
  692. continue;
  693. if (memcmp(reg->match, data, reg->match_len))
  694. continue;
  695. /* found match! */
  696. /* Indicate the received Action frame to user space */
  697. if (nl80211_send_mgmt(rdev, dev, reg->nlpid,
  698. freq, sig_mbm,
  699. buf, len, gfp))
  700. continue;
  701. result = true;
  702. break;
  703. }
  704. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  705. return result;
  706. }
  707. EXPORT_SYMBOL(cfg80211_rx_mgmt);
  708. void cfg80211_mgmt_tx_status(struct net_device *dev, u64 cookie,
  709. const u8 *buf, size_t len, bool ack, gfp_t gfp)
  710. {
  711. struct wireless_dev *wdev = dev->ieee80211_ptr;
  712. struct wiphy *wiphy = wdev->wiphy;
  713. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  714. /* Indicate TX status of the Action frame to user space */
  715. nl80211_send_mgmt_tx_status(rdev, dev, cookie, buf, len, ack, gfp);
  716. }
  717. EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
  718. void cfg80211_cqm_rssi_notify(struct net_device *dev,
  719. enum nl80211_cqm_rssi_threshold_event rssi_event,
  720. gfp_t gfp)
  721. {
  722. struct wireless_dev *wdev = dev->ieee80211_ptr;
  723. struct wiphy *wiphy = wdev->wiphy;
  724. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  725. /* Indicate roaming trigger event to user space */
  726. nl80211_send_cqm_rssi_notify(rdev, dev, rssi_event, gfp);
  727. }
  728. EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
  729. void cfg80211_cqm_pktloss_notify(struct net_device *dev,
  730. const u8 *peer, u32 num_packets, gfp_t gfp)
  731. {
  732. struct wireless_dev *wdev = dev->ieee80211_ptr;
  733. struct wiphy *wiphy = wdev->wiphy;
  734. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  735. /* Indicate roaming trigger event to user space */
  736. nl80211_send_cqm_pktloss_notify(rdev, dev, peer, num_packets, gfp);
  737. }
  738. EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
  739. void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
  740. const u8 *replay_ctr, gfp_t gfp)
  741. {
  742. struct wireless_dev *wdev = dev->ieee80211_ptr;
  743. struct wiphy *wiphy = wdev->wiphy;
  744. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  745. nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
  746. }
  747. EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
  748. void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
  749. const u8 *bssid, bool preauth, gfp_t gfp)
  750. {
  751. struct wireless_dev *wdev = dev->ieee80211_ptr;
  752. struct wiphy *wiphy = wdev->wiphy;
  753. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  754. nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
  755. }
  756. EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
  757. void cfg80211_ch_switch_notify(struct net_device *dev, int freq,
  758. enum nl80211_channel_type type)
  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. struct ieee80211_channel *chan;
  764. wdev_lock(wdev);
  765. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
  766. wdev->iftype != NL80211_IFTYPE_P2P_GO))
  767. goto out;
  768. chan = rdev_freq_to_chan(rdev, freq, type);
  769. if (WARN_ON(!chan))
  770. goto out;
  771. wdev->channel = chan;
  772. nl80211_ch_switch_notify(rdev, dev, freq, type, GFP_KERNEL);
  773. out:
  774. wdev_unlock(wdev);
  775. return;
  776. }
  777. EXPORT_SYMBOL(cfg80211_ch_switch_notify);
  778. bool cfg80211_rx_spurious_frame(struct net_device *dev,
  779. const u8 *addr, gfp_t gfp)
  780. {
  781. struct wireless_dev *wdev = dev->ieee80211_ptr;
  782. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
  783. wdev->iftype != NL80211_IFTYPE_P2P_GO))
  784. return false;
  785. return nl80211_unexpected_frame(dev, addr, gfp);
  786. }
  787. EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
  788. bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
  789. const u8 *addr, gfp_t gfp)
  790. {
  791. struct wireless_dev *wdev = dev->ieee80211_ptr;
  792. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
  793. wdev->iftype != NL80211_IFTYPE_P2P_GO &&
  794. wdev->iftype != NL80211_IFTYPE_AP_VLAN))
  795. return false;
  796. return nl80211_unexpected_4addr_frame(dev, addr, gfp);
  797. }
  798. EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);