cfg80211: fix channel configuration in IBSS join
[linux-block.git] / net / wireless / ibss.c
1 /*
2  * Some IBSS support code for cfg80211.
3  *
4  * Copyright 2009       Johannes Berg <johannes@sipsolutions.net>
5  */
6
7 #include <linux/etherdevice.h>
8 #include <linux/if_arp.h>
9 #include <linux/slab.h>
10 #include <linux/export.h>
11 #include <net/cfg80211.h>
12 #include "wext-compat.h"
13 #include "nl80211.h"
14 #include "rdev-ops.h"
15
16
17 void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
18                             struct ieee80211_channel *channel)
19 {
20         struct wireless_dev *wdev = dev->ieee80211_ptr;
21         struct cfg80211_bss *bss;
22 #ifdef CONFIG_CFG80211_WEXT
23         union iwreq_data wrqu;
24 #endif
25
26         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
27                 return;
28
29         if (!wdev->ssid_len)
30                 return;
31
32         bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
33                                WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
34
35         if (WARN_ON(!bss))
36                 return;
37
38         if (wdev->current_bss) {
39                 cfg80211_unhold_bss(wdev->current_bss);
40                 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
41         }
42
43         cfg80211_hold_bss(bss_from_pub(bss));
44         wdev->current_bss = bss_from_pub(bss);
45
46         cfg80211_upload_connect_keys(wdev);
47
48         nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
49                                 GFP_KERNEL);
50 #ifdef CONFIG_CFG80211_WEXT
51         memset(&wrqu, 0, sizeof(wrqu));
52         memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
53         wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
54 #endif
55 }
56
57 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
58                           struct ieee80211_channel *channel, gfp_t gfp)
59 {
60         struct wireless_dev *wdev = dev->ieee80211_ptr;
61         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
62         struct cfg80211_event *ev;
63         unsigned long flags;
64
65         trace_cfg80211_ibss_joined(dev, bssid, channel);
66
67         if (WARN_ON(!channel))
68                 return;
69
70         ev = kzalloc(sizeof(*ev), gfp);
71         if (!ev)
72                 return;
73
74         ev->type = EVENT_IBSS_JOINED;
75         memcpy(ev->ij.bssid, bssid, ETH_ALEN);
76         ev->ij.channel = channel;
77
78         spin_lock_irqsave(&wdev->event_lock, flags);
79         list_add_tail(&ev->list, &wdev->event_list);
80         spin_unlock_irqrestore(&wdev->event_lock, flags);
81         queue_work(cfg80211_wq, &rdev->event_work);
82 }
83 EXPORT_SYMBOL(cfg80211_ibss_joined);
84
85 int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
86                          struct net_device *dev,
87                          struct cfg80211_ibss_params *params,
88                          struct cfg80211_cached_keys *connkeys)
89 {
90         struct wireless_dev *wdev = dev->ieee80211_ptr;
91         struct ieee80211_channel *check_chan;
92         u8 radar_detect_width = 0;
93         int err;
94
95         ASSERT_WDEV_LOCK(wdev);
96
97         if (wdev->ssid_len)
98                 return -EALREADY;
99
100         if (!params->basic_rates) {
101                 /*
102                 * If no rates were explicitly configured,
103                 * use the mandatory rate set for 11b or
104                 * 11a for maximum compatibility.
105                 */
106                 struct ieee80211_supported_band *sband =
107                         rdev->wiphy.bands[params->chandef.chan->band];
108                 int j;
109                 u32 flag = params->chandef.chan->band == IEEE80211_BAND_5GHZ ?
110                         IEEE80211_RATE_MANDATORY_A :
111                         IEEE80211_RATE_MANDATORY_B;
112
113                 for (j = 0; j < sband->n_bitrates; j++) {
114                         if (sband->bitrates[j].flags & flag)
115                                 params->basic_rates |= BIT(j);
116                 }
117         }
118
119         if (WARN_ON(wdev->connect_keys))
120                 kfree(wdev->connect_keys);
121         wdev->connect_keys = connkeys;
122
123         wdev->ibss_fixed = params->channel_fixed;
124         wdev->ibss_dfs_possible = params->userspace_handles_dfs;
125 #ifdef CONFIG_CFG80211_WEXT
126         wdev->wext.ibss.chandef = params->chandef;
127 #endif
128         check_chan = params->chandef.chan;
129         if (params->userspace_handles_dfs) {
130                 /* use channel NULL to check for radar even if the current
131                  * channel is not a radar channel - it might decide to change
132                  * to DFS channel later.
133                  */
134                 radar_detect_width = BIT(params->chandef.width);
135                 check_chan = NULL;
136         }
137
138         err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
139                                            check_chan,
140                                            (params->channel_fixed &&
141                                             !radar_detect_width)
142                                            ? CHAN_MODE_SHARED
143                                            : CHAN_MODE_EXCLUSIVE,
144                                            radar_detect_width);
145
146         if (err) {
147                 wdev->connect_keys = NULL;
148                 return err;
149         }
150
151         err = rdev_join_ibss(rdev, dev, params);
152         if (err) {
153                 wdev->connect_keys = NULL;
154                 return err;
155         }
156
157         memcpy(wdev->ssid, params->ssid, params->ssid_len);
158         wdev->ssid_len = params->ssid_len;
159
160         return 0;
161 }
162
163 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
164                        struct net_device *dev,
165                        struct cfg80211_ibss_params *params,
166                        struct cfg80211_cached_keys *connkeys)
167 {
168         struct wireless_dev *wdev = dev->ieee80211_ptr;
169         int err;
170
171         ASSERT_RTNL();
172
173         wdev_lock(wdev);
174         err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
175         wdev_unlock(wdev);
176
177         return err;
178 }
179
180 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
181 {
182         struct wireless_dev *wdev = dev->ieee80211_ptr;
183         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
184         int i;
185
186         ASSERT_WDEV_LOCK(wdev);
187
188         kfree(wdev->connect_keys);
189         wdev->connect_keys = NULL;
190
191         rdev_set_qos_map(rdev, dev, NULL);
192
193         /*
194          * Delete all the keys ... pairwise keys can't really
195          * exist any more anyway, but default keys might.
196          */
197         if (rdev->ops->del_key)
198                 for (i = 0; i < 6; i++)
199                         rdev_del_key(rdev, dev, i, false, NULL);
200
201         if (wdev->current_bss) {
202                 cfg80211_unhold_bss(wdev->current_bss);
203                 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
204         }
205
206         wdev->current_bss = NULL;
207         wdev->ssid_len = 0;
208 #ifdef CONFIG_CFG80211_WEXT
209         if (!nowext)
210                 wdev->wext.ibss.ssid_len = 0;
211 #endif
212 }
213
214 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
215 {
216         struct wireless_dev *wdev = dev->ieee80211_ptr;
217
218         wdev_lock(wdev);
219         __cfg80211_clear_ibss(dev, nowext);
220         wdev_unlock(wdev);
221 }
222
223 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
224                           struct net_device *dev, bool nowext)
225 {
226         struct wireless_dev *wdev = dev->ieee80211_ptr;
227         int err;
228
229         ASSERT_WDEV_LOCK(wdev);
230
231         if (!wdev->ssid_len)
232                 return -ENOLINK;
233
234         err = rdev_leave_ibss(rdev, dev);
235
236         if (err)
237                 return err;
238
239         __cfg80211_clear_ibss(dev, nowext);
240
241         return 0;
242 }
243
244 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
245                         struct net_device *dev, bool nowext)
246 {
247         struct wireless_dev *wdev = dev->ieee80211_ptr;
248         int err;
249
250         wdev_lock(wdev);
251         err = __cfg80211_leave_ibss(rdev, dev, nowext);
252         wdev_unlock(wdev);
253
254         return err;
255 }
256
257 #ifdef CONFIG_CFG80211_WEXT
258 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
259                             struct wireless_dev *wdev)
260 {
261         struct cfg80211_cached_keys *ck = NULL;
262         enum ieee80211_band band;
263         int i, err;
264
265         ASSERT_WDEV_LOCK(wdev);
266
267         if (!wdev->wext.ibss.beacon_interval)
268                 wdev->wext.ibss.beacon_interval = 100;
269
270         /* try to find an IBSS channel if none requested ... */
271         if (!wdev->wext.ibss.chandef.chan) {
272                 struct ieee80211_channel *new_chan = NULL;
273
274                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
275                         struct ieee80211_supported_band *sband;
276                         struct ieee80211_channel *chan;
277
278                         sband = rdev->wiphy.bands[band];
279                         if (!sband)
280                                 continue;
281
282                         for (i = 0; i < sband->n_channels; i++) {
283                                 chan = &sband->channels[i];
284                                 if (chan->flags & IEEE80211_CHAN_NO_IR)
285                                         continue;
286                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
287                                         continue;
288                                 new_chan = chan;
289                                 break;
290                         }
291
292                         if (new_chan)
293                                 break;
294                 }
295
296                 if (!new_chan)
297                         return -EINVAL;
298
299                 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
300                                         NL80211_CHAN_NO_HT);
301         }
302
303         /* don't join -- SSID is not there */
304         if (!wdev->wext.ibss.ssid_len)
305                 return 0;
306
307         if (!netif_running(wdev->netdev))
308                 return 0;
309
310         if (wdev->wext.keys) {
311                 wdev->wext.keys->def = wdev->wext.default_key;
312                 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
313         }
314
315         wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
316
317         if (wdev->wext.keys) {
318                 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
319                 if (!ck)
320                         return -ENOMEM;
321                 for (i = 0; i < 6; i++)
322                         ck->params[i].key = ck->data[i];
323         }
324         err = __cfg80211_join_ibss(rdev, wdev->netdev,
325                                    &wdev->wext.ibss, ck);
326         if (err)
327                 kfree(ck);
328
329         return err;
330 }
331
332 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
333                                struct iw_request_info *info,
334                                struct iw_freq *wextfreq, char *extra)
335 {
336         struct wireless_dev *wdev = dev->ieee80211_ptr;
337         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
338         struct ieee80211_channel *chan = NULL;
339         int err, freq;
340
341         /* call only for ibss! */
342         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
343                 return -EINVAL;
344
345         if (!rdev->ops->join_ibss)
346                 return -EOPNOTSUPP;
347
348         freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
349         if (freq < 0)
350                 return freq;
351
352         if (freq) {
353                 chan = ieee80211_get_channel(wdev->wiphy, freq);
354                 if (!chan)
355                         return -EINVAL;
356                 if (chan->flags & IEEE80211_CHAN_NO_IR ||
357                     chan->flags & IEEE80211_CHAN_DISABLED)
358                         return -EINVAL;
359         }
360
361         if (wdev->wext.ibss.chandef.chan == chan)
362                 return 0;
363
364         wdev_lock(wdev);
365         err = 0;
366         if (wdev->ssid_len)
367                 err = __cfg80211_leave_ibss(rdev, dev, true);
368         wdev_unlock(wdev);
369
370         if (err)
371                 return err;
372
373         if (chan) {
374                 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
375                                         NL80211_CHAN_NO_HT);
376                 wdev->wext.ibss.channel_fixed = true;
377         } else {
378                 /* cfg80211_ibss_wext_join will pick one if needed */
379                 wdev->wext.ibss.channel_fixed = false;
380         }
381
382         wdev_lock(wdev);
383         err = cfg80211_ibss_wext_join(rdev, wdev);
384         wdev_unlock(wdev);
385
386         return err;
387 }
388
389 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
390                                struct iw_request_info *info,
391                                struct iw_freq *freq, char *extra)
392 {
393         struct wireless_dev *wdev = dev->ieee80211_ptr;
394         struct ieee80211_channel *chan = NULL;
395
396         /* call only for ibss! */
397         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
398                 return -EINVAL;
399
400         wdev_lock(wdev);
401         if (wdev->current_bss)
402                 chan = wdev->current_bss->pub.channel;
403         else if (wdev->wext.ibss.chandef.chan)
404                 chan = wdev->wext.ibss.chandef.chan;
405         wdev_unlock(wdev);
406
407         if (chan) {
408                 freq->m = chan->center_freq;
409                 freq->e = 6;
410                 return 0;
411         }
412
413         /* no channel if not joining */
414         return -EINVAL;
415 }
416
417 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
418                                 struct iw_request_info *info,
419                                 struct iw_point *data, char *ssid)
420 {
421         struct wireless_dev *wdev = dev->ieee80211_ptr;
422         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
423         size_t len = data->length;
424         int err;
425
426         /* call only for ibss! */
427         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
428                 return -EINVAL;
429
430         if (!rdev->ops->join_ibss)
431                 return -EOPNOTSUPP;
432
433         wdev_lock(wdev);
434         err = 0;
435         if (wdev->ssid_len)
436                 err = __cfg80211_leave_ibss(rdev, dev, true);
437         wdev_unlock(wdev);
438
439         if (err)
440                 return err;
441
442         /* iwconfig uses nul termination in SSID.. */
443         if (len > 0 && ssid[len - 1] == '\0')
444                 len--;
445
446         wdev->wext.ibss.ssid = wdev->ssid;
447         memcpy(wdev->wext.ibss.ssid, ssid, len);
448         wdev->wext.ibss.ssid_len = len;
449
450         wdev_lock(wdev);
451         err = cfg80211_ibss_wext_join(rdev, wdev);
452         wdev_unlock(wdev);
453
454         return err;
455 }
456
457 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
458                                 struct iw_request_info *info,
459                                 struct iw_point *data, char *ssid)
460 {
461         struct wireless_dev *wdev = dev->ieee80211_ptr;
462
463         /* call only for ibss! */
464         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
465                 return -EINVAL;
466
467         data->flags = 0;
468
469         wdev_lock(wdev);
470         if (wdev->ssid_len) {
471                 data->flags = 1;
472                 data->length = wdev->ssid_len;
473                 memcpy(ssid, wdev->ssid, data->length);
474         } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
475                 data->flags = 1;
476                 data->length = wdev->wext.ibss.ssid_len;
477                 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
478         }
479         wdev_unlock(wdev);
480
481         return 0;
482 }
483
484 int cfg80211_ibss_wext_siwap(struct net_device *dev,
485                              struct iw_request_info *info,
486                              struct sockaddr *ap_addr, char *extra)
487 {
488         struct wireless_dev *wdev = dev->ieee80211_ptr;
489         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
490         u8 *bssid = ap_addr->sa_data;
491         int err;
492
493         /* call only for ibss! */
494         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
495                 return -EINVAL;
496
497         if (!rdev->ops->join_ibss)
498                 return -EOPNOTSUPP;
499
500         if (ap_addr->sa_family != ARPHRD_ETHER)
501                 return -EINVAL;
502
503         /* automatic mode */
504         if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
505                 bssid = NULL;
506
507         /* both automatic */
508         if (!bssid && !wdev->wext.ibss.bssid)
509                 return 0;
510
511         /* fixed already - and no change */
512         if (wdev->wext.ibss.bssid && bssid &&
513             ether_addr_equal(bssid, wdev->wext.ibss.bssid))
514                 return 0;
515
516         wdev_lock(wdev);
517         err = 0;
518         if (wdev->ssid_len)
519                 err = __cfg80211_leave_ibss(rdev, dev, true);
520         wdev_unlock(wdev);
521
522         if (err)
523                 return err;
524
525         if (bssid) {
526                 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
527                 wdev->wext.ibss.bssid = wdev->wext.bssid;
528         } else
529                 wdev->wext.ibss.bssid = NULL;
530
531         wdev_lock(wdev);
532         err = cfg80211_ibss_wext_join(rdev, wdev);
533         wdev_unlock(wdev);
534
535         return err;
536 }
537
538 int cfg80211_ibss_wext_giwap(struct net_device *dev,
539                              struct iw_request_info *info,
540                              struct sockaddr *ap_addr, char *extra)
541 {
542         struct wireless_dev *wdev = dev->ieee80211_ptr;
543
544         /* call only for ibss! */
545         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
546                 return -EINVAL;
547
548         ap_addr->sa_family = ARPHRD_ETHER;
549
550         wdev_lock(wdev);
551         if (wdev->current_bss)
552                 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
553         else if (wdev->wext.ibss.bssid)
554                 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
555         else
556                 memset(ap_addr->sa_data, 0, ETH_ALEN);
557
558         wdev_unlock(wdev);
559
560         return 0;
561 }
562 #endif