wext-sme.c 9.2 KB

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