blob: 3a4eb451cce583241959557ec60bd785a7132572 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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
8#include <linux/export.h>
9#include <linux/etherdevice.h>
10#include <linux/if_arp.h>
11#include <linux/slab.h>
12#include <net/cfg80211.h>
13#include <net/cfg80211-wext.h>
14#include "wext-compat.h"
15#include "nl80211.h"
16
17int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
18 struct wireless_dev *wdev)
19{
20 struct cfg80211_cached_keys *ck = NULL;
21 const u8 *prev_bssid = NULL;
22 int err, i;
23
24 ASSERT_RDEV_LOCK(rdev);
25 ASSERT_WDEV_LOCK(wdev);
26
27 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;
32
33 /* Use default background scan period */
34 wdev->wext.connect.bg_scan_period = -1;
35
36 if (wdev->wext.keys) {
37 wdev->wext.keys->def = wdev->wext.default_key;
38 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
39 if (wdev->wext.default_key != -1)
40 wdev->wext.connect.privacy = true;
41 }
42
43 if (!wdev->wext.connect.ssid_len)
44 return 0;
45
46 if (wdev->wext.keys) {
47 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
48 if (!ck)
49 return -ENOMEM;
50 for (i = 0; i < 6; i++)
51 ck->params[i].key = ck->data[i];
52 }
53
54 if (wdev->wext.prev_bssid_valid)
55 prev_bssid = wdev->wext.prev_bssid;
56
57 err = __cfg80211_connect(rdev, wdev->netdev,
58 &wdev->wext.connect, ck, prev_bssid);
59 if (err)
60 kfree(ck);
61
62 return err;
63}
64
65int cfg80211_mgd_wext_siwfreq(struct net_device *dev,
66 struct iw_request_info *info,
67 struct iw_freq *wextfreq, char *extra)
68{
69 struct wireless_dev *wdev = dev->ieee80211_ptr;
70 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
71 struct ieee80211_channel *chan = NULL;
72 int err, freq;
73
74 /* call only for station! */
75 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
76 return -EINVAL;
77
78 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
79 if (freq < 0)
80 return freq;
81
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;
88 }
89
90 cfg80211_lock_rdev(rdev);
91 mutex_lock(&rdev->devlist_mtx);
92 wdev_lock(wdev);
93
94 if (wdev->sme_state != CFG80211_SME_IDLE) {
95 bool event = true;
96
97 if (wdev->wext.connect.channel == chan) {
98 err = 0;
99 goto out;
100 }
101
102 /* if SSID set, we'll try right again, avoid event */
103 if (wdev->wext.connect.ssid_len)
104 event = false;
105 err = __cfg80211_disconnect(rdev, dev,
106 WLAN_REASON_DEAUTH_LEAVING, event);
107 if (err)
108 goto out;
109 }
110
111
112 wdev->wext.connect.channel = chan;
113
114 /* SSID is not set, we just want to switch channel */
115 if (chan && !wdev->wext.connect.ssid_len) {
116 err = cfg80211_set_freq(rdev, wdev, freq, NL80211_CHAN_NO_HT);
117 goto out;
118 }
119
120 err = cfg80211_mgd_wext_connect(rdev, wdev);
121 out:
122 wdev_unlock(wdev);
123 mutex_unlock(&rdev->devlist_mtx);
124 cfg80211_unlock_rdev(rdev);
125 return err;
126}
127
128int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
129 struct iw_request_info *info,
130 struct iw_freq *freq, char *extra)
131{
132 struct wireless_dev *wdev = dev->ieee80211_ptr;
133 struct ieee80211_channel *chan = NULL;
134
135 /* call only for station! */
136 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
137 return -EINVAL;
138
139 wdev_lock(wdev);
140 if (wdev->current_bss)
141 chan = wdev->current_bss->pub.channel;
142 else if (wdev->wext.connect.channel)
143 chan = wdev->wext.connect.channel;
144 wdev_unlock(wdev);
145
146 if (chan) {
147 freq->m = chan->center_freq;
148 freq->e = 6;
149 return 0;
150 }
151
152 /* no channel if not joining */
153 return -EINVAL;
154}
155
156int cfg80211_mgd_wext_siwessid(struct net_device *dev,
157 struct iw_request_info *info,
158 struct iw_point *data, char *ssid)
159{
160 struct wireless_dev *wdev = dev->ieee80211_ptr;
161 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
162 size_t len = data->length;
163 int err;
164
165 /* call only for station! */
166 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
167 return -EINVAL;
168
169 if (!data->flags)
170 len = 0;
171
172 /* iwconfig uses nul termination in SSID.. */
173 if (len > 0 && ssid[len - 1] == '\0')
174 len--;
175
176 cfg80211_lock_rdev(rdev);
177 mutex_lock(&rdev->devlist_mtx);
178 wdev_lock(wdev);
179
180 err = 0;
181
182 if (wdev->sme_state != CFG80211_SME_IDLE) {
183 bool event = true;
184
185 if (wdev->wext.connect.ssid && len &&
186 len == wdev->wext.connect.ssid_len &&
187 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
188 goto out;
189
190 /* if SSID set now, we'll try to connect, avoid event */
191 if (len)
192 event = false;
193 err = __cfg80211_disconnect(rdev, dev,
194 WLAN_REASON_DEAUTH_LEAVING, event);
195 if (err)
196 goto out;
197 }
198
199 wdev->wext.prev_bssid_valid = false;
200 wdev->wext.connect.ssid = wdev->wext.ssid;
201 memcpy(wdev->wext.ssid, ssid, len);
202 wdev->wext.connect.ssid_len = len;
203
204 wdev->wext.connect.crypto.control_port = false;
205 wdev->wext.connect.crypto.control_port_ethertype =
206 cpu_to_be16(ETH_P_PAE);
207
208 err = cfg80211_mgd_wext_connect(rdev, wdev);
209 out:
210 wdev_unlock(wdev);
211 mutex_unlock(&rdev->devlist_mtx);
212 cfg80211_unlock_rdev(rdev);
213 return err;
214}
215
216int cfg80211_mgd_wext_giwessid(struct net_device *dev,
217 struct iw_request_info *info,
218 struct iw_point *data, char *ssid)
219{
220 struct wireless_dev *wdev = dev->ieee80211_ptr;
221 int ret = 0;
222 /* call only for station! */
223 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
224 return -EINVAL;
225
226 data->flags = 0;
227
228 wdev_lock(wdev);
229 if (wdev->current_bss) {
230 const u8 *ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
231 WLAN_EID_SSID);
232 if (ie) {
233 data->flags = 1;
234 data->length = ie[1];
235 memcpy(ssid, ie + 2, data->length);
236 if (data->length > IW_ESSID_MAX_SIZE)
237 ret = -EINVAL;
238 else
239 memcpy(ssid, ie + 2, data->length);
240 }
241 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
242 data->flags = 1;
243 data->length = wdev->wext.connect.ssid_len;
244 memcpy(ssid, wdev->wext.connect.ssid, data->length);
245 }
246 wdev_unlock(wdev);
247
248 return ret;
249}
250
251int cfg80211_mgd_wext_siwap(struct net_device *dev,
252 struct iw_request_info *info,
253 struct sockaddr *ap_addr, char *extra)
254{
255 struct wireless_dev *wdev = dev->ieee80211_ptr;
256 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
257 u8 *bssid = ap_addr->sa_data;
258 int err;
259
260 /* call only for station! */
261 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
262 return -EINVAL;
263
264 if (ap_addr->sa_family != ARPHRD_ETHER)
265 return -EINVAL;
266
267 /* automatic mode */
268 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
269 bssid = NULL;
270
271 cfg80211_lock_rdev(rdev);
272 mutex_lock(&rdev->devlist_mtx);
273 wdev_lock(wdev);
274
275 if (wdev->sme_state != CFG80211_SME_IDLE) {
276 err = 0;
277 /* both automatic */
278 if (!bssid && !wdev->wext.connect.bssid)
279 goto out;
280
281 /* fixed already - and no change */
282 if (wdev->wext.connect.bssid && bssid &&
283 compare_ether_addr(bssid, wdev->wext.connect.bssid) == 0)
284 goto out;
285
286 err = __cfg80211_disconnect(rdev, dev,
287 WLAN_REASON_DEAUTH_LEAVING, false);
288 if (err)
289 goto out;
290 }
291
292 if (bssid) {
293 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
294 wdev->wext.connect.bssid = wdev->wext.bssid;
295 } else
296 wdev->wext.connect.bssid = NULL;
297
298 err = cfg80211_mgd_wext_connect(rdev, wdev);
299 out:
300 wdev_unlock(wdev);
301 mutex_unlock(&rdev->devlist_mtx);
302 cfg80211_unlock_rdev(rdev);
303 return err;
304}
305
306int cfg80211_mgd_wext_giwap(struct net_device *dev,
307 struct iw_request_info *info,
308 struct sockaddr *ap_addr, char *extra)
309{
310 struct wireless_dev *wdev = dev->ieee80211_ptr;
311
312 /* call only for station! */
313 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
314 return -EINVAL;
315
316 ap_addr->sa_family = ARPHRD_ETHER;
317
318 wdev_lock(wdev);
319 if (wdev->current_bss)
320 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
321 else
322 memset(ap_addr->sa_data, 0, ETH_ALEN);
323 wdev_unlock(wdev);
324
325 return 0;
326}
327
328int cfg80211_wext_siwgenie(struct net_device *dev,
329 struct iw_request_info *info,
330 struct iw_point *data, char *extra)
331{
332 struct wireless_dev *wdev = dev->ieee80211_ptr;
333 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
334 u8 *ie = extra;
335 int ie_len = data->length, err;
336
337 if (wdev->iftype != NL80211_IFTYPE_STATION)
338 return -EOPNOTSUPP;
339
340 if (!ie_len)
341 ie = NULL;
342
343 wdev_lock(wdev);
344
345 /* no change */
346 err = 0;
347 if (wdev->wext.ie_len == ie_len &&
348 memcmp(wdev->wext.ie, ie, ie_len) == 0)
349 goto out;
350
351 if (ie_len) {
352 ie = kmemdup(extra, ie_len, GFP_KERNEL);
353 if (!ie) {
354 err = -ENOMEM;
355 goto out;
356 }
357 } else
358 ie = NULL;
359
360 kfree(wdev->wext.ie);
361 wdev->wext.ie = ie;
362 wdev->wext.ie_len = ie_len;
363
364 if (wdev->sme_state != CFG80211_SME_IDLE) {
365 err = __cfg80211_disconnect(rdev, dev,
366 WLAN_REASON_DEAUTH_LEAVING, false);
367 if (err)
368 goto out;
369 }
370
371 /* userspace better not think we'll reconnect */
372 err = 0;
373 out:
374 wdev_unlock(wdev);
375 return err;
376}
377
378int cfg80211_wext_siwmlme(struct net_device *dev,
379 struct iw_request_info *info,
380 struct iw_point *data, char *extra)
381{
382 struct wireless_dev *wdev = dev->ieee80211_ptr;
383 struct iw_mlme *mlme = (struct iw_mlme *)extra;
384 struct cfg80211_registered_device *rdev;
385 int err;
386
387 if (!wdev)
388 return -EOPNOTSUPP;
389
390 rdev = wiphy_to_dev(wdev->wiphy);
391
392 if (wdev->iftype != NL80211_IFTYPE_STATION)
393 return -EINVAL;
394
395 if (mlme->addr.sa_family != ARPHRD_ETHER)
396 return -EINVAL;
397
398 wdev_lock(wdev);
399 switch (mlme->cmd) {
400 case IW_MLME_DEAUTH:
401 case IW_MLME_DISASSOC:
402 err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
403 true);
404 break;
405 default:
406 err = -EOPNOTSUPP;
407 break;
408 }
409 wdev_unlock(wdev);
410
411 return err;
412}