blob: 509279a1cfb2ccb68cbc8cb8910f0d5a792ca017 [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>
10#include <net/cfg80211.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020011#include "wext-compat.h"
Johannes Bergf2129352009-07-01 21:26:56 +020012#include "nl80211.h"
13
Johannes Bergfffd0932009-07-08 14:22:54 +020014int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
15 struct wireless_dev *wdev)
Johannes Bergf2129352009-07-01 21:26:56 +020016{
Johannes Bergfffd0932009-07-08 14:22:54 +020017 struct cfg80211_cached_keys *ck = NULL;
18 int err, i;
Johannes Bergf2129352009-07-01 21:26:56 +020019
Johannes Berg667503d2009-07-07 03:56:11 +020020 ASSERT_RDEV_LOCK(rdev);
21 ASSERT_WDEV_LOCK(wdev);
22
Johannes Bergf2129352009-07-01 21:26:56 +020023 if (!netif_running(wdev->netdev))
24 return 0;
25
26 wdev->wext.connect.ie = wdev->wext.ie;
27 wdev->wext.connect.ie_len = wdev->wext.ie_len;
28 wdev->wext.connect.privacy = wdev->wext.default_key != -1;
29
Johannes Bergfffd0932009-07-08 14:22:54 +020030 if (wdev->wext.keys) {
31 wdev->wext.keys->def = wdev->wext.default_key;
32 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
33 }
34
35 if (!wdev->wext.connect.ssid_len)
36 return 0;
37
38 if (wdev->wext.keys) {
39 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
40 if (!ck)
41 return -ENOMEM;
42 for (i = 0; i < 6; i++)
43 ck->params[i].key = ck->data[i];
44 }
45 err = __cfg80211_connect(rdev, wdev->netdev,
46 &wdev->wext.connect, ck);
47 if (err)
48 kfree(ck);
Johannes Bergf2129352009-07-01 21:26:56 +020049
50 return err;
51}
52
53int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
54 struct iw_request_info *info,
55 struct iw_freq *freq, char *extra)
56{
57 struct wireless_dev *wdev = dev->ieee80211_ptr;
58 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
59 struct ieee80211_channel *chan;
60 int err;
61
62 /* call only for station! */
63 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
64 return -EINVAL;
65
66 chan = cfg80211_wext_freq(wdev->wiphy, freq);
67 if (chan && IS_ERR(chan))
68 return PTR_ERR(chan);
69
70 if (chan && (chan->flags & IEEE80211_CHAN_DISABLED))
71 return -EINVAL;
72
Johannes Berg667503d2009-07-07 03:56:11 +020073 cfg80211_lock_rdev(rdev);
74 wdev_lock(wdev);
75
Johannes Bergf2129352009-07-01 21:26:56 +020076 if (wdev->sme_state != CFG80211_SME_IDLE) {
77 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +020078
79 if (wdev->wext.connect.channel == chan) {
80 err = 0;
81 goto out;
82 }
83
Johannes Bergf2129352009-07-01 21:26:56 +020084 /* if SSID set, we'll try right again, avoid event */
85 if (wdev->wext.connect.ssid_len)
86 event = false;
Johannes Berg667503d2009-07-07 03:56:11 +020087 err = __cfg80211_disconnect(wiphy_to_dev(wdev->wiphy),
88 dev, WLAN_REASON_DEAUTH_LEAVING,
89 event);
Johannes Bergf2129352009-07-01 21:26:56 +020090 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +020091 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +020092 }
93
Johannes Berg667503d2009-07-07 03:56:11 +020094
Johannes Bergf2129352009-07-01 21:26:56 +020095 wdev->wext.connect.channel = chan;
96
97 /* SSID is not set, we just want to switch channel */
98 if (wdev->wext.connect.ssid_len && chan) {
Johannes Berg667503d2009-07-07 03:56:11 +020099 err = -EOPNOTSUPP;
100 if (rdev->ops->set_channel)
101 err = rdev->ops->set_channel(wdev->wiphy, chan,
102 NL80211_CHAN_NO_HT);
103 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200104 }
105
Johannes Berg667503d2009-07-07 03:56:11 +0200106 err = cfg80211_mgd_wext_connect(wiphy_to_dev(wdev->wiphy), wdev);
107 out:
108 wdev_unlock(wdev);
109 cfg80211_unlock_rdev(rdev);
110 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200111}
Johannes Bergf2129352009-07-01 21:26:56 +0200112
113int 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
120 /* call only for station! */
121 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
122 return -EINVAL;
123
Johannes Berg667503d2009-07-07 03:56:11 +0200124 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200125 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200126 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200127 else if (wdev->wext.connect.channel)
128 chan = wdev->wext.connect.channel;
Johannes Berg667503d2009-07-07 03:56:11 +0200129 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200130
131 if (chan) {
132 freq->m = chan->center_freq;
133 freq->e = 6;
134 return 0;
135 }
136
137 /* no channel if not joining */
138 return -EINVAL;
139}
Johannes Bergf2129352009-07-01 21:26:56 +0200140
141int cfg80211_mgd_wext_siwessid(struct net_device *dev,
142 struct iw_request_info *info,
143 struct iw_point *data, char *ssid)
144{
145 struct wireless_dev *wdev = dev->ieee80211_ptr;
146 size_t len = data->length;
147 int err;
148
149 /* call only for station! */
150 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
151 return -EINVAL;
152
153 if (!data->flags)
154 len = 0;
155
156 /* iwconfig uses nul termination in SSID.. */
157 if (len > 0 && ssid[len - 1] == '\0')
158 len--;
159
Johannes Berg667503d2009-07-07 03:56:11 +0200160 cfg80211_lock_rdev(wiphy_to_dev(wdev->wiphy));
161 wdev_lock(wdev);
162
163 err = 0;
164
Johannes Bergf2129352009-07-01 21:26:56 +0200165 if (wdev->sme_state != CFG80211_SME_IDLE) {
166 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200167
168 if (wdev->wext.connect.ssid && len &&
169 len == wdev->wext.connect.ssid_len &&
170 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
171 goto out;
172
Johannes Bergf2129352009-07-01 21:26:56 +0200173 /* if SSID set now, we'll try to connect, avoid event */
174 if (len)
175 event = false;
Johannes Berg667503d2009-07-07 03:56:11 +0200176 err = __cfg80211_disconnect(wiphy_to_dev(wdev->wiphy),
177 dev, WLAN_REASON_DEAUTH_LEAVING,
178 event);
Johannes Bergf2129352009-07-01 21:26:56 +0200179 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200180 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200181 }
182
183 wdev->wext.connect.ssid = wdev->wext.ssid;
184 memcpy(wdev->wext.ssid, ssid, len);
185 wdev->wext.connect.ssid_len = len;
186
187 wdev->wext.connect.crypto.control_port = false;
188
Johannes Berg667503d2009-07-07 03:56:11 +0200189 err = cfg80211_mgd_wext_connect(wiphy_to_dev(wdev->wiphy), wdev);
190 out:
191 wdev_unlock(wdev);
192 cfg80211_unlock_rdev(wiphy_to_dev(wdev->wiphy));
193 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200194}
195/* temporary symbol - mark GPL - in the future the handler won't be */
196EXPORT_SYMBOL_GPL(cfg80211_mgd_wext_siwessid);
197
198int cfg80211_mgd_wext_giwessid(struct net_device *dev,
199 struct iw_request_info *info,
200 struct iw_point *data, char *ssid)
201{
202 struct wireless_dev *wdev = dev->ieee80211_ptr;
203
204 /* call only for station! */
205 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
206 return -EINVAL;
207
208 data->flags = 0;
209
Johannes Berg667503d2009-07-07 03:56:11 +0200210 wdev_lock(wdev);
Johannes Berg908d4362009-07-10 17:53:34 +0200211 if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200212 data->flags = 1;
213 data->length = wdev->wext.connect.ssid_len;
214 memcpy(ssid, wdev->wext.connect.ssid, data->length);
215 } else
216 data->flags = 0;
Johannes Berg667503d2009-07-07 03:56:11 +0200217 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200218
219 return 0;
220}
221/* temporary symbol - mark GPL - in the future the handler won't be */
222EXPORT_SYMBOL_GPL(cfg80211_mgd_wext_giwessid);
223
224int cfg80211_mgd_wext_siwap(struct net_device *dev,
225 struct iw_request_info *info,
226 struct sockaddr *ap_addr, char *extra)
227{
228 struct wireless_dev *wdev = dev->ieee80211_ptr;
229 u8 *bssid = ap_addr->sa_data;
230 int err;
231
232 /* call only for station! */
233 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
234 return -EINVAL;
235
236 if (ap_addr->sa_family != ARPHRD_ETHER)
237 return -EINVAL;
238
239 /* automatic mode */
240 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
241 bssid = NULL;
242
Johannes Berg667503d2009-07-07 03:56:11 +0200243 cfg80211_lock_rdev(wiphy_to_dev(wdev->wiphy));
244 wdev_lock(wdev);
245
Johannes Bergf2129352009-07-01 21:26:56 +0200246 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200247 err = 0;
248 /* both automatic */
249 if (!bssid && !wdev->wext.connect.bssid)
250 goto out;
251
252 /* fixed already - and no change */
253 if (wdev->wext.connect.bssid && bssid &&
254 compare_ether_addr(bssid, wdev->wext.connect.bssid) == 0)
255 goto out;
256
Johannes Berg667503d2009-07-07 03:56:11 +0200257 err = __cfg80211_disconnect(wiphy_to_dev(wdev->wiphy),
258 dev, WLAN_REASON_DEAUTH_LEAVING,
259 false);
Johannes Bergf2129352009-07-01 21:26:56 +0200260 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200261 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200262 }
263
264 if (bssid) {
265 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
266 wdev->wext.connect.bssid = wdev->wext.bssid;
267 } else
268 wdev->wext.connect.bssid = NULL;
269
Johannes Berg667503d2009-07-07 03:56:11 +0200270 err = cfg80211_mgd_wext_connect(wiphy_to_dev(wdev->wiphy), wdev);
271 out:
272 wdev_unlock(wdev);
273 cfg80211_unlock_rdev(wiphy_to_dev(wdev->wiphy));
274 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200275}
276/* temporary symbol - mark GPL - in the future the handler won't be */
277EXPORT_SYMBOL_GPL(cfg80211_mgd_wext_siwap);
278
279int cfg80211_mgd_wext_giwap(struct net_device *dev,
280 struct iw_request_info *info,
281 struct sockaddr *ap_addr, char *extra)
282{
283 struct wireless_dev *wdev = dev->ieee80211_ptr;
284
285 /* call only for station! */
286 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
287 return -EINVAL;
288
289 ap_addr->sa_family = ARPHRD_ETHER;
290
Johannes Berg667503d2009-07-07 03:56:11 +0200291 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200292 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200293 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200294 else if (wdev->wext.connect.bssid)
295 memcpy(ap_addr->sa_data, wdev->wext.connect.bssid, ETH_ALEN);
296 else
297 memset(ap_addr->sa_data, 0, ETH_ALEN);
Johannes Berg667503d2009-07-07 03:56:11 +0200298 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200299
300 return 0;
301}
302/* temporary symbol - mark GPL - in the future the handler won't be */
303EXPORT_SYMBOL_GPL(cfg80211_mgd_wext_giwap);
304
305int cfg80211_wext_siwgenie(struct net_device *dev,
306 struct iw_request_info *info,
307 struct iw_point *data, char *extra)
308{
309 struct wireless_dev *wdev = dev->ieee80211_ptr;
310 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
311 u8 *ie = extra;
312 int ie_len = data->length, err;
313
314 if (wdev->iftype != NL80211_IFTYPE_STATION)
315 return -EOPNOTSUPP;
316
317 if (!ie_len)
318 ie = NULL;
319
Johannes Berg667503d2009-07-07 03:56:11 +0200320 wdev_lock(wdev);
321
Johannes Bergf2129352009-07-01 21:26:56 +0200322 /* no change */
Johannes Berg667503d2009-07-07 03:56:11 +0200323 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200324 if (wdev->wext.ie_len == ie_len &&
325 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503d2009-07-07 03:56:11 +0200326 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200327
328 if (ie_len) {
329 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503d2009-07-07 03:56:11 +0200330 if (!ie) {
331 err = -ENOMEM;
332 goto out;
333 }
Johannes Bergf2129352009-07-01 21:26:56 +0200334 } else
335 ie = NULL;
336
337 kfree(wdev->wext.ie);
338 wdev->wext.ie = ie;
339 wdev->wext.ie_len = ie_len;
340
341 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg667503d2009-07-07 03:56:11 +0200342 err = __cfg80211_disconnect(rdev, dev,
343 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200344 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200345 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200346 }
347
348 /* userspace better not think we'll reconnect */
Johannes Berg667503d2009-07-07 03:56:11 +0200349 err = 0;
350 out:
351 wdev_unlock(wdev);
352 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200353}
354EXPORT_SYMBOL_GPL(cfg80211_wext_siwgenie);
355
356int cfg80211_wext_siwmlme(struct net_device *dev,
357 struct iw_request_info *info,
358 struct iw_point *data, char *extra)
359{
360 struct wireless_dev *wdev = dev->ieee80211_ptr;
361 struct iw_mlme *mlme = (struct iw_mlme *)extra;
362 struct cfg80211_registered_device *rdev;
Johannes Berg667503d2009-07-07 03:56:11 +0200363 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200364
365 if (!wdev)
366 return -EOPNOTSUPP;
367
368 rdev = wiphy_to_dev(wdev->wiphy);
369
370 if (wdev->iftype != NL80211_IFTYPE_STATION)
371 return -EINVAL;
372
373 if (mlme->addr.sa_family != ARPHRD_ETHER)
374 return -EINVAL;
375
Johannes Berg667503d2009-07-07 03:56:11 +0200376 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200377 switch (mlme->cmd) {
378 case IW_MLME_DEAUTH:
379 case IW_MLME_DISASSOC:
Johannes Berg667503d2009-07-07 03:56:11 +0200380 err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
381 true);
382 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200383 default:
Johannes Berg667503d2009-07-07 03:56:11 +0200384 err = -EOPNOTSUPP;
385 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200386 }
Johannes Berg667503d2009-07-07 03:56:11 +0200387 wdev_unlock(wdev);
388
389 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200390}
391EXPORT_SYMBOL_GPL(cfg80211_wext_siwmlme);