localmodconfig: Check if configs are already set for selects
[linux-2.6.git] / net / wireless / wext-sme.c
blob7decbd357d516161c0c659a093d9066926daac4b
1 /*
2 * cfg80211 wext compat for managed mode.
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright (C) 2009 Intel Corporation. All rights reserved.
6 */
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"
17 int cfg80211_mgd_wext_connect(struct cfg80211_registered_device *rdev,
18 struct wireless_dev *wdev)
20 struct cfg80211_cached_keys *ck = NULL;
21 const u8 *prev_bssid = NULL;
22 int err, i;
24 ASSERT_RDEV_LOCK(rdev);
25 ASSERT_WDEV_LOCK(wdev);
27 if (!netif_running(wdev->netdev))
28 return 0;
30 wdev->wext.connect.ie = wdev->wext.ie;
31 wdev->wext.connect.ie_len = wdev->wext.ie_len;
33 /* Use default background scan period */
34 wdev->wext.connect.bg_scan_period = -1;
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;
43 if (!wdev->wext.connect.ssid_len)
44 return 0;
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];
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 kfree(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_dev(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(wdev->wiphy, 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 cfg80211_lock_rdev(rdev);
91 mutex_lock(&rdev->devlist_mtx);
92 wdev_lock(wdev);
94 if (wdev->sme_state != CFG80211_SME_IDLE) {
95 bool event = true;
97 if (wdev->wext.connect.channel == chan) {
98 err = 0;
99 goto out;
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;
112 wdev->wext.connect.channel = chan;
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;
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;
128 int cfg80211_mgd_wext_giwfreq(struct net_device *dev,
129 struct iw_request_info *info,
130 struct iw_freq *freq, char *extra)
132 struct wireless_dev *wdev = dev->ieee80211_ptr;
133 struct ieee80211_channel *chan = NULL;
135 /* call only for station! */
136 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
137 return -EINVAL;
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);
146 if (chan) {
147 freq->m = chan->center_freq;
148 freq->e = 6;
149 return 0;
152 /* no channel if not joining */
153 return -EINVAL;
156 int cfg80211_mgd_wext_siwessid(struct net_device *dev,
157 struct iw_request_info *info,
158 struct iw_point *data, char *ssid)
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;
165 /* call only for station! */
166 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
167 return -EINVAL;
169 if (!data->flags)
170 len = 0;
172 /* iwconfig uses nul termination in SSID.. */
173 if (len > 0 && ssid[len - 1] == '\0')
174 len--;
176 cfg80211_lock_rdev(rdev);
177 mutex_lock(&rdev->devlist_mtx);
178 wdev_lock(wdev);
180 err = 0;
182 if (wdev->sme_state != CFG80211_SME_IDLE) {
183 bool event = true;
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;
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;
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;
204 wdev->wext.connect.crypto.control_port = false;
205 wdev->wext.connect.crypto.control_port_ethertype =
206 cpu_to_be16(ETH_P_PAE);
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;
216 int cfg80211_mgd_wext_giwessid(struct net_device *dev,
217 struct iw_request_info *info,
218 struct iw_point *data, char *ssid)
220 struct wireless_dev *wdev = dev->ieee80211_ptr;
222 /* call only for station! */
223 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
224 return -EINVAL;
226 data->flags = 0;
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);
237 } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
238 data->flags = 1;
239 data->length = wdev->wext.connect.ssid_len;
240 memcpy(ssid, wdev->wext.connect.ssid, data->length);
242 wdev_unlock(wdev);
244 return 0;
247 int cfg80211_mgd_wext_siwap(struct net_device *dev,
248 struct iw_request_info *info,
249 struct sockaddr *ap_addr, char *extra)
251 struct wireless_dev *wdev = dev->ieee80211_ptr;
252 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
253 u8 *bssid = ap_addr->sa_data;
254 int err;
256 /* call only for station! */
257 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
258 return -EINVAL;
260 if (ap_addr->sa_family != ARPHRD_ETHER)
261 return -EINVAL;
263 /* automatic mode */
264 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
265 bssid = NULL;
267 cfg80211_lock_rdev(rdev);
268 mutex_lock(&rdev->devlist_mtx);
269 wdev_lock(wdev);
271 if (wdev->sme_state != CFG80211_SME_IDLE) {
272 err = 0;
273 /* both automatic */
274 if (!bssid && !wdev->wext.connect.bssid)
275 goto out;
277 /* fixed already - and no change */
278 if (wdev->wext.connect.bssid && bssid &&
279 ether_addr_equal(bssid, wdev->wext.connect.bssid))
280 goto out;
282 err = __cfg80211_disconnect(rdev, dev,
283 WLAN_REASON_DEAUTH_LEAVING, false);
284 if (err)
285 goto out;
288 if (bssid) {
289 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
290 wdev->wext.connect.bssid = wdev->wext.bssid;
291 } else
292 wdev->wext.connect.bssid = NULL;
294 err = cfg80211_mgd_wext_connect(rdev, wdev);
295 out:
296 wdev_unlock(wdev);
297 mutex_unlock(&rdev->devlist_mtx);
298 cfg80211_unlock_rdev(rdev);
299 return err;
302 int cfg80211_mgd_wext_giwap(struct net_device *dev,
303 struct iw_request_info *info,
304 struct sockaddr *ap_addr, char *extra)
306 struct wireless_dev *wdev = dev->ieee80211_ptr;
308 /* call only for station! */
309 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
310 return -EINVAL;
312 ap_addr->sa_family = ARPHRD_ETHER;
314 wdev_lock(wdev);
315 if (wdev->current_bss)
316 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
317 else
318 memset(ap_addr->sa_data, 0, ETH_ALEN);
319 wdev_unlock(wdev);
321 return 0;
324 int cfg80211_wext_siwgenie(struct net_device *dev,
325 struct iw_request_info *info,
326 struct iw_point *data, char *extra)
328 struct wireless_dev *wdev = dev->ieee80211_ptr;
329 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
330 u8 *ie = extra;
331 int ie_len = data->length, err;
333 if (wdev->iftype != NL80211_IFTYPE_STATION)
334 return -EOPNOTSUPP;
336 if (!ie_len)
337 ie = NULL;
339 wdev_lock(wdev);
341 /* no change */
342 err = 0;
343 if (wdev->wext.ie_len == ie_len &&
344 memcmp(wdev->wext.ie, ie, ie_len) == 0)
345 goto out;
347 if (ie_len) {
348 ie = kmemdup(extra, ie_len, GFP_KERNEL);
349 if (!ie) {
350 err = -ENOMEM;
351 goto out;
353 } else
354 ie = NULL;
356 kfree(wdev->wext.ie);
357 wdev->wext.ie = ie;
358 wdev->wext.ie_len = ie_len;
360 if (wdev->sme_state != CFG80211_SME_IDLE) {
361 err = __cfg80211_disconnect(rdev, dev,
362 WLAN_REASON_DEAUTH_LEAVING, false);
363 if (err)
364 goto out;
367 /* userspace better not think we'll reconnect */
368 err = 0;
369 out:
370 wdev_unlock(wdev);
371 return err;
374 int cfg80211_wext_siwmlme(struct net_device *dev,
375 struct iw_request_info *info,
376 struct iw_point *data, char *extra)
378 struct wireless_dev *wdev = dev->ieee80211_ptr;
379 struct iw_mlme *mlme = (struct iw_mlme *)extra;
380 struct cfg80211_registered_device *rdev;
381 int err;
383 if (!wdev)
384 return -EOPNOTSUPP;
386 rdev = wiphy_to_dev(wdev->wiphy);
388 if (wdev->iftype != NL80211_IFTYPE_STATION)
389 return -EINVAL;
391 if (mlme->addr.sa_family != ARPHRD_ETHER)
392 return -EINVAL;
394 wdev_lock(wdev);
395 switch (mlme->cmd) {
396 case IW_MLME_DEAUTH:
397 case IW_MLME_DISASSOC:
398 err = __cfg80211_disconnect(rdev, dev, mlme->reason_code,
399 true);
400 break;
401 default:
402 err = -EOPNOTSUPP;
403 break;
405 wdev_unlock(wdev);
407 return err;