mlme.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 ieee80211_channel *chan, bool offchan,
  418. unsigned int wait, const u8 *buf, size_t len,
  419. bool no_cck, bool dont_wait_for_ack, u64 *cookie)
  420. {
  421. const struct ieee80211_mgmt *mgmt;
  422. u16 stype;
  423. if (!wdev->wiphy->mgmt_stypes)
  424. return -EOPNOTSUPP;
  425. if (!rdev->ops->mgmt_tx)
  426. return -EOPNOTSUPP;
  427. if (len < 24 + 1)
  428. return -EINVAL;
  429. mgmt = (const struct ieee80211_mgmt *) buf;
  430. if (!ieee80211_is_mgmt(mgmt->frame_control))
  431. return -EINVAL;
  432. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  433. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
  434. return -EINVAL;
  435. if (ieee80211_is_action(mgmt->frame_control) &&
  436. mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
  437. int err = 0;
  438. wdev_lock(wdev);
  439. switch (wdev->iftype) {
  440. case NL80211_IFTYPE_ADHOC:
  441. case NL80211_IFTYPE_STATION:
  442. case NL80211_IFTYPE_P2P_CLIENT:
  443. if (!wdev->current_bss) {
  444. err = -ENOTCONN;
  445. break;
  446. }
  447. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  448. mgmt->bssid)) {
  449. err = -ENOTCONN;
  450. break;
  451. }
  452. /*
  453. * check for IBSS DA must be done by driver as
  454. * cfg80211 doesn't track the stations
  455. */
  456. if (wdev->iftype == NL80211_IFTYPE_ADHOC)
  457. break;
  458. /* for station, check that DA is the AP */
  459. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  460. mgmt->da)) {
  461. err = -ENOTCONN;
  462. break;
  463. }
  464. break;
  465. case NL80211_IFTYPE_AP:
  466. case NL80211_IFTYPE_P2P_GO:
  467. case NL80211_IFTYPE_AP_VLAN:
  468. if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
  469. err = -EINVAL;
  470. break;
  471. case NL80211_IFTYPE_MESH_POINT:
  472. if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
  473. err = -EINVAL;
  474. break;
  475. }
  476. /*
  477. * check for mesh DA must be done by driver as
  478. * cfg80211 doesn't track the stations
  479. */
  480. break;
  481. case NL80211_IFTYPE_P2P_DEVICE:
  482. /*
  483. * fall through, P2P device only supports
  484. * public action frames
  485. */
  486. default:
  487. err = -EOPNOTSUPP;
  488. break;
  489. }
  490. wdev_unlock(wdev);
  491. if (err)
  492. return err;
  493. }
  494. if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
  495. return -EINVAL;
  496. /* Transmit the Action frame as requested by user space */
  497. return rdev_mgmt_tx(rdev, wdev, chan, offchan,
  498. wait, buf, len, no_cck, dont_wait_for_ack,
  499. cookie);
  500. }
  501. bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
  502. const u8 *buf, size_t len, gfp_t gfp)
  503. {
  504. struct wiphy *wiphy = wdev->wiphy;
  505. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  506. struct cfg80211_mgmt_registration *reg;
  507. const struct ieee80211_txrx_stypes *stypes =
  508. &wiphy->mgmt_stypes[wdev->iftype];
  509. struct ieee80211_mgmt *mgmt = (void *)buf;
  510. const u8 *data;
  511. int data_len;
  512. bool result = false;
  513. __le16 ftype = mgmt->frame_control &
  514. cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
  515. u16 stype;
  516. trace_cfg80211_rx_mgmt(wdev, freq, sig_mbm);
  517. stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
  518. if (!(stypes->rx & BIT(stype))) {
  519. trace_cfg80211_return_bool(false);
  520. return false;
  521. }
  522. data = buf + ieee80211_hdrlen(mgmt->frame_control);
  523. data_len = len - ieee80211_hdrlen(mgmt->frame_control);
  524. spin_lock_bh(&wdev->mgmt_registrations_lock);
  525. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  526. if (reg->frame_type != ftype)
  527. continue;
  528. if (reg->match_len > data_len)
  529. continue;
  530. if (memcmp(reg->match, data, reg->match_len))
  531. continue;
  532. /* found match! */
  533. /* Indicate the received Action frame to user space */
  534. if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
  535. freq, sig_mbm,
  536. buf, len, gfp))
  537. continue;
  538. result = true;
  539. break;
  540. }
  541. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  542. trace_cfg80211_return_bool(result);
  543. return result;
  544. }
  545. EXPORT_SYMBOL(cfg80211_rx_mgmt);
  546. void cfg80211_dfs_channels_update_work(struct work_struct *work)
  547. {
  548. struct delayed_work *delayed_work;
  549. struct cfg80211_registered_device *rdev;
  550. struct cfg80211_chan_def chandef;
  551. struct ieee80211_supported_band *sband;
  552. struct ieee80211_channel *c;
  553. struct wiphy *wiphy;
  554. bool check_again = false;
  555. unsigned long timeout, next_time = 0;
  556. int bandid, i;
  557. delayed_work = container_of(work, struct delayed_work, work);
  558. rdev = container_of(delayed_work, struct cfg80211_registered_device,
  559. dfs_update_channels_wk);
  560. wiphy = &rdev->wiphy;
  561. rtnl_lock();
  562. for (bandid = 0; bandid < IEEE80211_NUM_BANDS; bandid++) {
  563. sband = wiphy->bands[bandid];
  564. if (!sband)
  565. continue;
  566. for (i = 0; i < sband->n_channels; i++) {
  567. c = &sband->channels[i];
  568. if (c->dfs_state != NL80211_DFS_UNAVAILABLE)
  569. continue;
  570. timeout = c->dfs_state_entered +
  571. IEEE80211_DFS_MIN_NOP_TIME_MS;
  572. if (time_after_eq(jiffies, timeout)) {
  573. c->dfs_state = NL80211_DFS_USABLE;
  574. cfg80211_chandef_create(&chandef, c,
  575. NL80211_CHAN_NO_HT);
  576. nl80211_radar_notify(rdev, &chandef,
  577. NL80211_RADAR_NOP_FINISHED,
  578. NULL, GFP_ATOMIC);
  579. continue;
  580. }
  581. if (!check_again)
  582. next_time = timeout - jiffies;
  583. else
  584. next_time = min(next_time, timeout - jiffies);
  585. check_again = true;
  586. }
  587. }
  588. rtnl_unlock();
  589. /* reschedule if there are other channels waiting to be cleared again */
  590. if (check_again)
  591. queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
  592. next_time);
  593. }
  594. void cfg80211_radar_event(struct wiphy *wiphy,
  595. struct cfg80211_chan_def *chandef,
  596. gfp_t gfp)
  597. {
  598. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  599. unsigned long timeout;
  600. trace_cfg80211_radar_event(wiphy, chandef);
  601. /* only set the chandef supplied channel to unavailable, in
  602. * case the radar is detected on only one of multiple channels
  603. * spanned by the chandef.
  604. */
  605. cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
  606. timeout = msecs_to_jiffies(IEEE80211_DFS_MIN_NOP_TIME_MS);
  607. queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
  608. timeout);
  609. nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
  610. }
  611. EXPORT_SYMBOL(cfg80211_radar_event);
  612. void cfg80211_cac_event(struct net_device *netdev,
  613. enum nl80211_radar_event event, gfp_t gfp)
  614. {
  615. struct wireless_dev *wdev = netdev->ieee80211_ptr;
  616. struct wiphy *wiphy = wdev->wiphy;
  617. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  618. struct cfg80211_chan_def chandef;
  619. unsigned long timeout;
  620. trace_cfg80211_cac_event(netdev, event);
  621. if (WARN_ON(!wdev->cac_started))
  622. return;
  623. if (WARN_ON(!wdev->channel))
  624. return;
  625. cfg80211_chandef_create(&chandef, wdev->channel, NL80211_CHAN_NO_HT);
  626. switch (event) {
  627. case NL80211_RADAR_CAC_FINISHED:
  628. timeout = wdev->cac_start_time +
  629. msecs_to_jiffies(IEEE80211_DFS_MIN_CAC_TIME_MS);
  630. WARN_ON(!time_after_eq(jiffies, timeout));
  631. cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_AVAILABLE);
  632. break;
  633. case NL80211_RADAR_CAC_ABORTED:
  634. break;
  635. default:
  636. WARN_ON(1);
  637. return;
  638. }
  639. wdev->cac_started = false;
  640. nl80211_radar_notify(rdev, &chandef, event, netdev, gfp);
  641. }
  642. EXPORT_SYMBOL(cfg80211_cac_event);