blob: 326750b99151292ffd653932a89bf5ee703fad19 [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
Johannes Bergfffd0932009-07-08 14:22:54 +020033 if (wdev->wext.keys) {
34 wdev->wext.keys->def = wdev->wext.default_key;
35 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
Johannes Berg4be3bd82009-09-24 09:00:57 +020036 if (wdev->wext.default_key != -1)
37 wdev->wext.connect.privacy = true;
Johannes Bergfffd0932009-07-08 14:22:54 +020038 }
39
40 if (!wdev->wext.connect.ssid_len)
41 return 0;
42
43 if (wdev->wext.keys) {
44 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
45 if (!ck)
46 return -ENOMEM;
47 for (i = 0; i < 6; i++)
48 ck->params[i].key = ck->data[i];
49 }
Johannes Bergf401a6f2009-08-07 14:51:05 +020050
51 if (wdev->wext.prev_bssid_valid)
52 prev_bssid = wdev->wext.prev_bssid;
53
Johannes Bergfffd0932009-07-08 14:22:54 +020054 err = __cfg80211_connect(rdev, wdev->netdev,
Johannes Bergf401a6f2009-08-07 14:51:05 +020055 &wdev->wext.connect, ck, prev_bssid);
Johannes Bergfffd0932009-07-08 14:22:54 +020056 if (err)
57 kfree(ck);
Johannes Bergf2129352009-07-01 21:26:56 +020058
59 return err;
60}
61
62int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
63 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +020064 struct iw_freq *wextfreq, char *extra)
Johannes Bergf2129352009-07-01 21:26:56 +020065{
66 struct wireless_dev *wdev = dev->ieee80211_ptr;
67 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020068 struct ieee80211_channel *chan = NULL;
69 int err, freq;
Johannes Bergf2129352009-07-01 21:26:56 +020070
71 /* call only for station! */
72 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
73 return -EINVAL;
74
Johannes Berg59bbb6f2009-08-07 17:22:35 +020075 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
76 if (freq < 0)
77 return freq;
Johannes Bergf2129352009-07-01 21:26:56 +020078
Johannes Berg59bbb6f2009-08-07 17:22:35 +020079 if (freq) {
80 chan = ieee80211_get_channel(wdev->wiphy, freq);
81 if (!chan)
82 return -EINVAL;
83 if (chan->flags & IEEE80211_CHAN_DISABLED)
84 return -EINVAL;
85 }
Johannes Bergf2129352009-07-01 21:26:56 +020086
Johannes Berg667503dd2009-07-07 03:56:11 +020087 cfg80211_lock_rdev(rdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020088 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +020089 wdev_lock(wdev);
90
Johannes Bergf2129352009-07-01 21:26:56 +020091 if (wdev->sme_state != CFG80211_SME_IDLE) {
92 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +020093
94 if (wdev->wext.connect.channel == chan) {
95 err = 0;
96 goto out;
97 }
98
Johannes Bergf2129352009-07-01 21:26:56 +020099 /* if SSID set, we'll try right again, avoid event */
100 if (wdev->wext.connect.ssid_len)
101 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200102 err = __cfg80211_disconnect(rdev, dev,
103 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200104 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200105 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200106 }
107
Johannes Berg667503dd2009-07-07 03:56:11 +0200108
Johannes Bergf2129352009-07-01 21:26:56 +0200109 wdev->wext.connect.channel = chan;
110
111 /* SSID is not set, we just want to switch channel */
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200112 if (chan && !wdev->wext.connect.ssid_len) {
Johannes Bergf444de02010-05-05 15:25:02 +0200113 err = cfg80211_set_freq(rdev, wdev, freq, NL80211_CHAN_NO_HT);
Johannes Berg667503dd2009-07-07 03:56:11 +0200114 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200115 }
116
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200117 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200118 out:
119 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200120 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200121 cfg80211_unlock_rdev(rdev);
122 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200123}
Johannes Bergf2129352009-07-01 21:26:56 +0200124
125int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
126 struct iw_request_info *info,
127 struct iw_freq *freq, char *extra)
128{
129 struct wireless_dev *wdev = dev->ieee80211_ptr;
130 struct ieee80211_channel *chan = NULL;
131
132 /* call only for station! */
133 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
134 return -EINVAL;
135
Johannes Berg667503dd2009-07-07 03:56:11 +0200136 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200137 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200138 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200139 else if (wdev->wext.connect.channel)
140 chan = wdev->wext.connect.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200141 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200142
143 if (chan) {
144 freq->m = chan->center_freq;
145 freq->e = 6;
146 return 0;
147 }
148
149 /* no channel if not joining */
150 return -EINVAL;
151}
Johannes Bergf2129352009-07-01 21:26:56 +0200152
153int cfg80211_mgd_wext_siwessid(struct net_device *dev,
154 struct iw_request_info *info,
155 struct iw_point *data, char *ssid)
156{
157 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200158 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200159 size_t len = data->length;
160 int err;
161
162 /* call only for station! */
163 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
164 return -EINVAL;
165
166 if (!data->flags)
167 len = 0;
168
169 /* iwconfig uses nul termination in SSID.. */
170 if (len > 0 && ssid[len - 1] == '\0')
171 len--;
172
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200173 cfg80211_lock_rdev(rdev);
174 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200175 wdev_lock(wdev);
176
177 err = 0;
178
Johannes Bergf2129352009-07-01 21:26:56 +0200179 if (wdev->sme_state != CFG80211_SME_IDLE) {
180 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200181
182 if (wdev->wext.connect.ssid && len &&
183 len == wdev->wext.connect.ssid_len &&
184 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
185 goto out;
186
Johannes Bergf2129352009-07-01 21:26:56 +0200187 /* if SSID set now, we'll try to connect, avoid event */
188 if (len)
189 event = false;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200190 err = __cfg80211_disconnect(rdev, dev,
191 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200192 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200193 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200194 }
195
Johannes Bergf401a6f2009-08-07 14:51:05 +0200196 wdev->wext.prev_bssid_valid = false;
Johannes Bergf2129352009-07-01 21:26:56 +0200197 wdev->wext.connect.ssid = wdev->wext.ssid;
198 memcpy(wdev->wext.ssid, ssid, len);
199 wdev->wext.connect.ssid_len = len;
200
201 wdev->wext.connect.crypto.control_port = false;
Johannes Bergc0692b82010-08-27 14:26:53 +0300202 wdev->wext.connect.crypto.control_port_ethertype =
203 cpu_to_be16(ETH_P_PAE);
Johannes Bergf2129352009-07-01 21:26:56 +0200204
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200205 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200206 out:
207 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200208 mutex_unlock(&rdev->devlist_mtx);
209 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200210 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200211}
Johannes Bergf2129352009-07-01 21:26:56 +0200212
213int cfg80211_mgd_wext_giwessid(struct net_device *dev,
214 struct iw_request_info *info,
215 struct iw_point *data, char *ssid)
216{
217 struct wireless_dev *wdev = dev->ieee80211_ptr;
218
219 /* call only for station! */
220 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
221 return -EINVAL;
222
223 data->flags = 0;
224
Johannes Berg667503dd2009-07-07 03:56:11 +0200225 wdev_lock(wdev);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800226 if (wdev->current_bss) {
227 const u8 *ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
228 WLAN_EID_SSID);
229 if (ie) {
230 data->flags = 1;
231 data->length = ie[1];
232 memcpy(ssid, ie + 2, data->length);
233 }
234 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200235 data->flags = 1;
236 data->length = wdev->wext.connect.ssid_len;
237 memcpy(ssid, wdev->wext.connect.ssid, data->length);
Johannes Berg33de4f92009-09-17 08:35:54 -0700238 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200239 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200240
241 return 0;
242}
Johannes Bergf2129352009-07-01 21:26:56 +0200243
244int cfg80211_mgd_wext_siwap(struct net_device *dev,
245 struct iw_request_info *info,
246 struct sockaddr *ap_addr, char *extra)
247{
248 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200249 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200250 u8 *bssid = ap_addr->sa_data;
251 int err;
252
253 /* call only for station! */
254 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
255 return -EINVAL;
256
257 if (ap_addr->sa_family != ARPHRD_ETHER)
258 return -EINVAL;
259
260 /* automatic mode */
261 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
262 bssid = NULL;
263
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200264 cfg80211_lock_rdev(rdev);
265 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200266 wdev_lock(wdev);
267
Johannes Bergf2129352009-07-01 21:26:56 +0200268 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200269 err = 0;
270 /* both automatic */
271 if (!bssid && !wdev->wext.connect.bssid)
272 goto out;
273
274 /* fixed already - and no change */
275 if (wdev->wext.connect.bssid && bssid &&
276 compare_ether_addr(bssid, wdev->wext.connect.bssid) == 0)
277 goto out;
278
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200279 err = __cfg80211_disconnect(rdev, dev,
280 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200281 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200282 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200283 }
284
285 if (bssid) {
286 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
287 wdev->wext.connect.bssid = wdev->wext.bssid;
288 } else
289 wdev->wext.connect.bssid = NULL;
290
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200291 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200292 out:
293 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200294 mutex_unlock(&rdev->devlist_mtx);
295 cfg80211_unlock_rdev(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +0200296 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200297}
Johannes Bergf2129352009-07-01 21:26:56 +0200298
299int cfg80211_mgd_wext_giwap(struct net_device *dev,
300 struct iw_request_info *info,
301 struct sockaddr *ap_addr, char *extra)
302{
303 struct wireless_dev *wdev = dev->ieee80211_ptr;
304
305 /* call only for station! */
306 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
307 return -EINVAL;
308
309 ap_addr->sa_family = ARPHRD_ETHER;
310
Johannes Berg667503dd2009-07-07 03:56:11 +0200311 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200312 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200313 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200314 else
315 memset(ap_addr->sa_data, 0, ETH_ALEN);
Johannes Berg667503dd2009-07-07 03:56:11 +0200316 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200317
318 return 0;
319}
Johannes Bergf2129352009-07-01 21:26:56 +0200320
321int cfg80211_wext_siwgenie(struct net_device *dev,
322 struct iw_request_info *info,
323 struct iw_point *data, char *extra)
324{
325 struct wireless_dev *wdev = dev->ieee80211_ptr;
326 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
327 u8 *ie = extra;
328 int ie_len = data->length, err;
329
330 if (wdev->iftype != NL80211_IFTYPE_STATION)
331 return -EOPNOTSUPP;
332
333 if (!ie_len)
334 ie = NULL;
335
Johannes Berg667503dd2009-07-07 03:56:11 +0200336 wdev_lock(wdev);
337
Johannes Bergf2129352009-07-01 21:26:56 +0200338 /* no change */
Johannes Berg667503dd2009-07-07 03:56:11 +0200339 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200340 if (wdev->wext.ie_len == ie_len &&
341 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503dd2009-07-07 03:56:11 +0200342 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200343
344 if (ie_len) {
345 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200346 if (!ie) {
347 err = -ENOMEM;
348 goto out;
349 }
Johannes Bergf2129352009-07-01 21:26:56 +0200350 } else
351 ie = NULL;
352
353 kfree(wdev->wext.ie);
354 wdev->wext.ie = ie;
355 wdev->wext.ie_len = ie_len;
356
357 if (wdev->sme_state != CFG80211_SME_IDLE) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200358 err = __cfg80211_disconnect(rdev, dev,
359 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200360 if (err)
Johannes Berg667503dd2009-07-07 03:56:11 +0200361 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200362 }
363
364 /* userspace better not think we'll reconnect */
Johannes Berg667503dd2009-07-07 03:56:11 +0200365 err = 0;
366 out:
367 wdev_unlock(wdev);
368 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200369}
Johannes Bergf2129352009-07-01 21:26:56 +0200370
371int cfg80211_wext_siwmlme(struct net_device *dev,
372 struct iw_request_info *info,
373 struct iw_point *data, char *extra)
374{
375 struct wireless_dev *wdev = dev->ieee80211_ptr;
376 struct iw_mlme *mlme = (struct iw_mlme *)extra;
377 struct cfg80211_registered_device *rdev;
Johannes Berg667503dd2009-07-07 03:56:11 +0200378 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200379
380 if (!wdev)
381 return -EOPNOTSUPP;
382
383 rdev = wiphy_to_dev(wdev->wiphy);
384
385 if (wdev->iftype != NL80211_IFTYPE_STATION)
386 return -EINVAL;
387
388 if (mlme->addr.sa_family != ARPHRD_ETHER)
389 return -EINVAL;
390
Johannes Berg667503dd2009-07-07 03:56:11 +0200391 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200392 switch (mlme->cmd) {
393 case IW_MLME_DEAUTH:
394 case IW_MLME_DISASSOC:
Johannes Berg667503dd2009-07-07 03:56:11 +0200395 err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
396 true);
397 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200398 default:
Johannes Berg667503dd2009-07-07 03:56:11 +0200399 err = -EOPNOTSUPP;
400 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200401 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200402 wdev_unlock(wdev);
403
404 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200405}