blob: 995163830a61f582c11b9234b370bb2c3766f7f0 [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
Johannes Berg667503d2009-07-07 03:56:11 +0200108
Johannes Bergf2129352009-07-01 21:26:56 +0200109 wdev->wext.connect.channel = chan;
110
Johannes Berge8c9bd52012-06-06 08:18:22 +0200111 /*
112 * SSID is not set, we just want to switch monitor channel,
113 * this is really just backward compatibility, if the SSID
114 * is set then we use the channel to select the BSS to use
115 * to connect to instead. If we were connected on another
116 * channel we disconnected above and reconnect below.
117 */
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200118 if (chan && !wdev->wext.connect.ssid_len) {
Johannes Berg683b6d32012-11-08 21:25:48 +0100119 struct cfg80211_chan_def chandef = {
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100120 .width = NL80211_CHAN_WIDTH_20_NOHT,
121 .center_freq1 = freq,
Johannes Berg683b6d32012-11-08 21:25:48 +0100122 };
123
124 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
125 if (chandef.chan)
126 err = cfg80211_set_monitor_channel(rdev, &chandef);
127 else
128 err = -EINVAL;
Johannes Berg667503d2009-07-07 03:56:11 +0200129 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200130 }
131
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200132 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200133 out:
134 wdev_unlock(wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200135 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200136}
Johannes Bergf2129352009-07-01 21:26:56 +0200137
138int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
139 struct iw_request_info *info,
140 struct iw_freq *freq, char *extra)
141{
142 struct wireless_dev *wdev = dev->ieee80211_ptr;
143 struct ieee80211_channel *chan = NULL;
144
145 /* call only for station! */
146 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
147 return -EINVAL;
148
Johannes Berg667503d2009-07-07 03:56:11 +0200149 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200150 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200151 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200152 else if (wdev->wext.connect.channel)
153 chan = wdev->wext.connect.channel;
Johannes Berg667503d2009-07-07 03:56:11 +0200154 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200155
156 if (chan) {
157 freq->m = chan->center_freq;
158 freq->e = 6;
159 return 0;
160 }
161
162 /* no channel if not joining */
163 return -EINVAL;
164}
Johannes Bergf2129352009-07-01 21:26:56 +0200165
166int cfg80211_mgd_wext_siwessid(struct net_device *dev,
167 struct iw_request_info *info,
168 struct iw_point *data, char *ssid)
169{
170 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800171 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200172 size_t len = data->length;
173 int err;
174
175 /* call only for station! */
176 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
177 return -EINVAL;
178
179 if (!data->flags)
180 len = 0;
181
182 /* iwconfig uses nul termination in SSID.. */
183 if (len > 0 && ssid[len - 1] == '\0')
184 len--;
185
Johannes Berg667503d2009-07-07 03:56:11 +0200186 wdev_lock(wdev);
187
188 err = 0;
189
Johannes Bergceca7b72013-05-16 00:55:45 +0200190 if (wdev->conn) {
Johannes Bergf2129352009-07-01 21:26:56 +0200191 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200192
193 if (wdev->wext.connect.ssid && len &&
194 len == wdev->wext.connect.ssid_len &&
195 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
196 goto out;
197
Johannes Bergf2129352009-07-01 21:26:56 +0200198 /* if SSID set now, we'll try to connect, avoid event */
199 if (len)
200 event = false;
Johannes Berg83739b02013-05-15 17:44:01 +0200201 err = cfg80211_disconnect(rdev, dev,
202 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200203 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200204 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200205 }
206
Johannes Bergf401a6f2009-08-07 14:51:05 +0200207 wdev->wext.prev_bssid_valid = false;
Johannes Bergf2129352009-07-01 21:26:56 +0200208 wdev->wext.connect.ssid = wdev->wext.ssid;
209 memcpy(wdev->wext.ssid, ssid, len);
210 wdev->wext.connect.ssid_len = len;
211
212 wdev->wext.connect.crypto.control_port = false;
Johannes Bergc0692b82010-08-27 14:26:53 +0300213 wdev->wext.connect.crypto.control_port_ethertype =
214 cpu_to_be16(ETH_P_PAE);
Johannes Bergf2129352009-07-01 21:26:56 +0200215
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200216 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200217 out:
218 wdev_unlock(wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200219 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200220}
Johannes Bergf2129352009-07-01 21:26:56 +0200221
222int cfg80211_mgd_wext_giwessid(struct net_device *dev,
223 struct iw_request_info *info,
224 struct iw_point *data, char *ssid)
225{
226 struct wireless_dev *wdev = dev->ieee80211_ptr;
227
228 /* call only for station! */
229 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
230 return -EINVAL;
231
232 data->flags = 0;
233
Johannes Berg667503d2009-07-07 03:56:11 +0200234 wdev_lock(wdev);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800235 if (wdev->current_bss) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100236 const u8 *ie;
237
238 rcu_read_lock();
239 ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
240 WLAN_EID_SSID);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800241 if (ie) {
242 data->flags = 1;
243 data->length = ie[1];
244 memcpy(ssid, ie + 2, data->length);
245 }
Johannes Berg9caf0362012-11-29 01:25:20 +0100246 rcu_read_unlock();
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800247 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200248 data->flags = 1;
249 data->length = wdev->wext.connect.ssid_len;
250 memcpy(ssid, wdev->wext.connect.ssid, data->length);
Johannes Berg33de4f92009-09-17 08:35:54 -0700251 }
Johannes Berg667503d2009-07-07 03:56:11 +0200252 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200253
254 return 0;
255}
Johannes Bergf2129352009-07-01 21:26:56 +0200256
257int cfg80211_mgd_wext_siwap(struct net_device *dev,
258 struct iw_request_info *info,
259 struct sockaddr *ap_addr, char *extra)
260{
261 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800262 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200263 u8 *bssid = ap_addr->sa_data;
264 int err;
265
266 /* call only for station! */
267 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
268 return -EINVAL;
269
270 if (ap_addr->sa_family != ARPHRD_ETHER)
271 return -EINVAL;
272
273 /* automatic mode */
274 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
275 bssid = NULL;
276
Johannes Berg667503d2009-07-07 03:56:11 +0200277 wdev_lock(wdev);
278
Johannes Bergceca7b72013-05-16 00:55:45 +0200279 if (wdev->conn) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200280 err = 0;
281 /* both automatic */
282 if (!bssid && !wdev->wext.connect.bssid)
283 goto out;
284
285 /* fixed already - and no change */
286 if (wdev->wext.connect.bssid && bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000287 ether_addr_equal(bssid, wdev->wext.connect.bssid))
Johannes Berg25e83c42009-07-16 11:39:04 +0200288 goto out;
289
Johannes Berg83739b02013-05-15 17:44:01 +0200290 err = cfg80211_disconnect(rdev, dev,
291 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200292 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200293 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200294 }
295
296 if (bssid) {
297 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
298 wdev->wext.connect.bssid = wdev->wext.bssid;
299 } else
300 wdev->wext.connect.bssid = NULL;
301
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200302 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200303 out:
304 wdev_unlock(wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200305 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200306}
Johannes Bergf2129352009-07-01 21:26:56 +0200307
308int cfg80211_mgd_wext_giwap(struct net_device *dev,
309 struct iw_request_info *info,
310 struct sockaddr *ap_addr, char *extra)
311{
312 struct wireless_dev *wdev = dev->ieee80211_ptr;
313
314 /* call only for station! */
315 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
316 return -EINVAL;
317
318 ap_addr->sa_family = ARPHRD_ETHER;
319
Johannes Berg667503d2009-07-07 03:56:11 +0200320 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200321 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200322 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200323 else
Joe Perchesd2beae12015-03-02 19:54:58 -0800324 eth_zero_addr(ap_addr->sa_data);
Johannes Berg667503d2009-07-07 03:56:11 +0200325 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200326
327 return 0;
328}
Johannes Bergf2129352009-07-01 21:26:56 +0200329
330int cfg80211_wext_siwgenie(struct net_device *dev,
331 struct iw_request_info *info,
332 struct iw_point *data, char *extra)
333{
334 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800335 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200336 u8 *ie = extra;
337 int ie_len = data->length, err;
338
339 if (wdev->iftype != NL80211_IFTYPE_STATION)
340 return -EOPNOTSUPP;
341
342 if (!ie_len)
343 ie = NULL;
344
Johannes Berg667503d2009-07-07 03:56:11 +0200345 wdev_lock(wdev);
346
Johannes Bergf2129352009-07-01 21:26:56 +0200347 /* no change */
Johannes Berg667503d2009-07-07 03:56:11 +0200348 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200349 if (wdev->wext.ie_len == ie_len &&
350 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503d2009-07-07 03:56:11 +0200351 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200352
353 if (ie_len) {
354 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503d2009-07-07 03:56:11 +0200355 if (!ie) {
356 err = -ENOMEM;
357 goto out;
358 }
Johannes Bergf2129352009-07-01 21:26:56 +0200359 } else
360 ie = NULL;
361
362 kfree(wdev->wext.ie);
363 wdev->wext.ie = ie;
364 wdev->wext.ie_len = ie_len;
365
Johannes Bergceca7b72013-05-16 00:55:45 +0200366 if (wdev->conn) {
Johannes Berg83739b02013-05-15 17:44:01 +0200367 err = cfg80211_disconnect(rdev, dev,
368 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200369 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200370 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200371 }
372
373 /* userspace better not think we'll reconnect */
Johannes Berg667503d2009-07-07 03:56:11 +0200374 err = 0;
375 out:
376 wdev_unlock(wdev);
377 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200378}
Johannes Bergf2129352009-07-01 21:26:56 +0200379
380int cfg80211_wext_siwmlme(struct net_device *dev,
381 struct iw_request_info *info,
382 struct iw_point *data, char *extra)
383{
384 struct wireless_dev *wdev = dev->ieee80211_ptr;
385 struct iw_mlme *mlme = (struct iw_mlme *)extra;
386 struct cfg80211_registered_device *rdev;
Johannes Berg667503d2009-07-07 03:56:11 +0200387 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200388
389 if (!wdev)
390 return -EOPNOTSUPP;
391
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800392 rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200393
394 if (wdev->iftype != NL80211_IFTYPE_STATION)
395 return -EINVAL;
396
397 if (mlme->addr.sa_family != ARPHRD_ETHER)
398 return -EINVAL;
399
Johannes Berg667503d2009-07-07 03:56:11 +0200400 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200401 switch (mlme->cmd) {
402 case IW_MLME_DEAUTH:
403 case IW_MLME_DISASSOC:
Johannes Berg83739b02013-05-15 17:44:01 +0200404 err = cfg80211_disconnect(rdev, dev, mlme->reason_code, true);
Johannes Berg667503d2009-07-07 03:56:11 +0200405 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200406 default:
Johannes Berg667503d2009-07-07 03:56:11 +0200407 err = -EOPNOTSUPP;
408 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200409 }
Johannes Berg667503d2009-07-07 03:56:11 +0200410 wdev_unlock(wdev);
411
412 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200413}