mlme.c 20 KB

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