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