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