wext-sme.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * cfg80211 wext compat for managed mode.
  3. *
  4. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright (C) 2009 Intel Corporation. All rights reserved.
  6. */
  7. #include <linux/export.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/if_arp.h>
  10. #include <linux/slab.h>
  11. #include <net/cfg80211.h>
  12. #include <net/cfg80211-wext.h>
  13. #include "wext-compat.h"
  14. #include "nl80211.h"
  15. int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
  16. struct wireless_dev *wdev)
  17. {
  18. struct cfg80211_cached_keys *ck = NULL;
  19. const u8 *prev_bssid = NULL;
  20. int err, i;
  21. ASSERT_RDEV_LOCK(rdev);
  22. ASSERT_WDEV_LOCK(wdev);
  23. if (!netif_running(wdev->netdev))
  24. return 0;
  25. wdev->wext.connect.ie = wdev->wext.ie;
  26. wdev->wext.connect.ie_len = wdev->wext.ie_len;
  27. /* Use default background scan period */
  28. wdev->wext.connect.bg_scan_period = -1;
  29. if (wdev->wext.keys) {
  30. wdev->wext.keys->def = wdev->wext.default_key;
  31. wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
  32. if (wdev->wext.default_key != -1)
  33. wdev->wext.connect.privacy = true;
  34. }
  35. if (!wdev->wext.connect.ssid_len)
  36. return 0;
  37. if (wdev->wext.keys) {
  38. ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
  39. if (!ck)
  40. return -ENOMEM;
  41. for (i = 0; i < 6; i++)
  42. ck->params[i].key = ck->data[i];
  43. }
  44. if (wdev->wext.prev_bssid_valid)
  45. prev_bssid = wdev->wext.prev_bssid;
  46. err = __cfg80211_connect(rdev, wdev->netdev,
  47. &wdev->wext.connect, ck, prev_bssid);
  48. if (err)
  49. kfree(ck);
  50. return err;
  51. }
  52. int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
  53. struct iw_request_info *info,
  54. struct iw_freq *wextfreq, char *extra)
  55. {
  56. struct wireless_dev *wdev = dev->ieee80211_ptr;
  57. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  58. struct ieee80211_channel *chan = NULL;
  59. int err, freq;
  60. /* call only for station! */
  61. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  62. return -EINVAL;
  63. freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
  64. if (freq < 0)
  65. return freq;
  66. if (freq) {
  67. chan = ieee80211_get_channel(wdev->wiphy, freq);
  68. if (!chan)
  69. return -EINVAL;
  70. if (chan->flags & IEEE80211_CHAN_DISABLED)
  71. return -EINVAL;
  72. }
  73. cfg80211_lock_rdev(rdev);
  74. mutex_lock(&rdev->devlist_mtx);
  75. wdev_lock(wdev);
  76. if (wdev->sme_state != CFG80211_SME_IDLE) {
  77. bool event = true;
  78. if (wdev->wext.connect.channel == chan) {
  79. err = 0;
  80. goto out;
  81. }
  82. /* if SSID set, we'll try right again, avoid event */
  83. if (wdev->wext.connect.ssid_len)
  84. event = false;
  85. err = __cfg80211_disconnect(rdev, dev,
  86. WLAN_REASON_DEAUTH_LEAVING, event);
  87. if (err)
  88. goto out;
  89. }
  90. wdev->wext.connect.channel = chan;
  91. /* SSID is not set, we just want to switch channel */
  92. if (chan && !wdev->wext.connect.ssid_len) {
  93. err = cfg80211_set_freq(rdev, wdev, freq, NL80211_CHAN_NO_HT);
  94. goto out;
  95. }
  96. err = cfg80211_mgd_wext_connect(rdev, wdev);
  97. out:
  98. wdev_unlock(wdev);
  99. mutex_unlock(&rdev->devlist_mtx);
  100. cfg80211_unlock_rdev(rdev);
  101. return err;
  102. }
  103. int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
  104. struct iw_request_info *info,
  105. struct iw_freq *freq, char *extra)
  106. {
  107. struct wireless_dev *wdev = dev->ieee80211_ptr;
  108. struct ieee80211_channel *chan = NULL;
  109. /* call only for station! */
  110. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  111. return -EINVAL;
  112. wdev_lock(wdev);
  113. if (wdev->current_bss)
  114. chan = wdev->current_bss->pub.channel;
  115. else if (wdev->wext.connect.channel)
  116. chan = wdev->wext.connect.channel;
  117. wdev_unlock(wdev);
  118. if (chan) {
  119. freq->m = chan->center_freq;
  120. freq->e = 6;
  121. return 0;
  122. }
  123. /* no channel if not joining */
  124. return -EINVAL;
  125. }
  126. int cfg80211_mgd_wext_siwessid(struct net_device *dev,
  127. struct iw_request_info *info,
  128. struct iw_point *data, char *ssid)
  129. {
  130. struct wireless_dev *wdev = dev->ieee80211_ptr;
  131. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  132. size_t len = data->length;
  133. int err;
  134. /* call only for station! */
  135. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  136. return -EINVAL;
  137. if (!data->flags)
  138. len = 0;
  139. /* iwconfig uses nul termination in SSID.. */
  140. if (len > 0 && ssid[len - 1] == '\0')
  141. len--;
  142. cfg80211_lock_rdev(rdev);
  143. mutex_lock(&rdev->devlist_mtx);
  144. wdev_lock(wdev);
  145. err = 0;
  146. if (wdev->sme_state != CFG80211_SME_IDLE) {
  147. bool event = true;
  148. if (wdev->wext.connect.ssid && len &&
  149. len == wdev->wext.connect.ssid_len &&
  150. memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
  151. goto out;
  152. /* if SSID set now, we'll try to connect, avoid event */
  153. if (len)
  154. event = false;
  155. err = __cfg80211_disconnect(rdev, dev,
  156. WLAN_REASON_DEAUTH_LEAVING, event);
  157. if (err)
  158. goto out;
  159. }
  160. wdev->wext.prev_bssid_valid = false;
  161. wdev->wext.connect.ssid = wdev->wext.ssid;
  162. memcpy(wdev->wext.ssid, ssid, len);
  163. wdev->wext.connect.ssid_len = len;
  164. wdev->wext.connect.crypto.control_port = false;
  165. wdev->wext.connect.crypto.control_port_ethertype =
  166. cpu_to_be16(ETH_P_PAE);
  167. err = cfg80211_mgd_wext_connect(rdev, wdev);
  168. out:
  169. wdev_unlock(wdev);
  170. mutex_unlock(&rdev->devlist_mtx);
  171. cfg80211_unlock_rdev(rdev);
  172. return err;
  173. }
  174. int cfg80211_mgd_wext_giwessid(struct net_device *dev,
  175. struct iw_request_info *info,
  176. struct iw_point *data, char *ssid)
  177. {
  178. int ret = 0;
  179. struct wireless_dev *wdev = dev->ieee80211_ptr;
  180. /* call only for station! */
  181. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  182. return -EINVAL;
  183. data->flags = 0;
  184. wdev_lock(wdev);
  185. if (wdev->current_bss) {
  186. const u8 *ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
  187. WLAN_EID_SSID);
  188. if (ie) {
  189. data->flags = 1;
  190. data->length = ie[1];
  191. if (data->length > IW_ESSID_MAX_SIZE)
  192. ret = -EINVAL;
  193. else
  194. memcpy(ssid, ie + 2, data->length);
  195. }
  196. } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
  197. data->flags = 1;
  198. data->length = wdev->wext.connect.ssid_len;
  199. memcpy(ssid, wdev->wext.connect.ssid, data->length);
  200. }
  201. wdev_unlock(wdev);
  202. return ret;
  203. }
  204. int cfg80211_mgd_wext_siwap(struct net_device *dev,
  205. struct iw_request_info *info,
  206. struct sockaddr *ap_addr, char *extra)
  207. {
  208. struct wireless_dev *wdev = dev->ieee80211_ptr;
  209. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  210. u8 *bssid = ap_addr->sa_data;
  211. int err;
  212. /* call only for station! */
  213. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  214. return -EINVAL;
  215. if (ap_addr->sa_family != ARPHRD_ETHER)
  216. return -EINVAL;
  217. /* automatic mode */
  218. if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
  219. bssid = NULL;
  220. cfg80211_lock_rdev(rdev);
  221. mutex_lock(&rdev->devlist_mtx);
  222. wdev_lock(wdev);
  223. if (wdev->sme_state != CFG80211_SME_IDLE) {
  224. err = 0;
  225. /* both automatic */
  226. if (!bssid && !wdev->wext.connect.bssid)
  227. goto out;
  228. /* fixed already - and no change */
  229. if (wdev->wext.connect.bssid && bssid &&
  230. compare_ether_addr(bssid, wdev->wext.connect.bssid) == 0)
  231. goto out;
  232. err = __cfg80211_disconnect(rdev, dev,
  233. WLAN_REASON_DEAUTH_LEAVING, false);
  234. if (err)
  235. goto out;
  236. }
  237. if (bssid) {
  238. memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
  239. wdev->wext.connect.bssid = wdev->wext.bssid;
  240. } else
  241. wdev->wext.connect.bssid = NULL;
  242. err = cfg80211_mgd_wext_connect(rdev, wdev);
  243. out:
  244. wdev_unlock(wdev);
  245. mutex_unlock(&rdev->devlist_mtx);
  246. cfg80211_unlock_rdev(rdev);
  247. return err;
  248. }
  249. int cfg80211_mgd_wext_giwap(struct net_device *dev,
  250. struct iw_request_info *info,
  251. struct sockaddr *ap_addr, char *extra)
  252. {
  253. struct wireless_dev *wdev = dev->ieee80211_ptr;
  254. /* call only for station! */
  255. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  256. return -EINVAL;
  257. ap_addr->sa_family = ARPHRD_ETHER;
  258. wdev_lock(wdev);
  259. if (wdev->current_bss)
  260. memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
  261. else
  262. memset(ap_addr->sa_data, 0, ETH_ALEN);
  263. wdev_unlock(wdev);
  264. return 0;
  265. }
  266. int cfg80211_wext_siwgenie(struct net_device *dev,
  267. struct iw_request_info *info,
  268. struct iw_point *data, char *extra)
  269. {
  270. struct wireless_dev *wdev = dev->ieee80211_ptr;
  271. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  272. u8 *ie = extra;
  273. int ie_len = data->length, err;
  274. if (wdev->iftype != NL80211_IFTYPE_STATION)
  275. return -EOPNOTSUPP;
  276. if (!ie_len)
  277. ie = NULL;
  278. wdev_lock(wdev);
  279. /* no change */
  280. err = 0;
  281. if (wdev->wext.ie_len == ie_len &&
  282. memcmp(wdev->wext.ie, ie, ie_len) == 0)
  283. goto out;
  284. if (ie_len) {
  285. ie = kmemdup(extra, ie_len, GFP_KERNEL);
  286. if (!ie) {
  287. err = -ENOMEM;
  288. goto out;
  289. }
  290. } else
  291. ie = NULL;
  292. kfree(wdev->wext.ie);
  293. wdev->wext.ie = ie;
  294. wdev->wext.ie_len = ie_len;
  295. if (wdev->sme_state != CFG80211_SME_IDLE) {
  296. err = __cfg80211_disconnect(rdev, dev,
  297. WLAN_REASON_DEAUTH_LEAVING, false);
  298. if (err)
  299. goto out;
  300. }
  301. /* userspace better not think we'll reconnect */
  302. err = 0;
  303. out:
  304. wdev_unlock(wdev);
  305. return err;
  306. }
  307. int cfg80211_wext_siwmlme(struct net_device *dev,
  308. struct iw_request_info *info,
  309. struct iw_point *data, char *extra)
  310. {
  311. struct wireless_dev *wdev = dev->ieee80211_ptr;
  312. struct iw_mlme *mlme = (struct iw_mlme *)extra;
  313. struct cfg80211_registered_device *rdev;
  314. int err;
  315. if (!wdev)
  316. return -EOPNOTSUPP;
  317. rdev = wiphy_to_dev(wdev->wiphy);
  318. if (wdev->iftype != NL80211_IFTYPE_STATION)
  319. return -EINVAL;
  320. if (mlme->addr.sa_family != ARPHRD_ETHER)
  321. return -EINVAL;
  322. wdev_lock(wdev);
  323. switch (mlme->cmd) {
  324. case IW_MLME_DEAUTH:
  325. case IW_MLME_DISASSOC:
  326. err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
  327. true);
  328. break;
  329. default:
  330. err = -EOPNOTSUPP;
  331. break;
  332. }
  333. wdev_unlock(wdev);
  334. return err;
  335. }