cfg80211: change netdev registration/unregistration semantics
[linux-block.git] / net / wireless / ibss.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
04a773ad
JB
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>
5a0e3ad6 10#include <linux/slab.h>
bc3b2d7f 11#include <linux/export.h>
04a773ad 12#include <net/cfg80211.h>
0e82ffe3 13#include "wext-compat.h"
04a773ad 14#include "nl80211.h"
e35e4d28 15#include "rdev-ops.h"
04a773ad
JB
16
17
fe94f3a4
AQ
18void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
19 struct ieee80211_channel *channel)
04a773ad
JB
20{
21 struct wireless_dev *wdev = dev->ieee80211_ptr;
22 struct cfg80211_bss *bss;
3d23e349 23#ifdef CONFIG_CFG80211_WEXT
04a773ad
JB
24 union iwreq_data wrqu;
25#endif
26
27 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
28 return;
29
f7969969 30 if (!wdev->ssid_len)
04a773ad
JB
31 return;
32
fe94f3a4 33 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
6eb18137 34 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
04a773ad
JB
35
36 if (WARN_ON(!bss))
37 return;
38
39 if (wdev->current_bss) {
40 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 41 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
04a773ad
JB
42 }
43
19957bb3
JB
44 cfg80211_hold_bss(bss_from_pub(bss));
45 wdev->current_bss = bss_from_pub(bss);
04a773ad 46
b8676221
DS
47 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
48 cfg80211_upload_connect_keys(wdev);
fffd0934 49
f26cbf40 50 nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid,
667503dd 51 GFP_KERNEL);
3d23e349 52#ifdef CONFIG_CFG80211_WEXT
04a773ad
JB
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}
667503dd 58
fe94f3a4
AQ
59void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
60 struct ieee80211_channel *channel, gfp_t gfp)
667503dd
JB
61{
62 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 63 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
667503dd
JB
64 struct cfg80211_event *ev;
65 unsigned long flags;
66
fe94f3a4
AQ
67 trace_cfg80211_ibss_joined(dev, bssid, channel);
68
69 if (WARN_ON(!channel))
70 return;
4ee3e063 71
667503dd
JB
72 ev = kzalloc(sizeof(*ev), gfp);
73 if (!ev)
74 return;
75
76 ev->type = EVENT_IBSS_JOINED;
fe94f3a4
AQ
77 memcpy(ev->ij.bssid, bssid, ETH_ALEN);
78 ev->ij.channel = channel;
667503dd
JB
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);
e60d7443 83 queue_work(cfg80211_wq, &rdev->event_work);
667503dd 84}
04a773ad
JB
85EXPORT_SYMBOL(cfg80211_ibss_joined);
86
f8d16d3e
DK
87int __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)
04a773ad
JB
91{
92 struct wireless_dev *wdev = dev->ieee80211_ptr;
93 int err;
94
f8d16d3e 95 ASSERT_RTNL();
667503dd
JB
96 ASSERT_WDEV_LOCK(wdev);
97
04a773ad
JB
98 if (wdev->ssid_len)
99 return -EALREADY;
100
93b05238
JB
101 if (!params->basic_rates) {
102 /*
103 * If no rates were explicitly configured,
104 * use the mandatory rate set for 11b or
105 * 11a for maximum compatibility.
106 */
5ea4e780
AS
107 struct ieee80211_supported_band *sband;
108 enum nl80211_band band;
109 u32 flag;
93b05238 110 int j;
93b05238 111
5ea4e780
AS
112 band = params->chandef.chan->band;
113 if (band == NL80211_BAND_5GHZ ||
114 band == NL80211_BAND_6GHZ)
115 flag = IEEE80211_RATE_MANDATORY_A;
116 else
117 flag = IEEE80211_RATE_MANDATORY_B;
118
119 sband = rdev->wiphy.bands[band];
93b05238
JB
120 for (j = 0; j < sband->n_bitrates; j++) {
121 if (sband->bitrates[j].flags & flag)
122 params->basic_rates |= BIT(j);
123 }
124 }
125
f1c1f17a
JB
126 if (WARN_ON(connkeys && connkeys->def < 0))
127 return -EINVAL;
128
fffd0934 129 if (WARN_ON(wdev->connect_keys))
453431a5 130 kfree_sensitive(wdev->connect_keys);
fffd0934
JB
131 wdev->connect_keys = connkeys;
132
c30a3d38 133 wdev->ibss_fixed = params->channel_fixed;
5336fa88 134 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
9e0e2961 135 wdev->chandef = params->chandef;
9ae3b172
TM
136 if (connkeys) {
137 params->wep_keys = connkeys->params;
138 params->wep_tx_key = connkeys->def;
139 }
140
3d23e349 141#ifdef CONFIG_CFG80211_WEXT
683b6d3b 142 wdev->wext.ibss.chandef = params->chandef;
04a773ad 143#endif
e35e4d28 144 err = rdev_join_ibss(rdev, dev, params);
fffd0934
JB
145 if (err) {
146 wdev->connect_keys = NULL;
04a773ad 147 return err;
fffd0934 148 }
04a773ad
JB
149
150 memcpy(wdev->ssid, params->ssid, params->ssid_len);
151 wdev->ssid_len = params->ssid_len;
152
153 return 0;
154}
155
667503dd 156static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
04a773ad
JB
157{
158 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 159 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
fffd0934 160 int i;
04a773ad 161
667503dd
JB
162 ASSERT_WDEV_LOCK(wdev);
163
453431a5 164 kfree_sensitive(wdev->connect_keys);
fffd0934
JB
165 wdev->connect_keys = NULL;
166
fa9ffc74
KP
167 rdev_set_qos_map(rdev, dev, NULL);
168
fffd0934
JB
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++)
e35e4d28 175 rdev_del_key(rdev, dev, i, false, NULL);
fffd0934 176
04a773ad
JB
177 if (wdev->current_bss) {
178 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 179 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
04a773ad
JB
180 }
181
182 wdev->current_bss = NULL;
183 wdev->ssid_len = 0;
9e0e2961 184 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
3d23e349 185#ifdef CONFIG_CFG80211_WEXT
9d308429 186 if (!nowext)
cbe8fa9c 187 wdev->wext.ibss.ssid_len = 0;
9d308429 188#endif
b35a51c7 189 cfg80211_sched_dfs_chan_update(rdev);
04a773ad
JB
190}
191
667503dd
JB
192void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
193{
194 struct wireless_dev *wdev = dev->ieee80211_ptr;
195
196 wdev_lock(wdev);
197 __cfg80211_clear_ibss(dev, nowext);
198 wdev_unlock(wdev);
199}
200
98d3a7ca
JB
201int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
202 struct net_device *dev, bool nowext)
04a773ad 203{
78485475 204 struct wireless_dev *wdev = dev->ieee80211_ptr;
04a773ad
JB
205 int err;
206
667503dd
JB
207 ASSERT_WDEV_LOCK(wdev);
208
78485475
JB
209 if (!wdev->ssid_len)
210 return -ENOLINK;
211
e35e4d28 212 err = rdev_leave_ibss(rdev, dev);
04a773ad
JB
213
214 if (err)
215 return err;
216
f8d16d3e 217 wdev->conn_owner_nlportid = 0;
667503dd 218 __cfg80211_clear_ibss(dev, nowext);
04a773ad
JB
219
220 return 0;
221}
222
667503dd
JB
223int 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 wdev_lock(wdev);
230 err = __cfg80211_leave_ibss(rdev, dev, nowext);
231 wdev_unlock(wdev);
232
233 return err;
234}
235
3d23e349 236#ifdef CONFIG_CFG80211_WEXT
fffd0934
JB
237int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
238 struct wireless_dev *wdev)
04a773ad 239{
fffd0934 240 struct cfg80211_cached_keys *ck = NULL;
57fbcce3 241 enum nl80211_band band;
fffd0934
JB
242 int i, err;
243
244 ASSERT_WDEV_LOCK(wdev);
04a773ad 245
cbe8fa9c
JB
246 if (!wdev->wext.ibss.beacon_interval)
247 wdev->wext.ibss.beacon_interval = 100;
8e30bc55 248
04a773ad 249 /* try to find an IBSS channel if none requested ... */
683b6d3b 250 if (!wdev->wext.ibss.chandef.chan) {
1fe4517c 251 struct ieee80211_channel *new_chan = NULL;
683b6d3b 252
57fbcce3 253 for (band = 0; band < NUM_NL80211_BANDS; band++) {
04a773ad
JB
254 struct ieee80211_supported_band *sband;
255 struct ieee80211_channel *chan;
256
257 sband = rdev->wiphy.bands[band];
258 if (!sband)
259 continue;
260
261 for (i = 0; i < sband->n_channels; i++) {
262 chan = &sband->channels[i];
8fe02e16 263 if (chan->flags & IEEE80211_CHAN_NO_IR)
04a773ad
JB
264 continue;
265 if (chan->flags & IEEE80211_CHAN_DISABLED)
266 continue;
1fe4517c 267 new_chan = chan;
04a773ad
JB
268 break;
269 }
270
1fe4517c 271 if (new_chan)
04a773ad
JB
272 break;
273 }
274
1fe4517c 275 if (!new_chan)
04a773ad 276 return -EINVAL;
1fe4517c
SW
277
278 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
279 NL80211_CHAN_NO_HT);
04a773ad
JB
280 }
281
282 /* don't join -- SSID is not there */
cbe8fa9c 283 if (!wdev->wext.ibss.ssid_len)
04a773ad
JB
284 return 0;
285
286 if (!netif_running(wdev->netdev))
287 return 0;
288
89b706fb 289 if (wdev->wext.keys)
fffd0934
JB
290 wdev->wext.keys->def = wdev->wext.default_key;
291
292 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
293
f1c1f17a 294 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
fffd0934
JB
295 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
296 if (!ck)
297 return -ENOMEM;
b8676221 298 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
fffd0934
JB
299 ck->params[i].key = ck->data[i];
300 }
301 err = __cfg80211_join_ibss(rdev, wdev->netdev,
302 &wdev->wext.ibss, ck);
303 if (err)
304 kfree(ck);
305
306 return err;
04a773ad
JB
307}
308
309int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
310 struct iw_request_info *info,
59bbb6f7 311 struct iw_freq *wextfreq, char *extra)
04a773ad
JB
312{
313 struct wireless_dev *wdev = dev->ieee80211_ptr;
f26cbf40 314 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
59bbb6f7
JB
315 struct ieee80211_channel *chan = NULL;
316 int err, freq;
04a773ad
JB
317
318 /* call only for ibss! */
319 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
320 return -EINVAL;
321
59bbb6f7 322 if (!rdev->ops->join_ibss)
04a773ad
JB
323 return -EOPNOTSUPP;
324
96998e3a 325 freq = cfg80211_wext_freq(wextfreq);
59bbb6f7
JB
326 if (freq < 0)
327 return freq;
04a773ad 328
59bbb6f7
JB
329 if (freq) {
330 chan = ieee80211_get_channel(wdev->wiphy, freq);
331 if (!chan)
332 return -EINVAL;
8fe02e16 333 if (chan->flags & IEEE80211_CHAN_NO_IR ||
59bbb6f7
JB
334 chan->flags & IEEE80211_CHAN_DISABLED)
335 return -EINVAL;
336 }
04a773ad 337
683b6d3b 338 if (wdev->wext.ibss.chandef.chan == chan)
04a773ad
JB
339 return 0;
340
667503dd
JB
341 wdev_lock(wdev);
342 err = 0;
343 if (wdev->ssid_len)
59bbb6f7 344 err = __cfg80211_leave_ibss(rdev, dev, true);
667503dd
JB
345 wdev_unlock(wdev);
346
347 if (err)
348 return err;
04a773ad
JB
349
350 if (chan) {
1fe4517c
SW
351 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
352 NL80211_CHAN_NO_HT);
cbe8fa9c 353 wdev->wext.ibss.channel_fixed = true;
04a773ad
JB
354 } else {
355 /* cfg80211_ibss_wext_join will pick one if needed */
cbe8fa9c 356 wdev->wext.ibss.channel_fixed = false;
04a773ad
JB
357 }
358
fffd0934 359 wdev_lock(wdev);
59bbb6f7 360 err = cfg80211_ibss_wext_join(rdev, wdev);
fffd0934
JB
361 wdev_unlock(wdev);
362
363 return err;
04a773ad 364}
04a773ad
JB
365
366int 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
667503dd 377 wdev_lock(wdev);
04a773ad 378 if (wdev->current_bss)
19957bb3 379 chan = wdev->current_bss->pub.channel;
683b6d3b
JB
380 else if (wdev->wext.ibss.chandef.chan)
381 chan = wdev->wext.ibss.chandef.chan;
667503dd 382 wdev_unlock(wdev);
04a773ad
JB
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}
04a773ad
JB
393
394int 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;
f26cbf40 399 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
04a773ad
JB
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
59bbb6f7 407 if (!rdev->ops->join_ibss)
04a773ad
JB
408 return -EOPNOTSUPP;
409
667503dd
JB
410 wdev_lock(wdev);
411 err = 0;
412 if (wdev->ssid_len)
59bbb6f7 413 err = __cfg80211_leave_ibss(rdev, dev, true);
667503dd
JB
414 wdev_unlock(wdev);
415
416 if (err)
417 return err;
04a773ad
JB
418
419 /* iwconfig uses nul termination in SSID.. */
420 if (len > 0 && ssid[len - 1] == '\0')
421 len--;
422
c1e5f471 423 memcpy(wdev->ssid, ssid, len);
cbe8fa9c 424 wdev->wext.ibss.ssid = wdev->ssid;
cbe8fa9c 425 wdev->wext.ibss.ssid_len = len;
04a773ad 426
fffd0934 427 wdev_lock(wdev);
59bbb6f7 428 err = cfg80211_ibss_wext_join(rdev, wdev);
fffd0934
JB
429 wdev_unlock(wdev);
430
431 return err;
04a773ad 432}
04a773ad
JB
433
434int 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
667503dd 446 wdev_lock(wdev);
04a773ad
JB
447 if (wdev->ssid_len) {
448 data->flags = 1;
449 data->length = wdev->ssid_len;
450 memcpy(ssid, wdev->ssid, data->length);
cbe8fa9c 451 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
04a773ad 452 data->flags = 1;
cbe8fa9c
JB
453 data->length = wdev->wext.ibss.ssid_len;
454 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
04a773ad 455 }
667503dd 456 wdev_unlock(wdev);
04a773ad
JB
457
458 return 0;
459}
04a773ad
JB
460
461int 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;
f26cbf40 466 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
04a773ad
JB
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
59bbb6f7 474 if (!rdev->ops->join_ibss)
04a773ad
JB
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
74f82741
JB
484 if (bssid && !is_valid_ether_addr(bssid))
485 return -EINVAL;
486
04a773ad 487 /* both automatic */
cbe8fa9c 488 if (!bssid && !wdev->wext.ibss.bssid)
04a773ad
JB
489 return 0;
490
491 /* fixed already - and no change */
cbe8fa9c 492 if (wdev->wext.ibss.bssid && bssid &&
ac422d3c 493 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
04a773ad
JB
494 return 0;
495
667503dd
JB
496 wdev_lock(wdev);
497 err = 0;
498 if (wdev->ssid_len)
59bbb6f7 499 err = __cfg80211_leave_ibss(rdev, dev, true);
667503dd
JB
500 wdev_unlock(wdev);
501
502 if (err)
503 return err;
04a773ad
JB
504
505 if (bssid) {
cbe8fa9c
JB
506 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
507 wdev->wext.ibss.bssid = wdev->wext.bssid;
04a773ad 508 } else
cbe8fa9c 509 wdev->wext.ibss.bssid = NULL;
04a773ad 510
fffd0934 511 wdev_lock(wdev);
59bbb6f7 512 err = cfg80211_ibss_wext_join(rdev, wdev);
fffd0934
JB
513 wdev_unlock(wdev);
514
515 return err;
04a773ad 516}
04a773ad
JB
517
518int cfg80211_ibss_wext_giwap(struct net_device *dev,
519 struct iw_request_info *info,
520 struct sockaddr *ap_addr, char *extra)
521{
522 struct wireless_dev *wdev = dev->ieee80211_ptr;
523
524 /* call only for ibss! */
525 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
526 return -EINVAL;
527
528 ap_addr->sa_family = ARPHRD_ETHER;
529
667503dd 530 wdev_lock(wdev);
7ebbe6bd 531 if (wdev->current_bss)
19957bb3 532 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
80e5b06a 533 else if (wdev->wext.ibss.bssid)
cbe8fa9c 534 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
80e5b06a 535 else
d2beae10 536 eth_zero_addr(ap_addr->sa_data);
80e5b06a 537
667503dd
JB
538 wdev_unlock(wdev);
539
04a773ad
JB
540 return 0;
541}
04a773ad 542#endif