GNU Linux-libre 4.9.309-gnu1
[releases.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 && wdev->wext.keys->def != -1) {
46                 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
47                 if (!ck)
48                         return -ENOMEM;
49                 for (i = 0; i < CFG80211_MAX_WEP_KEYS; 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         int ret = 0;
228
229         /* call only for station! */
230         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
231                 return -EINVAL;
232
233         data->flags = 0;
234
235         wdev_lock(wdev);
236         if (wdev->current_bss) {
237                 const u8 *ie;
238
239                 rcu_read_lock();
240                 ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
241                                           WLAN_EID_SSID);
242                 if (ie) {
243                         data->flags = 1;
244                         data->length = ie[1];
245                         if (data->length > IW_ESSID_MAX_SIZE)
246                                 ret = -EINVAL;
247                         else
248                                 memcpy(ssid, ie + 2, data->length);
249                 }
250                 rcu_read_unlock();
251         } else if (wdev->wext.connect.ssid && wdev->wext.connect.ssid_len) {
252                 data->flags = 1;
253                 data->length = wdev->wext.connect.ssid_len;
254                 memcpy(ssid, wdev->wext.connect.ssid, data->length);
255         }
256         wdev_unlock(wdev);
257
258         return ret;
259 }
260
261 int cfg80211_mgd_wext_siwap(struct net_device *dev,
262                             struct iw_request_info *info,
263                             struct sockaddr *ap_addr, char *extra)
264 {
265         struct wireless_dev *wdev = dev->ieee80211_ptr;
266         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
267         u8 *bssid = ap_addr->sa_data;
268         int err;
269
270         /* call only for station! */
271         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
272                 return -EINVAL;
273
274         if (ap_addr->sa_family != ARPHRD_ETHER)
275                 return -EINVAL;
276
277         /* automatic mode */
278         if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
279                 bssid = NULL;
280
281         wdev_lock(wdev);
282
283         if (wdev->conn) {
284                 err = 0;
285                 /* both automatic */
286                 if (!bssid && !wdev->wext.connect.bssid)
287                         goto out;
288
289                 /* fixed already - and no change */
290                 if (wdev->wext.connect.bssid && bssid &&
291                     ether_addr_equal(bssid, wdev->wext.connect.bssid))
292                         goto out;
293
294                 err = cfg80211_disconnect(rdev, dev,
295                                           WLAN_REASON_DEAUTH_LEAVING, false);
296                 if (err)
297                         goto out;
298         }
299
300         if (bssid) {
301                 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
302                 wdev->wext.connect.bssid = wdev->wext.bssid;
303         } else
304                 wdev->wext.connect.bssid = NULL;
305
306         err = cfg80211_mgd_wext_connect(rdev, wdev);
307  out:
308         wdev_unlock(wdev);
309         return err;
310 }
311
312 int cfg80211_mgd_wext_giwap(struct net_device *dev,
313                             struct iw_request_info *info,
314                             struct sockaddr *ap_addr, char *extra)
315 {
316         struct wireless_dev *wdev = dev->ieee80211_ptr;
317
318         /* call only for station! */
319         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
320                 return -EINVAL;
321
322         ap_addr->sa_family = ARPHRD_ETHER;
323
324         wdev_lock(wdev);
325         if (wdev->current_bss)
326                 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
327         else
328                 eth_zero_addr(ap_addr->sa_data);
329         wdev_unlock(wdev);
330
331         return 0;
332 }
333
334 int cfg80211_wext_siwgenie(struct net_device *dev,
335                            struct iw_request_info *info,
336                            struct iw_point *data, char *extra)
337 {
338         struct wireless_dev *wdev = dev->ieee80211_ptr;
339         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
340         u8 *ie = extra;
341         int ie_len = data->length, err;
342
343         if (wdev->iftype != NL80211_IFTYPE_STATION)
344                 return -EOPNOTSUPP;
345
346         if (!ie_len)
347                 ie = NULL;
348
349         wdev_lock(wdev);
350
351         /* no change */
352         err = 0;
353         if (wdev->wext.ie_len == ie_len &&
354             memcmp(wdev->wext.ie, ie, ie_len) == 0)
355                 goto out;
356
357         if (ie_len) {
358                 ie = kmemdup(extra, ie_len, GFP_KERNEL);
359                 if (!ie) {
360                         err = -ENOMEM;
361                         goto out;
362                 }
363         } else
364                 ie = NULL;
365
366         kfree(wdev->wext.ie);
367         wdev->wext.ie = ie;
368         wdev->wext.ie_len = ie_len;
369
370         if (wdev->conn) {
371                 err = cfg80211_disconnect(rdev, dev,
372                                           WLAN_REASON_DEAUTH_LEAVING, false);
373                 if (err)
374                         goto out;
375         }
376
377         /* userspace better not think we'll reconnect */
378         err = 0;
379  out:
380         wdev_unlock(wdev);
381         return err;
382 }
383
384 int cfg80211_wext_siwmlme(struct net_device *dev,
385                           struct iw_request_info *info,
386                           struct iw_point *data, char *extra)
387 {
388         struct wireless_dev *wdev = dev->ieee80211_ptr;
389         struct iw_mlme *mlme = (struct iw_mlme *)extra;
390         struct cfg80211_registered_device *rdev;
391         int err;
392
393         if (!wdev)
394                 return -EOPNOTSUPP;
395
396         rdev = wiphy_to_rdev(wdev->wiphy);
397
398         if (wdev->iftype != NL80211_IFTYPE_STATION)
399                 return -EINVAL;
400
401         if (mlme->addr.sa_family != ARPHRD_ETHER)
402                 return -EINVAL;
403
404         wdev_lock(wdev);
405         switch (mlme->cmd) {
406         case IW_MLME_DEAUTH:
407         case IW_MLME_DISASSOC:
408                 err = cfg80211_disconnect(rdev, dev, mlme->reason_code, true);
409                 break;
410         default:
411                 err = -EOPNOTSUPP;
412                 break;
413         }
414         wdev_unlock(wdev);
415
416         return err;
417 }