blob: c434f193f39aa6a1215c2791e25a891faed6b573 [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
Zhao, Gang73fb08e2014-03-19 17:04:36 +080024 ASSERT_RTNL();
Johannes Berg667503d2009-07-07 03:56:11 +020025 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;
Johannes Berg4be3bd82009-09-24 09:00:57 +020038 if (wdev->wext.default_key != -1)
39 wdev->wext.connect.privacy = true;
Johannes Bergfffd0932009-07-08 14:22:54 +020040 }
41
42 if (!wdev->wext.connect.ssid_len)
43 return 0;
44
Johannes Bergf1c1f172016-09-13 17:08:23 +020045 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
Johannes Bergfffd0932009-07-08 14:22:54 +020046 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
47 if (!ck)
48 return -ENOMEM;
David Spinadelb8676222016-09-22 23:16:50 +030049 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
Johannes Bergfffd0932009-07-08 14:22:54 +020050 ck->params[i].key = ck->data[i];
51 }
Johannes Bergf401a6f2009-08-07 14:51:05 +020052
53 if (wdev->wext.prev_bssid_valid)
54 prev_bssid = wdev->wext.prev_bssid;
55
Johannes Berg83739b02013-05-15 17:44:01 +020056 err = cfg80211_connect(rdev, wdev->netdev,
57 &wdev->wext.connect, ck, prev_bssid);
Johannes Bergfffd0932009-07-08 14:22:54 +020058 if (err)
Johannes Bergb47f6102014-09-10 13:39:54 +030059 kzfree(ck);
Johannes Bergf2129352009-07-01 21:26:56 +020060
61 return err;
62}
63
64int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
65 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +020066 struct iw_freq *wextfreq, char *extra)
Johannes Bergf2129352009-07-01 21:26:56 +020067{
68 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080069 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020070 struct ieee80211_channel *chan = NULL;
71 int err, freq;
Johannes Bergf2129352009-07-01 21:26:56 +020072
73 /* call only for station! */
74 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
75 return -EINVAL;
76
Zhao, Gang96998e32014-04-09 09:28:06 +080077 freq = cfg80211_wext_freq(wextfreq);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020078 if (freq < 0)
79 return freq;
Johannes Bergf2129352009-07-01 21:26:56 +020080
Johannes Berg59bbb6f2009-08-07 17:22:35 +020081 if (freq) {
82 chan = ieee80211_get_channel(wdev->wiphy, freq);
83 if (!chan)
84 return -EINVAL;
85 if (chan->flags & IEEE80211_CHAN_DISABLED)
86 return -EINVAL;
87 }
Johannes Bergf2129352009-07-01 21:26:56 +020088
Johannes Berg667503d2009-07-07 03:56:11 +020089 wdev_lock(wdev);
90
Johannes Bergceca7b72013-05-16 00:55:45 +020091 if (wdev->conn) {
Johannes Bergf2129352009-07-01 21:26:56 +020092 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 Berg83739b02013-05-15 17:44:01 +0200102 err = cfg80211_disconnect(rdev, dev,
103 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200104 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200105 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200106 }
107
108 wdev->wext.connect.channel = chan;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200109 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200110 out:
111 wdev_unlock(wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200112 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200113}
Johannes Bergf2129352009-07-01 21:26:56 +0200114
115int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
116 struct iw_request_info *info,
117 struct iw_freq *freq, char *extra)
118{
119 struct wireless_dev *wdev = dev->ieee80211_ptr;
120 struct ieee80211_channel *chan = NULL;
121
122 /* call only for station! */
123 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
124 return -EINVAL;
125
Johannes Berg667503d2009-07-07 03:56:11 +0200126 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200127 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200128 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200129 else if (wdev->wext.connect.channel)
130 chan = wdev->wext.connect.channel;
Johannes Berg667503d2009-07-07 03:56:11 +0200131 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200132
133 if (chan) {
134 freq->m = chan->center_freq;
135 freq->e = 6;
136 return 0;
137 }
138
139 /* no channel if not joining */
140 return -EINVAL;
141}
Johannes Bergf2129352009-07-01 21:26:56 +0200142
143int cfg80211_mgd_wext_siwessid(struct net_device *dev,
144 struct iw_request_info *info,
145 struct iw_point *data, char *ssid)
146{
147 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800148 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200149 size_t len = data->length;
150 int err;
151
152 /* call only for station! */
153 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
154 return -EINVAL;
155
156 if (!data->flags)
157 len = 0;
158
159 /* iwconfig uses nul termination in SSID.. */
160 if (len > 0 && ssid[len - 1] == '\0')
161 len--;
162
Johannes Berg667503d2009-07-07 03:56:11 +0200163 wdev_lock(wdev);
164
165 err = 0;
166
Johannes Bergceca7b72013-05-16 00:55:45 +0200167 if (wdev->conn) {
Johannes Bergf2129352009-07-01 21:26:56 +0200168 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200169
170 if (wdev->wext.connect.ssid && len &&
171 len == wdev->wext.connect.ssid_len &&
172 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
173 goto out;
174
Johannes Bergf2129352009-07-01 21:26:56 +0200175 /* if SSID set now, we'll try to connect, avoid event */
176 if (len)
177 event = false;
Johannes Berg83739b02013-05-15 17:44:01 +0200178 err = cfg80211_disconnect(rdev, dev,
179 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200180 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200181 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200182 }
183
Johannes Bergf401a6f2009-08-07 14:51:05 +0200184 wdev->wext.prev_bssid_valid = false;
Johannes Bergf2129352009-07-01 21:26:56 +0200185 wdev->wext.connect.ssid = wdev->wext.ssid;
186 memcpy(wdev->wext.ssid, ssid, len);
187 wdev->wext.connect.ssid_len = len;
188
189 wdev->wext.connect.crypto.control_port = false;
Johannes Bergc0692b82010-08-27 14:26:53 +0300190 wdev->wext.connect.crypto.control_port_ethertype =
191 cpu_to_be16(ETH_P_PAE);
Johannes Bergf2129352009-07-01 21:26:56 +0200192
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200193 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200194 out:
195 wdev_unlock(wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200196 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200197}
Johannes Bergf2129352009-07-01 21:26:56 +0200198
199int cfg80211_mgd_wext_giwessid(struct net_device *dev,
200 struct iw_request_info *info,
201 struct iw_point *data, char *ssid)
202{
203 struct wireless_dev *wdev = dev->ieee80211_ptr;
204
205 /* call only for station! */
206 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
207 return -EINVAL;
208
209 data->flags = 0;
210
Johannes Berg667503d2009-07-07 03:56:11 +0200211 wdev_lock(wdev);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800212 if (wdev->current_bss) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100213 const u8 *ie;
214
215 rcu_read_lock();
216 ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
217 WLAN_EID_SSID);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800218 if (ie) {
219 data->flags = 1;
220 data->length = ie[1];
221 memcpy(ssid, ie + 2, data->length);
222 }
Johannes Berg9caf0362012-11-29 01:25:20 +0100223 rcu_read_unlock();
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800224 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200225 data->flags = 1;
226 data->length = wdev->wext.connect.ssid_len;
227 memcpy(ssid, wdev->wext.connect.ssid, data->length);
Johannes Berg33de4f92009-09-17 08:35:54 -0700228 }
Johannes Berg667503d2009-07-07 03:56:11 +0200229 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200230
231 return 0;
232}
Johannes Bergf2129352009-07-01 21:26:56 +0200233
234int cfg80211_mgd_wext_siwap(struct net_device *dev,
235 struct iw_request_info *info,
236 struct sockaddr *ap_addr, char *extra)
237{
238 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800239 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200240 u8 *bssid = ap_addr->sa_data;
241 int err;
242
243 /* call only for station! */
244 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
245 return -EINVAL;
246
247 if (ap_addr->sa_family != ARPHRD_ETHER)
248 return -EINVAL;
249
250 /* automatic mode */
251 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
252 bssid = NULL;
253
Johannes Berg667503d2009-07-07 03:56:11 +0200254 wdev_lock(wdev);
255
Johannes Bergceca7b72013-05-16 00:55:45 +0200256 if (wdev->conn) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200257 err = 0;
258 /* both automatic */
259 if (!bssid && !wdev->wext.connect.bssid)
260 goto out;
261
262 /* fixed already - and no change */
263 if (wdev->wext.connect.bssid && bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000264 ether_addr_equal(bssid, wdev->wext.connect.bssid))
Johannes Berg25e83c42009-07-16 11:39:04 +0200265 goto out;
266
Johannes Berg83739b02013-05-15 17:44:01 +0200267 err = cfg80211_disconnect(rdev, dev,
268 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200269 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200270 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200271 }
272
273 if (bssid) {
274 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
275 wdev->wext.connect.bssid = wdev->wext.bssid;
276 } else
277 wdev->wext.connect.bssid = NULL;
278
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200279 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200280 out:
281 wdev_unlock(wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200282 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200283}
Johannes Bergf2129352009-07-01 21:26:56 +0200284
285int cfg80211_mgd_wext_giwap(struct net_device *dev,
286 struct iw_request_info *info,
287 struct sockaddr *ap_addr, char *extra)
288{
289 struct wireless_dev *wdev = dev->ieee80211_ptr;
290
291 /* call only for station! */
292 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
293 return -EINVAL;
294
295 ap_addr->sa_family = ARPHRD_ETHER;
296
Johannes Berg667503d2009-07-07 03:56:11 +0200297 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200298 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200299 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200300 else
Joe Perchesd2beae12015-03-02 19:54:58 -0800301 eth_zero_addr(ap_addr->sa_data);
Johannes Berg667503d2009-07-07 03:56:11 +0200302 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200303
304 return 0;
305}
Johannes Bergf2129352009-07-01 21:26:56 +0200306
307int cfg80211_wext_siwgenie(struct net_device *dev,
308 struct iw_request_info *info,
309 struct iw_point *data, char *extra)
310{
311 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800312 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200313 u8 *ie = extra;
314 int ie_len = data->length, err;
315
316 if (wdev->iftype != NL80211_IFTYPE_STATION)
317 return -EOPNOTSUPP;
318
319 if (!ie_len)
320 ie = NULL;
321
Johannes Berg667503d2009-07-07 03:56:11 +0200322 wdev_lock(wdev);
323
Johannes Bergf2129352009-07-01 21:26:56 +0200324 /* no change */
Johannes Berg667503d2009-07-07 03:56:11 +0200325 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200326 if (wdev->wext.ie_len == ie_len &&
327 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503d2009-07-07 03:56:11 +0200328 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200329
330 if (ie_len) {
331 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503d2009-07-07 03:56:11 +0200332 if (!ie) {
333 err = -ENOMEM;
334 goto out;
335 }
Johannes Bergf2129352009-07-01 21:26:56 +0200336 } else
337 ie = NULL;
338
339 kfree(wdev->wext.ie);
340 wdev->wext.ie = ie;
341 wdev->wext.ie_len = ie_len;
342
Johannes Bergceca7b72013-05-16 00:55:45 +0200343 if (wdev->conn) {
Johannes Berg83739b02013-05-15 17:44:01 +0200344 err = cfg80211_disconnect(rdev, dev,
345 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200346 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200347 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200348 }
349
350 /* userspace better not think we'll reconnect */
Johannes Berg667503d2009-07-07 03:56:11 +0200351 err = 0;
352 out:
353 wdev_unlock(wdev);
354 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200355}
Johannes Bergf2129352009-07-01 21:26:56 +0200356
357int cfg80211_wext_siwmlme(struct net_device *dev,
358 struct iw_request_info *info,
359 struct iw_point *data, char *extra)
360{
361 struct wireless_dev *wdev = dev->ieee80211_ptr;
362 struct iw_mlme *mlme = (struct iw_mlme *)extra;
363 struct cfg80211_registered_device *rdev;
Johannes Berg667503d2009-07-07 03:56:11 +0200364 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200365
366 if (!wdev)
367 return -EOPNOTSUPP;
368
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800369 rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200370
371 if (wdev->iftype != NL80211_IFTYPE_STATION)
372 return -EINVAL;
373
374 if (mlme->addr.sa_family != ARPHRD_ETHER)
375 return -EINVAL;
376
Johannes Berg667503d2009-07-07 03:56:11 +0200377 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200378 switch (mlme->cmd) {
379 case IW_MLME_DEAUTH:
380 case IW_MLME_DISASSOC:
Johannes Berg83739b02013-05-15 17:44:01 +0200381 err = cfg80211_disconnect(rdev, dev, mlme->reason_code, true);
Johannes Berg667503d2009-07-07 03:56:11 +0200382 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200383 default:
Johannes Berg667503d2009-07-07 03:56:11 +0200384 err = -EOPNOTSUPP;
385 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200386 }
Johannes Berg667503d2009-07-07 03:56:11 +0200387 wdev_unlock(wdev);
388
389 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200390}