mlme.c 25 KB

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