mlme.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. void cfg80211_send_rx_auth(struct net_device *dev, const u8 *buf, size_t len)
  18. {
  19. struct wireless_dev *wdev = dev->ieee80211_ptr;
  20. struct wiphy *wiphy = wdev->wiphy;
  21. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  22. wdev_lock(wdev);
  23. nl80211_send_rx_auth(rdev, dev, buf, len, GFP_KERNEL);
  24. cfg80211_sme_rx_auth(dev, buf, len);
  25. wdev_unlock(wdev);
  26. }
  27. EXPORT_SYMBOL(cfg80211_send_rx_auth);
  28. void cfg80211_send_rx_assoc(struct net_device *dev, struct cfg80211_bss *bss,
  29. const u8 *buf, size_t len)
  30. {
  31. u16 status_code;
  32. struct wireless_dev *wdev = dev->ieee80211_ptr;
  33. struct wiphy *wiphy = wdev->wiphy;
  34. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  35. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  36. u8 *ie = mgmt->u.assoc_resp.variable;
  37. int ieoffs = offsetof(struct ieee80211_mgmt, u.assoc_resp.variable);
  38. wdev_lock(wdev);
  39. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  40. /*
  41. * This is a bit of a hack, we don't notify userspace of
  42. * a (re-)association reply if we tried to send a reassoc
  43. * and got a reject -- we only try again with an assoc
  44. * frame instead of reassoc.
  45. */
  46. if (status_code != WLAN_STATUS_SUCCESS && wdev->conn &&
  47. cfg80211_sme_failed_reassoc(wdev)) {
  48. cfg80211_put_bss(bss);
  49. goto out;
  50. }
  51. nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL);
  52. if (status_code != WLAN_STATUS_SUCCESS && wdev->conn) {
  53. cfg80211_sme_failed_assoc(wdev);
  54. /*
  55. * do not call connect_result() now because the
  56. * sme will schedule work that does it later.
  57. */
  58. cfg80211_put_bss(bss);
  59. goto out;
  60. }
  61. if (!wdev->conn && wdev->sme_state == CFG80211_SME_IDLE) {
  62. /*
  63. * This is for the userspace SME, the CONNECTING
  64. * state will be changed to CONNECTED by
  65. * __cfg80211_connect_result() below.
  66. */
  67. wdev->sme_state = CFG80211_SME_CONNECTING;
  68. }
  69. /* this consumes the bss reference */
  70. __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, ie, len - ieoffs,
  71. status_code,
  72. status_code == WLAN_STATUS_SUCCESS, bss);
  73. out:
  74. wdev_unlock(wdev);
  75. }
  76. EXPORT_SYMBOL(cfg80211_send_rx_assoc);
  77. void __cfg80211_send_deauth(struct net_device *dev,
  78. const u8 *buf, size_t len)
  79. {
  80. struct wireless_dev *wdev = dev->ieee80211_ptr;
  81. struct wiphy *wiphy = wdev->wiphy;
  82. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  83. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  84. const u8 *bssid = mgmt->bssid;
  85. bool was_current = false;
  86. ASSERT_WDEV_LOCK(wdev);
  87. if (wdev->current_bss &&
  88. ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
  89. cfg80211_unhold_bss(wdev->current_bss);
  90. cfg80211_put_bss(&wdev->current_bss->pub);
  91. wdev->current_bss = NULL;
  92. was_current = true;
  93. }
  94. nl80211_send_deauth(rdev, dev, buf, len, GFP_KERNEL);
  95. if (wdev->sme_state == CFG80211_SME_CONNECTED && was_current) {
  96. u16 reason_code;
  97. bool from_ap;
  98. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  99. from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
  100. __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
  101. } else if (wdev->sme_state == CFG80211_SME_CONNECTING) {
  102. __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
  103. WLAN_STATUS_UNSPECIFIED_FAILURE,
  104. false, NULL);
  105. }
  106. }
  107. EXPORT_SYMBOL(__cfg80211_send_deauth);
  108. void cfg80211_send_deauth(struct net_device *dev, const u8 *buf, size_t len)
  109. {
  110. struct wireless_dev *wdev = dev->ieee80211_ptr;
  111. wdev_lock(wdev);
  112. __cfg80211_send_deauth(dev, buf, len);
  113. wdev_unlock(wdev);
  114. }
  115. EXPORT_SYMBOL(cfg80211_send_deauth);
  116. void __cfg80211_send_disassoc(struct net_device *dev,
  117. const u8 *buf, size_t len)
  118. {
  119. struct wireless_dev *wdev = dev->ieee80211_ptr;
  120. struct wiphy *wiphy = wdev->wiphy;
  121. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  122. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
  123. const u8 *bssid = mgmt->bssid;
  124. u16 reason_code;
  125. bool from_ap;
  126. ASSERT_WDEV_LOCK(wdev);
  127. nl80211_send_disassoc(rdev, dev, buf, len, GFP_KERNEL);
  128. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  129. return;
  130. if (wdev->current_bss &&
  131. ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
  132. cfg80211_sme_disassoc(dev, wdev->current_bss);
  133. cfg80211_unhold_bss(wdev->current_bss);
  134. cfg80211_put_bss(&wdev->current_bss->pub);
  135. wdev->current_bss = NULL;
  136. } else
  137. WARN_ON(1);
  138. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  139. from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
  140. __cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
  141. }
  142. EXPORT_SYMBOL(__cfg80211_send_disassoc);
  143. void cfg80211_send_disassoc(struct net_device *dev, const u8 *buf, size_t len)
  144. {
  145. struct wireless_dev *wdev = dev->ieee80211_ptr;
  146. wdev_lock(wdev);
  147. __cfg80211_send_disassoc(dev, buf, len);
  148. wdev_unlock(wdev);
  149. }
  150. EXPORT_SYMBOL(cfg80211_send_disassoc);
  151. void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
  152. size_t len)
  153. {
  154. struct wireless_dev *wdev = dev->ieee80211_ptr;
  155. struct wiphy *wiphy = wdev->wiphy;
  156. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  157. nl80211_send_unprot_deauth(rdev, dev, buf, len, GFP_ATOMIC);
  158. }
  159. EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
  160. void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
  161. size_t len)
  162. {
  163. struct wireless_dev *wdev = dev->ieee80211_ptr;
  164. struct wiphy *wiphy = wdev->wiphy;
  165. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  166. nl80211_send_unprot_disassoc(rdev, dev, buf, len, GFP_ATOMIC);
  167. }
  168. EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
  169. void cfg80211_send_auth_timeout(struct net_device *dev, const u8 *addr)
  170. {
  171. struct wireless_dev *wdev = dev->ieee80211_ptr;
  172. struct wiphy *wiphy = wdev->wiphy;
  173. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  174. wdev_lock(wdev);
  175. nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
  176. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  177. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  178. WLAN_STATUS_UNSPECIFIED_FAILURE,
  179. false, NULL);
  180. wdev_unlock(wdev);
  181. }
  182. EXPORT_SYMBOL(cfg80211_send_auth_timeout);
  183. void cfg80211_send_assoc_timeout(struct net_device *dev, const u8 *addr)
  184. {
  185. struct wireless_dev *wdev = dev->ieee80211_ptr;
  186. struct wiphy *wiphy = wdev->wiphy;
  187. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  188. wdev_lock(wdev);
  189. nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
  190. if (wdev->sme_state == CFG80211_SME_CONNECTING)
  191. __cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
  192. WLAN_STATUS_UNSPECIFIED_FAILURE,
  193. false, NULL);
  194. wdev_unlock(wdev);
  195. }
  196. EXPORT_SYMBOL(cfg80211_send_assoc_timeout);
  197. void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
  198. enum nl80211_key_type key_type, int key_id,
  199. const u8 *tsc, gfp_t gfp)
  200. {
  201. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  202. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  203. #ifdef CONFIG_CFG80211_WEXT
  204. union iwreq_data wrqu;
  205. char *buf = kmalloc(128, gfp);
  206. if (buf) {
  207. sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
  208. "keyid=%d %scast addr=%pM)", key_id,
  209. key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
  210. addr);
  211. memset(&wrqu, 0, sizeof(wrqu));
  212. wrqu.data.length = strlen(buf);
  213. wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
  214. kfree(buf);
  215. }
  216. #endif
  217. nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
  218. }
  219. EXPORT_SYMBOL(cfg80211_michael_mic_failure);
  220. /* some MLME handling for userspace SME */
  221. int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  222. struct net_device *dev,
  223. struct ieee80211_channel *chan,
  224. enum nl80211_auth_type auth_type,
  225. const u8 *bssid,
  226. const u8 *ssid, int ssid_len,
  227. const u8 *ie, int ie_len,
  228. const u8 *key, int key_len, int key_idx)
  229. {
  230. struct wireless_dev *wdev = dev->ieee80211_ptr;
  231. struct cfg80211_auth_request req;
  232. int err;
  233. ASSERT_WDEV_LOCK(wdev);
  234. if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
  235. if (!key || !key_len || key_idx < 0 || key_idx > 4)
  236. return -EINVAL;
  237. if (wdev->current_bss &&
  238. ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
  239. return -EALREADY;
  240. memset(&req, 0, sizeof(req));
  241. req.ie = ie;
  242. req.ie_len = ie_len;
  243. req.auth_type = auth_type;
  244. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  245. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  246. req.key = key;
  247. req.key_len = key_len;
  248. req.key_idx = key_idx;
  249. if (!req.bss)
  250. return -ENOENT;
  251. err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
  252. CHAN_MODE_SHARED);
  253. if (err)
  254. goto out;
  255. err = rdev->ops->auth(&rdev->wiphy, dev, &req);
  256. out:
  257. cfg80211_put_bss(req.bss);
  258. return err;
  259. }
  260. int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
  261. struct net_device *dev, struct ieee80211_channel *chan,
  262. enum nl80211_auth_type auth_type, const u8 *bssid,
  263. const u8 *ssid, int ssid_len,
  264. const u8 *ie, int ie_len,
  265. const u8 *key, int key_len, int key_idx)
  266. {
  267. int err;
  268. mutex_lock(&rdev->devlist_mtx);
  269. wdev_lock(dev->ieee80211_ptr);
  270. err = __cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
  271. ssid, ssid_len, ie, ie_len,
  272. key, key_len, key_idx);
  273. wdev_unlock(dev->ieee80211_ptr);
  274. mutex_unlock(&rdev->devlist_mtx);
  275. return err;
  276. }
  277. /* Do a logical ht_capa &= ht_capa_mask. */
  278. void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
  279. const struct ieee80211_ht_cap *ht_capa_mask)
  280. {
  281. int i;
  282. u8 *p1, *p2;
  283. if (!ht_capa_mask) {
  284. memset(ht_capa, 0, sizeof(*ht_capa));
  285. return;
  286. }
  287. p1 = (u8*)(ht_capa);
  288. p2 = (u8*)(ht_capa_mask);
  289. for (i = 0; i<sizeof(*ht_capa); i++)
  290. p1[i] &= p2[i];
  291. }
  292. int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  293. struct net_device *dev,
  294. struct ieee80211_channel *chan,
  295. const u8 *bssid, const u8 *prev_bssid,
  296. const u8 *ssid, int ssid_len,
  297. const u8 *ie, int ie_len, bool use_mfp,
  298. struct cfg80211_crypto_settings *crypt,
  299. u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
  300. struct ieee80211_ht_cap *ht_capa_mask)
  301. {
  302. struct wireless_dev *wdev = dev->ieee80211_ptr;
  303. struct cfg80211_assoc_request req;
  304. int err;
  305. bool was_connected = false;
  306. ASSERT_WDEV_LOCK(wdev);
  307. memset(&req, 0, sizeof(req));
  308. if (wdev->current_bss && prev_bssid &&
  309. ether_addr_equal(wdev->current_bss->pub.bssid, prev_bssid)) {
  310. /*
  311. * Trying to reassociate: Allow this to proceed and let the old
  312. * association to be dropped when the new one is completed.
  313. */
  314. if (wdev->sme_state == CFG80211_SME_CONNECTED) {
  315. was_connected = true;
  316. wdev->sme_state = CFG80211_SME_CONNECTING;
  317. }
  318. } else if (wdev->current_bss)
  319. return -EALREADY;
  320. req.ie = ie;
  321. req.ie_len = ie_len;
  322. memcpy(&req.crypto, crypt, sizeof(req.crypto));
  323. req.use_mfp = use_mfp;
  324. req.prev_bssid = prev_bssid;
  325. req.flags = assoc_flags;
  326. if (ht_capa)
  327. memcpy(&req.ht_capa, ht_capa, sizeof(req.ht_capa));
  328. if (ht_capa_mask)
  329. memcpy(&req.ht_capa_mask, ht_capa_mask,
  330. sizeof(req.ht_capa_mask));
  331. cfg80211_oper_and_ht_capa(&req.ht_capa_mask,
  332. rdev->wiphy.ht_capa_mod_mask);
  333. req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
  334. WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
  335. if (!req.bss) {
  336. if (was_connected)
  337. wdev->sme_state = CFG80211_SME_CONNECTED;
  338. return -ENOENT;
  339. }
  340. err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
  341. CHAN_MODE_SHARED);
  342. if (err)
  343. goto out;
  344. err = rdev->ops->assoc(&rdev->wiphy, dev, &req);
  345. out:
  346. if (err) {
  347. if (was_connected)
  348. wdev->sme_state = CFG80211_SME_CONNECTED;
  349. cfg80211_put_bss(req.bss);
  350. }
  351. return err;
  352. }
  353. int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
  354. struct net_device *dev,
  355. struct ieee80211_channel *chan,
  356. const u8 *bssid, const u8 *prev_bssid,
  357. const u8 *ssid, int ssid_len,
  358. const u8 *ie, int ie_len, bool use_mfp,
  359. struct cfg80211_crypto_settings *crypt,
  360. u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
  361. struct ieee80211_ht_cap *ht_capa_mask)
  362. {
  363. struct wireless_dev *wdev = dev->ieee80211_ptr;
  364. int err;
  365. mutex_lock(&rdev->devlist_mtx);
  366. wdev_lock(wdev);
  367. err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
  368. ssid, ssid_len, ie, ie_len, use_mfp, crypt,
  369. assoc_flags, ht_capa, ht_capa_mask);
  370. wdev_unlock(wdev);
  371. mutex_unlock(&rdev->devlist_mtx);
  372. return err;
  373. }
  374. int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  375. struct net_device *dev, const u8 *bssid,
  376. const u8 *ie, int ie_len, u16 reason,
  377. bool local_state_change)
  378. {
  379. struct wireless_dev *wdev = dev->ieee80211_ptr;
  380. struct cfg80211_deauth_request req = {
  381. .bssid = bssid,
  382. .reason_code = reason,
  383. .ie = ie,
  384. .ie_len = ie_len,
  385. };
  386. ASSERT_WDEV_LOCK(wdev);
  387. if (local_state_change) {
  388. if (wdev->current_bss &&
  389. ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
  390. cfg80211_unhold_bss(wdev->current_bss);
  391. cfg80211_put_bss(&wdev->current_bss->pub);
  392. wdev->current_bss = NULL;
  393. }
  394. return 0;
  395. }
  396. return rdev->ops->deauth(&rdev->wiphy, dev, &req);
  397. }
  398. int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
  399. struct net_device *dev, const u8 *bssid,
  400. const u8 *ie, int ie_len, u16 reason,
  401. bool local_state_change)
  402. {
  403. struct wireless_dev *wdev = dev->ieee80211_ptr;
  404. int err;
  405. wdev_lock(wdev);
  406. err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
  407. local_state_change);
  408. wdev_unlock(wdev);
  409. return err;
  410. }
  411. static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  412. struct net_device *dev, const u8 *bssid,
  413. const u8 *ie, int ie_len, u16 reason,
  414. bool local_state_change)
  415. {
  416. struct wireless_dev *wdev = dev->ieee80211_ptr;
  417. struct cfg80211_disassoc_request req;
  418. ASSERT_WDEV_LOCK(wdev);
  419. if (wdev->sme_state != CFG80211_SME_CONNECTED)
  420. return -ENOTCONN;
  421. if (WARN_ON(!wdev->current_bss))
  422. return -ENOTCONN;
  423. memset(&req, 0, sizeof(req));
  424. req.reason_code = reason;
  425. req.local_state_change = local_state_change;
  426. req.ie = ie;
  427. req.ie_len = ie_len;
  428. if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
  429. req.bss = &wdev->current_bss->pub;
  430. else
  431. return -ENOTCONN;
  432. return rdev->ops->disassoc(&rdev->wiphy, dev, &req);
  433. }
  434. int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
  435. struct net_device *dev, const u8 *bssid,
  436. const u8 *ie, int ie_len, u16 reason,
  437. bool local_state_change)
  438. {
  439. struct wireless_dev *wdev = dev->ieee80211_ptr;
  440. int err;
  441. wdev_lock(wdev);
  442. err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
  443. local_state_change);
  444. wdev_unlock(wdev);
  445. return err;
  446. }
  447. void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
  448. struct net_device *dev)
  449. {
  450. struct wireless_dev *wdev = dev->ieee80211_ptr;
  451. struct cfg80211_deauth_request req;
  452. u8 bssid[ETH_ALEN];
  453. ASSERT_WDEV_LOCK(wdev);
  454. if (!rdev->ops->deauth)
  455. return;
  456. memset(&req, 0, sizeof(req));
  457. req.reason_code = WLAN_REASON_DEAUTH_LEAVING;
  458. req.ie = NULL;
  459. req.ie_len = 0;
  460. if (!wdev->current_bss)
  461. return;
  462. memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
  463. req.bssid = bssid;
  464. rdev->ops->deauth(&rdev->wiphy, dev, &req);
  465. if (wdev->current_bss) {
  466. cfg80211_unhold_bss(wdev->current_bss);
  467. cfg80211_put_bss(&wdev->current_bss->pub);
  468. wdev->current_bss = NULL;
  469. }
  470. }
  471. void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
  472. struct ieee80211_channel *chan,
  473. enum nl80211_channel_type channel_type,
  474. unsigned int duration, gfp_t gfp)
  475. {
  476. struct wiphy *wiphy = wdev->wiphy;
  477. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  478. nl80211_send_remain_on_channel(rdev, wdev, cookie, chan, channel_type,
  479. duration, gfp);
  480. }
  481. EXPORT_SYMBOL(cfg80211_ready_on_channel);
  482. void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
  483. struct ieee80211_channel *chan,
  484. enum nl80211_channel_type channel_type,
  485. gfp_t gfp)
  486. {
  487. struct wiphy *wiphy = wdev->wiphy;
  488. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  489. nl80211_send_remain_on_channel_cancel(rdev, wdev, cookie, chan,
  490. channel_type, gfp);
  491. }
  492. EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
  493. void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
  494. struct station_info *sinfo, gfp_t gfp)
  495. {
  496. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  497. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  498. nl80211_send_sta_event(rdev, dev, mac_addr, sinfo, gfp);
  499. }
  500. EXPORT_SYMBOL(cfg80211_new_sta);
  501. void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
  502. {
  503. struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
  504. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  505. nl80211_send_sta_del_event(rdev, dev, mac_addr, gfp);
  506. }
  507. EXPORT_SYMBOL(cfg80211_del_sta);
  508. struct cfg80211_mgmt_registration {
  509. struct list_head list;
  510. u32 nlpid;
  511. int match_len;
  512. __le16 frame_type;
  513. u8 match[];
  514. };
  515. int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_pid,
  516. u16 frame_type, const u8 *match_data,
  517. int match_len)
  518. {
  519. struct wiphy *wiphy = wdev->wiphy;
  520. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  521. struct cfg80211_mgmt_registration *reg, *nreg;
  522. int err = 0;
  523. u16 mgmt_type;
  524. if (!wdev->wiphy->mgmt_stypes)
  525. return -EOPNOTSUPP;
  526. if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
  527. return -EINVAL;
  528. if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
  529. return -EINVAL;
  530. mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
  531. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
  532. return -EINVAL;
  533. nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
  534. if (!nreg)
  535. return -ENOMEM;
  536. spin_lock_bh(&wdev->mgmt_registrations_lock);
  537. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  538. int mlen = min(match_len, reg->match_len);
  539. if (frame_type != le16_to_cpu(reg->frame_type))
  540. continue;
  541. if (memcmp(reg->match, match_data, mlen) == 0) {
  542. err = -EALREADY;
  543. break;
  544. }
  545. }
  546. if (err) {
  547. kfree(nreg);
  548. goto out;
  549. }
  550. memcpy(nreg->match, match_data, match_len);
  551. nreg->match_len = match_len;
  552. nreg->nlpid = snd_pid;
  553. nreg->frame_type = cpu_to_le16(frame_type);
  554. list_add(&nreg->list, &wdev->mgmt_registrations);
  555. if (rdev->ops->mgmt_frame_register)
  556. rdev->ops->mgmt_frame_register(wiphy, wdev, frame_type, true);
  557. out:
  558. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  559. return err;
  560. }
  561. void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlpid)
  562. {
  563. struct wiphy *wiphy = wdev->wiphy;
  564. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  565. struct cfg80211_mgmt_registration *reg, *tmp;
  566. spin_lock_bh(&wdev->mgmt_registrations_lock);
  567. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  568. if (reg->nlpid != nlpid)
  569. continue;
  570. if (rdev->ops->mgmt_frame_register) {
  571. u16 frame_type = le16_to_cpu(reg->frame_type);
  572. rdev->ops->mgmt_frame_register(wiphy, wdev,
  573. frame_type, false);
  574. }
  575. list_del(&reg->list);
  576. kfree(reg);
  577. }
  578. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  579. if (nlpid == wdev->ap_unexpected_nlpid)
  580. wdev->ap_unexpected_nlpid = 0;
  581. }
  582. void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
  583. {
  584. struct cfg80211_mgmt_registration *reg, *tmp;
  585. spin_lock_bh(&wdev->mgmt_registrations_lock);
  586. list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
  587. list_del(&reg->list);
  588. kfree(reg);
  589. }
  590. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  591. }
  592. int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
  593. struct wireless_dev *wdev,
  594. struct ieee80211_channel *chan, bool offchan,
  595. enum nl80211_channel_type channel_type,
  596. bool channel_type_valid, unsigned int wait,
  597. const u8 *buf, size_t len, bool no_cck,
  598. bool dont_wait_for_ack, u64 *cookie)
  599. {
  600. const struct ieee80211_mgmt *mgmt;
  601. u16 stype;
  602. if (!wdev->wiphy->mgmt_stypes)
  603. return -EOPNOTSUPP;
  604. if (!rdev->ops->mgmt_tx)
  605. return -EOPNOTSUPP;
  606. if (len < 24 + 1)
  607. return -EINVAL;
  608. mgmt = (const struct ieee80211_mgmt *) buf;
  609. if (!ieee80211_is_mgmt(mgmt->frame_control))
  610. return -EINVAL;
  611. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  612. if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
  613. return -EINVAL;
  614. if (ieee80211_is_action(mgmt->frame_control) &&
  615. mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
  616. int err = 0;
  617. wdev_lock(wdev);
  618. switch (wdev->iftype) {
  619. case NL80211_IFTYPE_ADHOC:
  620. case NL80211_IFTYPE_STATION:
  621. case NL80211_IFTYPE_P2P_CLIENT:
  622. if (!wdev->current_bss) {
  623. err = -ENOTCONN;
  624. break;
  625. }
  626. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  627. mgmt->bssid)) {
  628. err = -ENOTCONN;
  629. break;
  630. }
  631. /*
  632. * check for IBSS DA must be done by driver as
  633. * cfg80211 doesn't track the stations
  634. */
  635. if (wdev->iftype == NL80211_IFTYPE_ADHOC)
  636. break;
  637. /* for station, check that DA is the AP */
  638. if (!ether_addr_equal(wdev->current_bss->pub.bssid,
  639. mgmt->da)) {
  640. err = -ENOTCONN;
  641. break;
  642. }
  643. break;
  644. case NL80211_IFTYPE_AP:
  645. case NL80211_IFTYPE_P2P_GO:
  646. case NL80211_IFTYPE_AP_VLAN:
  647. if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
  648. err = -EINVAL;
  649. break;
  650. case NL80211_IFTYPE_MESH_POINT:
  651. if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
  652. err = -EINVAL;
  653. break;
  654. }
  655. /*
  656. * check for mesh DA must be done by driver as
  657. * cfg80211 doesn't track the stations
  658. */
  659. break;
  660. case NL80211_IFTYPE_P2P_DEVICE:
  661. /*
  662. * fall through, P2P device only supports
  663. * public action frames
  664. */
  665. default:
  666. err = -EOPNOTSUPP;
  667. break;
  668. }
  669. wdev_unlock(wdev);
  670. if (err)
  671. return err;
  672. }
  673. if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
  674. return -EINVAL;
  675. /* Transmit the Action frame as requested by user space */
  676. return rdev->ops->mgmt_tx(&rdev->wiphy, wdev, chan, offchan,
  677. channel_type, channel_type_valid,
  678. wait, buf, len, no_cck, dont_wait_for_ack,
  679. cookie);
  680. }
  681. bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
  682. const u8 *buf, size_t len, gfp_t gfp)
  683. {
  684. struct wiphy *wiphy = wdev->wiphy;
  685. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  686. struct cfg80211_mgmt_registration *reg;
  687. const struct ieee80211_txrx_stypes *stypes =
  688. &wiphy->mgmt_stypes[wdev->iftype];
  689. struct ieee80211_mgmt *mgmt = (void *)buf;
  690. const u8 *data;
  691. int data_len;
  692. bool result = false;
  693. __le16 ftype = mgmt->frame_control &
  694. cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
  695. u16 stype;
  696. stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
  697. if (!(stypes->rx & BIT(stype)))
  698. return false;
  699. data = buf + ieee80211_hdrlen(mgmt->frame_control);
  700. data_len = len - ieee80211_hdrlen(mgmt->frame_control);
  701. spin_lock_bh(&wdev->mgmt_registrations_lock);
  702. list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
  703. if (reg->frame_type != ftype)
  704. continue;
  705. if (reg->match_len > data_len)
  706. continue;
  707. if (memcmp(reg->match, data, reg->match_len))
  708. continue;
  709. /* found match! */
  710. /* Indicate the received Action frame to user space */
  711. if (nl80211_send_mgmt(rdev, wdev, reg->nlpid,
  712. freq, sig_mbm,
  713. buf, len, gfp))
  714. continue;
  715. result = true;
  716. break;
  717. }
  718. spin_unlock_bh(&wdev->mgmt_registrations_lock);
  719. return result;
  720. }
  721. EXPORT_SYMBOL(cfg80211_rx_mgmt);
  722. void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
  723. const u8 *buf, size_t len, bool ack, gfp_t gfp)
  724. {
  725. struct wiphy *wiphy = wdev->wiphy;
  726. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  727. /* Indicate TX status of the Action frame to user space */
  728. nl80211_send_mgmt_tx_status(rdev, wdev, cookie, buf, len, ack, gfp);
  729. }
  730. EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
  731. void cfg80211_cqm_rssi_notify(struct net_device *dev,
  732. enum nl80211_cqm_rssi_threshold_event rssi_event,
  733. gfp_t gfp)
  734. {
  735. struct wireless_dev *wdev = dev->ieee80211_ptr;
  736. struct wiphy *wiphy = wdev->wiphy;
  737. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  738. /* Indicate roaming trigger event to user space */
  739. nl80211_send_cqm_rssi_notify(rdev, dev, rssi_event, gfp);
  740. }
  741. EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
  742. void cfg80211_cqm_pktloss_notify(struct net_device *dev,
  743. const u8 *peer, u32 num_packets, gfp_t gfp)
  744. {
  745. struct wireless_dev *wdev = dev->ieee80211_ptr;
  746. struct wiphy *wiphy = wdev->wiphy;
  747. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  748. /* Indicate roaming trigger event to user space */
  749. nl80211_send_cqm_pktloss_notify(rdev, dev, peer, num_packets, gfp);
  750. }
  751. EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
  752. void cfg80211_cqm_txe_notify(struct net_device *dev,
  753. const u8 *peer, u32 num_packets,
  754. u32 rate, u32 intvl, gfp_t gfp)
  755. {
  756. struct wireless_dev *wdev = dev->ieee80211_ptr;
  757. struct wiphy *wiphy = wdev->wiphy;
  758. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  759. nl80211_send_cqm_txe_notify(rdev, dev, peer, num_packets,
  760. rate, intvl, gfp);
  761. }
  762. EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
  763. void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
  764. const u8 *replay_ctr, gfp_t gfp)
  765. {
  766. struct wireless_dev *wdev = dev->ieee80211_ptr;
  767. struct wiphy *wiphy = wdev->wiphy;
  768. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  769. nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
  770. }
  771. EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
  772. void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
  773. const u8 *bssid, bool preauth, gfp_t gfp)
  774. {
  775. struct wireless_dev *wdev = dev->ieee80211_ptr;
  776. struct wiphy *wiphy = wdev->wiphy;
  777. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  778. nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
  779. }
  780. EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
  781. void cfg80211_ch_switch_notify(struct net_device *dev, int freq,
  782. enum nl80211_channel_type type)
  783. {
  784. struct wireless_dev *wdev = dev->ieee80211_ptr;
  785. struct wiphy *wiphy = wdev->wiphy;
  786. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  787. struct ieee80211_channel *chan;
  788. wdev_lock(wdev);
  789. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
  790. wdev->iftype != NL80211_IFTYPE_P2P_GO))
  791. goto out;
  792. chan = rdev_freq_to_chan(rdev, freq, type);
  793. if (WARN_ON(!chan))
  794. goto out;
  795. wdev->channel = chan;
  796. nl80211_ch_switch_notify(rdev, dev, freq, type, GFP_KERNEL);
  797. out:
  798. wdev_unlock(wdev);
  799. return;
  800. }
  801. EXPORT_SYMBOL(cfg80211_ch_switch_notify);
  802. bool cfg80211_rx_spurious_frame(struct net_device *dev,
  803. const u8 *addr, gfp_t gfp)
  804. {
  805. struct wireless_dev *wdev = dev->ieee80211_ptr;
  806. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
  807. wdev->iftype != NL80211_IFTYPE_P2P_GO))
  808. return false;
  809. return nl80211_unexpected_frame(dev, addr, gfp);
  810. }
  811. EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
  812. bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
  813. const u8 *addr, gfp_t gfp)
  814. {
  815. struct wireless_dev *wdev = dev->ieee80211_ptr;
  816. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
  817. wdev->iftype != NL80211_IFTYPE_P2P_GO &&
  818. wdev->iftype != NL80211_IFTYPE_AP_VLAN))
  819. return false;
  820. return nl80211_unexpected_4addr_frame(dev, addr, gfp);
  821. }
  822. EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);