Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / net / wireless / wext-sme.c
blobc67d7a82ab132a50be6305f69df6576fd158a15e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * cfg80211 wext compat for managed mode.
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright (C) 2009 Intel Corporation. All rights reserved.
7 */
9 #include <linux/export.h>
10 #include <linux/etherdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/slab.h>
13 #include <net/cfg80211.h>
14 #include <net/cfg80211-wext.h>
15 #include "wext-compat.h"
16 #include "nl80211.h"
18 int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
19 struct wireless_dev *wdev)
21 struct cfg80211_cached_keys *ck = NULL;
22 const u8 *prev_bssid = NULL;
23 int err, i;
25 ASSERT_RTNL();
26 ASSERT_WDEV_LOCK(wdev);
28 if (!netif_running(wdev->netdev))
29 return 0;
31 wdev->wext.connect.ie = wdev->wext.ie;
32 wdev->wext.connect.ie_len = wdev->wext.ie_len;
34 /* Use default background scan period */
35 wdev->wext.connect.bg_scan_period = -1;
37 if (wdev->wext.keys) {
38 wdev->wext.keys->def = wdev->wext.default_key;
39 if (wdev->wext.default_key != -1)
40 wdev->wext.connect.privacy = true;
43 if (!wdev->wext.connect.ssid_len)
44 return 0;
46 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
47 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
48 if (!ck)
49 return -ENOMEM;
50 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
51 ck->params[i].key = ck->data[i];
54 if (wdev->wext.prev_bssid_valid)
55 prev_bssid = wdev->wext.prev_bssid;
57 err = cfg80211_connect(rdev, wdev->netdev,
58 &wdev->wext.connect, ck, prev_bssid);
59 if (err)
60 kzfree(ck);
62 return err;
65 int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
66 struct iw_request_info *info,
67 struct iw_freq *wextfreq, char *extra)
69 struct wireless_dev *wdev = dev->ieee80211_ptr;
70 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
71 struct ieee80211_channel *chan = NULL;
72 int err, freq;
74 /* call only for station! */
75 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
76 return -EINVAL;
78 freq = cfg80211_wext_freq(wextfreq);
79 if (freq < 0)
80 return freq;
82 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;
90 wdev_lock(wdev);
92 if (wdev->conn) {
93 bool event = true;
95 if (wdev->wext.connect.channel == chan) {
96 err = 0;
97 goto out;
100 /* if SSID set, we'll try right again, avoid event */
101 if (wdev->wext.connect.ssid_len)
102 event = false;
103 err = cfg80211_disconnect(rdev, dev,
104 WLAN_REASON_DEAUTH_LEAVING, event);
105 if (err)
106 goto out;
109 wdev->wext.connect.channel = chan;
110 err = cfg80211_mgd_wext_connect(rdev, wdev);
111 out:
112 wdev_unlock(wdev);
113 return err;
116 int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
117 struct iw_request_info *info,
118 struct iw_freq *freq, char *extra)
120 struct wireless_dev *wdev = dev->ieee80211_ptr;
121 struct ieee80211_channel *chan = NULL;
123 /* call only for station! */
124 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
125 return -EINVAL;
127 wdev_lock(wdev);
128 if (wdev->current_bss)
129 chan = wdev->current_bss->pub.channel;
130 else if (wdev->wext.connect.channel)
131 chan = wdev->wext.connect.channel;
132 wdev_unlock(wdev);
134 if (chan) {
135 freq->m = chan->center_freq;
136 freq->e = 6;
137 return 0;
140 /* no channel if not joining */
141 return -EINVAL;
144 int cfg80211_mgd_wext_siwessid(struct net_device *dev,
145 struct iw_request_info *info,
146 struct iw_point *data, char *ssid)
148 struct wireless_dev *wdev = dev->ieee80211_ptr;
149 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
150 size_t len = data->length;
151 int err;
153 /* call only for station! */
154 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
155 return -EINVAL;
157 if (!data->flags)
158 len = 0;
160 /* iwconfig uses nul termination in SSID.. */
161 if (len > 0 && ssid[len - 1] == '\0')
162 len--;
164 wdev_lock(wdev);
166 err = 0;
168 if (wdev->conn) {
169 bool event = true;
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;
176 /* if SSID set now, we'll try to connect, avoid event */
177 if (len)
178 event = false;
179 err = cfg80211_disconnect(rdev, dev,
180 WLAN_REASON_DEAUTH_LEAVING, event);
181 if (err)
182 goto out;
185 wdev->wext.prev_bssid_valid = false;
186 wdev->wext.connect.ssid = wdev->wext.ssid;
187 memcpy(wdev->wext.ssid, ssid, len);
188 wdev->wext.connect.ssid_len = len;
190 wdev->wext.connect.crypto.control_port = false;
191 wdev->wext.connect.crypto.control_port_ethertype =
192 cpu_to_be16(ETH_P_PAE);
194 err = cfg80211_mgd_wext_connect(rdev, wdev);
195 out:
196 wdev_unlock(wdev);
197 return err;
200 int cfg80211_mgd_wext_giwessid(struct net_device *dev,
201 struct iw_request_info *info,
202 struct iw_point *data, char *ssid)
204 struct wireless_dev *wdev = dev->ieee80211_ptr;
206 /* call only for station! */
207 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
208 return -EINVAL;
210 data->flags = 0;
212 wdev_lock(wdev);
213 if (wdev->current_bss) {
214 const u8 *ie;
216 rcu_read_lock();
217 ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
218 WLAN_EID_SSID);
219 if (ie) {
220 data->flags = 1;
221 data->length = ie[1];
222 memcpy(ssid, ie + 2, data->length);
224 rcu_read_unlock();
225 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
226 data->flags = 1;
227 data->length = wdev->wext.connect.ssid_len;
228 memcpy(ssid, wdev->wext.connect.ssid, data->length);
230 wdev_unlock(wdev);
232 return 0;
235 int cfg80211_mgd_wext_siwap(struct net_device *dev,
236 struct iw_request_info *info,
237 struct sockaddr *ap_addr, char *extra)
239 struct wireless_dev *wdev = dev->ieee80211_ptr;
240 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
241 u8 *bssid = ap_addr->sa_data;
242 int err;
244 /* call only for station! */
245 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
246 return -EINVAL;
248 if (ap_addr->sa_family != ARPHRD_ETHER)
249 return -EINVAL;
251 /* automatic mode */
252 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
253 bssid = NULL;
255 wdev_lock(wdev);
257 if (wdev->conn) {
258 err = 0;
259 /* both automatic */
260 if (!bssid && !wdev->wext.connect.bssid)
261 goto out;
263 /* fixed already - and no change */
264 if (wdev->wext.connect.bssid && bssid &&
265 ether_addr_equal(bssid, wdev->wext.connect.bssid))
266 goto out;
268 err = cfg80211_disconnect(rdev, dev,
269 WLAN_REASON_DEAUTH_LEAVING, false);
270 if (err)
271 goto out;
274 if (bssid) {
275 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
276 wdev->wext.connect.bssid = wdev->wext.bssid;
277 } else
278 wdev->wext.connect.bssid = NULL;
280 err = cfg80211_mgd_wext_connect(rdev, wdev);
281 out:
282 wdev_unlock(wdev);
283 return err;
286 int cfg80211_mgd_wext_giwap(struct net_device *dev,
287 struct iw_request_info *info,
288 struct sockaddr *ap_addr, char *extra)
290 struct wireless_dev *wdev = dev->ieee80211_ptr;
292 /* call only for station! */
293 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
294 return -EINVAL;
296 ap_addr->sa_family = ARPHRD_ETHER;
298 wdev_lock(wdev);
299 if (wdev->current_bss)
300 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
301 else
302 eth_zero_addr(ap_addr->sa_data);
303 wdev_unlock(wdev);
305 return 0;
308 int cfg80211_wext_siwgenie(struct net_device *dev,
309 struct iw_request_info *info,
310 struct iw_point *data, char *extra)
312 struct wireless_dev *wdev = dev->ieee80211_ptr;
313 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
314 u8 *ie = extra;
315 int ie_len = data->length, err;
317 if (wdev->iftype != NL80211_IFTYPE_STATION)
318 return -EOPNOTSUPP;
320 if (!ie_len)
321 ie = NULL;
323 wdev_lock(wdev);
325 /* no change */
326 err = 0;
327 if (wdev->wext.ie_len == ie_len &&
328 memcmp(wdev->wext.ie, ie, ie_len) == 0)
329 goto out;
331 if (ie_len) {
332 ie = kmemdup(extra, ie_len, GFP_KERNEL);
333 if (!ie) {
334 err = -ENOMEM;
335 goto out;
337 } else
338 ie = NULL;
340 kfree(wdev->wext.ie);
341 wdev->wext.ie = ie;
342 wdev->wext.ie_len = ie_len;
344 if (wdev->conn) {
345 err = cfg80211_disconnect(rdev, dev,
346 WLAN_REASON_DEAUTH_LEAVING, false);
347 if (err)
348 goto out;
351 /* userspace better not think we'll reconnect */
352 err = 0;
353 out:
354 wdev_unlock(wdev);
355 return err;
358 int cfg80211_wext_siwmlme(struct net_device *dev,
359 struct iw_request_info *info,
360 struct iw_point *data, char *extra)
362 struct wireless_dev *wdev = dev->ieee80211_ptr;
363 struct iw_mlme *mlme = (struct iw_mlme *)extra;
364 struct cfg80211_registered_device *rdev;
365 int err;
367 if (!wdev)
368 return -EOPNOTSUPP;
370 rdev = wiphy_to_rdev(wdev->wiphy);
372 if (wdev->iftype != NL80211_IFTYPE_STATION)
373 return -EINVAL;
375 if (mlme->addr.sa_family != ARPHRD_ETHER)
376 return -EINVAL;
378 wdev_lock(wdev);
379 switch (mlme->cmd) {
380 case IW_MLME_DEAUTH:
381 case IW_MLME_DISASSOC:
382 err = cfg80211_disconnect(rdev, dev, mlme->reason_code, true);
383 break;
384 default:
385 err = -EOPNOTSUPP;
386 break;
388 wdev_unlock(wdev);
390 return err;