blob: 9818198add8a21444ba9183bbb8e4fc2c2199491 [file] [log] [blame]
Johannes Bergf2129352009-07-01 21:26:56 +02001/*
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
8#include <linux/etherdevice.h>
9#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Bergf2129352009-07-01 21:26:56 +020011#include <net/cfg80211.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020012#include "wext-compat.h"
Johannes Bergf2129352009-07-01 21:26:56 +020013#include "nl80211.h"
14
Johannes Bergfffd0932009-07-08 14:22:54 +020015int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
16 struct wireless_dev *wdev)
Johannes Bergf2129352009-07-01 21:26:56 +020017{
Johannes Bergfffd0932009-07-08 14:22:54 +020018 struct cfg80211_cached_keys *ck = NULL;
Johannes Bergf401a6f2009-08-07 14:51:05 +020019 const u8 *prev_bssid = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +020020 int err, i;
Johannes Bergf2129352009-07-01 21:26:56 +020021
Johannes Berg667503dd2009-07-07 03:56:11 +020022 ASSERT_RDEV_LOCK(rdev);
23 ASSERT_WDEV_LOCK(wdev);
24
Johannes Bergf2129352009-07-01 21:26:56 +020025 if (!netif_running(wdev->netdev))
26 return 0;
27
28 wdev->wext.connect.ie = wdev->wext.ie;
29 wdev->wext.connect.ie_len = wdev->wext.ie_len;
Johannes Bergf2129352009-07-01 21:26:56 +020030
Johannes Bergfffd0932009-07-08 14:22:54 +020031 if (wdev->wext.keys) {
32 wdev->wext.keys->def = wdev->wext.default_key;
33 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
Johannes Berg4be3bd82009-09-24 09:00:57 +020034 if (wdev->wext.default_key != -1)
35 wdev->wext.connect.privacy = true;
Johannes Bergfffd0932009-07-08 14:22:54 +020036 }
37
38 if (!wdev->wext.connect.ssid_len)
39 return 0;
40
41 if (wdev->wext.keys) {
42 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
43 if (!ck)
44 return -ENOMEM;
45 for (i = 0; i < 6; i++)
46 ck->params[i].key = ck->data[i];
47 }
Johannes Bergf401a6f2009-08-07 14:51:05 +020048
49 if (wdev->wext.prev_bssid_valid)
50 prev_bssid = wdev->wext.prev_bssid;
51
Johannes Bergfffd0932009-07-08 14:22:54 +020052 err = __cfg80211_connect(rdev, wdev->netdev,
Johannes Bergf401a6f2009-08-07 14:51:05 +020053 &wdev->wext.connect, ck, prev_bssid);
Johannes Bergfffd0932009-07-08 14:22:54 +020054 if (err)
55 kfree(ck);
Johannes Bergf2129352009-07-01 21:26:56 +020056
57 return err;
58}
59
60int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
61 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +020062 struct iw_freq *wextfreq, char *extra)
Johannes Bergf2129352009-07-01 21:26:56 +020063{
64 struct wireless_dev *wdev = dev->ieee80211_ptr;
65 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020066 struct ieee80211_channel *chan = NULL;
67 int err, freq;
Johannes Bergf2129352009-07-01 21:26:56 +020068
69 /* call only for station! */
70 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
71 return -EINVAL;
72
Johannes Berg59bbb6f2009-08-07 17:22:35 +020073 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
74 if (freq < 0)
75 return freq;
Johannes Bergf2129352009-07-01 21:26:56 +020076
Johannes Berg59bbb6f2009-08-07 17:22:35 +020077 if (freq) {
78 chan = ieee80211_get_channel(wdev->wiphy, freq);
79 if (!chan)
80 return -EINVAL;
81 if (chan->flags & IEEE80211_CHAN_DISABLED)
82 return -EINVAL;
83 }
Johannes Bergf2129352009-07-01 21:26:56 +020084
Johannes Berg667503dd2009-07-07 03:56:11 +020085 cfg80211_lock_rdev(rdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020086 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +020087 wdev_lock(wdev);
88
Johannes Bergf2129352009-07-01 21:26:56 +020089 if (wdev->sme_state != CFG80211_SME_IDLE) {
90 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +020091
92 if (wdev->wext.connect.channel == chan) {
93 err = 0;
94 goto out;
95 }
96
Johannes Bergf2129352009-07-01 21:26:56 +020097 /* if SSID set, we'll try right again, avoid event */
98 if (wdev->wext.connect.ssid_len)
99 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200100 err = __cfg80211_disconnect(rdev, dev,
101 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200102 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200103 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200104 }
105
Johannes Berg667503dd2009-07-07 03:56:11 +0200106
Johannes Bergf2129352009-07-01 21:26:56 +0200107 wdev->wext.connect.channel = chan;
108
109 /* SSID is not set, we just want to switch channel */
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200110 if (chan && !wdev->wext.connect.ssid_len) {
Johannes Bergf444de02010-05-05 15:25:02 +0200111 err = cfg80211_set_freq(rdev, wdev, freq, NL80211_CHAN_NO_HT);
Johannes Berg667503dd2009-07-07 03:56:11 +0200112 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200113 }
114
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200115 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200116 out:
117 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200118 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200119 cfg80211_unlock_rdev(rdev);
120 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200121}
Johannes Bergf2129352009-07-01 21:26:56 +0200122
123int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
124 struct iw_request_info *info,
125 struct iw_freq *freq, char *extra)
126{
127 struct wireless_dev *wdev = dev->ieee80211_ptr;
128 struct ieee80211_channel *chan = NULL;
129
130 /* call only for station! */
131 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
132 return -EINVAL;
133
Johannes Berg667503dd2009-07-07 03:56:11 +0200134 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200135 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200136 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200137 else if (wdev->wext.connect.channel)
138 chan = wdev->wext.connect.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200139 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200140
141 if (chan) {
142 freq->m = chan->center_freq;
143 freq->e = 6;
144 return 0;
145 }
146
147 /* no channel if not joining */
148 return -EINVAL;
149}
Johannes Bergf2129352009-07-01 21:26:56 +0200150
151int cfg80211_mgd_wext_siwessid(struct net_device *dev,
152 struct iw_request_info *info,
153 struct iw_point *data, char *ssid)
154{
155 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200156 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200157 size_t len = data->length;
158 int err;
159
160 /* call only for station! */
161 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
162 return -EINVAL;
163
164 if (!data->flags)
165 len = 0;
166
167 /* iwconfig uses nul termination in SSID.. */
168 if (len > 0 && ssid[len - 1] == '\0')
169 len--;
170
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200171 cfg80211_lock_rdev(rdev);
172 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200173 wdev_lock(wdev);
174
175 err = 0;
176
Johannes Bergf2129352009-07-01 21:26:56 +0200177 if (wdev->sme_state != CFG80211_SME_IDLE) {
178 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200179
180 if (wdev->wext.connect.ssid && len &&
181 len == wdev->wext.connect.ssid_len &&
182 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
183 goto out;
184
Johannes Bergf2129352009-07-01 21:26:56 +0200185 /* if SSID set now, we'll try to connect, avoid event */
186 if (len)
187 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200188 err = __cfg80211_disconnect(rdev, dev,
189 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200190 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200191 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200192 }
193
Johannes Bergf401a6f2009-08-07 14:51:05 +0200194 wdev->wext.prev_bssid_valid = false;
Johannes Bergf2129352009-07-01 21:26:56 +0200195 wdev->wext.connect.ssid = wdev->wext.ssid;
196 memcpy(wdev->wext.ssid, ssid, len);
197 wdev->wext.connect.ssid_len = len;
198
199 wdev->wext.connect.crypto.control_port = false;
200
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200201 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200202 out:
203 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200204 mutex_unlock(&rdev->devlist_mtx);
205 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200206 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200207}
Johannes Bergf2129352009-07-01 21:26:56 +0200208
209int cfg80211_mgd_wext_giwessid(struct net_device *dev,
210 struct iw_request_info *info,
211 struct iw_point *data, char *ssid)
212{
213 struct wireless_dev *wdev = dev->ieee80211_ptr;
214
215 /* call only for station! */
216 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
217 return -EINVAL;
218
219 data->flags = 0;
220
Johannes Berg667503dd2009-07-07 03:56:11 +0200221 wdev_lock(wdev);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800222 if (wdev->current_bss) {
223 const u8 *ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
224 WLAN_EID_SSID);
225 if (ie) {
226 data->flags = 1;
227 data->length = ie[1];
228 memcpy(ssid, ie + 2, data->length);
229 }
230 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200231 data->flags = 1;
232 data->length = wdev->wext.connect.ssid_len;
233 memcpy(ssid, wdev->wext.connect.ssid, data->length);
Johannes Berg33de4f92009-09-17 08:35:54 -0700234 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200235 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200236
237 return 0;
238}
Johannes Bergf2129352009-07-01 21:26:56 +0200239
240int cfg80211_mgd_wext_siwap(struct net_device *dev,
241 struct iw_request_info *info,
242 struct sockaddr *ap_addr, char *extra)
243{
244 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200245 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200246 u8 *bssid = ap_addr->sa_data;
247 int err;
248
249 /* call only for station! */
250 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
251 return -EINVAL;
252
253 if (ap_addr->sa_family != ARPHRD_ETHER)
254 return -EINVAL;
255
256 /* automatic mode */
257 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
258 bssid = NULL;
259
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200260 cfg80211_lock_rdev(rdev);
261 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200262 wdev_lock(wdev);
263
Johannes Bergf2129352009-07-01 21:26:56 +0200264 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200265 err = 0;
266 /* both automatic */
267 if (!bssid && !wdev->wext.connect.bssid)
268 goto out;
269
270 /* fixed already - and no change */
271 if (wdev->wext.connect.bssid && bssid &&
272 compare_ether_addr(bssid, wdev->wext.connect.bssid) == 0)
273 goto out;
274
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200275 err = __cfg80211_disconnect(rdev, dev,
276 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200277 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200278 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200279 }
280
281 if (bssid) {
282 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
283 wdev->wext.connect.bssid = wdev->wext.bssid;
284 } else
285 wdev->wext.connect.bssid = NULL;
286
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200287 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200288 out:
289 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200290 mutex_unlock(&rdev->devlist_mtx);
291 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200292 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200293}
Johannes Bergf2129352009-07-01 21:26:56 +0200294
295int cfg80211_mgd_wext_giwap(struct net_device *dev,
296 struct iw_request_info *info,
297 struct sockaddr *ap_addr, char *extra)
298{
299 struct wireless_dev *wdev = dev->ieee80211_ptr;
300
301 /* call only for station! */
302 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
303 return -EINVAL;
304
305 ap_addr->sa_family = ARPHRD_ETHER;
306
Johannes Berg667503dd2009-07-07 03:56:11 +0200307 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200308 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200309 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200310 else
311 memset(ap_addr->sa_data, 0, ETH_ALEN);
Johannes Berg667503dd2009-07-07 03:56:11 +0200312 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200313
314 return 0;
315}
Johannes Bergf2129352009-07-01 21:26:56 +0200316
317int cfg80211_wext_siwgenie(struct net_device *dev,
318 struct iw_request_info *info,
319 struct iw_point *data, char *extra)
320{
321 struct wireless_dev *wdev = dev->ieee80211_ptr;
322 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
323 u8 *ie = extra;
324 int ie_len = data->length, err;
325
326 if (wdev->iftype != NL80211_IFTYPE_STATION)
327 return -EOPNOTSUPP;
328
329 if (!ie_len)
330 ie = NULL;
331
Johannes Berg667503dd2009-07-07 03:56:11 +0200332 wdev_lock(wdev);
333
Johannes Bergf2129352009-07-01 21:26:56 +0200334 /* no change */
Johannes Berg667503dd2009-07-07 03:56:11 +0200335 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200336 if (wdev->wext.ie_len == ie_len &&
337 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503dd2009-07-07 03:56:11 +0200338 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200339
340 if (ie_len) {
341 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200342 if (!ie) {
343 err = -ENOMEM;
344 goto out;
345 }
Johannes Bergf2129352009-07-01 21:26:56 +0200346 } else
347 ie = NULL;
348
349 kfree(wdev->wext.ie);
350 wdev->wext.ie = ie;
351 wdev->wext.ie_len = ie_len;
352
353 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200354 err = __cfg80211_disconnect(rdev, dev,
355 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200356 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200357 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200358 }
359
360 /* userspace better not think we'll reconnect */
Johannes Berg667503dd2009-07-07 03:56:11 +0200361 err = 0;
362 out:
363 wdev_unlock(wdev);
364 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200365}
366EXPORT_SYMBOL_GPL(cfg80211_wext_siwgenie);
367
368int cfg80211_wext_siwmlme(struct net_device *dev,
369 struct iw_request_info *info,
370 struct iw_point *data, char *extra)
371{
372 struct wireless_dev *wdev = dev->ieee80211_ptr;
373 struct iw_mlme *mlme = (struct iw_mlme *)extra;
374 struct cfg80211_registered_device *rdev;
Johannes Berg667503dd2009-07-07 03:56:11 +0200375 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200376
377 if (!wdev)
378 return -EOPNOTSUPP;
379
380 rdev = wiphy_to_dev(wdev->wiphy);
381
382 if (wdev->iftype != NL80211_IFTYPE_STATION)
383 return -EINVAL;
384
385 if (mlme->addr.sa_family != ARPHRD_ETHER)
386 return -EINVAL;
387
Johannes Berg667503dd2009-07-07 03:56:11 +0200388 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200389 switch (mlme->cmd) {
390 case IW_MLME_DEAUTH:
391 case IW_MLME_DISASSOC:
Johannes Berg667503dd2009-07-07 03:56:11 +0200392 err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
393 true);
394 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200395 default:
Johannes Berg667503dd2009-07-07 03:56:11 +0200396 err = -EOPNOTSUPP;
397 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200398 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200399 wdev_unlock(wdev);
400
401 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200402}
403EXPORT_SYMBOL_GPL(cfg80211_wext_siwmlme);