mlme.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. .ie = ie,
  224. .ie_len = ie_len,
  225. .sae_data = sae_data,
  226. .sae_data_len = sae_data_len,
  227. .auth_type = auth_type,
  228. .key = key,
  229. .key_len = key_len,
  230. .key_idx = key_idx,
  231. };
  232. int err;
  233. ASSERT_WDEV_LOCK(wdev);
  234. if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  235. if (!key || !key_len || key_idx < 0 || key_idx > 4)
  236. return -EINVAL;
  237. if (wdev->current_bss &&
  238. ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
  239. return -EALREADY;
  240. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  241. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  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. ASSERT_RTNL();
  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. return err;
  270. }
  271. /* Do a logical ht_capa &= ht_capa_mask. */
  272. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  273. const struct ieee80211_ht_cap *ht_capa_mask)
  274. {
  275. int i;
  276. u8 *p1, *p2;
  277. if (!ht_capa_mask) {
  278. memset(ht_capa, 0, sizeof(*ht_capa));
  279. return;
  280. }
  281. p1 = (u8*)(ht_capa);
  282. p2 = (u8*)(ht_capa_mask);
  283. for (i = 0; i<sizeof(*ht_capa); i++)
  284. p1[i] &= p2[i];
  285. }
  286. /* Do a logical ht_capa &= ht_capa_mask. */
  287. void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
  288. const struct ieee80211_vht_cap *vht_capa_mask)
  289. {
  290. int i;
  291. u8 *p1, *p2;
  292. if (!vht_capa_mask) {
  293. memset(vht_capa, 0, sizeof(*vht_capa));
  294. return;
  295. }
  296. p1 = (u8*)(vht_capa);
  297. p2 = (u8*)(vht_capa_mask);
  298. for (i = 0; i < sizeof(*vht_capa); i++)
  299. p1[i] &= p2[i];
  300. }
  301. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  302. struct net_device *dev,
  303. struct ieee80211_channel *chan,
  304. const u8 *bssid,
  305. const u8 *ssid, int ssid_len,
  306. struct cfg80211_assoc_request *req)
  307. {
  308. struct wireless_dev *wdev = dev->ieee80211_ptr;
  309. int err;
  310. bool was_connected = false;
  311. ASSERT_WDEV_LOCK(wdev);
  312. if (wdev->current_bss && req->prev_bssid &&
  313. ether_addr_equal(wdev->current_bss->pub.bssid, req->prev_bssid)) {
  314. /*
  315. * Trying to reassociate: Allow this to proceed and let the old
  316. * association to be dropped when the new one is completed.
  317. */
  318. if (wdev->sme_state == CFG80211_SME_CONNECTED) {
  319. was_connected = true;
  320. wdev->sme_state = CFG80211_SME_CONNECTING;
  321. }
  322. } else if (wdev->current_bss)
  323. return -EALREADY;
  324. cfg80211_oper_and_ht_capa(&req->ht_capa_mask,
  325. rdev->wiphy.ht_capa_mod_mask);
  326. cfg80211_oper_and_vht_capa(&req->vht_capa_mask,
  327. rdev->wiphy.vht_capa_mod_mask);
  328. req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  329. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  330. if (!req->bss) {
  331. if (was_connected)
  332. wdev->sme_state = CFG80211_SME_CONNECTED;
  333. return -ENOENT;
  334. }
  335. err = cfg80211_can_use_chan(rdev, wdev, chan, CHAN_MODE_SHARED);
  336. if (err)
  337. goto out;
  338. err = rdev_assoc(rdev, dev, req);
  339. out:
  340. if (err) {
  341. if (was_connected)
  342. wdev->sme_state = CFG80211_SME_CONNECTED;
  343. cfg80211_put_bss(&rdev->wiphy, req->bss);
  344. }
  345. return err;
  346. }
  347. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  348. struct net_device *dev,
  349. struct ieee80211_channel *chan,
  350. const u8 *bssid,
  351. const u8 *ssid, int ssid_len,
  352. struct cfg80211_assoc_request *req)
  353. {
  354. struct wireless_dev *wdev = dev->ieee80211_ptr;
  355. int err;
  356. ASSERT_RTNL();
  357. wdev_lock(wdev);
  358. err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid,
  359. ssid, ssid_len, req);
  360. wdev_unlock(wdev);
  361. return err;
  362. }
  363. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  364. struct net_device *dev, const u8 *bssid,
  365. const u8 *ie, int ie_len, u16 reason,
  366. bool local_state_change)
  367. {
  368. struct wireless_dev *wdev = dev->ieee80211_ptr;
  369. struct cfg80211_deauth_request req = {
  370. .bssid = bssid,
  371. .reason_code = reason,
  372. .ie = ie,
  373. .ie_len = ie_len,
  374. .local_state_change = local_state_change,
  375. };
  376. ASSERT_WDEV_LOCK(wdev);
  377. if (local_state_change && (!wdev->current_bss ||
  378. !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
  379. return 0;
  380. return rdev_deauth(rdev, dev, &req);
  381. }
  382. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  383. struct net_device *dev, const u8 *bssid,
  384. const u8 *ie, int ie_len, u16 reason,
  385. bool local_state_change)
  386. {
  387. struct wireless_dev *wdev = dev->ieee80211_ptr;
  388. int err;
  389. wdev_lock(wdev);
  390. err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
  391. local_state_change);
  392. wdev_unlock(wdev);
  393. return err;
  394. }
  395. static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  396. struct net_device *dev, const u8 *bssid,
  397. const u8 *ie, int ie_len, u16 reason,
  398. bool local_state_change)
  399. {
  400. struct wireless_dev *wdev = dev->ieee80211_ptr;
  401. struct cfg80211_disassoc_request req = {
  402. .reason_code = reason,
  403. .local_state_change = local_state_change,
  404. .ie = ie,
  405. .ie_len = ie_len,
  406. };
  407. ASSERT_WDEV_LOCK(wdev);
  408. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  409. return -ENOTCONN;
  410. if (WARN(!wdev->current_bss, "sme_state=%d\n", wdev->sme_state))
  411. return -ENOTCONN;
  412. if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
  413. req.bss = &wdev->current_bss->pub;
  414. else
  415. return -ENOTCONN;
  416. return rdev_disassoc(rdev, dev, &req);
  417. }
  418. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  419. struct net_device *dev, const u8 *bssid,
  420. const u8 *ie, int ie_len, u16 reason,
  421. bool local_state_change)
  422. {
  423. struct wireless_dev *wdev = dev->ieee80211_ptr;
  424. int err;
  425. wdev_lock(wdev);
  426. err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
  427. local_state_change);
  428. wdev_unlock(wdev);
  429. return err;
  430. }
  431. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  432. struct net_device *dev)
  433. {
  434. struct wireless_dev *wdev = dev->ieee80211_ptr;
  435. u8 bssid[ETH_ALEN];
  436. struct cfg80211_deauth_request req = {
  437. .reason_code = WLAN_REASON_DEAUTH_LEAVING,
  438. .bssid = bssid,
  439. };
  440. ASSERT_WDEV_LOCK(wdev);
  441. if (!rdev->ops->deauth)
  442. return;
  443. if (!wdev->current_bss)
  444. return;
  445. memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
  446. rdev_deauth(rdev, dev, &req);
  447. if (wdev->current_bss) {
  448. cfg80211_unhold_bss(wdev->current_bss);
  449. cfg80211_put_bss(&rdev->wiphy, &wdev->current_bss->pub);
  450. wdev->current_bss = NULL;
  451. }
  452. }
  453. struct cfg80211_mgmt_registration {
  454. struct list_head list;
  455. u32 nlportid;
  456. int match_len;
  457. __le16 frame_type;
  458. u8 match[];
  459. };
  460. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
  461. u16 frame_type, const u8 *match_data,
  462. int match_len)
  463. {
  464. struct wiphy *wiphy = wdev->wiphy;
  465. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  466. struct cfg80211_mgmt_registration *reg, *nreg;
  467. int err = 0;
  468. u16 mgmt_type;
  469. if (!wdev->wiphy->mgmt_stypes)
  470. return -EOPNOTSUPP;
  471. if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
  472. return -EINVAL;
  473. if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
  474. return -EINVAL;
  475. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  476. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
  477. return -EINVAL;
  478. nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
  479. if (!nreg)
  480. return -ENOMEM;
  481. spin_lock_bh(&wdev->mgmt_registrations_lock);
  482. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  483. int mlen = min(match_len, reg->match_len);
  484. if (frame_type != le16_to_cpu(reg->frame_type))
  485. continue;
  486. if (memcmp(reg->match, match_data, mlen) == 0) {
  487. err = -EALREADY;
  488. break;
  489. }
  490. }
  491. if (err) {
  492. kfree(nreg);
  493. goto out;
  494. }
  495. memcpy(nreg->match, match_data, match_len);
  496. nreg->match_len = match_len;
  497. nreg->nlportid = snd_portid;
  498. nreg->frame_type = cpu_to_le16(frame_type);
  499. list_add(&nreg->list, &wdev->mgmt_registrations);
  500. if (rdev->ops->mgmt_frame_register)
  501. rdev_mgmt_frame_register(rdev, wdev, frame_type, true);
  502. out:
  503. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  504. return err;
  505. }
  506. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
  507. {
  508. struct wiphy *wiphy = wdev->wiphy;
  509. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  510. struct cfg80211_mgmt_registration *reg, *tmp;
  511. spin_lock_bh(&wdev->mgmt_registrations_lock);
  512. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  513. if (reg->nlportid != nlportid)
  514. continue;
  515. if (rdev->ops->mgmt_frame_register) {
  516. u16 frame_type = le16_to_cpu(reg->frame_type);
  517. rdev_mgmt_frame_register(rdev, wdev,
  518. frame_type, false);
  519. }
  520. list_del(&reg->list);
  521. kfree(reg);
  522. }
  523. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  524. if (nlportid && rdev->crit_proto_nlportid == nlportid) {
  525. rdev->crit_proto_nlportid = 0;
  526. rdev_crit_proto_stop(rdev, wdev);
  527. }
  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. rtnl_lock();
  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. rtnl_unlock();
  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);