1 // SPDX-License-Identifier: GPL-2.0
3 * cfg80211 wext compat for managed mode.
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright (C) 2009, 2020-2021 Intel Corporation.
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"
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;
26 ASSERT_WDEV_LOCK(wdev);
28 if (!netif_running(wdev->netdev))
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)
46 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
47 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
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);
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;
74 /* call only for station! */
75 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
78 freq = cfg80211_wext_freq(wextfreq);
83 chan = ieee80211_get_channel(wdev->wiphy, freq);
86 if (chan->flags & IEEE80211_CHAN_DISABLED)
95 if (wdev->wext.connect.channel == chan) {
100 /* if SSID set, we'll try right again, avoid event */
101 if (wdev->wext.connect.ssid_len)
103 err = cfg80211_disconnect(rdev, dev,
104 WLAN_REASON_DEAUTH_LEAVING, event);
109 wdev->wext.connect.channel = chan;
110 err = cfg80211_mgd_wext_connect(rdev, wdev);
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))
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;
135 freq->m = chan->center_freq;
140 /* no channel if not joining */
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;
153 /* call only for station! */
154 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
160 /* iwconfig uses nul termination in SSID.. */
161 if (len > 0 && ssid[len - 1] == '\0')
171 if (wdev->wext.connect.ssid && len &&
172 len == wdev->wext.connect.ssid_len &&
173 memcmp(wdev->wext.connect.ssid, ssid, len) == 0)
176 /* if SSID set now, we'll try to connect, avoid event */
179 err = cfg80211_disconnect(rdev, dev,
180 WLAN_REASON_DEAUTH_LEAVING, event);
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);
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;
207 /* call only for station! */
208 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
214 if (wdev->current_bss) {
218 ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
222 data->length = ie[1];
223 if (data->length > IW_ESSID_MAX_SIZE)
226 memcpy(ssid, ie + 2, data->length);
229 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
231 data->length = wdev->wext.connect.ssid_len;
232 memcpy(ssid, wdev->wext.connect.ssid, data->length);
239 int cfg80211_mgd_wext_siwap(struct net_device *dev,
240 struct iw_request_info *info,
241 struct sockaddr *ap_addr, char *extra)
243 struct wireless_dev *wdev = dev->ieee80211_ptr;
244 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
245 u8 *bssid = ap_addr->sa_data;
248 /* call only for station! */
249 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
252 if (ap_addr->sa_family != ARPHRD_ETHER)
256 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
264 if (!bssid && !wdev->wext.connect.bssid)
267 /* fixed already - and no change */
268 if (wdev->wext.connect.bssid && bssid &&
269 ether_addr_equal(bssid, wdev->wext.connect.bssid))
272 err = cfg80211_disconnect(rdev, dev,
273 WLAN_REASON_DEAUTH_LEAVING, false);
279 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
280 wdev->wext.connect.bssid = wdev->wext.bssid;
282 wdev->wext.connect.bssid = NULL;
284 err = cfg80211_mgd_wext_connect(rdev, wdev);
290 int cfg80211_mgd_wext_giwap(struct net_device *dev,
291 struct iw_request_info *info,
292 struct sockaddr *ap_addr, char *extra)
294 struct wireless_dev *wdev = dev->ieee80211_ptr;
296 /* call only for station! */
297 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
300 ap_addr->sa_family = ARPHRD_ETHER;
303 if (wdev->current_bss)
304 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
306 eth_zero_addr(ap_addr->sa_data);
312 int cfg80211_wext_siwgenie(struct net_device *dev,
313 struct iw_request_info *info,
314 struct iw_point *data, char *extra)
316 struct wireless_dev *wdev = dev->ieee80211_ptr;
317 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
319 int ie_len = data->length, err;
321 if (wdev->iftype != NL80211_IFTYPE_STATION)
331 if (wdev->wext.ie_len == ie_len &&
332 memcmp(wdev->wext.ie, ie, ie_len) == 0)
336 ie = kmemdup(extra, ie_len, GFP_KERNEL);
344 kfree(wdev->wext.ie);
346 wdev->wext.ie_len = ie_len;
349 err = cfg80211_disconnect(rdev, dev,
350 WLAN_REASON_DEAUTH_LEAVING, false);
355 /* userspace better not think we'll reconnect */
362 int cfg80211_wext_siwmlme(struct net_device *dev,
363 struct iw_request_info *info,
364 struct iw_point *data, char *extra)
366 struct wireless_dev *wdev = dev->ieee80211_ptr;
367 struct iw_mlme *mlme = (struct iw_mlme *)extra;
368 struct cfg80211_registered_device *rdev;
374 rdev = wiphy_to_rdev(wdev->wiphy);
376 if (wdev->iftype != NL80211_IFTYPE_STATION)
379 if (mlme->addr.sa_family != ARPHRD_ETHER)
382 wiphy_lock(&rdev->wiphy);
386 case IW_MLME_DISASSOC:
387 err = cfg80211_disconnect(rdev, dev, mlme->reason_code, true);
394 wiphy_unlock(&rdev->wiphy);