blob: 7c01c2f3b6cf34d86fc281942e125fe0706986d8 [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
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04008#include <linux/export.h>
Johannes Bergf2129352009-07-01 21:26:56 +02009#include <linux/etherdevice.h>
10#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Johannes Bergf2129352009-07-01 21:26:56 +020012#include <net/cfg80211.h>
Johannes Berg262eb9b22011-07-13 10:39:09 +020013#include <net/cfg80211-wext.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020014#include "wext-compat.h"
Johannes Bergf2129352009-07-01 21:26:56 +020015#include "nl80211.h"
16
Johannes Bergfffd0932009-07-08 14:22:54 +020017int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
18 struct wireless_dev *wdev)
Johannes Bergf2129352009-07-01 21:26:56 +020019{
Johannes Bergfffd0932009-07-08 14:22:54 +020020 struct cfg80211_cached_keys *ck = NULL;
Johannes Bergf401a6f2009-08-07 14:51:05 +020021 const u8 *prev_bssid = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +020022 int err, i;
Johannes Bergf2129352009-07-01 21:26:56 +020023
Johannes Berg667503dd2009-07-07 03:56:11 +020024 ASSERT_RDEV_LOCK(rdev);
25 ASSERT_WDEV_LOCK(wdev);
26
Johannes Bergf2129352009-07-01 21:26:56 +020027 if (!netif_running(wdev->netdev))
28 return 0;
29
30 wdev->wext.connect.ie = wdev->wext.ie;
31 wdev->wext.connect.ie_len = wdev->wext.ie_len;
Johannes Bergf2129352009-07-01 21:26:56 +020032
Bala Shanmugam4486ea92012-03-07 17:27:12 +053033 /* Use default background scan period */
34 wdev->wext.connect.bg_scan_period = -1;
35
Johannes Bergfffd0932009-07-08 14:22:54 +020036 if (wdev->wext.keys) {
37 wdev->wext.keys->def = wdev->wext.default_key;
38 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
Johannes Berg4be3bd82009-09-24 09:00:57 +020039 if (wdev->wext.default_key != -1)
40 wdev->wext.connect.privacy = true;
Johannes Bergfffd0932009-07-08 14:22:54 +020041 }
42
43 if (!wdev->wext.connect.ssid_len)
44 return 0;
45
46 if (wdev->wext.keys) {
47 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
48 if (!ck)
49 return -ENOMEM;
50 for (i = 0; i < 6; i++)
51 ck->params[i].key = ck->data[i];
52 }
Johannes Bergf401a6f2009-08-07 14:51:05 +020053
54 if (wdev->wext.prev_bssid_valid)
55 prev_bssid = wdev->wext.prev_bssid;
56
Johannes Bergfffd0932009-07-08 14:22:54 +020057 err = __cfg80211_connect(rdev, wdev->netdev,
Johannes Bergf401a6f2009-08-07 14:51:05 +020058 &wdev->wext.connect, ck, prev_bssid);
Johannes Bergfffd0932009-07-08 14:22:54 +020059 if (err)
60 kfree(ck);
Johannes Bergf2129352009-07-01 21:26:56 +020061
62 return err;
63}
64
65int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
66 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +020067 struct iw_freq *wextfreq, char *extra)
Johannes Bergf2129352009-07-01 21:26:56 +020068{
69 struct wireless_dev *wdev = dev->ieee80211_ptr;
70 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020071 struct ieee80211_channel *chan = NULL;
72 int err, freq;
Johannes Bergf2129352009-07-01 21:26:56 +020073
74 /* call only for station! */
75 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
76 return -EINVAL;
77
Johannes Berg59bbb6f2009-08-07 17:22:35 +020078 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
79 if (freq < 0)
80 return freq;
Johannes Bergf2129352009-07-01 21:26:56 +020081
Johannes Berg59bbb6f2009-08-07 17:22:35 +020082 if (freq) {
83 chan = ieee80211_get_channel(wdev->wiphy, freq);
84 if (!chan)
85 return -EINVAL;
86 if (chan->flags & IEEE80211_CHAN_DISABLED)
87 return -EINVAL;
88 }
Johannes Bergf2129352009-07-01 21:26:56 +020089
Johannes Berg667503dd2009-07-07 03:56:11 +020090 cfg80211_lock_rdev(rdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020091 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +020092 wdev_lock(wdev);
93
Johannes Bergf2129352009-07-01 21:26:56 +020094 if (wdev->sme_state != CFG80211_SME_IDLE) {
95 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +020096
97 if (wdev->wext.connect.channel == chan) {
98 err = 0;
99 goto out;
100 }
101
Johannes Bergf2129352009-07-01 21:26:56 +0200102 /* if SSID set, we'll try right again, avoid event */
103 if (wdev->wext.connect.ssid_len)
104 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200105 err = __cfg80211_disconnect(rdev, dev,
106 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200107 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200108 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200109 }
110
Johannes Berg667503dd2009-07-07 03:56:11 +0200111
Johannes Bergf2129352009-07-01 21:26:56 +0200112 wdev->wext.connect.channel = chan;
113
114 /* SSID is not set, we just want to switch channel */
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200115 if (chan && !wdev->wext.connect.ssid_len) {
Johannes Bergf444de02010-05-05 15:25:02 +0200116 err = cfg80211_set_freq(rdev, wdev, freq, NL80211_CHAN_NO_HT);
Johannes Berg667503dd2009-07-07 03:56:11 +0200117 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200118 }
119
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200120 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200121 out:
122 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200123 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200124 cfg80211_unlock_rdev(rdev);
125 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200126}
Johannes Bergf2129352009-07-01 21:26:56 +0200127
128int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
129 struct iw_request_info *info,
130 struct iw_freq *freq, char *extra)
131{
132 struct wireless_dev *wdev = dev->ieee80211_ptr;
133 struct ieee80211_channel *chan = NULL;
134
135 /* call only for station! */
136 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
137 return -EINVAL;
138
Johannes Berg667503dd2009-07-07 03:56:11 +0200139 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200140 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200141 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200142 else if (wdev->wext.connect.channel)
143 chan = wdev->wext.connect.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200144 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200145
146 if (chan) {
147 freq->m = chan->center_freq;
148 freq->e = 6;
149 return 0;
150 }
151
152 /* no channel if not joining */
153 return -EINVAL;
154}
Johannes Bergf2129352009-07-01 21:26:56 +0200155
156int cfg80211_mgd_wext_siwessid(struct net_device *dev,
157 struct iw_request_info *info,
158 struct iw_point *data, char *ssid)
159{
160 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200161 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200162 size_t len = data->length;
163 int err;
164
165 /* call only for station! */
166 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
167 return -EINVAL;
168
169 if (!data->flags)
170 len = 0;
171
172 /* iwconfig uses nul termination in SSID.. */
173 if (len > 0 && ssid[len - 1] == '\0')
174 len--;
175
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200176 cfg80211_lock_rdev(rdev);
177 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200178 wdev_lock(wdev);
179
180 err = 0;
181
Johannes Bergf2129352009-07-01 21:26:56 +0200182 if (wdev->sme_state != CFG80211_SME_IDLE) {
183 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200184
185 if (wdev->wext.connect.ssid && len &&
186 len == wdev->wext.connect.ssid_len &&
187 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
188 goto out;
189
Johannes Bergf2129352009-07-01 21:26:56 +0200190 /* if SSID set now, we'll try to connect, avoid event */
191 if (len)
192 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200193 err = __cfg80211_disconnect(rdev, dev,
194 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200195 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200196 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200197 }
198
Johannes Bergf401a6f2009-08-07 14:51:05 +0200199 wdev->wext.prev_bssid_valid = false;
Johannes Bergf2129352009-07-01 21:26:56 +0200200 wdev->wext.connect.ssid = wdev->wext.ssid;
201 memcpy(wdev->wext.ssid, ssid, len);
202 wdev->wext.connect.ssid_len = len;
203
204 wdev->wext.connect.crypto.control_port = false;
Johannes Bergc0692b82010-08-27 14:26:53 +0300205 wdev->wext.connect.crypto.control_port_ethertype =
206 cpu_to_be16(ETH_P_PAE);
Johannes Bergf2129352009-07-01 21:26:56 +0200207
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200208 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200209 out:
210 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200211 mutex_unlock(&rdev->devlist_mtx);
212 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200213 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200214}
Johannes Bergf2129352009-07-01 21:26:56 +0200215
216int cfg80211_mgd_wext_giwessid(struct net_device *dev,
217 struct iw_request_info *info,
218 struct iw_point *data, char *ssid)
219{
220 struct wireless_dev *wdev = dev->ieee80211_ptr;
221
222 /* call only for station! */
223 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
224 return -EINVAL;
225
226 data->flags = 0;
227
Johannes Berg667503dd2009-07-07 03:56:11 +0200228 wdev_lock(wdev);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800229 if (wdev->current_bss) {
230 const u8 *ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
231 WLAN_EID_SSID);
232 if (ie) {
233 data->flags = 1;
234 data->length = ie[1];
235 memcpy(ssid, ie + 2, data->length);
236 }
237 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200238 data->flags = 1;
239 data->length = wdev->wext.connect.ssid_len;
240 memcpy(ssid, wdev->wext.connect.ssid, data->length);
Johannes Berg33de4f92009-09-17 08:35:54 -0700241 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200242 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200243
244 return 0;
245}
Johannes Bergf2129352009-07-01 21:26:56 +0200246
247int cfg80211_mgd_wext_siwap(struct net_device *dev,
248 struct iw_request_info *info,
249 struct sockaddr *ap_addr, char *extra)
250{
251 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200252 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200253 u8 *bssid = ap_addr->sa_data;
254 int err;
255
256 /* call only for station! */
257 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
258 return -EINVAL;
259
260 if (ap_addr->sa_family != ARPHRD_ETHER)
261 return -EINVAL;
262
263 /* automatic mode */
264 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
265 bssid = NULL;
266
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200267 cfg80211_lock_rdev(rdev);
268 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200269 wdev_lock(wdev);
270
Johannes Bergf2129352009-07-01 21:26:56 +0200271 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200272 err = 0;
273 /* both automatic */
274 if (!bssid && !wdev->wext.connect.bssid)
275 goto out;
276
277 /* fixed already - and no change */
278 if (wdev->wext.connect.bssid && bssid &&
279 compare_ether_addr(bssid, wdev->wext.connect.bssid) == 0)
280 goto out;
281
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200282 err = __cfg80211_disconnect(rdev, dev,
283 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200284 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200285 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200286 }
287
288 if (bssid) {
289 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
290 wdev->wext.connect.bssid = wdev->wext.bssid;
291 } else
292 wdev->wext.connect.bssid = NULL;
293
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200294 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200295 out:
296 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200297 mutex_unlock(&rdev->devlist_mtx);
298 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200299 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200300}
Johannes Bergf2129352009-07-01 21:26:56 +0200301
302int cfg80211_mgd_wext_giwap(struct net_device *dev,
303 struct iw_request_info *info,
304 struct sockaddr *ap_addr, char *extra)
305{
306 struct wireless_dev *wdev = dev->ieee80211_ptr;
307
308 /* call only for station! */
309 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
310 return -EINVAL;
311
312 ap_addr->sa_family = ARPHRD_ETHER;
313
Johannes Berg667503dd2009-07-07 03:56:11 +0200314 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200315 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200316 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200317 else
318 memset(ap_addr->sa_data, 0, ETH_ALEN);
Johannes Berg667503dd2009-07-07 03:56:11 +0200319 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200320
321 return 0;
322}
Johannes Bergf2129352009-07-01 21:26:56 +0200323
324int cfg80211_wext_siwgenie(struct net_device *dev,
325 struct iw_request_info *info,
326 struct iw_point *data, char *extra)
327{
328 struct wireless_dev *wdev = dev->ieee80211_ptr;
329 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
330 u8 *ie = extra;
331 int ie_len = data->length, err;
332
333 if (wdev->iftype != NL80211_IFTYPE_STATION)
334 return -EOPNOTSUPP;
335
336 if (!ie_len)
337 ie = NULL;
338
Johannes Berg667503dd2009-07-07 03:56:11 +0200339 wdev_lock(wdev);
340
Johannes Bergf2129352009-07-01 21:26:56 +0200341 /* no change */
Johannes Berg667503dd2009-07-07 03:56:11 +0200342 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200343 if (wdev->wext.ie_len == ie_len &&
344 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503dd2009-07-07 03:56:11 +0200345 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200346
347 if (ie_len) {
348 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200349 if (!ie) {
350 err = -ENOMEM;
351 goto out;
352 }
Johannes Bergf2129352009-07-01 21:26:56 +0200353 } else
354 ie = NULL;
355
356 kfree(wdev->wext.ie);
357 wdev->wext.ie = ie;
358 wdev->wext.ie_len = ie_len;
359
360 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200361 err = __cfg80211_disconnect(rdev, dev,
362 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200363 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200364 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200365 }
366
367 /* userspace better not think we'll reconnect */
Johannes Berg667503dd2009-07-07 03:56:11 +0200368 err = 0;
369 out:
370 wdev_unlock(wdev);
371 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200372}
Johannes Bergf2129352009-07-01 21:26:56 +0200373
374int cfg80211_wext_siwmlme(struct net_device *dev,
375 struct iw_request_info *info,
376 struct iw_point *data, char *extra)
377{
378 struct wireless_dev *wdev = dev->ieee80211_ptr;
379 struct iw_mlme *mlme = (struct iw_mlme *)extra;
380 struct cfg80211_registered_device *rdev;
Johannes Berg667503dd2009-07-07 03:56:11 +0200381 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200382
383 if (!wdev)
384 return -EOPNOTSUPP;
385
386 rdev = wiphy_to_dev(wdev->wiphy);
387
388 if (wdev->iftype != NL80211_IFTYPE_STATION)
389 return -EINVAL;
390
391 if (mlme->addr.sa_family != ARPHRD_ETHER)
392 return -EINVAL;
393
Johannes Berg667503dd2009-07-07 03:56:11 +0200394 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200395 switch (mlme->cmd) {
396 case IW_MLME_DEAUTH:
397 case IW_MLME_DISASSOC:
Johannes Berg667503dd2009-07-07 03:56:11 +0200398 err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
399 true);
400 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200401 default:
Johannes Berg667503dd2009-07-07 03:56:11 +0200402 err = -EOPNOTSUPP;
403 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200404 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200405 wdev_unlock(wdev);
406
407 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200408}