896cbb20b6e11037bb6a1bb3b92073c88424bc61
[cascardo/linux.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                                IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
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_rdev(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_rdev(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 static 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         int err;
92
93         ASSERT_WDEV_LOCK(wdev);
94
95         if (wdev->ssid_len)
96                 return -EALREADY;
97
98         if (!params->basic_rates) {
99                 /*
100                 * If no rates were explicitly configured,
101                 * use the mandatory rate set for 11b or
102                 * 11a for maximum compatibility.
103                 */
104                 struct ieee80211_supported_band *sband =
105                         rdev->wiphy.bands[params->chandef.chan->band];
106                 int j;
107                 u32 flag = params->chandef.chan->band == NL80211_BAND_5GHZ ?
108                         IEEE80211_RATE_MANDATORY_A :
109                         IEEE80211_RATE_MANDATORY_B;
110
111                 for (j = 0; j < sband->n_bitrates; j++) {
112                         if (sband->bitrates[j].flags & flag)
113                                 params->basic_rates |= BIT(j);
114                 }
115         }
116
117         if (WARN_ON(wdev->connect_keys))
118                 kzfree(wdev->connect_keys);
119         wdev->connect_keys = connkeys;
120
121         wdev->ibss_fixed = params->channel_fixed;
122         wdev->ibss_dfs_possible = params->userspace_handles_dfs;
123         wdev->chandef = params->chandef;
124 #ifdef CONFIG_CFG80211_WEXT
125         wdev->wext.ibss.chandef = params->chandef;
126 #endif
127         err = rdev_join_ibss(rdev, dev, params);
128         if (err) {
129                 wdev->connect_keys = NULL;
130                 return err;
131         }
132
133         memcpy(wdev->ssid, params->ssid, params->ssid_len);
134         wdev->ssid_len = params->ssid_len;
135
136         return 0;
137 }
138
139 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
140                        struct net_device *dev,
141                        struct cfg80211_ibss_params *params,
142                        struct cfg80211_cached_keys *connkeys)
143 {
144         struct wireless_dev *wdev = dev->ieee80211_ptr;
145         int err;
146
147         ASSERT_RTNL();
148
149         wdev_lock(wdev);
150         err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
151         wdev_unlock(wdev);
152
153         return err;
154 }
155
156 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
157 {
158         struct wireless_dev *wdev = dev->ieee80211_ptr;
159         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
160         int i;
161
162         ASSERT_WDEV_LOCK(wdev);
163
164         kzfree(wdev->connect_keys);
165         wdev->connect_keys = NULL;
166
167         rdev_set_qos_map(rdev, dev, NULL);
168
169         /*
170          * Delete all the keys ... pairwise keys can't really
171          * exist any more anyway, but default keys might.
172          */
173         if (rdev->ops->del_key)
174                 for (i = 0; i < 6; i++)
175                         rdev_del_key(rdev, dev, i, false, NULL);
176
177         if (wdev->current_bss) {
178                 cfg80211_unhold_bss(wdev->current_bss);
179                 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
180         }
181
182         wdev->current_bss = NULL;
183         wdev->ssid_len = 0;
184         memset(&wdev->chandef, 0, sizeof(wdev->chandef));
185 #ifdef CONFIG_CFG80211_WEXT
186         if (!nowext)
187                 wdev->wext.ibss.ssid_len = 0;
188 #endif
189 }
190
191 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
192 {
193         struct wireless_dev *wdev = dev->ieee80211_ptr;
194
195         wdev_lock(wdev);
196         __cfg80211_clear_ibss(dev, nowext);
197         wdev_unlock(wdev);
198 }
199
200 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
201                           struct net_device *dev, bool nowext)
202 {
203         struct wireless_dev *wdev = dev->ieee80211_ptr;
204         int err;
205
206         ASSERT_WDEV_LOCK(wdev);
207
208         if (!wdev->ssid_len)
209                 return -ENOLINK;
210
211         err = rdev_leave_ibss(rdev, dev);
212
213         if (err)
214                 return err;
215
216         __cfg80211_clear_ibss(dev, nowext);
217
218         return 0;
219 }
220
221 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
222                         struct net_device *dev, bool nowext)
223 {
224         struct wireless_dev *wdev = dev->ieee80211_ptr;
225         int err;
226
227         wdev_lock(wdev);
228         err = __cfg80211_leave_ibss(rdev, dev, nowext);
229         wdev_unlock(wdev);
230
231         return err;
232 }
233
234 #ifdef CONFIG_CFG80211_WEXT
235 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
236                             struct wireless_dev *wdev)
237 {
238         struct cfg80211_cached_keys *ck = NULL;
239         enum nl80211_band band;
240         int i, err;
241
242         ASSERT_WDEV_LOCK(wdev);
243
244         if (!wdev->wext.ibss.beacon_interval)
245                 wdev->wext.ibss.beacon_interval = 100;
246
247         /* try to find an IBSS channel if none requested ... */
248         if (!wdev->wext.ibss.chandef.chan) {
249                 struct ieee80211_channel *new_chan = NULL;
250
251                 for (band = 0; band < NUM_NL80211_BANDS; band++) {
252                         struct ieee80211_supported_band *sband;
253                         struct ieee80211_channel *chan;
254
255                         sband = rdev->wiphy.bands[band];
256                         if (!sband)
257                                 continue;
258
259                         for (i = 0; i < sband->n_channels; i++) {
260                                 chan = &sband->channels[i];
261                                 if (chan->flags & IEEE80211_CHAN_NO_IR)
262                                         continue;
263                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
264                                         continue;
265                                 new_chan = chan;
266                                 break;
267                         }
268
269                         if (new_chan)
270                                 break;
271                 }
272
273                 if (!new_chan)
274                         return -EINVAL;
275
276                 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
277                                         NL80211_CHAN_NO_HT);
278         }
279
280         /* don't join -- SSID is not there */
281         if (!wdev->wext.ibss.ssid_len)
282                 return 0;
283
284         if (!netif_running(wdev->netdev))
285                 return 0;
286
287         if (wdev->wext.keys)
288                 wdev->wext.keys->def = wdev->wext.default_key;
289
290         wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
291
292         if (wdev->wext.keys) {
293                 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
294                 if (!ck)
295                         return -ENOMEM;
296                 for (i = 0; i < 4; i++)
297                         ck->params[i].key = ck->data[i];
298         }
299         err = __cfg80211_join_ibss(rdev, wdev->netdev,
300                                    &wdev->wext.ibss, ck);
301         if (err)
302                 kfree(ck);
303
304         return err;
305 }
306
307 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
308                                struct iw_request_info *info,
309                                struct iw_freq *wextfreq, char *extra)
310 {
311         struct wireless_dev *wdev = dev->ieee80211_ptr;
312         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
313         struct ieee80211_channel *chan = NULL;
314         int err, freq;
315
316         /* call only for ibss! */
317         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
318                 return -EINVAL;
319
320         if (!rdev->ops->join_ibss)
321                 return -EOPNOTSUPP;
322
323         freq = cfg80211_wext_freq(wextfreq);
324         if (freq < 0)
325                 return freq;
326
327         if (freq) {
328                 chan = ieee80211_get_channel(wdev->wiphy, freq);
329                 if (!chan)
330                         return -EINVAL;
331                 if (chan->flags & IEEE80211_CHAN_NO_IR ||
332                     chan->flags & IEEE80211_CHAN_DISABLED)
333                         return -EINVAL;
334         }
335
336         if (wdev->wext.ibss.chandef.chan == chan)
337                 return 0;
338
339         wdev_lock(wdev);
340         err = 0;
341         if (wdev->ssid_len)
342                 err = __cfg80211_leave_ibss(rdev, dev, true);
343         wdev_unlock(wdev);
344
345         if (err)
346                 return err;
347
348         if (chan) {
349                 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
350                                         NL80211_CHAN_NO_HT);
351                 wdev->wext.ibss.channel_fixed = true;
352         } else {
353                 /* cfg80211_ibss_wext_join will pick one if needed */
354                 wdev->wext.ibss.channel_fixed = false;
355         }
356
357         wdev_lock(wdev);
358         err = cfg80211_ibss_wext_join(rdev, wdev);
359         wdev_unlock(wdev);
360
361         return err;
362 }
363
364 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
365                                struct iw_request_info *info,
366                                struct iw_freq *freq, char *extra)
367 {
368         struct wireless_dev *wdev = dev->ieee80211_ptr;
369         struct ieee80211_channel *chan = NULL;
370
371         /* call only for ibss! */
372         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
373                 return -EINVAL;
374
375         wdev_lock(wdev);
376         if (wdev->current_bss)
377                 chan = wdev->current_bss->pub.channel;
378         else if (wdev->wext.ibss.chandef.chan)
379                 chan = wdev->wext.ibss.chandef.chan;
380         wdev_unlock(wdev);
381
382         if (chan) {
383                 freq->m = chan->center_freq;
384                 freq->e = 6;
385                 return 0;
386         }
387
388         /* no channel if not joining */
389         return -EINVAL;
390 }
391
392 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
393                                 struct iw_request_info *info,
394                                 struct iw_point *data, char *ssid)
395 {
396         struct wireless_dev *wdev = dev->ieee80211_ptr;
397         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
398         size_t len = data->length;
399         int err;
400
401         /* call only for ibss! */
402         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
403                 return -EINVAL;
404
405         if (!rdev->ops->join_ibss)
406                 return -EOPNOTSUPP;
407
408         wdev_lock(wdev);
409         err = 0;
410         if (wdev->ssid_len)
411                 err = __cfg80211_leave_ibss(rdev, dev, true);
412         wdev_unlock(wdev);
413
414         if (err)
415                 return err;
416
417         /* iwconfig uses nul termination in SSID.. */
418         if (len > 0 && ssid[len - 1] == '\0')
419                 len--;
420
421         memcpy(wdev->ssid, ssid, len);
422         wdev->wext.ibss.ssid = wdev->ssid;
423         wdev->wext.ibss.ssid_len = len;
424
425         wdev_lock(wdev);
426         err = cfg80211_ibss_wext_join(rdev, wdev);
427         wdev_unlock(wdev);
428
429         return err;
430 }
431
432 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
433                                 struct iw_request_info *info,
434                                 struct iw_point *data, char *ssid)
435 {
436         struct wireless_dev *wdev = dev->ieee80211_ptr;
437
438         /* call only for ibss! */
439         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
440                 return -EINVAL;
441
442         data->flags = 0;
443
444         wdev_lock(wdev);
445         if (wdev->ssid_len) {
446                 data->flags = 1;
447                 data->length = wdev->ssid_len;
448                 memcpy(ssid, wdev->ssid, data->length);
449         } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
450                 data->flags = 1;
451                 data->length = wdev->wext.ibss.ssid_len;
452                 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
453         }
454         wdev_unlock(wdev);
455
456         return 0;
457 }
458
459 int cfg80211_ibss_wext_siwap(struct net_device *dev,
460                              struct iw_request_info *info,
461                              struct sockaddr *ap_addr, char *extra)
462 {
463         struct wireless_dev *wdev = dev->ieee80211_ptr;
464         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
465         u8 *bssid = ap_addr->sa_data;
466         int err;
467
468         /* call only for ibss! */
469         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
470                 return -EINVAL;
471
472         if (!rdev->ops->join_ibss)
473                 return -EOPNOTSUPP;
474
475         if (ap_addr->sa_family != ARPHRD_ETHER)
476                 return -EINVAL;
477
478         /* automatic mode */
479         if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
480                 bssid = NULL;
481
482         if (bssid && !is_valid_ether_addr(bssid))
483                 return -EINVAL;
484
485         /* both automatic */
486         if (!bssid && !wdev->wext.ibss.bssid)
487                 return 0;
488
489         /* fixed already - and no change */
490         if (wdev->wext.ibss.bssid && bssid &&
491             ether_addr_equal(bssid, wdev->wext.ibss.bssid))
492                 return 0;
493
494         wdev_lock(wdev);
495         err = 0;
496         if (wdev->ssid_len)
497                 err = __cfg80211_leave_ibss(rdev, dev, true);
498         wdev_unlock(wdev);
499
500         if (err)
501                 return err;
502
503         if (bssid) {
504                 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
505                 wdev->wext.ibss.bssid = wdev->wext.bssid;
506         } else
507                 wdev->wext.ibss.bssid = NULL;
508
509         wdev_lock(wdev);
510         err = cfg80211_ibss_wext_join(rdev, wdev);
511         wdev_unlock(wdev);
512
513         return err;
514 }
515
516 int cfg80211_ibss_wext_giwap(struct net_device *dev,
517                              struct iw_request_info *info,
518                              struct sockaddr *ap_addr, char *extra)
519 {
520         struct wireless_dev *wdev = dev->ieee80211_ptr;
521
522         /* call only for ibss! */
523         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
524                 return -EINVAL;
525
526         ap_addr->sa_family = ARPHRD_ETHER;
527
528         wdev_lock(wdev);
529         if (wdev->current_bss)
530                 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
531         else if (wdev->wext.ibss.bssid)
532                 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
533         else
534                 eth_zero_addr(ap_addr->sa_data);
535
536         wdev_unlock(wdev);
537
538         return 0;
539 }
540 #endif