mlme.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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(wiphy, 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(wiphy, 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(wiphy, &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(wiphy, &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_auth_timeout(struct net_device *dev, const u8 *addr)
  157. {
  158. struct wireless_dev *wdev = dev->ieee80211_ptr;
  159. struct wiphy *wiphy = wdev->wiphy;
  160. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  161. trace_cfg80211_send_auth_timeout(dev, addr);
  162. wdev_lock(wdev);
  163. nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
  164. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  165. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  166. WLAN_STATUS_UNSPECIFIED_FAILURE,
  167. false, NULL);
  168. wdev_unlock(wdev);
  169. }
  170. EXPORT_SYMBOL(cfg80211_send_auth_timeout);
  171. void cfg80211_send_assoc_timeout(struct net_device *dev, const u8 *addr)
  172. {
  173. struct wireless_dev *wdev = dev->ieee80211_ptr;
  174. struct wiphy *wiphy = wdev->wiphy;
  175. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  176. trace_cfg80211_send_assoc_timeout(dev, addr);
  177. wdev_lock(wdev);
  178. nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
  179. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  180. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  181. WLAN_STATUS_UNSPECIFIED_FAILURE,
  182. false, NULL);
  183. wdev_unlock(wdev);
  184. }
  185. EXPORT_SYMBOL(cfg80211_send_assoc_timeout);
  186. void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
  187. enum nl80211_key_type key_type, int key_id,
  188. const u8 *tsc, gfp_t gfp)
  189. {
  190. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  191. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  192. #ifdef CONFIG_CFG80211_WEXT
  193. union iwreq_data wrqu;
  194. char *buf = kmalloc(128, gfp);
  195. if (buf) {
  196. sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
  197. "keyid=%d %scast addr=%pM)", key_id,
  198. key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
  199. addr);
  200. memset(&wrqu, 0, sizeof(wrqu));
  201. wrqu.data.length = strlen(buf);
  202. wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
  203. kfree(buf);
  204. }
  205. #endif
  206. trace_cfg80211_michael_mic_failure(dev, addr, key_type, key_id, tsc);
  207. nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
  208. }
  209. EXPORT_SYMBOL(cfg80211_michael_mic_failure);
  210. /* some MLME handling for userspace SME */
  211. int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  212. struct net_device *dev,
  213. struct ieee80211_channel *chan,
  214. enum nl80211_auth_type auth_type,
  215. const u8 *bssid,
  216. const u8 *ssid, int ssid_len,
  217. const u8 *ie, int ie_len,
  218. const u8 *key, int key_len, int key_idx,
  219. const u8 *sae_data, int sae_data_len)
  220. {
  221. struct wireless_dev *wdev = dev->ieee80211_ptr;
  222. struct cfg80211_auth_request req;
  223. int err;
  224. ASSERT_WDEV_LOCK(wdev);
  225. if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  226. if (!key || !key_len || key_idx < 0 || key_idx > 4)
  227. return -EINVAL;
  228. if (wdev->current_bss &&
  229. ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
  230. return -EALREADY;
  231. memset(&req, 0, sizeof(req));
  232. req.ie = ie;
  233. req.ie_len = ie_len;
  234. req.sae_data = sae_data;
  235. req.sae_data_len = sae_data_len;
  236. req.auth_type = auth_type;
  237. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  238. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  239. req.key = key;
  240. req.key_len = key_len;
  241. req.key_idx = key_idx;
  242. if (!req.bss)
  243. return -ENOENT;
  244. err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
  245. CHAN_MODE_SHARED);
  246. if (err)
  247. goto out;
  248. err = rdev_auth(rdev, dev, &req);
  249. out:
  250. cfg80211_put_bss(&rdev->wiphy, req.bss);
  251. return err;
  252. }
  253. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  254. struct net_device *dev, struct ieee80211_channel *chan,
  255. enum nl80211_auth_type auth_type, const u8 *bssid,
  256. const u8 *ssid, int ssid_len,
  257. const u8 *ie, int ie_len,
  258. const u8 *key, int key_len, int key_idx,
  259. const u8 *sae_data, int sae_data_len)
  260. {
  261. int err;
  262. mutex_lock(&rdev->devlist_mtx);
  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. sae_data, sae_data_len);
  268. wdev_unlock(dev->ieee80211_ptr);
  269. mutex_unlock(&rdev->devlist_mtx);
  270. return err;
  271. }
  272. /* Do a logical ht_capa &= ht_capa_mask. */
  273. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  274. const struct ieee80211_ht_cap *ht_capa_mask)
  275. {
  276. int i;
  277. u8 *p1, *p2;
  278. if (!ht_capa_mask) {
  279. memset(ht_capa, 0, sizeof(*ht_capa));
  280. return;
  281. }
  282. p1 = (u8*)(ht_capa);
  283. p2 = (u8*)(ht_capa_mask);
  284. for (i = 0; i<sizeof(*ht_capa); i++)
  285. p1[i] &= p2[i];
  286. }
  287. /* Do a logical ht_capa &= ht_capa_mask. */
  288. void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
  289. const struct ieee80211_vht_cap *vht_capa_mask)
  290. {
  291. int i;
  292. u8 *p1, *p2;
  293. if (!vht_capa_mask) {
  294. memset(vht_capa, 0, sizeof(*vht_capa));
  295. return;
  296. }
  297. p1 = (u8*)(vht_capa);
  298. p2 = (u8*)(vht_capa_mask);
  299. for (i = 0; i < sizeof(*vht_capa); i++)
  300. p1[i] &= p2[i];
  301. }
  302. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  303. struct net_device *dev,
  304. struct ieee80211_channel *chan,
  305. const u8 *bssid,
  306. const u8 *ssid, int ssid_len,
  307. struct cfg80211_assoc_request *req)
  308. {
  309. struct wireless_dev *wdev = dev->ieee80211_ptr;
  310. int err;
  311. bool was_connected = false;
  312. ASSERT_WDEV_LOCK(wdev);
  313. if (wdev->current_bss && req->prev_bssid &&
  314. ether_addr_equal(wdev->current_bss->pub.bssid, req->prev_bssid)) {
  315. /*
  316. * Trying to reassociate: Allow this to proceed and let the old
  317. * association to be dropped when the new one is completed.
  318. */
  319. if (wdev->sme_state == CFG80211_SME_CONNECTED) {
  320. was_connected = true;
  321. wdev->sme_state = CFG80211_SME_CONNECTING;
  322. }
  323. } else if (wdev->current_bss)
  324. return -EALREADY;
  325. cfg80211_oper_and_ht_capa(&req->ht_capa_mask,
  326. rdev->wiphy.ht_capa_mod_mask);
  327. cfg80211_oper_and_vht_capa(&req->vht_capa_mask,
  328. rdev->wiphy.vht_capa_mod_mask);
  329. req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  330. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  331. if (!req->bss) {
  332. if (was_connected)
  333. wdev->sme_state = CFG80211_SME_CONNECTED;
  334. return -ENOENT;
  335. }
  336. err = cfg80211_can_use_chan(rdev, wdev, chan, CHAN_MODE_SHARED);
  337. if (err)
  338. goto out;
  339. err = rdev_assoc(rdev, dev, req);
  340. out:
  341. if (err) {
  342. if (was_connected)
  343. wdev->sme_state = CFG80211_SME_CONNECTED;
  344. cfg80211_put_bss(&rdev->wiphy, req->bss);
  345. }
  346. return err;
  347. }
  348. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  349. struct net_device *dev,
  350. struct ieee80211_channel *chan,
  351. const u8 *bssid,
  352. const u8 *ssid, int ssid_len,
  353. struct cfg80211_assoc_request *req)
  354. {
  355. struct wireless_dev *wdev = dev->ieee80211_ptr;
  356. int err;
  357. mutex_lock(&rdev->devlist_mtx);
  358. wdev_lock(wdev);
  359. err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid,
  360. ssid, ssid_len, req);
  361. wdev_unlock(wdev);
  362. mutex_unlock(&rdev->devlist_mtx);
  363. return err;
  364. }
  365. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  366. struct net_device *dev, const u8 *bssid,
  367. const u8 *ie, int ie_len, u16 reason,
  368. bool local_state_change)
  369. {
  370. struct wireless_dev *wdev = dev->ieee80211_ptr;
  371. struct cfg80211_deauth_request req = {
  372. .bssid = bssid,
  373. .reason_code = reason,
  374. .ie = ie,
  375. .ie_len = ie_len,
  376. .local_state_change = local_state_change,
  377. };
  378. ASSERT_WDEV_LOCK(wdev);
  379. if (local_state_change && (!wdev->current_bss ||
  380. !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
  381. return 0;
  382. return rdev_deauth(rdev, dev, &req);
  383. }
  384. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  385. struct net_device *dev, const u8 *bssid,
  386. const u8 *ie, int ie_len, u16 reason,
  387. bool local_state_change)
  388. {
  389. struct wireless_dev *wdev = dev->ieee80211_ptr;
  390. int err;
  391. wdev_lock(wdev);
  392. err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
  393. local_state_change);
  394. wdev_unlock(wdev);
  395. return err;
  396. }
  397. static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  398. struct net_device *dev, const u8 *bssid,
  399. const u8 *ie, int ie_len, u16 reason,
  400. bool local_state_change)
  401. {
  402. struct wireless_dev *wdev = dev->ieee80211_ptr;
  403. struct cfg80211_disassoc_request req;
  404. ASSERT_WDEV_LOCK(wdev);
  405. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  406. return -ENOTCONN;
  407. if (WARN(!wdev->current_bss, "sme_state=%d\n", wdev->sme_state))
  408. return -ENOTCONN;
  409. memset(&req, 0, sizeof(req));
  410. req.reason_code = reason;
  411. req.local_state_change = local_state_change;
  412. req.ie = ie;
  413. req.ie_len = ie_len;
  414. if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
  415. req.bss = &wdev->current_bss->pub;
  416. else
  417. return -ENOTCONN;
  418. return rdev_disassoc(rdev, dev, &req);
  419. }
  420. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  421. struct net_device *dev, const u8 *bssid,
  422. const u8 *ie, int ie_len, u16 reason,
  423. bool local_state_change)
  424. {
  425. struct wireless_dev *wdev = dev->ieee80211_ptr;
  426. int err;
  427. wdev_lock(wdev);
  428. err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
  429. local_state_change);
  430. wdev_unlock(wdev);
  431. return err;
  432. }
  433. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  434. struct net_device *dev)
  435. {
  436. struct wireless_dev *wdev = dev->ieee80211_ptr;
  437. struct cfg80211_deauth_request req;
  438. u8 bssid[ETH_ALEN];
  439. ASSERT_WDEV_LOCK(wdev);
  440. if (!rdev->ops->deauth)
  441. return;
  442. memset(&req, 0, sizeof(req));
  443. req.reason_code = WLAN_REASON_DEAUTH_LEAVING;
  444. req.ie = NULL;
  445. req.ie_len = 0;
  446. if (!wdev->current_bss)
  447. return;
  448. memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
  449. req.bssid = bssid;
  450. rdev_deauth(rdev, dev, &req);
  451. if (wdev->current_bss) {
  452. cfg80211_unhold_bss(wdev->current_bss);
  453. cfg80211_put_bss(&rdev->wiphy, &wdev->current_bss->pub);
  454. wdev->current_bss = NULL;
  455. }
  456. }
  457. struct cfg80211_mgmt_registration {
  458. struct list_head list;
  459. u32 nlportid;
  460. int match_len;
  461. __le16 frame_type;
  462. u8 match[];
  463. };
  464. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
  465. u16 frame_type, const u8 *match_data,
  466. int match_len)
  467. {
  468. struct wiphy *wiphy = wdev->wiphy;
  469. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  470. struct cfg80211_mgmt_registration *reg, *nreg;
  471. int err = 0;
  472. u16 mgmt_type;
  473. if (!wdev->wiphy->mgmt_stypes)
  474. return -EOPNOTSUPP;
  475. if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
  476. return -EINVAL;
  477. if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
  478. return -EINVAL;
  479. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  480. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
  481. return -EINVAL;
  482. nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
  483. if (!nreg)
  484. return -ENOMEM;
  485. spin_lock_bh(&wdev->mgmt_registrations_lock);
  486. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  487. int mlen = min(match_len, reg->match_len);
  488. if (frame_type != le16_to_cpu(reg->frame_type))
  489. continue;
  490. if (memcmp(reg->match, match_data, mlen) == 0) {
  491. err = -EALREADY;
  492. break;
  493. }
  494. }
  495. if (err) {
  496. kfree(nreg);
  497. goto out;
  498. }
  499. memcpy(nreg->match, match_data, match_len);
  500. nreg->match_len = match_len;
  501. nreg->nlportid = snd_portid;
  502. nreg->frame_type = cpu_to_le16(frame_type);
  503. list_add(&nreg->list, &wdev->mgmt_registrations);
  504. if (rdev->ops->mgmt_frame_register)
  505. rdev_mgmt_frame_register(rdev, wdev, frame_type, true);
  506. out:
  507. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  508. return err;
  509. }
  510. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
  511. {
  512. struct wiphy *wiphy = wdev->wiphy;
  513. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  514. struct cfg80211_mgmt_registration *reg, *tmp;
  515. spin_lock_bh(&wdev->mgmt_registrations_lock);
  516. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  517. if (reg->nlportid != nlportid)
  518. continue;
  519. if (rdev->ops->mgmt_frame_register) {
  520. u16 frame_type = le16_to_cpu(reg->frame_type);
  521. rdev_mgmt_frame_register(rdev, wdev,
  522. frame_type, false);
  523. }
  524. list_del(&reg->list);
  525. kfree(reg);
  526. }
  527. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  528. if (nlportid == wdev->ap_unexpected_nlportid)
  529. wdev->ap_unexpected_nlportid = 0;
  530. }
  531. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
  532. {
  533. struct cfg80211_mgmt_registration *reg, *tmp;
  534. spin_lock_bh(&wdev->mgmt_registrations_lock);
  535. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  536. list_del(&reg->list);
  537. kfree(reg);
  538. }
  539. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  540. }
  541. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  542. struct wireless_dev *wdev,
  543. struct ieee80211_channel *chan, bool offchan,
  544. unsigned int wait, const u8 *buf, size_t len,
  545. bool no_cck, bool dont_wait_for_ack, u64 *cookie)
  546. {
  547. const struct ieee80211_mgmt *mgmt;
  548. u16 stype;
  549. if (!wdev->wiphy->mgmt_stypes)
  550. return -EOPNOTSUPP;
  551. if (!rdev->ops->mgmt_tx)
  552. return -EOPNOTSUPP;
  553. if (len < 24 + 1)
  554. return -EINVAL;
  555. mgmt = (const struct ieee80211_mgmt *) buf;
  556. if (!ieee80211_is_mgmt(mgmt->frame_control))
  557. return -EINVAL;
  558. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  559. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
  560. return -EINVAL;
  561. if (ieee80211_is_action(mgmt->frame_control) &&
  562. mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
  563. int err = 0;
  564. wdev_lock(wdev);
  565. switch (wdev->iftype) {
  566. case NL80211_IFTYPE_ADHOC:
  567. case NL80211_IFTYPE_STATION:
  568. case NL80211_IFTYPE_P2P_CLIENT:
  569. if (!wdev->current_bss) {
  570. err = -ENOTCONN;
  571. break;
  572. }
  573. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  574. mgmt->bssid)) {
  575. err = -ENOTCONN;
  576. break;
  577. }
  578. /*
  579. * check for IBSS DA must be done by driver as
  580. * cfg80211 doesn't track the stations
  581. */
  582. if (wdev->iftype == NL80211_IFTYPE_ADHOC)
  583. break;
  584. /* for station, check that DA is the AP */
  585. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  586. mgmt->da)) {
  587. err = -ENOTCONN;
  588. break;
  589. }
  590. break;
  591. case NL80211_IFTYPE_AP:
  592. case NL80211_IFTYPE_P2P_GO:
  593. case NL80211_IFTYPE_AP_VLAN:
  594. if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
  595. err = -EINVAL;
  596. break;
  597. case NL80211_IFTYPE_MESH_POINT:
  598. if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
  599. err = -EINVAL;
  600. break;
  601. }
  602. /*
  603. * check for mesh DA must be done by driver as
  604. * cfg80211 doesn't track the stations
  605. */
  606. break;
  607. case NL80211_IFTYPE_P2P_DEVICE:
  608. /*
  609. * fall through, P2P device only supports
  610. * public action frames
  611. */
  612. default:
  613. err = -EOPNOTSUPP;
  614. break;
  615. }
  616. wdev_unlock(wdev);
  617. if (err)
  618. return err;
  619. }
  620. if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
  621. return -EINVAL;
  622. /* Transmit the Action frame as requested by user space */
  623. return rdev_mgmt_tx(rdev, wdev, chan, offchan,
  624. wait, buf, len, no_cck, dont_wait_for_ack,
  625. cookie);
  626. }
  627. bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
  628. const u8 *buf, size_t len, gfp_t gfp)
  629. {
  630. struct wiphy *wiphy = wdev->wiphy;
  631. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  632. struct cfg80211_mgmt_registration *reg;
  633. const struct ieee80211_txrx_stypes *stypes =
  634. &wiphy->mgmt_stypes[wdev->iftype];
  635. struct ieee80211_mgmt *mgmt = (void *)buf;
  636. const u8 *data;
  637. int data_len;
  638. bool result = false;
  639. __le16 ftype = mgmt->frame_control &
  640. cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
  641. u16 stype;
  642. trace_cfg80211_rx_mgmt(wdev, freq, sig_mbm);
  643. stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
  644. if (!(stypes->rx & BIT(stype))) {
  645. trace_cfg80211_return_bool(false);
  646. return false;
  647. }
  648. data = buf + ieee80211_hdrlen(mgmt->frame_control);
  649. data_len = len - ieee80211_hdrlen(mgmt->frame_control);
  650. spin_lock_bh(&wdev->mgmt_registrations_lock);
  651. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  652. if (reg->frame_type != ftype)
  653. continue;
  654. if (reg->match_len > data_len)
  655. continue;
  656. if (memcmp(reg->match, data, reg->match_len))
  657. continue;
  658. /* found match! */
  659. /* Indicate the received Action frame to user space */
  660. if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
  661. freq, sig_mbm,
  662. buf, len, gfp))
  663. continue;
  664. result = true;
  665. break;
  666. }
  667. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  668. trace_cfg80211_return_bool(result);
  669. return result;
  670. }
  671. EXPORT_SYMBOL(cfg80211_rx_mgmt);
  672. void cfg80211_dfs_channels_update_work(struct work_struct *work)
  673. {
  674. struct delayed_work *delayed_work;
  675. struct cfg80211_registered_device *rdev;
  676. struct cfg80211_chan_def chandef;
  677. struct ieee80211_supported_band *sband;
  678. struct ieee80211_channel *c;
  679. struct wiphy *wiphy;
  680. bool check_again = false;
  681. unsigned long timeout, next_time = 0;
  682. int bandid, i;
  683. delayed_work = container_of(work, struct delayed_work, work);
  684. rdev = container_of(delayed_work, struct cfg80211_registered_device,
  685. dfs_update_channels_wk);
  686. wiphy = &rdev->wiphy;
  687. mutex_lock(&cfg80211_mutex);
  688. for (bandid = 0; bandid < IEEE80211_NUM_BANDS; bandid++) {
  689. sband = wiphy->bands[bandid];
  690. if (!sband)
  691. continue;
  692. for (i = 0; i < sband->n_channels; i++) {
  693. c = &sband->channels[i];
  694. if (c->dfs_state != NL80211_DFS_UNAVAILABLE)
  695. continue;
  696. timeout = c->dfs_state_entered +
  697. IEEE80211_DFS_MIN_NOP_TIME_MS;
  698. if (time_after_eq(jiffies, timeout)) {
  699. c->dfs_state = NL80211_DFS_USABLE;
  700. cfg80211_chandef_create(&chandef, c,
  701. NL80211_CHAN_NO_HT);
  702. nl80211_radar_notify(rdev, &chandef,
  703. NL80211_RADAR_NOP_FINISHED,
  704. NULL, GFP_ATOMIC);
  705. continue;
  706. }
  707. if (!check_again)
  708. next_time = timeout - jiffies;
  709. else
  710. next_time = min(next_time, timeout - jiffies);
  711. check_again = true;
  712. }
  713. }
  714. mutex_unlock(&cfg80211_mutex);
  715. /* reschedule if there are other channels waiting to be cleared again */
  716. if (check_again)
  717. queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
  718. next_time);
  719. }
  720. void cfg80211_radar_event(struct wiphy *wiphy,
  721. struct cfg80211_chan_def *chandef,
  722. gfp_t gfp)
  723. {
  724. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  725. unsigned long timeout;
  726. trace_cfg80211_radar_event(wiphy, chandef);
  727. /* only set the chandef supplied channel to unavailable, in
  728. * case the radar is detected on only one of multiple channels
  729. * spanned by the chandef.
  730. */
  731. cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
  732. timeout = msecs_to_jiffies(IEEE80211_DFS_MIN_NOP_TIME_MS);
  733. queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
  734. timeout);
  735. nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
  736. }
  737. EXPORT_SYMBOL(cfg80211_radar_event);
  738. void cfg80211_cac_event(struct net_device *netdev,
  739. enum nl80211_radar_event event, gfp_t gfp)
  740. {
  741. struct wireless_dev *wdev = netdev->ieee80211_ptr;
  742. struct wiphy *wiphy = wdev->wiphy;
  743. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  744. struct cfg80211_chan_def chandef;
  745. unsigned long timeout;
  746. trace_cfg80211_cac_event(netdev, event);
  747. if (WARN_ON(!wdev->cac_started))
  748. return;
  749. if (WARN_ON(!wdev->channel))
  750. return;
  751. cfg80211_chandef_create(&chandef, wdev->channel, NL80211_CHAN_NO_HT);
  752. switch (event) {
  753. case NL80211_RADAR_CAC_FINISHED:
  754. timeout = wdev->cac_start_time +
  755. msecs_to_jiffies(IEEE80211_DFS_MIN_CAC_TIME_MS);
  756. WARN_ON(!time_after_eq(jiffies, timeout));
  757. cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_AVAILABLE);
  758. break;
  759. case NL80211_RADAR_CAC_ABORTED:
  760. break;
  761. default:
  762. WARN_ON(1);
  763. return;
  764. }
  765. wdev->cac_started = false;
  766. nl80211_radar_notify(rdev, &chandef, event, netdev, gfp);
  767. }
  768. EXPORT_SYMBOL(cfg80211_cac_event);