wext-sme.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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_RTNL();
  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. kzfree(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_rdev(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(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. wdev_lock(wdev);
  74. if (wdev->conn) {
  75. bool event = true;
  76. if (wdev->wext.connect.channel == chan) {
  77. err = 0;
  78. goto out;
  79. }
  80. /* if SSID set, we'll try right again, avoid event */
  81. if (wdev->wext.connect.ssid_len)
  82. event = false;
  83. err = cfg80211_disconnect(rdev, dev,
  84. WLAN_REASON_DEAUTH_LEAVING, event);
  85. if (err)
  86. goto out;
  87. }
  88. wdev->wext.connect.channel = chan;
  89. /*
  90. * SSID is not set, we just want to switch monitor channel,
  91. * this is really just backward compatibility, if the SSID
  92. * is set then we use the channel to select the BSS to use
  93. * to connect to instead. If we were connected on another
  94. * channel we disconnected above and reconnect below.
  95. */
  96. if (chan && !wdev->wext.connect.ssid_len) {
  97. struct cfg80211_chan_def chandef = {
  98. .width = NL80211_CHAN_WIDTH_20_NOHT,
  99. .center_freq1 = freq,
  100. };
  101. chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
  102. if (chandef.chan)
  103. err = cfg80211_set_monitor_channel(rdev, &chandef);
  104. else
  105. err = -EINVAL;
  106. goto out;
  107. }
  108. err = cfg80211_mgd_wext_connect(rdev, wdev);
  109. out:
  110. wdev_unlock(wdev);
  111. return err;
  112. }
  113. int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
  114. struct iw_request_info *info,
  115. struct iw_freq *freq, char *extra)
  116. {
  117. struct wireless_dev *wdev = dev->ieee80211_ptr;
  118. struct ieee80211_channel *chan = NULL;
  119. /* call only for station! */
  120. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  121. return -EINVAL;
  122. wdev_lock(wdev);
  123. if (wdev->current_bss)
  124. chan = wdev->current_bss->pub.channel;
  125. else if (wdev->wext.connect.channel)
  126. chan = wdev->wext.connect.channel;
  127. wdev_unlock(wdev);
  128. if (chan) {
  129. freq->m = chan->center_freq;
  130. freq->e = 6;
  131. return 0;
  132. }
  133. /* no channel if not joining */
  134. return -EINVAL;
  135. }
  136. int cfg80211_mgd_wext_siwessid(struct net_device *dev,
  137. struct iw_request_info *info,
  138. struct iw_point *data, char *ssid)
  139. {
  140. struct wireless_dev *wdev = dev->ieee80211_ptr;
  141. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  142. size_t len = data->length;
  143. int err;
  144. /* call only for station! */
  145. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  146. return -EINVAL;
  147. if (!data->flags)
  148. len = 0;
  149. /* iwconfig uses nul termination in SSID.. */
  150. if (len > 0 && ssid[len - 1] == '\0')
  151. len--;
  152. wdev_lock(wdev);
  153. err = 0;
  154. if (wdev->conn) {
  155. bool event = true;
  156. if (wdev->wext.connect.ssid && len &&
  157. len == wdev->wext.connect.ssid_len &&
  158. memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
  159. goto out;
  160. /* if SSID set now, we'll try to connect, avoid event */
  161. if (len)
  162. event = false;
  163. err = cfg80211_disconnect(rdev, dev,
  164. WLAN_REASON_DEAUTH_LEAVING, event);
  165. if (err)
  166. goto out;
  167. }
  168. wdev->wext.prev_bssid_valid = false;
  169. wdev->wext.connect.ssid = wdev->wext.ssid;
  170. memcpy(wdev->wext.ssid, ssid, len);
  171. wdev->wext.connect.ssid_len = len;
  172. wdev->wext.connect.crypto.control_port = false;
  173. wdev->wext.connect.crypto.control_port_ethertype =
  174. cpu_to_be16(ETH_P_PAE);
  175. err = cfg80211_mgd_wext_connect(rdev, wdev);
  176. out:
  177. wdev_unlock(wdev);
  178. return err;
  179. }
  180. int cfg80211_mgd_wext_giwessid(struct net_device *dev,
  181. struct iw_request_info *info,
  182. struct iw_point *data, char *ssid)
  183. {
  184. struct wireless_dev *wdev = dev->ieee80211_ptr;
  185. /* call only for station! */
  186. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  187. return -EINVAL;
  188. data->flags = 0;
  189. wdev_lock(wdev);
  190. if (wdev->current_bss) {
  191. const u8 *ie;
  192. rcu_read_lock();
  193. ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
  194. WLAN_EID_SSID);
  195. if (ie) {
  196. data->flags = 1;
  197. data->length = ie[1];
  198. memcpy(ssid, ie + 2, data->length);
  199. }
  200. rcu_read_unlock();
  201. } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
  202. data->flags = 1;
  203. data->length = wdev->wext.connect.ssid_len;
  204. memcpy(ssid, wdev->wext.connect.ssid, data->length);
  205. }
  206. wdev_unlock(wdev);
  207. return 0;
  208. }
  209. int cfg80211_mgd_wext_siwap(struct net_device *dev,
  210. struct iw_request_info *info,
  211. struct sockaddr *ap_addr, char *extra)
  212. {
  213. struct wireless_dev *wdev = dev->ieee80211_ptr;
  214. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  215. u8 *bssid = ap_addr->sa_data;
  216. int err;
  217. /* call only for station! */
  218. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  219. return -EINVAL;
  220. if (ap_addr->sa_family != ARPHRD_ETHER)
  221. return -EINVAL;
  222. /* automatic mode */
  223. if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
  224. bssid = NULL;
  225. wdev_lock(wdev);
  226. if (wdev->conn) {
  227. err = 0;
  228. /* both automatic */
  229. if (!bssid && !wdev->wext.connect.bssid)
  230. goto out;
  231. /* fixed already - and no change */
  232. if (wdev->wext.connect.bssid && bssid &&
  233. ether_addr_equal(bssid, wdev->wext.connect.bssid))
  234. goto out;
  235. err = cfg80211_disconnect(rdev, dev,
  236. WLAN_REASON_DEAUTH_LEAVING, false);
  237. if (err)
  238. goto out;
  239. }
  240. if (bssid) {
  241. memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
  242. wdev->wext.connect.bssid = wdev->wext.bssid;
  243. } else
  244. wdev->wext.connect.bssid = NULL;
  245. err = cfg80211_mgd_wext_connect(rdev, wdev);
  246. out:
  247. wdev_unlock(wdev);
  248. return err;
  249. }
  250. int cfg80211_mgd_wext_giwap(struct net_device *dev,
  251. struct iw_request_info *info,
  252. struct sockaddr *ap_addr, char *extra)
  253. {
  254. struct wireless_dev *wdev = dev->ieee80211_ptr;
  255. /* call only for station! */
  256. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  257. return -EINVAL;
  258. ap_addr->sa_family = ARPHRD_ETHER;
  259. wdev_lock(wdev);
  260. if (wdev->current_bss)
  261. memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
  262. else
  263. eth_zero_addr(ap_addr->sa_data);
  264. wdev_unlock(wdev);
  265. return 0;
  266. }
  267. int cfg80211_wext_siwgenie(struct net_device *dev,
  268. struct iw_request_info *info,
  269. struct iw_point *data, char *extra)
  270. {
  271. struct wireless_dev *wdev = dev->ieee80211_ptr;
  272. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  273. u8 *ie = extra;
  274. int ie_len = data->length, err;
  275. if (wdev->iftype != NL80211_IFTYPE_STATION)
  276. return -EOPNOTSUPP;
  277. if (!ie_len)
  278. ie = NULL;
  279. wdev_lock(wdev);
  280. /* no change */
  281. err = 0;
  282. if (wdev->wext.ie_len == ie_len &&
  283. memcmp(wdev->wext.ie, ie, ie_len) == 0)
  284. goto out;
  285. if (ie_len) {
  286. ie = kmemdup(extra, ie_len, GFP_KERNEL);
  287. if (!ie) {
  288. err = -ENOMEM;
  289. goto out;
  290. }
  291. } else
  292. ie = NULL;
  293. kfree(wdev->wext.ie);
  294. wdev->wext.ie = ie;
  295. wdev->wext.ie_len = ie_len;
  296. if (wdev->conn) {
  297. err = cfg80211_disconnect(rdev, dev,
  298. WLAN_REASON_DEAUTH_LEAVING, false);
  299. if (err)
  300. goto out;
  301. }
  302. /* userspace better not think we'll reconnect */
  303. err = 0;
  304. out:
  305. wdev_unlock(wdev);
  306. return err;
  307. }
  308. int cfg80211_wext_siwmlme(struct net_device *dev,
  309. struct iw_request_info *info,
  310. struct iw_point *data, char *extra)
  311. {
  312. struct wireless_dev *wdev = dev->ieee80211_ptr;
  313. struct iw_mlme *mlme = (struct iw_mlme *)extra;
  314. struct cfg80211_registered_device *rdev;
  315. int err;
  316. if (!wdev)
  317. return -EOPNOTSUPP;
  318. rdev = wiphy_to_rdev(wdev->wiphy);
  319. if (wdev->iftype != NL80211_IFTYPE_STATION)
  320. return -EINVAL;
  321. if (mlme->addr.sa_family != ARPHRD_ETHER)
  322. return -EINVAL;
  323. wdev_lock(wdev);
  324. switch (mlme->cmd) {
  325. case IW_MLME_DEAUTH:
  326. case IW_MLME_DISASSOC:
  327. err = cfg80211_disconnect(rdev, dev, mlme->reason_code, true);
  328. break;
  329. default:
  330. err = -EOPNOTSUPP;
  331. break;
  332. }
  333. wdev_unlock(wdev);
  334. return err;
  335. }