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-2023 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 lockdep_assert_wiphy(wdev
->wiphy
);
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
< 4; 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
)
93 if (wdev
->wext
.connect
.channel
== chan
)
96 /* if SSID set, we'll try right again, avoid event */
97 if (wdev
->wext
.connect
.ssid_len
)
99 err
= cfg80211_disconnect(rdev
, dev
,
100 WLAN_REASON_DEAUTH_LEAVING
, event
);
105 wdev
->wext
.connect
.channel
= chan
;
106 return cfg80211_mgd_wext_connect(rdev
, wdev
);
109 int cfg80211_mgd_wext_giwfreq(struct net_device
*dev
,
110 struct iw_request_info
*info
,
111 struct iw_freq
*freq
, char *extra
)
113 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
114 struct ieee80211_channel
*chan
= NULL
;
116 /* call only for station! */
117 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
))
120 if (wdev
->valid_links
)
123 if (wdev
->links
[0].client
.current_bss
)
124 chan
= wdev
->links
[0].client
.current_bss
->pub
.channel
;
125 else if (wdev
->wext
.connect
.channel
)
126 chan
= wdev
->wext
.connect
.channel
;
129 freq
->m
= chan
->center_freq
;
134 /* no channel if not joining */
138 int cfg80211_mgd_wext_siwessid(struct net_device
*dev
,
139 struct iw_request_info
*info
,
140 struct iw_point
*data
, char *ssid
)
142 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
143 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
144 size_t len
= data
->length
;
147 /* call only for station! */
148 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
))
154 /* iwconfig uses nul termination in SSID.. */
155 if (len
> 0 && ssid
[len
- 1] == '\0')
161 if (wdev
->wext
.connect
.ssid
&& len
&&
162 len
== wdev
->wext
.connect
.ssid_len
&&
163 memcmp(wdev
->wext
.connect
.ssid
, ssid
, len
) == 0)
166 /* if SSID set now, we'll try to connect, avoid event */
169 err
= cfg80211_disconnect(rdev
, dev
,
170 WLAN_REASON_DEAUTH_LEAVING
, event
);
175 wdev
->wext
.prev_bssid_valid
= false;
176 wdev
->wext
.connect
.ssid
= wdev
->wext
.ssid
;
177 memcpy(wdev
->wext
.ssid
, ssid
, len
);
178 wdev
->wext
.connect
.ssid_len
= len
;
180 wdev
->wext
.connect
.crypto
.control_port
= false;
181 wdev
->wext
.connect
.crypto
.control_port_ethertype
=
182 cpu_to_be16(ETH_P_PAE
);
184 return cfg80211_mgd_wext_connect(rdev
, wdev
);
187 int cfg80211_mgd_wext_giwessid(struct net_device
*dev
,
188 struct iw_request_info
*info
,
189 struct iw_point
*data
, char *ssid
)
191 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
194 /* call only for station! */
195 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
))
198 if (wdev
->valid_links
)
203 if (wdev
->links
[0].client
.current_bss
) {
204 const struct element
*ssid_elem
;
207 ssid_elem
= ieee80211_bss_get_elem(
208 &wdev
->links
[0].client
.current_bss
->pub
,
212 data
->length
= ssid_elem
->datalen
;
213 if (data
->length
> IW_ESSID_MAX_SIZE
)
216 memcpy(ssid
, ssid_elem
->data
, data
->length
);
219 } else if (wdev
->wext
.connect
.ssid
&& wdev
->wext
.connect
.ssid_len
) {
221 data
->length
= wdev
->wext
.connect
.ssid_len
;
222 memcpy(ssid
, wdev
->wext
.connect
.ssid
, data
->length
);
228 int cfg80211_mgd_wext_siwap(struct net_device
*dev
,
229 struct iw_request_info
*info
,
230 struct sockaddr
*ap_addr
, char *extra
)
232 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
233 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
234 u8
*bssid
= ap_addr
->sa_data
;
237 /* call only for station! */
238 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
))
241 if (ap_addr
->sa_family
!= ARPHRD_ETHER
)
245 if (is_zero_ether_addr(bssid
) || is_broadcast_ether_addr(bssid
))
250 if (!bssid
&& !wdev
->wext
.connect
.bssid
)
253 /* fixed already - and no change */
254 if (wdev
->wext
.connect
.bssid
&& bssid
&&
255 ether_addr_equal(bssid
, wdev
->wext
.connect
.bssid
))
258 err
= cfg80211_disconnect(rdev
, dev
,
259 WLAN_REASON_DEAUTH_LEAVING
, false);
265 memcpy(wdev
->wext
.bssid
, bssid
, ETH_ALEN
);
266 wdev
->wext
.connect
.bssid
= wdev
->wext
.bssid
;
268 wdev
->wext
.connect
.bssid
= NULL
;
270 return cfg80211_mgd_wext_connect(rdev
, wdev
);
273 int cfg80211_mgd_wext_giwap(struct net_device
*dev
,
274 struct iw_request_info
*info
,
275 struct sockaddr
*ap_addr
, char *extra
)
277 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
279 /* call only for station! */
280 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
))
283 ap_addr
->sa_family
= ARPHRD_ETHER
;
285 if (wdev
->valid_links
)
288 if (wdev
->links
[0].client
.current_bss
)
289 memcpy(ap_addr
->sa_data
,
290 wdev
->links
[0].client
.current_bss
->pub
.bssid
,
293 eth_zero_addr(ap_addr
->sa_data
);
298 int cfg80211_wext_siwgenie(struct net_device
*dev
,
299 struct iw_request_info
*info
,
300 union iwreq_data
*wrqu
, char *extra
)
302 struct iw_point
*data
= &wrqu
->data
;
303 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
304 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
306 int ie_len
= data
->length
, err
;
308 if (wdev
->iftype
!= NL80211_IFTYPE_STATION
)
314 wiphy_lock(wdev
->wiphy
);
318 if (wdev
->wext
.ie_len
== ie_len
&&
319 memcmp(wdev
->wext
.ie
, ie
, ie_len
) == 0)
323 ie
= kmemdup(extra
, ie_len
, GFP_KERNEL
);
331 kfree(wdev
->wext
.ie
);
333 wdev
->wext
.ie_len
= ie_len
;
336 err
= cfg80211_disconnect(rdev
, dev
,
337 WLAN_REASON_DEAUTH_LEAVING
, false);
342 /* userspace better not think we'll reconnect */
345 wiphy_unlock(wdev
->wiphy
);
349 int cfg80211_wext_siwmlme(struct net_device
*dev
,
350 struct iw_request_info
*info
,
351 union iwreq_data
*wrqu
, char *extra
)
353 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
354 struct iw_mlme
*mlme
= (struct iw_mlme
*)extra
;
355 struct cfg80211_registered_device
*rdev
;
361 rdev
= wiphy_to_rdev(wdev
->wiphy
);
363 if (wdev
->iftype
!= NL80211_IFTYPE_STATION
)
366 if (mlme
->addr
.sa_family
!= ARPHRD_ETHER
)
369 wiphy_lock(&rdev
->wiphy
);
372 case IW_MLME_DISASSOC
:
373 err
= cfg80211_disconnect(rdev
, dev
, mlme
->reason_code
, true);
379 wiphy_unlock(&rdev
->wiphy
);