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