blob: 5615a88025367f5beaee4e90e9040d5e4deb9f15 [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;
Johannes Bergf401a6f2009-08-07 14:51:05 +020018 const u8 *prev_bssid = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +020019 int err, i;
Johannes Bergf2129352009-07-01 21:26:56 +020020
Johannes Berg667503dd2009-07-07 03:56:11 +020021 ASSERT_RDEV_LOCK(rdev);
22 ASSERT_WDEV_LOCK(wdev);
23
Johannes Bergf2129352009-07-01 21:26:56 +020024 if (!netif_running(wdev->netdev))
25 return 0;
26
27 wdev->wext.connect.ie = wdev->wext.ie;
28 wdev->wext.connect.ie_len = wdev->wext.ie_len;
Johannes Bergf2129352009-07-01 21:26:56 +020029
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;
Johannes Berg4be3bd82009-09-24 09:00:57 +020033 if (wdev->wext.default_key != -1)
34 wdev->wext.connect.privacy = true;
Johannes Bergfffd0932009-07-08 14:22:54 +020035 }
36
37 if (!wdev->wext.connect.ssid_len)
38 return 0;
39
40 if (wdev->wext.keys) {
41 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
42 if (!ck)
43 return -ENOMEM;
44 for (i = 0; i < 6; i++)
45 ck->params[i].key = ck->data[i];
46 }
Johannes Bergf401a6f2009-08-07 14:51:05 +020047
48 if (wdev->wext.prev_bssid_valid)
49 prev_bssid = wdev->wext.prev_bssid;
50
Johannes Bergfffd0932009-07-08 14:22:54 +020051 err = __cfg80211_connect(rdev, wdev->netdev,
Johannes Bergf401a6f2009-08-07 14:51:05 +020052 &wdev->wext.connect, ck, prev_bssid);
Johannes Bergfffd0932009-07-08 14:22:54 +020053 if (err)
54 kfree(ck);
Johannes Bergf2129352009-07-01 21:26:56 +020055
56 return err;
57}
58
59int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
60 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +020061 struct iw_freq *wextfreq, char *extra)
Johannes Bergf2129352009-07-01 21:26:56 +020062{
63 struct wireless_dev *wdev = dev->ieee80211_ptr;
64 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020065 struct ieee80211_channel *chan = NULL;
66 int err, freq;
Johannes Bergf2129352009-07-01 21:26:56 +020067
68 /* call only for station! */
69 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
70 return -EINVAL;
71
Johannes Berg59bbb6f2009-08-07 17:22:35 +020072 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
73 if (freq < 0)
74 return freq;
Johannes Bergf2129352009-07-01 21:26:56 +020075
Johannes Berg59bbb6f2009-08-07 17:22:35 +020076 if (freq) {
77 chan = ieee80211_get_channel(wdev->wiphy, freq);
78 if (!chan)
79 return -EINVAL;
80 if (chan->flags & IEEE80211_CHAN_DISABLED)
81 return -EINVAL;
82 }
Johannes Bergf2129352009-07-01 21:26:56 +020083
Johannes Berg667503dd2009-07-07 03:56:11 +020084 cfg80211_lock_rdev(rdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020085 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +020086 wdev_lock(wdev);
87
Johannes Bergf2129352009-07-01 21:26:56 +020088 if (wdev->sme_state != CFG80211_SME_IDLE) {
89 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +020090
91 if (wdev->wext.connect.channel == chan) {
92 err = 0;
93 goto out;
94 }
95
Johannes Bergf2129352009-07-01 21:26:56 +020096 /* if SSID set, we'll try right again, avoid event */
97 if (wdev->wext.connect.ssid_len)
98 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +020099 err = __cfg80211_disconnect(rdev, dev,
100 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200101 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200102 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200103 }
104
Johannes Berg667503dd2009-07-07 03:56:11 +0200105
Johannes Bergf2129352009-07-01 21:26:56 +0200106 wdev->wext.connect.channel = chan;
107
108 /* SSID is not set, we just want to switch channel */
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200109 if (chan && !wdev->wext.connect.ssid_len) {
Johannes Berg4b181142009-08-08 11:03:58 +0200110 err = rdev_set_freq(rdev, wdev, freq, NL80211_CHAN_NO_HT);
Johannes Berg667503dd2009-07-07 03:56:11 +0200111 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200112 }
113
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200114 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200115 out:
116 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200117 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200118 cfg80211_unlock_rdev(rdev);
119 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200120}
Johannes Bergf2129352009-07-01 21:26:56 +0200121
122int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
123 struct iw_request_info *info,
124 struct iw_freq *freq, char *extra)
125{
126 struct wireless_dev *wdev = dev->ieee80211_ptr;
127 struct ieee80211_channel *chan = NULL;
128
129 /* call only for station! */
130 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
131 return -EINVAL;
132
Johannes Berg667503dd2009-07-07 03:56:11 +0200133 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200134 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200135 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200136 else if (wdev->wext.connect.channel)
137 chan = wdev->wext.connect.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200138 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200139
140 if (chan) {
141 freq->m = chan->center_freq;
142 freq->e = 6;
143 return 0;
144 }
145
146 /* no channel if not joining */
147 return -EINVAL;
148}
Johannes Bergf2129352009-07-01 21:26:56 +0200149
150int cfg80211_mgd_wext_siwessid(struct net_device *dev,
151 struct iw_request_info *info,
152 struct iw_point *data, char *ssid)
153{
154 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200155 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200156 size_t len = data->length;
157 int err;
158
159 /* call only for station! */
160 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
161 return -EINVAL;
162
163 if (!data->flags)
164 len = 0;
165
166 /* iwconfig uses nul termination in SSID.. */
167 if (len > 0 && ssid[len - 1] == '\0')
168 len--;
169
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200170 cfg80211_lock_rdev(rdev);
171 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200172 wdev_lock(wdev);
173
174 err = 0;
175
Johannes Bergf2129352009-07-01 21:26:56 +0200176 if (wdev->sme_state != CFG80211_SME_IDLE) {
177 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200178
179 if (wdev->wext.connect.ssid && len &&
180 len == wdev->wext.connect.ssid_len &&
181 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
182 goto out;
183
Johannes Bergf2129352009-07-01 21:26:56 +0200184 /* if SSID set now, we'll try to connect, avoid event */
185 if (len)
186 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200187 err = __cfg80211_disconnect(rdev, dev,
188 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200189 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200190 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200191 }
192
Johannes Bergf401a6f2009-08-07 14:51:05 +0200193 wdev->wext.prev_bssid_valid = false;
Johannes Bergf2129352009-07-01 21:26:56 +0200194 wdev->wext.connect.ssid = wdev->wext.ssid;
195 memcpy(wdev->wext.ssid, ssid, len);
196 wdev->wext.connect.ssid_len = len;
197
198 wdev->wext.connect.crypto.control_port = false;
199
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200200 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200201 out:
202 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200203 mutex_unlock(&rdev->devlist_mtx);
204 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200205 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200206}
Johannes Bergf2129352009-07-01 21:26:56 +0200207
208int cfg80211_mgd_wext_giwessid(struct net_device *dev,
209 struct iw_request_info *info,
210 struct iw_point *data, char *ssid)
211{
212 struct wireless_dev *wdev = dev->ieee80211_ptr;
213
214 /* call only for station! */
215 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
216 return -EINVAL;
217
218 data->flags = 0;
219
Johannes Berg667503dd2009-07-07 03:56:11 +0200220 wdev_lock(wdev);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800221 if (wdev->current_bss) {
222 const u8 *ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
223 WLAN_EID_SSID);
224 if (ie) {
225 data->flags = 1;
226 data->length = ie[1];
227 memcpy(ssid, ie + 2, data->length);
228 }
229 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200230 data->flags = 1;
231 data->length = wdev->wext.connect.ssid_len;
232 memcpy(ssid, wdev->wext.connect.ssid, data->length);
Johannes Berg33de4f92009-09-17 08:35:54 -0700233 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200234 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200235
236 return 0;
237}
Johannes Bergf2129352009-07-01 21:26:56 +0200238
239int cfg80211_mgd_wext_siwap(struct net_device *dev,
240 struct iw_request_info *info,
241 struct sockaddr *ap_addr, char *extra)
242{
243 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200244 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200245 u8 *bssid = ap_addr->sa_data;
246 int err;
247
248 /* call only for station! */
249 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
250 return -EINVAL;
251
252 if (ap_addr->sa_family != ARPHRD_ETHER)
253 return -EINVAL;
254
255 /* automatic mode */
256 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
257 bssid = NULL;
258
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200259 cfg80211_lock_rdev(rdev);
260 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200261 wdev_lock(wdev);
262
Johannes Bergf2129352009-07-01 21:26:56 +0200263 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200264 err = 0;
265 /* both automatic */
266 if (!bssid && !wdev->wext.connect.bssid)
267 goto out;
268
269 /* fixed already - and no change */
270 if (wdev->wext.connect.bssid && bssid &&
271 compare_ether_addr(bssid, wdev->wext.connect.bssid) == 0)
272 goto out;
273
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200274 err = __cfg80211_disconnect(rdev, dev,
275 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200276 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200277 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200278 }
279
280 if (bssid) {
281 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
282 wdev->wext.connect.bssid = wdev->wext.bssid;
283 } else
284 wdev->wext.connect.bssid = NULL;
285
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200286 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200287 out:
288 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200289 mutex_unlock(&rdev->devlist_mtx);
290 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200291 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200292}
Johannes Bergf2129352009-07-01 21:26:56 +0200293
294int cfg80211_mgd_wext_giwap(struct net_device *dev,
295 struct iw_request_info *info,
296 struct sockaddr *ap_addr, char *extra)
297{
298 struct wireless_dev *wdev = dev->ieee80211_ptr;
299
300 /* call only for station! */
301 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
302 return -EINVAL;
303
304 ap_addr->sa_family = ARPHRD_ETHER;
305
Johannes Berg667503dd2009-07-07 03:56:11 +0200306 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200307 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200308 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200309 else
310 memset(ap_addr->sa_data, 0, ETH_ALEN);
Johannes Berg667503dd2009-07-07 03:56:11 +0200311 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200312
313 return 0;
314}
Johannes Bergf2129352009-07-01 21:26:56 +0200315
316int cfg80211_wext_siwgenie(struct net_device *dev,
317 struct iw_request_info *info,
318 struct iw_point *data, char *extra)
319{
320 struct wireless_dev *wdev = dev->ieee80211_ptr;
321 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
322 u8 *ie = extra;
323 int ie_len = data->length, err;
324
325 if (wdev->iftype != NL80211_IFTYPE_STATION)
326 return -EOPNOTSUPP;
327
328 if (!ie_len)
329 ie = NULL;
330
Johannes Berg667503dd2009-07-07 03:56:11 +0200331 wdev_lock(wdev);
332
Johannes Bergf2129352009-07-01 21:26:56 +0200333 /* no change */
Johannes Berg667503dd2009-07-07 03:56:11 +0200334 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200335 if (wdev->wext.ie_len == ie_len &&
336 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503dd2009-07-07 03:56:11 +0200337 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200338
339 if (ie_len) {
340 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200341 if (!ie) {
342 err = -ENOMEM;
343 goto out;
344 }
Johannes Bergf2129352009-07-01 21:26:56 +0200345 } else
346 ie = NULL;
347
348 kfree(wdev->wext.ie);
349 wdev->wext.ie = ie;
350 wdev->wext.ie_len = ie_len;
351
352 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200353 err = __cfg80211_disconnect(rdev, dev,
354 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200355 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200356 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200357 }
358
359 /* userspace better not think we'll reconnect */
Johannes Berg667503dd2009-07-07 03:56:11 +0200360 err = 0;
361 out:
362 wdev_unlock(wdev);
363 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200364}
365EXPORT_SYMBOL_GPL(cfg80211_wext_siwgenie);
366
367int cfg80211_wext_siwmlme(struct net_device *dev,
368 struct iw_request_info *info,
369 struct iw_point *data, char *extra)
370{
371 struct wireless_dev *wdev = dev->ieee80211_ptr;
372 struct iw_mlme *mlme = (struct iw_mlme *)extra;
373 struct cfg80211_registered_device *rdev;
Johannes Berg667503dd2009-07-07 03:56:11 +0200374 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200375
376 if (!wdev)
377 return -EOPNOTSUPP;
378
379 rdev = wiphy_to_dev(wdev->wiphy);
380
381 if (wdev->iftype != NL80211_IFTYPE_STATION)
382 return -EINVAL;
383
384 if (mlme->addr.sa_family != ARPHRD_ETHER)
385 return -EINVAL;
386
Johannes Berg667503dd2009-07-07 03:56:11 +0200387 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200388 switch (mlme->cmd) {
389 case IW_MLME_DEAUTH:
390 case IW_MLME_DISASSOC:
Johannes Berg667503dd2009-07-07 03:56:11 +0200391 err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
392 true);
393 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200394 default:
Johannes Berg667503dd2009-07-07 03:56:11 +0200395 err = -EOPNOTSUPP;
396 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200397 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200398 wdev_unlock(wdev);
399
400 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200401}
402EXPORT_SYMBOL_GPL(cfg80211_wext_siwmlme);