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