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. if (wdev->wext.default_key != -1)
  32. wdev->wext.connect.privacy = true;
  33. }
  34. if (!wdev->wext.connect.ssid_len)
  35. return 0;
  36. if (wdev->wext.keys && wdev->wext.keys->def != -1) {
  37. ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
  38. if (!ck)
  39. return -ENOMEM;
  40. for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
  41. ck->params[i].key = ck->data[i];
  42. }
  43. if (wdev->wext.prev_bssid_valid)
  44. prev_bssid = wdev->wext.prev_bssid;
  45. err = cfg80211_connect(rdev, wdev->netdev,
  46. &wdev->wext.connect, ck, prev_bssid);
  47. if (err)
  48. kzfree(ck);
  49. return err;
  50. }
  51. int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
  52. struct iw_request_info *info,
  53. struct iw_freq *wextfreq, char *extra)
  54. {
  55. struct wireless_dev *wdev = dev->ieee80211_ptr;
  56. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  57. struct ieee80211_channel *chan = NULL;
  58. int err, freq;
  59. /* call only for station! */
  60. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  61. return -EINVAL;
  62. freq = cfg80211_wext_freq(wextfreq);
  63. if (freq < 0)
  64. return freq;
  65. if (freq) {
  66. chan = ieee80211_get_channel(wdev->wiphy, freq);
  67. if (!chan)
  68. return -EINVAL;
  69. if (chan->flags & IEEE80211_CHAN_DISABLED)
  70. return -EINVAL;
  71. }
  72. wdev_lock(wdev);
  73. if (wdev->conn) {
  74. bool event = true;
  75. if (wdev->wext.connect.channel == chan) {
  76. err = 0;
  77. goto out;
  78. }
  79. /* if SSID set, we'll try right again, avoid event */
  80. if (wdev->wext.connect.ssid_len)
  81. event = false;
  82. err = cfg80211_disconnect(rdev, dev,
  83. WLAN_REASON_DEAUTH_LEAVING, event);
  84. if (err)
  85. goto out;
  86. }
  87. wdev->wext.connect.channel = chan;
  88. /*
  89. * SSID is not set, we just want to switch monitor channel,
  90. * this is really just backward compatibility, if the SSID
  91. * is set then we use the channel to select the BSS to use
  92. * to connect to instead. If we were connected on another
  93. * channel we disconnected above and reconnect below.
  94. */
  95. if (chan && !wdev->wext.connect.ssid_len) {
  96. struct cfg80211_chan_def chandef = {
  97. .width = NL80211_CHAN_WIDTH_20_NOHT,
  98. .center_freq1 = freq,
  99. };
  100. chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
  101. if (chandef.chan)
  102. err = cfg80211_set_monitor_channel(rdev, &chandef);
  103. else
  104. err = -EINVAL;
  105. goto out;
  106. }
  107. err = cfg80211_mgd_wext_connect(rdev, wdev);
  108. out:
  109. wdev_unlock(wdev);
  110. return err;
  111. }
  112. int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
  113. struct iw_request_info *info,
  114. struct iw_freq *freq, char *extra)
  115. {
  116. struct wireless_dev *wdev = dev->ieee80211_ptr;
  117. struct ieee80211_channel *chan = NULL;
  118. /* call only for station! */
  119. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  120. return -EINVAL;
  121. wdev_lock(wdev);
  122. if (wdev->current_bss)
  123. chan = wdev->current_bss->pub.channel;
  124. else if (wdev->wext.connect.channel)
  125. chan = wdev->wext.connect.channel;
  126. wdev_unlock(wdev);
  127. if (chan) {
  128. freq->m = chan->center_freq;
  129. freq->e = 6;
  130. return 0;
  131. }
  132. /* no channel if not joining */
  133. return -EINVAL;
  134. }
  135. int cfg80211_mgd_wext_siwessid(struct net_device *dev,
  136. struct iw_request_info *info,
  137. struct iw_point *data, char *ssid)
  138. {
  139. struct wireless_dev *wdev = dev->ieee80211_ptr;
  140. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  141. size_t len = data->length;
  142. int err;
  143. /* call only for station! */
  144. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  145. return -EINVAL;
  146. if (!data->flags)
  147. len = 0;
  148. /* iwconfig uses nul termination in SSID.. */
  149. if (len > 0 && ssid[len - 1] == '\0')
  150. len--;
  151. wdev_lock(wdev);
  152. err = 0;
  153. if (wdev->conn) {
  154. bool event = true;
  155. if (wdev->wext.connect.ssid && len &&
  156. len == wdev->wext.connect.ssid_len &&
  157. memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
  158. goto out;
  159. /* if SSID set now, we'll try to connect, avoid event */
  160. if (len)
  161. event = false;
  162. err = cfg80211_disconnect(rdev, dev,
  163. WLAN_REASON_DEAUTH_LEAVING, event);
  164. if (err)
  165. goto out;
  166. }
  167. wdev->wext.prev_bssid_valid = false;
  168. wdev->wext.connect.ssid = wdev->wext.ssid;
  169. memcpy(wdev->wext.ssid, ssid, len);
  170. wdev->wext.connect.ssid_len = len;
  171. wdev->wext.connect.crypto.control_port = false;
  172. wdev->wext.connect.crypto.control_port_ethertype =
  173. cpu_to_be16(ETH_P_PAE);
  174. err = cfg80211_mgd_wext_connect(rdev, wdev);
  175. out:
  176. wdev_unlock(wdev);
  177. return err;
  178. }
  179. int cfg80211_mgd_wext_giwessid(struct net_device *dev,
  180. struct iw_request_info *info,
  181. struct iw_point *data, char *ssid)
  182. {
  183. struct wireless_dev *wdev = dev->ieee80211_ptr;
  184. /* call only for station! */
  185. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  186. return -EINVAL;
  187. data->flags = 0;
  188. wdev_lock(wdev);
  189. if (wdev->current_bss) {
  190. const u8 *ie;
  191. rcu_read_lock();
  192. ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
  193. WLAN_EID_SSID);
  194. if (ie) {
  195. data->flags = 1;
  196. data->length = ie[1];
  197. memcpy(ssid, ie + 2, data->length);
  198. }
  199. rcu_read_unlock();
  200. } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
  201. data->flags = 1;
  202. data->length = wdev->wext.connect.ssid_len;
  203. memcpy(ssid, wdev->wext.connect.ssid, data->length);
  204. }
  205. wdev_unlock(wdev);
  206. return 0;
  207. }
  208. int cfg80211_mgd_wext_siwap(struct net_device *dev,
  209. struct iw_request_info *info,
  210. struct sockaddr *ap_addr, char *extra)
  211. {
  212. struct wireless_dev *wdev = dev->ieee80211_ptr;
  213. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  214. u8 *bssid = ap_addr->sa_data;
  215. int err;
  216. /* call only for station! */
  217. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
  218. return -EINVAL;
  219. if (ap_addr->sa_family != ARPHRD_ETHER)
  220. return -EINVAL;
  221. /* automatic mode */
  222. if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
  223. bssid = NULL;
  224. wdev_lock(wdev);
  225. if (wdev->conn) {
  226. err = 0;
  227. /* both automatic */
  228. if (!bssid && !wdev->wext.connect.bssid)
  229. goto out;
  230. /* fixed already - and no change */
  231. if (wdev->wext.connect.bssid && bssid &&
  232. ether_addr_equal(bssid, wdev->wext.connect.bssid))
  233. goto out;
  234. err = cfg80211_disconnect(rdev, dev,
  235. WLAN_REASON_DEAUTH_LEAVING, false);
  236. if (err)
  237. goto out;
  238. }
  239. if (bssid) {
  240. memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
  241. wdev->wext.connect.bssid = wdev->wext.bssid;
  242. } else
  243. wdev->wext.connect.bssid = NULL;
  244. err = cfg80211_mgd_wext_connect(rdev, wdev);
  245. out:
  246. wdev_unlock(wdev);
  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. eth_zero_addr(ap_addr->sa_data);
  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_rdev(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->conn) {
  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_rdev(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, true);
  327. break;
  328. default:
  329. err = -EOPNOTSUPP;
  330. break;
  331. }
  332. wdev_unlock(wdev);
  333. return err;
  334. }