mlme.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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, int uapsd_queues)
  20. {
  21. struct wireless_dev *wdev = dev->ieee80211_ptr;
  22. struct wiphy *wiphy = wdev->wiphy;
  23. struct cfg80211_registered_device *rdev = wiphy_to_rdev(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, uapsd_queues);
  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_rdev(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_rdev(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_rdev(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_rdev(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_rdev(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_rdev(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. IEEE80211_BSS_TYPE_ESS,
  192. IEEE80211_PRIVACY_ANY);
  193. if (!req.bss)
  194. return -ENOENT;
  195. err = rdev_auth(rdev, dev, &req);
  196. cfg80211_put_bss(&rdev->wiphy, req.bss);
  197. return err;
  198. }
  199. /* Do a logical ht_capa &= ht_capa_mask. */
  200. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  201. const struct ieee80211_ht_cap *ht_capa_mask)
  202. {
  203. int i;
  204. u8 *p1, *p2;
  205. if (!ht_capa_mask) {
  206. memset(ht_capa, 0, sizeof(*ht_capa));
  207. return;
  208. }
  209. p1 = (u8*)(ht_capa);
  210. p2 = (u8*)(ht_capa_mask);
  211. for (i = 0; i<sizeof(*ht_capa); i++)
  212. p1[i] &= p2[i];
  213. }
  214. /* Do a logical ht_capa &= ht_capa_mask. */
  215. void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
  216. const struct ieee80211_vht_cap *vht_capa_mask)
  217. {
  218. int i;
  219. u8 *p1, *p2;
  220. if (!vht_capa_mask) {
  221. memset(vht_capa, 0, sizeof(*vht_capa));
  222. return;
  223. }
  224. p1 = (u8*)(vht_capa);
  225. p2 = (u8*)(vht_capa_mask);
  226. for (i = 0; i < sizeof(*vht_capa); i++)
  227. p1[i] &= p2[i];
  228. }
  229. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  230. struct net_device *dev,
  231. struct ieee80211_channel *chan,
  232. const u8 *bssid,
  233. const u8 *ssid, int ssid_len,
  234. struct cfg80211_assoc_request *req)
  235. {
  236. struct wireless_dev *wdev = dev->ieee80211_ptr;
  237. int err;
  238. ASSERT_WDEV_LOCK(wdev);
  239. if (wdev->current_bss &&
  240. (!req->prev_bssid || !ether_addr_equal(wdev->current_bss->pub.bssid,
  241. req->prev_bssid)))
  242. return -EALREADY;
  243. cfg80211_oper_and_ht_capa(&req->ht_capa_mask,
  244. rdev->wiphy.ht_capa_mod_mask);
  245. cfg80211_oper_and_vht_capa(&req->vht_capa_mask,
  246. rdev->wiphy.vht_capa_mod_mask);
  247. req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  248. IEEE80211_BSS_TYPE_ESS,
  249. IEEE80211_PRIVACY_ANY);
  250. if (!req->bss)
  251. return -ENOENT;
  252. err = rdev_assoc(rdev, dev, req);
  253. if (!err)
  254. cfg80211_hold_bss(bss_from_pub(req->bss));
  255. else
  256. cfg80211_put_bss(&rdev->wiphy, req->bss);
  257. return err;
  258. }
  259. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  260. struct net_device *dev, const u8 *bssid,
  261. const u8 *ie, int ie_len, u16 reason,
  262. bool local_state_change)
  263. {
  264. struct wireless_dev *wdev = dev->ieee80211_ptr;
  265. struct cfg80211_deauth_request req = {
  266. .bssid = bssid,
  267. .reason_code = reason,
  268. .ie = ie,
  269. .ie_len = ie_len,
  270. .local_state_change = local_state_change,
  271. };
  272. ASSERT_WDEV_LOCK(wdev);
  273. if (local_state_change &&
  274. (!wdev->current_bss ||
  275. !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
  276. return 0;
  277. return rdev_deauth(rdev, dev, &req);
  278. }
  279. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  280. struct net_device *dev, const u8 *bssid,
  281. const u8 *ie, int ie_len, u16 reason,
  282. bool local_state_change)
  283. {
  284. struct wireless_dev *wdev = dev->ieee80211_ptr;
  285. struct cfg80211_disassoc_request req = {
  286. .reason_code = reason,
  287. .local_state_change = local_state_change,
  288. .ie = ie,
  289. .ie_len = ie_len,
  290. };
  291. int err;
  292. ASSERT_WDEV_LOCK(wdev);
  293. if (!wdev->current_bss)
  294. return -ENOTCONN;
  295. if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
  296. req.bss = &wdev->current_bss->pub;
  297. else
  298. return -ENOTCONN;
  299. err = rdev_disassoc(rdev, dev, &req);
  300. if (err)
  301. return err;
  302. /* driver should have reported the disassoc */
  303. WARN_ON(wdev->current_bss);
  304. return 0;
  305. }
  306. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  307. struct net_device *dev)
  308. {
  309. struct wireless_dev *wdev = dev->ieee80211_ptr;
  310. u8 bssid[ETH_ALEN];
  311. ASSERT_WDEV_LOCK(wdev);
  312. if (!rdev->ops->deauth)
  313. return;
  314. if (!wdev->current_bss)
  315. return;
  316. memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
  317. cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
  318. WLAN_REASON_DEAUTH_LEAVING, false);
  319. }
  320. struct cfg80211_mgmt_registration {
  321. struct list_head list;
  322. u32 nlportid;
  323. int match_len;
  324. __le16 frame_type;
  325. u8 match[];
  326. };
  327. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
  328. u16 frame_type, const u8 *match_data,
  329. int match_len)
  330. {
  331. struct wiphy *wiphy = wdev->wiphy;
  332. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  333. struct cfg80211_mgmt_registration *reg, *nreg;
  334. int err = 0;
  335. u16 mgmt_type;
  336. if (!wdev->wiphy->mgmt_stypes)
  337. return -EOPNOTSUPP;
  338. if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
  339. return -EINVAL;
  340. if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
  341. return -EINVAL;
  342. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  343. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
  344. return -EINVAL;
  345. nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
  346. if (!nreg)
  347. return -ENOMEM;
  348. spin_lock_bh(&wdev->mgmt_registrations_lock);
  349. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  350. int mlen = min(match_len, reg->match_len);
  351. if (frame_type != le16_to_cpu(reg->frame_type))
  352. continue;
  353. if (memcmp(reg->match, match_data, mlen) == 0) {
  354. err = -EALREADY;
  355. break;
  356. }
  357. }
  358. if (err) {
  359. kfree(nreg);
  360. goto out;
  361. }
  362. memcpy(nreg->match, match_data, match_len);
  363. nreg->match_len = match_len;
  364. nreg->nlportid = snd_portid;
  365. nreg->frame_type = cpu_to_le16(frame_type);
  366. list_add(&nreg->list, &wdev->mgmt_registrations);
  367. if (rdev->ops->mgmt_frame_register)
  368. rdev_mgmt_frame_register(rdev, wdev, frame_type, true);
  369. out:
  370. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  371. return err;
  372. }
  373. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
  374. {
  375. struct wiphy *wiphy = wdev->wiphy;
  376. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  377. struct cfg80211_mgmt_registration *reg, *tmp;
  378. spin_lock_bh(&wdev->mgmt_registrations_lock);
  379. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  380. if (reg->nlportid != nlportid)
  381. continue;
  382. if (rdev->ops->mgmt_frame_register) {
  383. u16 frame_type = le16_to_cpu(reg->frame_type);
  384. rdev_mgmt_frame_register(rdev, wdev,
  385. frame_type, false);
  386. }
  387. list_del(&reg->list);
  388. kfree(reg);
  389. }
  390. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  391. if (nlportid && rdev->crit_proto_nlportid == nlportid) {
  392. rdev->crit_proto_nlportid = 0;
  393. rdev_crit_proto_stop(rdev, wdev);
  394. }
  395. if (nlportid == wdev->ap_unexpected_nlportid)
  396. wdev->ap_unexpected_nlportid = 0;
  397. }
  398. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
  399. {
  400. struct cfg80211_mgmt_registration *reg, *tmp;
  401. spin_lock_bh(&wdev->mgmt_registrations_lock);
  402. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  403. list_del(&reg->list);
  404. kfree(reg);
  405. }
  406. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  407. }
  408. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  409. struct wireless_dev *wdev,
  410. struct cfg80211_mgmt_tx_params *params, u64 *cookie)
  411. {
  412. const struct ieee80211_mgmt *mgmt;
  413. u16 stype;
  414. if (!wdev->wiphy->mgmt_stypes)
  415. return -EOPNOTSUPP;
  416. if (!rdev->ops->mgmt_tx)
  417. return -EOPNOTSUPP;
  418. if (params->len < 24 + 1)
  419. return -EINVAL;
  420. mgmt = (const struct ieee80211_mgmt *)params->buf;
  421. if (!ieee80211_is_mgmt(mgmt->frame_control))
  422. return -EINVAL;
  423. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  424. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
  425. return -EINVAL;
  426. if (ieee80211_is_action(mgmt->frame_control) &&
  427. mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
  428. int err = 0;
  429. wdev_lock(wdev);
  430. switch (wdev->iftype) {
  431. case NL80211_IFTYPE_ADHOC:
  432. case NL80211_IFTYPE_STATION:
  433. case NL80211_IFTYPE_P2P_CLIENT:
  434. if (!wdev->current_bss) {
  435. err = -ENOTCONN;
  436. break;
  437. }
  438. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  439. mgmt->bssid)) {
  440. err = -ENOTCONN;
  441. break;
  442. }
  443. /*
  444. * check for IBSS DA must be done by driver as
  445. * cfg80211 doesn't track the stations
  446. */
  447. if (wdev->iftype == NL80211_IFTYPE_ADHOC)
  448. break;
  449. /* for station, check that DA is the AP */
  450. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  451. mgmt->da)) {
  452. err = -ENOTCONN;
  453. break;
  454. }
  455. break;
  456. case NL80211_IFTYPE_AP:
  457. case NL80211_IFTYPE_P2P_GO:
  458. case NL80211_IFTYPE_AP_VLAN:
  459. if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
  460. err = -EINVAL;
  461. break;
  462. case NL80211_IFTYPE_MESH_POINT:
  463. if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
  464. err = -EINVAL;
  465. break;
  466. }
  467. /*
  468. * check for mesh DA must be done by driver as
  469. * cfg80211 doesn't track the stations
  470. */
  471. break;
  472. case NL80211_IFTYPE_P2P_DEVICE:
  473. /*
  474. * fall through, P2P device only supports
  475. * public action frames
  476. */
  477. default:
  478. err = -EOPNOTSUPP;
  479. break;
  480. }
  481. wdev_unlock(wdev);
  482. if (err)
  483. return err;
  484. }
  485. if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
  486. return -EINVAL;
  487. /* Transmit the Action frame as requested by user space */
  488. return rdev_mgmt_tx(rdev, wdev, params, cookie);
  489. }
  490. bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
  491. const u8 *buf, size_t len, u32 flags)
  492. {
  493. struct wiphy *wiphy = wdev->wiphy;
  494. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  495. struct cfg80211_mgmt_registration *reg;
  496. const struct ieee80211_txrx_stypes *stypes =
  497. &wiphy->mgmt_stypes[wdev->iftype];
  498. struct ieee80211_mgmt *mgmt = (void *)buf;
  499. const u8 *data;
  500. int data_len;
  501. bool result = false;
  502. __le16 ftype = mgmt->frame_control &
  503. cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
  504. u16 stype;
  505. trace_cfg80211_rx_mgmt(wdev, freq, sig_mbm);
  506. stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
  507. if (!(stypes->rx & BIT(stype))) {
  508. trace_cfg80211_return_bool(false);
  509. return false;
  510. }
  511. data = buf + ieee80211_hdrlen(mgmt->frame_control);
  512. data_len = len - ieee80211_hdrlen(mgmt->frame_control);
  513. spin_lock_bh(&wdev->mgmt_registrations_lock);
  514. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  515. if (reg->frame_type != ftype)
  516. continue;
  517. if (reg->match_len > data_len)
  518. continue;
  519. if (memcmp(reg->match, data, reg->match_len))
  520. continue;
  521. /* found match! */
  522. /* Indicate the received Action frame to user space */
  523. if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
  524. freq, sig_mbm,
  525. buf, len, flags, GFP_ATOMIC))
  526. continue;
  527. result = true;
  528. break;
  529. }
  530. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  531. trace_cfg80211_return_bool(result);
  532. return result;
  533. }
  534. EXPORT_SYMBOL(cfg80211_rx_mgmt);
  535. void cfg80211_dfs_channels_update_work(struct work_struct *work)
  536. {
  537. struct delayed_work *delayed_work;
  538. struct cfg80211_registered_device *rdev;
  539. struct cfg80211_chan_def chandef;
  540. struct ieee80211_supported_band *sband;
  541. struct ieee80211_channel *c;
  542. struct wiphy *wiphy;
  543. bool check_again = false;
  544. unsigned long timeout, next_time = 0;
  545. int bandid, i;
  546. delayed_work = container_of(work, struct delayed_work, work);
  547. rdev = container_of(delayed_work, struct cfg80211_registered_device,
  548. dfs_update_channels_wk);
  549. wiphy = &rdev->wiphy;
  550. rtnl_lock();
  551. for (bandid = 0; bandid < IEEE80211_NUM_BANDS; bandid++) {
  552. sband = wiphy->bands[bandid];
  553. if (!sband)
  554. continue;
  555. for (i = 0; i < sband->n_channels; i++) {
  556. c = &sband->channels[i];
  557. if (c->dfs_state != NL80211_DFS_UNAVAILABLE)
  558. continue;
  559. timeout = c->dfs_state_entered + msecs_to_jiffies(
  560. IEEE80211_DFS_MIN_NOP_TIME_MS);
  561. if (time_after_eq(jiffies, timeout)) {
  562. c->dfs_state = NL80211_DFS_USABLE;
  563. c->dfs_state_entered = jiffies;
  564. cfg80211_chandef_create(&chandef, c,
  565. NL80211_CHAN_NO_HT);
  566. nl80211_radar_notify(rdev, &chandef,
  567. NL80211_RADAR_NOP_FINISHED,
  568. NULL, GFP_ATOMIC);
  569. continue;
  570. }
  571. if (!check_again)
  572. next_time = timeout - jiffies;
  573. else
  574. next_time = min(next_time, timeout - jiffies);
  575. check_again = true;
  576. }
  577. }
  578. rtnl_unlock();
  579. /* reschedule if there are other channels waiting to be cleared again */
  580. if (check_again)
  581. queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
  582. next_time);
  583. }
  584. void cfg80211_radar_event(struct wiphy *wiphy,
  585. struct cfg80211_chan_def *chandef,
  586. gfp_t gfp)
  587. {
  588. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  589. unsigned long timeout;
  590. trace_cfg80211_radar_event(wiphy, chandef);
  591. /* only set the chandef supplied channel to unavailable, in
  592. * case the radar is detected on only one of multiple channels
  593. * spanned by the chandef.
  594. */
  595. cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
  596. timeout = msecs_to_jiffies(IEEE80211_DFS_MIN_NOP_TIME_MS);
  597. queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
  598. timeout);
  599. nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
  600. }
  601. EXPORT_SYMBOL(cfg80211_radar_event);
  602. void cfg80211_cac_event(struct net_device *netdev,
  603. const struct cfg80211_chan_def *chandef,
  604. enum nl80211_radar_event event, gfp_t gfp)
  605. {
  606. struct wireless_dev *wdev = netdev->ieee80211_ptr;
  607. struct wiphy *wiphy = wdev->wiphy;
  608. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  609. unsigned long timeout;
  610. trace_cfg80211_cac_event(netdev, event);
  611. if (WARN_ON(!wdev->cac_started))
  612. return;
  613. if (WARN_ON(!wdev->chandef.chan))
  614. return;
  615. switch (event) {
  616. case NL80211_RADAR_CAC_FINISHED:
  617. timeout = wdev->cac_start_time +
  618. msecs_to_jiffies(wdev->cac_time_ms);
  619. WARN_ON(!time_after_eq(jiffies, timeout));
  620. cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE);
  621. break;
  622. case NL80211_RADAR_CAC_ABORTED:
  623. break;
  624. default:
  625. WARN_ON(1);
  626. return;
  627. }
  628. wdev->cac_started = false;
  629. nl80211_radar_notify(rdev, chandef, event, netdev, gfp);
  630. }
  631. EXPORT_SYMBOL(cfg80211_cac_event);