mlme.c 29 KB

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