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