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