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