blob: fb9622f6d99c4b6b6c87f8a8d8e2c88e4fb3705c [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
Johannes Berge8c9bd52012-06-06 08:18:22 +0200114 /*
115 * SSID is not set, we just want to switch monitor channel,
116 * this is really just backward compatibility, if the SSID
117 * is set then we use the channel to select the BSS to use
118 * to connect to instead. If we were connected on another
119 * channel we disconnected above and reconnect below.
120 */
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200121 if (chan && !wdev->wext.connect.ssid_len) {
Johannes Berg683b6d32012-11-08 21:25:48 +0100122 struct cfg80211_chan_def chandef = {
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100123 .width = NL80211_CHAN_WIDTH_20_NOHT,
124 .center_freq1 = freq,
Johannes Berg683b6d32012-11-08 21:25:48 +0100125 };
126
127 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
128 if (chandef.chan)
129 err = cfg80211_set_monitor_channel(rdev, &chandef);
130 else
131 err = -EINVAL;
Johannes Berg667503dd2009-07-07 03:56:11 +0200132 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200133 }
134
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200135 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200136 out:
137 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200138 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200139 cfg80211_unlock_rdev(rdev);
140 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200141}
Johannes Bergf2129352009-07-01 21:26:56 +0200142
143int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
144 struct iw_request_info *info,
145 struct iw_freq *freq, char *extra)
146{
147 struct wireless_dev *wdev = dev->ieee80211_ptr;
148 struct ieee80211_channel *chan = NULL;
149
150 /* call only for station! */
151 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
152 return -EINVAL;
153
Johannes Berg667503dd2009-07-07 03:56:11 +0200154 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200155 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200156 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200157 else if (wdev->wext.connect.channel)
158 chan = wdev->wext.connect.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200159 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200160
161 if (chan) {
162 freq->m = chan->center_freq;
163 freq->e = 6;
164 return 0;
165 }
166
167 /* no channel if not joining */
168 return -EINVAL;
169}
Johannes Bergf2129352009-07-01 21:26:56 +0200170
171int cfg80211_mgd_wext_siwessid(struct net_device *dev,
172 struct iw_request_info *info,
173 struct iw_point *data, char *ssid)
174{
175 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200176 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200177 size_t len = data->length;
178 int err;
179
180 /* call only for station! */
181 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
182 return -EINVAL;
183
184 if (!data->flags)
185 len = 0;
186
187 /* iwconfig uses nul termination in SSID.. */
188 if (len > 0 && ssid[len - 1] == '\0')
189 len--;
190
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200191 cfg80211_lock_rdev(rdev);
192 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200193 wdev_lock(wdev);
194
195 err = 0;
196
Johannes Bergf2129352009-07-01 21:26:56 +0200197 if (wdev->sme_state != CFG80211_SME_IDLE) {
198 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200199
200 if (wdev->wext.connect.ssid && len &&
201 len == wdev->wext.connect.ssid_len &&
202 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
203 goto out;
204
Johannes Bergf2129352009-07-01 21:26:56 +0200205 /* if SSID set now, we'll try to connect, avoid event */
206 if (len)
207 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200208 err = __cfg80211_disconnect(rdev, dev,
209 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200210 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200211 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200212 }
213
Johannes Bergf401a6f2009-08-07 14:51:05 +0200214 wdev->wext.prev_bssid_valid = false;
Johannes Bergf2129352009-07-01 21:26:56 +0200215 wdev->wext.connect.ssid = wdev->wext.ssid;
216 memcpy(wdev->wext.ssid, ssid, len);
217 wdev->wext.connect.ssid_len = len;
218
219 wdev->wext.connect.crypto.control_port = false;
Johannes Bergc0692b82010-08-27 14:26:53 +0300220 wdev->wext.connect.crypto.control_port_ethertype =
221 cpu_to_be16(ETH_P_PAE);
Johannes Bergf2129352009-07-01 21:26:56 +0200222
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200223 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200224 out:
225 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200226 mutex_unlock(&rdev->devlist_mtx);
227 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200228 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200229}
Johannes Bergf2129352009-07-01 21:26:56 +0200230
231int cfg80211_mgd_wext_giwessid(struct net_device *dev,
232 struct iw_request_info *info,
233 struct iw_point *data, char *ssid)
234{
235 struct wireless_dev *wdev = dev->ieee80211_ptr;
236
237 /* call only for station! */
238 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
239 return -EINVAL;
240
241 data->flags = 0;
242
Johannes Berg667503dd2009-07-07 03:56:11 +0200243 wdev_lock(wdev);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800244 if (wdev->current_bss) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100245 const u8 *ie;
246
247 rcu_read_lock();
248 ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
249 WLAN_EID_SSID);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800250 if (ie) {
251 data->flags = 1;
252 data->length = ie[1];
253 memcpy(ssid, ie + 2, data->length);
254 }
Johannes Berg9caf0362012-11-29 01:25:20 +0100255 rcu_read_unlock();
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800256 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200257 data->flags = 1;
258 data->length = wdev->wext.connect.ssid_len;
259 memcpy(ssid, wdev->wext.connect.ssid, data->length);
Johannes Berg33de4f92009-09-17 08:35:54 -0700260 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200261 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200262
263 return 0;
264}
Johannes Bergf2129352009-07-01 21:26:56 +0200265
266int cfg80211_mgd_wext_siwap(struct net_device *dev,
267 struct iw_request_info *info,
268 struct sockaddr *ap_addr, char *extra)
269{
270 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200271 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200272 u8 *bssid = ap_addr->sa_data;
273 int err;
274
275 /* call only for station! */
276 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
277 return -EINVAL;
278
279 if (ap_addr->sa_family != ARPHRD_ETHER)
280 return -EINVAL;
281
282 /* automatic mode */
283 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
284 bssid = NULL;
285
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200286 cfg80211_lock_rdev(rdev);
287 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200288 wdev_lock(wdev);
289
Johannes Bergf2129352009-07-01 21:26:56 +0200290 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200291 err = 0;
292 /* both automatic */
293 if (!bssid && !wdev->wext.connect.bssid)
294 goto out;
295
296 /* fixed already - and no change */
297 if (wdev->wext.connect.bssid && bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000298 ether_addr_equal(bssid, wdev->wext.connect.bssid))
Johannes Berg25e83c42009-07-16 11:39:04 +0200299 goto out;
300
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200301 err = __cfg80211_disconnect(rdev, dev,
302 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200303 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200304 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200305 }
306
307 if (bssid) {
308 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
309 wdev->wext.connect.bssid = wdev->wext.bssid;
310 } else
311 wdev->wext.connect.bssid = NULL;
312
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200313 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200314 out:
315 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200316 mutex_unlock(&rdev->devlist_mtx);
317 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200318 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200319}
Johannes Bergf2129352009-07-01 21:26:56 +0200320
321int cfg80211_mgd_wext_giwap(struct net_device *dev,
322 struct iw_request_info *info,
323 struct sockaddr *ap_addr, char *extra)
324{
325 struct wireless_dev *wdev = dev->ieee80211_ptr;
326
327 /* call only for station! */
328 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
329 return -EINVAL;
330
331 ap_addr->sa_family = ARPHRD_ETHER;
332
Johannes Berg667503dd2009-07-07 03:56:11 +0200333 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200334 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200335 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200336 else
337 memset(ap_addr->sa_data, 0, ETH_ALEN);
Johannes Berg667503dd2009-07-07 03:56:11 +0200338 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200339
340 return 0;
341}
Johannes Bergf2129352009-07-01 21:26:56 +0200342
343int cfg80211_wext_siwgenie(struct net_device *dev,
344 struct iw_request_info *info,
345 struct iw_point *data, char *extra)
346{
347 struct wireless_dev *wdev = dev->ieee80211_ptr;
348 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
349 u8 *ie = extra;
350 int ie_len = data->length, err;
351
352 if (wdev->iftype != NL80211_IFTYPE_STATION)
353 return -EOPNOTSUPP;
354
355 if (!ie_len)
356 ie = NULL;
357
Johannes Berg667503dd2009-07-07 03:56:11 +0200358 wdev_lock(wdev);
359
Johannes Bergf2129352009-07-01 21:26:56 +0200360 /* no change */
Johannes Berg667503dd2009-07-07 03:56:11 +0200361 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200362 if (wdev->wext.ie_len == ie_len &&
363 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503dd2009-07-07 03:56:11 +0200364 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200365
366 if (ie_len) {
367 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200368 if (!ie) {
369 err = -ENOMEM;
370 goto out;
371 }
Johannes Bergf2129352009-07-01 21:26:56 +0200372 } else
373 ie = NULL;
374
375 kfree(wdev->wext.ie);
376 wdev->wext.ie = ie;
377 wdev->wext.ie_len = ie_len;
378
379 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200380 err = __cfg80211_disconnect(rdev, dev,
381 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200382 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200383 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200384 }
385
386 /* userspace better not think we'll reconnect */
Johannes Berg667503dd2009-07-07 03:56:11 +0200387 err = 0;
388 out:
389 wdev_unlock(wdev);
390 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200391}
Johannes Bergf2129352009-07-01 21:26:56 +0200392
393int cfg80211_wext_siwmlme(struct net_device *dev,
394 struct iw_request_info *info,
395 struct iw_point *data, char *extra)
396{
397 struct wireless_dev *wdev = dev->ieee80211_ptr;
398 struct iw_mlme *mlme = (struct iw_mlme *)extra;
399 struct cfg80211_registered_device *rdev;
Johannes Berg667503dd2009-07-07 03:56:11 +0200400 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200401
402 if (!wdev)
403 return -EOPNOTSUPP;
404
405 rdev = wiphy_to_dev(wdev->wiphy);
406
407 if (wdev->iftype != NL80211_IFTYPE_STATION)
408 return -EINVAL;
409
410 if (mlme->addr.sa_family != ARPHRD_ETHER)
411 return -EINVAL;
412
Johannes Berg667503dd2009-07-07 03:56:11 +0200413 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200414 switch (mlme->cmd) {
415 case IW_MLME_DEAUTH:
416 case IW_MLME_DISASSOC:
Johannes Berg667503dd2009-07-07 03:56:11 +0200417 err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
418 true);
419 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200420 default:
Johannes Berg667503dd2009-07-07 03:56:11 +0200421 err = -EOPNOTSUPP;
422 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200423 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200424 wdev_unlock(wdev);
425
426 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200427}