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