mlme.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. nl80211_send_rx_auth(rdev, dev, buf, len, GFP_KERNEL);
  25. cfg80211_sme_rx_auth(dev, buf, len);
  26. }
  27. EXPORT_SYMBOL(cfg80211_send_rx_auth);
  28. void cfg80211_send_rx_assoc(struct net_device *dev, struct cfg80211_bss *bss,
  29. const u8 *buf, size_t len)
  30. {
  31. u16 status_code;
  32. struct wireless_dev *wdev = dev->ieee80211_ptr;
  33. struct wiphy *wiphy = wdev->wiphy;
  34. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  35. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  36. u8 *ie = mgmt->u.assoc_resp.variable;
  37. int ieoffs = offsetof(struct ieee80211_mgmt, u.assoc_resp.variable);
  38. trace_cfg80211_send_rx_assoc(dev, bss);
  39. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  40. /*
  41. * This is a bit of a hack, we don't notify userspace of
  42. * a (re-)association reply if we tried to send a reassoc
  43. * and got a reject -- we only try again with an assoc
  44. * frame instead of reassoc.
  45. */
  46. if (status_code != WLAN_STATUS_SUCCESS && wdev->conn &&
  47. cfg80211_sme_failed_reassoc(wdev)) {
  48. cfg80211_put_bss(wiphy, bss);
  49. return;
  50. }
  51. nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL);
  52. if (status_code != WLAN_STATUS_SUCCESS && wdev->conn) {
  53. cfg80211_sme_failed_assoc(wdev);
  54. /*
  55. * do not call connect_result() now because the
  56. * sme will schedule work that does it later.
  57. */
  58. cfg80211_put_bss(wiphy, bss);
  59. return;
  60. }
  61. if (!wdev->conn && wdev->sme_state == CFG80211_SME_IDLE) {
  62. /*
  63. * This is for the userspace SME, the CONNECTING
  64. * state will be changed to CONNECTED by
  65. * __cfg80211_connect_result() below.
  66. */
  67. wdev->sme_state = CFG80211_SME_CONNECTING;
  68. }
  69. /* this consumes the bss reference */
  70. __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, ie, len - ieoffs,
  71. status_code,
  72. status_code == WLAN_STATUS_SUCCESS, bss);
  73. }
  74. EXPORT_SYMBOL(cfg80211_send_rx_assoc);
  75. void cfg80211_send_deauth(struct net_device *dev,
  76. const u8 *buf, size_t len)
  77. {
  78. struct wireless_dev *wdev = dev->ieee80211_ptr;
  79. struct wiphy *wiphy = wdev->wiphy;
  80. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  81. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  82. const u8 *bssid = mgmt->bssid;
  83. bool was_current = false;
  84. trace_cfg80211_send_deauth(dev);
  85. ASSERT_WDEV_LOCK(wdev);
  86. if (wdev->current_bss &&
  87. ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
  88. cfg80211_unhold_bss(wdev->current_bss);
  89. cfg80211_put_bss(wiphy, &wdev->current_bss->pub);
  90. wdev->current_bss = NULL;
  91. was_current = true;
  92. }
  93. nl80211_send_deauth(rdev, dev, buf, len, GFP_KERNEL);
  94. if (wdev->sme_state == CFG80211_SME_CONNECTED && was_current) {
  95. u16 reason_code;
  96. bool from_ap;
  97. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  98. from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
  99. __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
  100. } else if (wdev->sme_state == CFG80211_SME_CONNECTING) {
  101. __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
  102. WLAN_STATUS_UNSPECIFIED_FAILURE,
  103. false, NULL);
  104. }
  105. }
  106. EXPORT_SYMBOL(cfg80211_send_deauth);
  107. void cfg80211_send_disassoc(struct net_device *dev,
  108. const u8 *buf, size_t len)
  109. {
  110. struct wireless_dev *wdev = dev->ieee80211_ptr;
  111. struct wiphy *wiphy = wdev->wiphy;
  112. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  113. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  114. const u8 *bssid = mgmt->bssid;
  115. u16 reason_code;
  116. bool from_ap;
  117. trace_cfg80211_send_disassoc(dev);
  118. ASSERT_WDEV_LOCK(wdev);
  119. nl80211_send_disassoc(rdev, dev, buf, len, GFP_KERNEL);
  120. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  121. return;
  122. if (wdev->current_bss &&
  123. ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
  124. cfg80211_sme_disassoc(dev, wdev->current_bss);
  125. cfg80211_unhold_bss(wdev->current_bss);
  126. cfg80211_put_bss(wiphy, &wdev->current_bss->pub);
  127. wdev->current_bss = NULL;
  128. } else
  129. WARN_ON(1);
  130. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  131. from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
  132. __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
  133. }
  134. EXPORT_SYMBOL(cfg80211_send_disassoc);
  135. void cfg80211_send_auth_timeout(struct net_device *dev, const u8 *addr)
  136. {
  137. struct wireless_dev *wdev = dev->ieee80211_ptr;
  138. struct wiphy *wiphy = wdev->wiphy;
  139. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  140. trace_cfg80211_send_auth_timeout(dev, addr);
  141. nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
  142. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  143. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  144. WLAN_STATUS_UNSPECIFIED_FAILURE,
  145. false, NULL);
  146. }
  147. EXPORT_SYMBOL(cfg80211_send_auth_timeout);
  148. void cfg80211_send_assoc_timeout(struct net_device *dev, const u8 *addr)
  149. {
  150. struct wireless_dev *wdev = dev->ieee80211_ptr;
  151. struct wiphy *wiphy = wdev->wiphy;
  152. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  153. trace_cfg80211_send_assoc_timeout(dev, addr);
  154. nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
  155. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  156. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  157. WLAN_STATUS_UNSPECIFIED_FAILURE,
  158. false, NULL);
  159. }
  160. EXPORT_SYMBOL(cfg80211_send_assoc_timeout);
  161. void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
  162. enum nl80211_key_type key_type, int key_id,
  163. const u8 *tsc, gfp_t gfp)
  164. {
  165. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  166. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  167. #ifdef CONFIG_CFG80211_WEXT
  168. union iwreq_data wrqu;
  169. char *buf = kmalloc(128, gfp);
  170. if (buf) {
  171. sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
  172. "keyid=%d %scast addr=%pM)", key_id,
  173. key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
  174. addr);
  175. memset(&wrqu, 0, sizeof(wrqu));
  176. wrqu.data.length = strlen(buf);
  177. wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
  178. kfree(buf);
  179. }
  180. #endif
  181. trace_cfg80211_michael_mic_failure(dev, addr, key_type, key_id, tsc);
  182. nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
  183. }
  184. EXPORT_SYMBOL(cfg80211_michael_mic_failure);
  185. /* some MLME handling for userspace SME */
  186. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  187. struct net_device *dev,
  188. struct ieee80211_channel *chan,
  189. enum nl80211_auth_type auth_type,
  190. const u8 *bssid,
  191. const u8 *ssid, int ssid_len,
  192. const u8 *ie, int ie_len,
  193. const u8 *key, int key_len, int key_idx,
  194. const u8 *sae_data, int sae_data_len)
  195. {
  196. struct wireless_dev *wdev = dev->ieee80211_ptr;
  197. struct cfg80211_auth_request req = {
  198. .ie = ie,
  199. .ie_len = ie_len,
  200. .sae_data = sae_data,
  201. .sae_data_len = sae_data_len,
  202. .auth_type = auth_type,
  203. .key = key,
  204. .key_len = key_len,
  205. .key_idx = key_idx,
  206. };
  207. int err;
  208. ASSERT_WDEV_LOCK(wdev);
  209. if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  210. if (!key || !key_len || key_idx < 0 || key_idx > 4)
  211. return -EINVAL;
  212. if (wdev->current_bss &&
  213. ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
  214. return -EALREADY;
  215. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  216. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  217. if (!req.bss)
  218. return -ENOENT;
  219. err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
  220. CHAN_MODE_SHARED);
  221. if (err)
  222. goto out;
  223. err = rdev_auth(rdev, dev, &req);
  224. out:
  225. cfg80211_put_bss(&rdev->wiphy, req.bss);
  226. return err;
  227. }
  228. /* Do a logical ht_capa &= ht_capa_mask. */
  229. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  230. const struct ieee80211_ht_cap *ht_capa_mask)
  231. {
  232. int i;
  233. u8 *p1, *p2;
  234. if (!ht_capa_mask) {
  235. memset(ht_capa, 0, sizeof(*ht_capa));
  236. return;
  237. }
  238. p1 = (u8*)(ht_capa);
  239. p2 = (u8*)(ht_capa_mask);
  240. for (i = 0; i<sizeof(*ht_capa); i++)
  241. p1[i] &= p2[i];
  242. }
  243. /* Do a logical ht_capa &= ht_capa_mask. */
  244. void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
  245. const struct ieee80211_vht_cap *vht_capa_mask)
  246. {
  247. int i;
  248. u8 *p1, *p2;
  249. if (!vht_capa_mask) {
  250. memset(vht_capa, 0, sizeof(*vht_capa));
  251. return;
  252. }
  253. p1 = (u8*)(vht_capa);
  254. p2 = (u8*)(vht_capa_mask);
  255. for (i = 0; i < sizeof(*vht_capa); i++)
  256. p1[i] &= p2[i];
  257. }
  258. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  259. struct net_device *dev,
  260. struct ieee80211_channel *chan,
  261. const u8 *bssid,
  262. const u8 *ssid, int ssid_len,
  263. struct cfg80211_assoc_request *req)
  264. {
  265. struct wireless_dev *wdev = dev->ieee80211_ptr;
  266. int err;
  267. bool was_connected = false;
  268. ASSERT_WDEV_LOCK(wdev);
  269. if (wdev->current_bss && req->prev_bssid &&
  270. ether_addr_equal(wdev->current_bss->pub.bssid, req->prev_bssid)) {
  271. /*
  272. * Trying to reassociate: Allow this to proceed and let the old
  273. * association to be dropped when the new one is completed.
  274. */
  275. if (wdev->sme_state == CFG80211_SME_CONNECTED) {
  276. was_connected = true;
  277. wdev->sme_state = CFG80211_SME_CONNECTING;
  278. }
  279. } else if (wdev->current_bss)
  280. return -EALREADY;
  281. cfg80211_oper_and_ht_capa(&req->ht_capa_mask,
  282. rdev->wiphy.ht_capa_mod_mask);
  283. cfg80211_oper_and_vht_capa(&req->vht_capa_mask,
  284. rdev->wiphy.vht_capa_mod_mask);
  285. req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  286. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  287. if (!req->bss) {
  288. if (was_connected)
  289. wdev->sme_state = CFG80211_SME_CONNECTED;
  290. return -ENOENT;
  291. }
  292. err = cfg80211_can_use_chan(rdev, wdev, chan, CHAN_MODE_SHARED);
  293. if (err)
  294. goto out;
  295. err = rdev_assoc(rdev, dev, req);
  296. out:
  297. if (err) {
  298. if (was_connected)
  299. wdev->sme_state = CFG80211_SME_CONNECTED;
  300. cfg80211_put_bss(&rdev->wiphy, req->bss);
  301. }
  302. return err;
  303. }
  304. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  305. struct net_device *dev, const u8 *bssid,
  306. const u8 *ie, int ie_len, u16 reason,
  307. bool local_state_change)
  308. {
  309. struct wireless_dev *wdev = dev->ieee80211_ptr;
  310. struct cfg80211_deauth_request req = {
  311. .bssid = bssid,
  312. .reason_code = reason,
  313. .ie = ie,
  314. .ie_len = ie_len,
  315. .local_state_change = local_state_change,
  316. };
  317. ASSERT_WDEV_LOCK(wdev);
  318. if (local_state_change && (!wdev->current_bss ||
  319. !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
  320. return 0;
  321. return rdev_deauth(rdev, dev, &req);
  322. }
  323. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  324. struct net_device *dev, const u8 *bssid,
  325. const u8 *ie, int ie_len, u16 reason,
  326. bool local_state_change)
  327. {
  328. struct wireless_dev *wdev = dev->ieee80211_ptr;
  329. struct cfg80211_disassoc_request req = {
  330. .reason_code = reason,
  331. .local_state_change = local_state_change,
  332. .ie = ie,
  333. .ie_len = ie_len,
  334. };
  335. ASSERT_WDEV_LOCK(wdev);
  336. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  337. return -ENOTCONN;
  338. if (WARN(!wdev->current_bss, "sme_state=%d\n", wdev->sme_state))
  339. return -ENOTCONN;
  340. if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
  341. req.bss = &wdev->current_bss->pub;
  342. else
  343. return -ENOTCONN;
  344. return rdev_disassoc(rdev, dev, &req);
  345. }
  346. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  347. struct net_device *dev)
  348. {
  349. struct wireless_dev *wdev = dev->ieee80211_ptr;
  350. u8 bssid[ETH_ALEN];
  351. struct cfg80211_deauth_request req = {
  352. .reason_code = WLAN_REASON_DEAUTH_LEAVING,
  353. .bssid = bssid,
  354. };
  355. ASSERT_WDEV_LOCK(wdev);
  356. if (!rdev->ops->deauth)
  357. return;
  358. if (!wdev->current_bss)
  359. return;
  360. memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
  361. rdev_deauth(rdev, dev, &req);
  362. if (wdev->current_bss) {
  363. cfg80211_unhold_bss(wdev->current_bss);
  364. cfg80211_put_bss(&rdev->wiphy, &wdev->current_bss->pub);
  365. wdev->current_bss = NULL;
  366. }
  367. }
  368. struct cfg80211_mgmt_registration {
  369. struct list_head list;
  370. u32 nlportid;
  371. int match_len;
  372. __le16 frame_type;
  373. u8 match[];
  374. };
  375. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
  376. u16 frame_type, const u8 *match_data,
  377. int match_len)
  378. {
  379. struct wiphy *wiphy = wdev->wiphy;
  380. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  381. struct cfg80211_mgmt_registration *reg, *nreg;
  382. int err = 0;
  383. u16 mgmt_type;
  384. if (!wdev->wiphy->mgmt_stypes)
  385. return -EOPNOTSUPP;
  386. if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
  387. return -EINVAL;
  388. if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
  389. return -EINVAL;
  390. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  391. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
  392. return -EINVAL;
  393. nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
  394. if (!nreg)
  395. return -ENOMEM;
  396. spin_lock_bh(&wdev->mgmt_registrations_lock);
  397. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  398. int mlen = min(match_len, reg->match_len);
  399. if (frame_type != le16_to_cpu(reg->frame_type))
  400. continue;
  401. if (memcmp(reg->match, match_data, mlen) == 0) {
  402. err = -EALREADY;
  403. break;
  404. }
  405. }
  406. if (err) {
  407. kfree(nreg);
  408. goto out;
  409. }
  410. memcpy(nreg->match, match_data, match_len);
  411. nreg->match_len = match_len;
  412. nreg->nlportid = snd_portid;
  413. nreg->frame_type = cpu_to_le16(frame_type);
  414. list_add(&nreg->list, &wdev->mgmt_registrations);
  415. if (rdev->ops->mgmt_frame_register)
  416. rdev_mgmt_frame_register(rdev, wdev, frame_type, true);
  417. out:
  418. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  419. return err;
  420. }
  421. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
  422. {
  423. struct wiphy *wiphy = wdev->wiphy;
  424. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  425. struct cfg80211_mgmt_registration *reg, *tmp;
  426. spin_lock_bh(&wdev->mgmt_registrations_lock);
  427. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  428. if (reg->nlportid != nlportid)
  429. continue;
  430. if (rdev->ops->mgmt_frame_register) {
  431. u16 frame_type = le16_to_cpu(reg->frame_type);
  432. rdev_mgmt_frame_register(rdev, wdev,
  433. frame_type, false);
  434. }
  435. list_del(&reg->list);
  436. kfree(reg);
  437. }
  438. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  439. if (nlportid && rdev->crit_proto_nlportid == nlportid) {
  440. rdev->crit_proto_nlportid = 0;
  441. rdev_crit_proto_stop(rdev, wdev);
  442. }
  443. if (nlportid == wdev->ap_unexpected_nlportid)
  444. wdev->ap_unexpected_nlportid = 0;
  445. }
  446. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
  447. {
  448. struct cfg80211_mgmt_registration *reg, *tmp;
  449. spin_lock_bh(&wdev->mgmt_registrations_lock);
  450. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  451. list_del(&reg->list);
  452. kfree(reg);
  453. }
  454. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  455. }
  456. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  457. struct wireless_dev *wdev,
  458. struct ieee80211_channel *chan, bool offchan,
  459. unsigned int wait, const u8 *buf, size_t len,
  460. bool no_cck, bool dont_wait_for_ack, u64 *cookie)
  461. {
  462. const struct ieee80211_mgmt *mgmt;
  463. u16 stype;
  464. if (!wdev->wiphy->mgmt_stypes)
  465. return -EOPNOTSUPP;
  466. if (!rdev->ops->mgmt_tx)
  467. return -EOPNOTSUPP;
  468. if (len < 24 + 1)
  469. return -EINVAL;
  470. mgmt = (const struct ieee80211_mgmt *) buf;
  471. if (!ieee80211_is_mgmt(mgmt->frame_control))
  472. return -EINVAL;
  473. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  474. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
  475. return -EINVAL;
  476. if (ieee80211_is_action(mgmt->frame_control) &&
  477. mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
  478. int err = 0;
  479. wdev_lock(wdev);
  480. switch (wdev->iftype) {
  481. case NL80211_IFTYPE_ADHOC:
  482. case NL80211_IFTYPE_STATION:
  483. case NL80211_IFTYPE_P2P_CLIENT:
  484. if (!wdev->current_bss) {
  485. err = -ENOTCONN;
  486. break;
  487. }
  488. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  489. mgmt->bssid)) {
  490. err = -ENOTCONN;
  491. break;
  492. }
  493. /*
  494. * check for IBSS DA must be done by driver as
  495. * cfg80211 doesn't track the stations
  496. */
  497. if (wdev->iftype == NL80211_IFTYPE_ADHOC)
  498. break;
  499. /* for station, check that DA is the AP */
  500. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  501. mgmt->da)) {
  502. err = -ENOTCONN;
  503. break;
  504. }
  505. break;
  506. case NL80211_IFTYPE_AP:
  507. case NL80211_IFTYPE_P2P_GO:
  508. case NL80211_IFTYPE_AP_VLAN:
  509. if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
  510. err = -EINVAL;
  511. break;
  512. case NL80211_IFTYPE_MESH_POINT:
  513. if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
  514. err = -EINVAL;
  515. break;
  516. }
  517. /*
  518. * check for mesh DA must be done by driver as
  519. * cfg80211 doesn't track the stations
  520. */
  521. break;
  522. case NL80211_IFTYPE_P2P_DEVICE:
  523. /*
  524. * fall through, P2P device only supports
  525. * public action frames
  526. */
  527. default:
  528. err = -EOPNOTSUPP;
  529. break;
  530. }
  531. wdev_unlock(wdev);
  532. if (err)
  533. return err;
  534. }
  535. if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
  536. return -EINVAL;
  537. /* Transmit the Action frame as requested by user space */
  538. return rdev_mgmt_tx(rdev, wdev, chan, offchan,
  539. wait, buf, len, no_cck, dont_wait_for_ack,
  540. cookie);
  541. }
  542. bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
  543. const u8 *buf, size_t len, gfp_t gfp)
  544. {
  545. struct wiphy *wiphy = wdev->wiphy;
  546. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  547. struct cfg80211_mgmt_registration *reg;
  548. const struct ieee80211_txrx_stypes *stypes =
  549. &wiphy->mgmt_stypes[wdev->iftype];
  550. struct ieee80211_mgmt *mgmt = (void *)buf;
  551. const u8 *data;
  552. int data_len;
  553. bool result = false;
  554. __le16 ftype = mgmt->frame_control &
  555. cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
  556. u16 stype;
  557. trace_cfg80211_rx_mgmt(wdev, freq, sig_mbm);
  558. stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
  559. if (!(stypes->rx & BIT(stype))) {
  560. trace_cfg80211_return_bool(false);
  561. return false;
  562. }
  563. data = buf + ieee80211_hdrlen(mgmt->frame_control);
  564. data_len = len - ieee80211_hdrlen(mgmt->frame_control);
  565. spin_lock_bh(&wdev->mgmt_registrations_lock);
  566. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  567. if (reg->frame_type != ftype)
  568. continue;
  569. if (reg->match_len > data_len)
  570. continue;
  571. if (memcmp(reg->match, data, reg->match_len))
  572. continue;
  573. /* found match! */
  574. /* Indicate the received Action frame to user space */
  575. if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
  576. freq, sig_mbm,
  577. buf, len, gfp))
  578. continue;
  579. result = true;
  580. break;
  581. }
  582. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  583. trace_cfg80211_return_bool(result);
  584. return result;
  585. }
  586. EXPORT_SYMBOL(cfg80211_rx_mgmt);
  587. void cfg80211_dfs_channels_update_work(struct work_struct *work)
  588. {
  589. struct delayed_work *delayed_work;
  590. struct cfg80211_registered_device *rdev;
  591. struct cfg80211_chan_def chandef;
  592. struct ieee80211_supported_band *sband;
  593. struct ieee80211_channel *c;
  594. struct wiphy *wiphy;
  595. bool check_again = false;
  596. unsigned long timeout, next_time = 0;
  597. int bandid, i;
  598. delayed_work = container_of(work, struct delayed_work, work);
  599. rdev = container_of(delayed_work, struct cfg80211_registered_device,
  600. dfs_update_channels_wk);
  601. wiphy = &rdev->wiphy;
  602. rtnl_lock();
  603. for (bandid = 0; bandid < IEEE80211_NUM_BANDS; bandid++) {
  604. sband = wiphy->bands[bandid];
  605. if (!sband)
  606. continue;
  607. for (i = 0; i < sband->n_channels; i++) {
  608. c = &sband->channels[i];
  609. if (c->dfs_state != NL80211_DFS_UNAVAILABLE)
  610. continue;
  611. timeout = c->dfs_state_entered +
  612. IEEE80211_DFS_MIN_NOP_TIME_MS;
  613. if (time_after_eq(jiffies, timeout)) {
  614. c->dfs_state = NL80211_DFS_USABLE;
  615. cfg80211_chandef_create(&chandef, c,
  616. NL80211_CHAN_NO_HT);
  617. nl80211_radar_notify(rdev, &chandef,
  618. NL80211_RADAR_NOP_FINISHED,
  619. NULL, GFP_ATOMIC);
  620. continue;
  621. }
  622. if (!check_again)
  623. next_time = timeout - jiffies;
  624. else
  625. next_time = min(next_time, timeout - jiffies);
  626. check_again = true;
  627. }
  628. }
  629. rtnl_unlock();
  630. /* reschedule if there are other channels waiting to be cleared again */
  631. if (check_again)
  632. queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
  633. next_time);
  634. }
  635. void cfg80211_radar_event(struct wiphy *wiphy,
  636. struct cfg80211_chan_def *chandef,
  637. gfp_t gfp)
  638. {
  639. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  640. unsigned long timeout;
  641. trace_cfg80211_radar_event(wiphy, chandef);
  642. /* only set the chandef supplied channel to unavailable, in
  643. * case the radar is detected on only one of multiple channels
  644. * spanned by the chandef.
  645. */
  646. cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
  647. timeout = msecs_to_jiffies(IEEE80211_DFS_MIN_NOP_TIME_MS);
  648. queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
  649. timeout);
  650. nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
  651. }
  652. EXPORT_SYMBOL(cfg80211_radar_event);
  653. void cfg80211_cac_event(struct net_device *netdev,
  654. enum nl80211_radar_event event, gfp_t gfp)
  655. {
  656. struct wireless_dev *wdev = netdev->ieee80211_ptr;
  657. struct wiphy *wiphy = wdev->wiphy;
  658. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  659. struct cfg80211_chan_def chandef;
  660. unsigned long timeout;
  661. trace_cfg80211_cac_event(netdev, event);
  662. if (WARN_ON(!wdev->cac_started))
  663. return;
  664. if (WARN_ON(!wdev->channel))
  665. return;
  666. cfg80211_chandef_create(&chandef, wdev->channel, NL80211_CHAN_NO_HT);
  667. switch (event) {
  668. case NL80211_RADAR_CAC_FINISHED:
  669. timeout = wdev->cac_start_time +
  670. msecs_to_jiffies(IEEE80211_DFS_MIN_CAC_TIME_MS);
  671. WARN_ON(!time_after_eq(jiffies, timeout));
  672. cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_AVAILABLE);
  673. break;
  674. case NL80211_RADAR_CAC_ABORTED:
  675. break;
  676. default:
  677. WARN_ON(1);
  678. return;
  679. }
  680. wdev->cac_started = false;
  681. nl80211_radar_notify(rdev, &chandef, event, netdev, gfp);
  682. }
  683. EXPORT_SYMBOL(cfg80211_cac_event);