blob: 73fd0eae08caa960eef0b72211c6fc2485cbfb43 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Johannes Bergf2129352009-07-01 21:26:56 +02002/*
3 * cfg80211 wext compat for managed mode.
4 *
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright (C) 2009 Intel Corporation. All rights reserved.
7 */
8
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04009#include <linux/export.h>
Johannes Bergf2129352009-07-01 21:26:56 +020010#include <linux/etherdevice.h>
11#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Johannes Bergf2129352009-07-01 21:26:56 +020013#include <net/cfg80211.h>
Johannes Berg262eb9b22011-07-13 10:39:09 +020014#include <net/cfg80211-wext.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020015#include "wext-compat.h"
Johannes Bergf2129352009-07-01 21:26:56 +020016#include "nl80211.h"
17
Johannes Bergfffd0932009-07-08 14:22:54 +020018int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
19 struct wireless_dev *wdev)
Johannes Bergf2129352009-07-01 21:26:56 +020020{
Johannes Bergfffd0932009-07-08 14:22:54 +020021 struct cfg80211_cached_keys *ck = NULL;
Johannes Bergf401a6f2009-08-07 14:51:05 +020022 const u8 *prev_bssid = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +020023 int err, i;
Johannes Bergf2129352009-07-01 21:26:56 +020024
Zhao, Gang73fb08e2014-03-19 17:04:36 +080025 ASSERT_RTNL();
Johannes Berg667503d2009-07-07 03:56:11 +020026 ASSERT_WDEV_LOCK(wdev);
27
Johannes Bergf2129352009-07-01 21:26:56 +020028 if (!netif_running(wdev->netdev))
29 return 0;
30
31 wdev->wext.connect.ie = wdev->wext.ie;
32 wdev->wext.connect.ie_len = wdev->wext.ie_len;
Johannes Bergf2129352009-07-01 21:26:56 +020033
Bala Shanmugam4486ea92012-03-07 17:27:12 +053034 /* Use default background scan period */
35 wdev->wext.connect.bg_scan_period = -1;
36
Johannes Bergfffd0932009-07-08 14:22:54 +020037 if (wdev->wext.keys) {
38 wdev->wext.keys->def = wdev->wext.default_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
Johannes Bergf1c1f172016-09-13 17:08:23 +020046 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
Johannes Bergfffd0932009-07-08 14:22:54 +020047 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
48 if (!ck)
49 return -ENOMEM;
David Spinadelb8676222016-09-22 23:16:50 +030050 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
Johannes Bergfffd0932009-07-08 14:22:54 +020051 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 Berg83739b02013-05-15 17:44:01 +020057 err = cfg80211_connect(rdev, wdev->netdev,
58 &wdev->wext.connect, ck, prev_bssid);
Johannes Bergfffd0932009-07-08 14:22:54 +020059 if (err)
Johannes Bergb47f6102014-09-10 13:39:54 +030060 kzfree(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;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080070 struct cfg80211_registered_device *rdev = wiphy_to_rdev(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
Zhao, Gang96998e32014-04-09 09:28:06 +080078 freq = cfg80211_wext_freq(wextfreq);
Johannes Berg59bbb6f2009-08-07 17:22:35 +020079 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 Berg667503d2009-07-07 03:56:11 +020090 wdev_lock(wdev);
91
Johannes Bergceca7b72013-05-16 00:55:45 +020092 if (wdev->conn) {
Johannes Bergf2129352009-07-01 21:26:56 +020093 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +020094
95 if (wdev->wext.connect.channel == chan) {
96 err = 0;
97 goto out;
98 }
99
Johannes Bergf2129352009-07-01 21:26:56 +0200100 /* if SSID set, we'll try right again, avoid event */
101 if (wdev->wext.connect.ssid_len)
102 event = false;
Johannes Berg83739b02013-05-15 17:44:01 +0200103 err = cfg80211_disconnect(rdev, dev,
104 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200105 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200106 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200107 }
108
109 wdev->wext.connect.channel = chan;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200110 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200111 out:
112 wdev_unlock(wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200113 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200114}
Johannes Bergf2129352009-07-01 21:26:56 +0200115
116int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
117 struct iw_request_info *info,
118 struct iw_freq *freq, char *extra)
119{
120 struct wireless_dev *wdev = dev->ieee80211_ptr;
121 struct ieee80211_channel *chan = NULL;
122
123 /* call only for station! */
124 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
125 return -EINVAL;
126
Johannes Berg667503d2009-07-07 03:56:11 +0200127 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200128 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200129 chan = wdev->current_bss->pub.channel;
Johannes Bergf2129352009-07-01 21:26:56 +0200130 else if (wdev->wext.connect.channel)
131 chan = wdev->wext.connect.channel;
Johannes Berg667503d2009-07-07 03:56:11 +0200132 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200133
134 if (chan) {
135 freq->m = chan->center_freq;
136 freq->e = 6;
137 return 0;
138 }
139
140 /* no channel if not joining */
141 return -EINVAL;
142}
Johannes Bergf2129352009-07-01 21:26:56 +0200143
144int cfg80211_mgd_wext_siwessid(struct net_device *dev,
145 struct iw_request_info *info,
146 struct iw_point *data, char *ssid)
147{
148 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800149 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200150 size_t len = data->length;
151 int err;
152
153 /* call only for station! */
154 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
155 return -EINVAL;
156
157 if (!data->flags)
158 len = 0;
159
160 /* iwconfig uses nul termination in SSID.. */
161 if (len > 0 && ssid[len - 1] == '\0')
162 len--;
163
Johannes Berg667503d2009-07-07 03:56:11 +0200164 wdev_lock(wdev);
165
166 err = 0;
167
Johannes Bergceca7b72013-05-16 00:55:45 +0200168 if (wdev->conn) {
Johannes Bergf2129352009-07-01 21:26:56 +0200169 bool event = true;
Johannes Berg25e83c42009-07-16 11:39:04 +0200170
171 if (wdev->wext.connect.ssid && len &&
172 len == wdev->wext.connect.ssid_len &&
173 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
174 goto out;
175
Johannes Bergf2129352009-07-01 21:26:56 +0200176 /* if SSID set now, we'll try to connect, avoid event */
177 if (len)
178 event = false;
Johannes Berg83739b02013-05-15 17:44:01 +0200179 err = cfg80211_disconnect(rdev, dev,
180 WLAN_REASON_DEAUTH_LEAVING, event);
Johannes Bergf2129352009-07-01 21:26:56 +0200181 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200182 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200183 }
184
Johannes Bergf401a6f2009-08-07 14:51:05 +0200185 wdev->wext.prev_bssid_valid = false;
Johannes Bergf2129352009-07-01 21:26:56 +0200186 wdev->wext.connect.ssid = wdev->wext.ssid;
187 memcpy(wdev->wext.ssid, ssid, len);
188 wdev->wext.connect.ssid_len = len;
189
190 wdev->wext.connect.crypto.control_port = false;
Johannes Bergc0692b82010-08-27 14:26:53 +0300191 wdev->wext.connect.crypto.control_port_ethertype =
192 cpu_to_be16(ETH_P_PAE);
Johannes Bergf2129352009-07-01 21:26:56 +0200193
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200194 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200195 out:
196 wdev_unlock(wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200197 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200198}
Johannes Bergf2129352009-07-01 21:26:56 +0200199
200int cfg80211_mgd_wext_giwessid(struct net_device *dev,
201 struct iw_request_info *info,
202 struct iw_point *data, char *ssid)
203{
204 struct wireless_dev *wdev = dev->ieee80211_ptr;
Will Deacon63eb9c22019-10-04 10:51:32 +0100205 int ret = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200206
207 /* call only for station! */
208 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
209 return -EINVAL;
210
211 data->flags = 0;
212
Johannes Berg667503d2009-07-07 03:56:11 +0200213 wdev_lock(wdev);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800214 if (wdev->current_bss) {
Johannes Berg9caf0362012-11-29 01:25:20 +0100215 const u8 *ie;
216
217 rcu_read_lock();
218 ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
219 WLAN_EID_SSID);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800220 if (ie) {
221 data->flags = 1;
222 data->length = ie[1];
Will Deacon63eb9c22019-10-04 10:51:32 +0100223 if (data->length > IW_ESSID_MAX_SIZE)
224 ret = -EINVAL;
225 else
226 memcpy(ssid, ie + 2, data->length);
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800227 }
Johannes Berg9caf0362012-11-29 01:25:20 +0100228 rcu_read_unlock();
Zhu Yia42dd7ef2009-08-05 15:07:13 +0800229 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
Johannes Bergf2129352009-07-01 21:26:56 +0200230 data->flags = 1;
231 data->length = wdev->wext.connect.ssid_len;
232 memcpy(ssid, wdev->wext.connect.ssid, data->length);
Johannes Berg33de4f92009-09-17 08:35:54 -0700233 }
Johannes Berg667503d2009-07-07 03:56:11 +0200234 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200235
Will Deacon63eb9c22019-10-04 10:51:32 +0100236 return ret;
Johannes Bergf2129352009-07-01 21:26:56 +0200237}
Johannes Bergf2129352009-07-01 21:26:56 +0200238
239int cfg80211_mgd_wext_siwap(struct net_device *dev,
240 struct iw_request_info *info,
241 struct sockaddr *ap_addr, char *extra)
242{
243 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800244 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200245 u8 *bssid = ap_addr->sa_data;
246 int err;
247
248 /* call only for station! */
249 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
250 return -EINVAL;
251
252 if (ap_addr->sa_family != ARPHRD_ETHER)
253 return -EINVAL;
254
255 /* automatic mode */
256 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
257 bssid = NULL;
258
Johannes Berg667503d2009-07-07 03:56:11 +0200259 wdev_lock(wdev);
260
Johannes Bergceca7b72013-05-16 00:55:45 +0200261 if (wdev->conn) {
Johannes Berg25e83c42009-07-16 11:39:04 +0200262 err = 0;
263 /* both automatic */
264 if (!bssid && !wdev->wext.connect.bssid)
265 goto out;
266
267 /* fixed already - and no change */
268 if (wdev->wext.connect.bssid && bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000269 ether_addr_equal(bssid, wdev->wext.connect.bssid))
Johannes Berg25e83c42009-07-16 11:39:04 +0200270 goto out;
271
Johannes Berg83739b02013-05-15 17:44:01 +0200272 err = cfg80211_disconnect(rdev, dev,
273 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200274 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200275 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200276 }
277
278 if (bssid) {
279 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
280 wdev->wext.connect.bssid = wdev->wext.bssid;
281 } else
282 wdev->wext.connect.bssid = NULL;
283
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200284 err = cfg80211_mgd_wext_connect(rdev, wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200285 out:
286 wdev_unlock(wdev);
Johannes Berg667503d2009-07-07 03:56:11 +0200287 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200288}
Johannes Bergf2129352009-07-01 21:26:56 +0200289
290int cfg80211_mgd_wext_giwap(struct net_device *dev,
291 struct iw_request_info *info,
292 struct sockaddr *ap_addr, char *extra)
293{
294 struct wireless_dev *wdev = dev->ieee80211_ptr;
295
296 /* call only for station! */
297 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
298 return -EINVAL;
299
300 ap_addr->sa_family = ARPHRD_ETHER;
301
Johannes Berg667503d2009-07-07 03:56:11 +0200302 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200303 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200304 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Bergf2129352009-07-01 21:26:56 +0200305 else
Joe Perchesd2beae12015-03-02 19:54:58 -0800306 eth_zero_addr(ap_addr->sa_data);
Johannes Berg667503d2009-07-07 03:56:11 +0200307 wdev_unlock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200308
309 return 0;
310}
Johannes Bergf2129352009-07-01 21:26:56 +0200311
312int cfg80211_wext_siwgenie(struct net_device *dev,
313 struct iw_request_info *info,
314 struct iw_point *data, char *extra)
315{
316 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800317 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200318 u8 *ie = extra;
319 int ie_len = data->length, err;
320
321 if (wdev->iftype != NL80211_IFTYPE_STATION)
322 return -EOPNOTSUPP;
323
324 if (!ie_len)
325 ie = NULL;
326
Johannes Berg667503d2009-07-07 03:56:11 +0200327 wdev_lock(wdev);
328
Johannes Bergf2129352009-07-01 21:26:56 +0200329 /* no change */
Johannes Berg667503d2009-07-07 03:56:11 +0200330 err = 0;
Johannes Bergf2129352009-07-01 21:26:56 +0200331 if (wdev->wext.ie_len == ie_len &&
332 memcmp(wdev->wext.ie, ie, ie_len) == 0)
Johannes Berg667503d2009-07-07 03:56:11 +0200333 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200334
335 if (ie_len) {
336 ie = kmemdup(extra, ie_len, GFP_KERNEL);
Johannes Berg667503d2009-07-07 03:56:11 +0200337 if (!ie) {
338 err = -ENOMEM;
339 goto out;
340 }
Johannes Bergf2129352009-07-01 21:26:56 +0200341 } else
342 ie = NULL;
343
344 kfree(wdev->wext.ie);
345 wdev->wext.ie = ie;
346 wdev->wext.ie_len = ie_len;
347
Johannes Bergceca7b72013-05-16 00:55:45 +0200348 if (wdev->conn) {
Johannes Berg83739b02013-05-15 17:44:01 +0200349 err = cfg80211_disconnect(rdev, dev,
350 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Bergf2129352009-07-01 21:26:56 +0200351 if (err)
Johannes Berg667503d2009-07-07 03:56:11 +0200352 goto out;
Johannes Bergf2129352009-07-01 21:26:56 +0200353 }
354
355 /* userspace better not think we'll reconnect */
Johannes Berg667503d2009-07-07 03:56:11 +0200356 err = 0;
357 out:
358 wdev_unlock(wdev);
359 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200360}
Johannes Bergf2129352009-07-01 21:26:56 +0200361
362int cfg80211_wext_siwmlme(struct net_device *dev,
363 struct iw_request_info *info,
364 struct iw_point *data, char *extra)
365{
366 struct wireless_dev *wdev = dev->ieee80211_ptr;
367 struct iw_mlme *mlme = (struct iw_mlme *)extra;
368 struct cfg80211_registered_device *rdev;
Johannes Berg667503d2009-07-07 03:56:11 +0200369 int err;
Johannes Bergf2129352009-07-01 21:26:56 +0200370
371 if (!wdev)
372 return -EOPNOTSUPP;
373
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800374 rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergf2129352009-07-01 21:26:56 +0200375
376 if (wdev->iftype != NL80211_IFTYPE_STATION)
377 return -EINVAL;
378
379 if (mlme->addr.sa_family != ARPHRD_ETHER)
380 return -EINVAL;
381
Johannes Berg667503d2009-07-07 03:56:11 +0200382 wdev_lock(wdev);
Johannes Bergf2129352009-07-01 21:26:56 +0200383 switch (mlme->cmd) {
384 case IW_MLME_DEAUTH:
385 case IW_MLME_DISASSOC:
Johannes Berg83739b02013-05-15 17:44:01 +0200386 err = cfg80211_disconnect(rdev, dev, mlme->reason_code, true);
Johannes Berg667503d2009-07-07 03:56:11 +0200387 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200388 default:
Johannes Berg667503d2009-07-07 03:56:11 +0200389 err = -EOPNOTSUPP;
390 break;
Johannes Bergf2129352009-07-01 21:26:56 +0200391 }
Johannes Berg667503d2009-07-07 03:56:11 +0200392 wdev_unlock(wdev);
393
394 return err;
Johannes Bergf2129352009-07-01 21:26:56 +0200395}