blob: e79cb5c0655ad34ec0e8df85471e6c77ad856716 [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 Bergf9f47522013-03-19 15:04:07 +010092 mutex_lock(&rdev->sched_scan_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +020093 wdev_lock(wdev);
94
Johannes Bergf2129352009-07-01 21:26:56 +020095 if (wdev->sme_state != CFG80211_SME_IDLE) {
96 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +020097
98 if (wdev->wext.connect.channel == chan) {
99 err = 0;
100 goto out;
101 }
102
Johannes Bergf2129352009-07-01 21:26:56 +0200103 /* if SSID set, we'll try right again, avoid event */
104 if (wdev->wext.connect.ssid_len)
105 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200106 err = __cfg80211_disconnect(rdev, dev,
107 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200108 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200109 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200110 }
111
Johannes Berg667503dd2009-07-07 03:56:11 +0200112
Johannes Bergf2129352009-07-01 21:26:56 +0200113 wdev->wext.connect.channel = chan;
114
Johannes Berge8c9bd52012-06-06 08:18:22 +0200115 /*
116 * SSID is not set, we just want to switch monitor channel,
117 * this is really just backward compatibility, if the SSID
118 * is set then we use the channel to select the BSS to use
119 * to connect to instead. If we were connected on another
120 * channel we disconnected above and reconnect below.
121 */
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200122 if (chan && !wdev->wext.connect.ssid_len) {
Johannes Berg683b6d32012-11-08 21:25:48 +0100123 struct cfg80211_chan_def chandef = {
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100124 .width = NL80211_CHAN_WIDTH_20_NOHT,
125 .center_freq1 = freq,
Johannes Berg683b6d32012-11-08 21:25:48 +0100126 };
127
128 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
129 if (chandef.chan)
130 err = cfg80211_set_monitor_channel(rdev, &chandef);
131 else
132 err = -EINVAL;
Johannes Berg667503dd2009-07-07 03:56:11 +0200133 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200134 }
135
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200136 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200137 out:
138 wdev_unlock(wdev);
Johannes Bergf9f47522013-03-19 15:04:07 +0100139 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200140 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200141 cfg80211_unlock_rdev(rdev);
142 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200143}
Johannes Bergf2129352009-07-01 21:26:56 +0200144
145int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
146 struct iw_request_info *info,
147 struct iw_freq *freq, char *extra)
148{
149 struct wireless_dev *wdev = dev->ieee80211_ptr;
150 struct ieee80211_channel *chan = NULL;
151
152 /* call only for station! */
153 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
154 return -EINVAL;
155
Johannes Berg667503dd2009-07-07 03:56:11 +0200156 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200157 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200158 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200159 else if (wdev->wext.connect.channel)
160 chan = wdev->wext.connect.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200161 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200162
163 if (chan) {
164 freq->m = chan->center_freq;
165 freq->e = 6;
166 return 0;
167 }
168
169 /* no channel if not joining */
170 return -EINVAL;
171}
Johannes Bergf2129352009-07-01 21:26:56 +0200172
173int cfg80211_mgd_wext_siwessid(struct net_device *dev,
174 struct iw_request_info *info,
175 struct iw_point *data, char *ssid)
176{
177 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200178 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200179 size_t len = data->length;
180 int err;
181
182 /* call only for station! */
183 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
184 return -EINVAL;
185
186 if (!data->flags)
187 len = 0;
188
189 /* iwconfig uses nul termination in SSID.. */
190 if (len > 0 && ssid[len - 1] == '\0')
191 len--;
192
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200193 cfg80211_lock_rdev(rdev);
194 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +0100195 mutex_lock(&rdev->sched_scan_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200196 wdev_lock(wdev);
197
198 err = 0;
199
Johannes Bergf2129352009-07-01 21:26:56 +0200200 if (wdev->sme_state != CFG80211_SME_IDLE) {
201 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200202
203 if (wdev->wext.connect.ssid && len &&
204 len == wdev->wext.connect.ssid_len &&
205 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
206 goto out;
207
Johannes Bergf2129352009-07-01 21:26:56 +0200208 /* if SSID set now, we'll try to connect, avoid event */
209 if (len)
210 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200211 err = __cfg80211_disconnect(rdev, dev,
212 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200213 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200214 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200215 }
216
Johannes Bergf401a6f2009-08-07 14:51:05 +0200217 wdev->wext.prev_bssid_valid = false;
Johannes Bergf2129352009-07-01 21:26:56 +0200218 wdev->wext.connect.ssid = wdev->wext.ssid;
219 memcpy(wdev->wext.ssid, ssid, len);
220 wdev->wext.connect.ssid_len = len;
221
222 wdev->wext.connect.crypto.control_port = false;
Johannes Bergc0692b82010-08-27 14:26:53 +0300223 wdev->wext.connect.crypto.control_port_ethertype =
224 cpu_to_be16(ETH_P_PAE);
Johannes Bergf2129352009-07-01 21:26:56 +0200225
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200226 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200227 out:
228 wdev_unlock(wdev);
Johannes Bergf9f47522013-03-19 15:04:07 +0100229 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200230 mutex_unlock(&rdev->devlist_mtx);
231 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200232 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200233}
Johannes Bergf2129352009-07-01 21:26:56 +0200234
235int cfg80211_mgd_wext_giwessid(struct net_device *dev,
236 struct iw_request_info *info,
237 struct iw_point *data, char *ssid)
238{
239 struct wireless_dev *wdev = dev->ieee80211_ptr;
240
241 /* call only for station! */
242 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
243 return -EINVAL;
244
245 data->flags = 0;
246
Johannes Berg667503dd2009-07-07 03:56:11 +0200247 wdev_lock(wdev);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800248 if (wdev->current_bss) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100249 const u8 *ie;
250
251 rcu_read_lock();
252 ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
253 WLAN_EID_SSID);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800254 if (ie) {
255 data->flags = 1;
256 data->length = ie[1];
257 memcpy(ssid, ie + 2, data->length);
258 }
Johannes Berg9caf0362012-11-29 01:25:20 +0100259 rcu_read_unlock();
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800260 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200261 data->flags = 1;
262 data->length = wdev->wext.connect.ssid_len;
263 memcpy(ssid, wdev->wext.connect.ssid, data->length);
Johannes Berg33de4f92009-09-17 08:35:54 -0700264 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200265 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200266
267 return 0;
268}
Johannes Bergf2129352009-07-01 21:26:56 +0200269
270int cfg80211_mgd_wext_siwap(struct net_device *dev,
271 struct iw_request_info *info,
272 struct sockaddr *ap_addr, char *extra)
273{
274 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200275 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200276 u8 *bssid = ap_addr->sa_data;
277 int err;
278
279 /* call only for station! */
280 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
281 return -EINVAL;
282
283 if (ap_addr->sa_family != ARPHRD_ETHER)
284 return -EINVAL;
285
286 /* automatic mode */
287 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
288 bssid = NULL;
289
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200290 cfg80211_lock_rdev(rdev);
291 mutex_lock(&rdev->devlist_mtx);
Johannes Bergf9f47522013-03-19 15:04:07 +0100292 mutex_lock(&rdev->sched_scan_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200293 wdev_lock(wdev);
294
Johannes Bergf2129352009-07-01 21:26:56 +0200295 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200296 err = 0;
297 /* both automatic */
298 if (!bssid && !wdev->wext.connect.bssid)
299 goto out;
300
301 /* fixed already - and no change */
302 if (wdev->wext.connect.bssid && bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000303 ether_addr_equal(bssid, wdev->wext.connect.bssid))
Johannes Berg25e83c42009-07-16 11:39:04 +0200304 goto out;
305
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200306 err = __cfg80211_disconnect(rdev, dev,
307 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200308 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200309 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200310 }
311
312 if (bssid) {
313 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
314 wdev->wext.connect.bssid = wdev->wext.bssid;
315 } else
316 wdev->wext.connect.bssid = NULL;
317
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200318 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200319 out:
320 wdev_unlock(wdev);
Johannes Bergf9f47522013-03-19 15:04:07 +0100321 mutex_unlock(&rdev->sched_scan_mtx);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200322 mutex_unlock(&rdev->devlist_mtx);
323 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200324 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200325}
Johannes Bergf2129352009-07-01 21:26:56 +0200326
327int cfg80211_mgd_wext_giwap(struct net_device *dev,
328 struct iw_request_info *info,
329 struct sockaddr *ap_addr, char *extra)
330{
331 struct wireless_dev *wdev = dev->ieee80211_ptr;
332
333 /* call only for station! */
334 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
335 return -EINVAL;
336
337 ap_addr->sa_family = ARPHRD_ETHER;
338
Johannes Berg667503dd2009-07-07 03:56:11 +0200339 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200340 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200341 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200342 else
343 memset(ap_addr->sa_data, 0, ETH_ALEN);
Johannes Berg667503dd2009-07-07 03:56:11 +0200344 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200345
346 return 0;
347}
Johannes Bergf2129352009-07-01 21:26:56 +0200348
349int cfg80211_wext_siwgenie(struct net_device *dev,
350 struct iw_request_info *info,
351 struct iw_point *data, char *extra)
352{
353 struct wireless_dev *wdev = dev->ieee80211_ptr;
354 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
355 u8 *ie = extra;
356 int ie_len = data->length, err;
357
358 if (wdev->iftype != NL80211_IFTYPE_STATION)
359 return -EOPNOTSUPP;
360
361 if (!ie_len)
362 ie = NULL;
363
Johannes Berg667503dd2009-07-07 03:56:11 +0200364 wdev_lock(wdev);
365
Johannes Bergf2129352009-07-01 21:26:56 +0200366 /* no change */
Johannes Berg667503dd2009-07-07 03:56:11 +0200367 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200368 if (wdev->wext.ie_len == ie_len &&
369 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503dd2009-07-07 03:56:11 +0200370 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200371
372 if (ie_len) {
373 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200374 if (!ie) {
375 err = -ENOMEM;
376 goto out;
377 }
Johannes Bergf2129352009-07-01 21:26:56 +0200378 } else
379 ie = NULL;
380
381 kfree(wdev->wext.ie);
382 wdev->wext.ie = ie;
383 wdev->wext.ie_len = ie_len;
384
385 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200386 err = __cfg80211_disconnect(rdev, dev,
387 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200388 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200389 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200390 }
391
392 /* userspace better not think we'll reconnect */
Johannes Berg667503dd2009-07-07 03:56:11 +0200393 err = 0;
394 out:
395 wdev_unlock(wdev);
396 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200397}
Johannes Bergf2129352009-07-01 21:26:56 +0200398
399int cfg80211_wext_siwmlme(struct net_device *dev,
400 struct iw_request_info *info,
401 struct iw_point *data, char *extra)
402{
403 struct wireless_dev *wdev = dev->ieee80211_ptr;
404 struct iw_mlme *mlme = (struct iw_mlme *)extra;
405 struct cfg80211_registered_device *rdev;
Johannes Berg667503dd2009-07-07 03:56:11 +0200406 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200407
408 if (!wdev)
409 return -EOPNOTSUPP;
410
411 rdev = wiphy_to_dev(wdev->wiphy);
412
413 if (wdev->iftype != NL80211_IFTYPE_STATION)
414 return -EINVAL;
415
416 if (mlme->addr.sa_family != ARPHRD_ETHER)
417 return -EINVAL;
418
Johannes Berg667503dd2009-07-07 03:56:11 +0200419 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200420 switch (mlme->cmd) {
421 case IW_MLME_DEAUTH:
422 case IW_MLME_DISASSOC:
Johannes Berg667503dd2009-07-07 03:56:11 +0200423 err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
424 true);
425 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200426 default:
Johannes Berg667503dd2009-07-07 03:56:11 +0200427 err = -EOPNOTSUPP;
428 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200429 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200430 wdev_unlock(wdev);
431
432 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200433}