]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/wireless/ibss.c
cfg80211: keep track of current_bss for userspace SME
[mirror_ubuntu-bionic-kernel.git] / net / wireless / ibss.c
CommitLineData
04a773ad
JB
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 <net/cfg80211.h>
0e82ffe3 10#include "wext-compat.h"
04a773ad
JB
11#include "nl80211.h"
12
13
667503dd 14void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
04a773ad
JB
15{
16 struct wireless_dev *wdev = dev->ieee80211_ptr;
17 struct cfg80211_bss *bss;
18#ifdef CONFIG_WIRELESS_EXT
19 union iwreq_data wrqu;
20#endif
21
22 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
23 return;
24
25 if (WARN_ON(!wdev->ssid_len))
26 return;
27
04a773ad
JB
28 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
29 wdev->ssid, wdev->ssid_len,
30 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
31
32 if (WARN_ON(!bss))
33 return;
34
35 if (wdev->current_bss) {
36 cfg80211_unhold_bss(wdev->current_bss);
19957bb3 37 cfg80211_put_bss(&wdev->current_bss->pub);
04a773ad
JB
38 }
39
19957bb3
JB
40 cfg80211_hold_bss(bss_from_pub(bss));
41 wdev->current_bss = bss_from_pub(bss);
04a773ad 42
fffd0934
JB
43 cfg80211_upload_connect_keys(wdev);
44
667503dd
JB
45 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
46 GFP_KERNEL);
04a773ad
JB
47#ifdef CONFIG_WIRELESS_EXT
48 memset(&wrqu, 0, sizeof(wrqu));
49 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
50 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
51#endif
52}
667503dd
JB
53
54void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
55{
56 struct wireless_dev *wdev = dev->ieee80211_ptr;
57 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
58 struct cfg80211_event *ev;
59 unsigned long flags;
60
61 ev = kzalloc(sizeof(*ev), gfp);
62 if (!ev)
63 return;
64
65 ev->type = EVENT_IBSS_JOINED;
66 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
67
68 spin_lock_irqsave(&wdev->event_lock, flags);
69 list_add_tail(&ev->list, &wdev->event_list);
70 spin_unlock_irqrestore(&wdev->event_lock, flags);
71 schedule_work(&rdev->event_work);
72}
04a773ad
JB
73EXPORT_SYMBOL(cfg80211_ibss_joined);
74
667503dd
JB
75int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
76 struct net_device *dev,
fffd0934
JB
77 struct cfg80211_ibss_params *params,
78 struct cfg80211_cached_keys *connkeys)
04a773ad
JB
79{
80 struct wireless_dev *wdev = dev->ieee80211_ptr;
81 int err;
82
667503dd
JB
83 ASSERT_WDEV_LOCK(wdev);
84
04a773ad
JB
85 if (wdev->ssid_len)
86 return -EALREADY;
87
fffd0934
JB
88 if (WARN_ON(wdev->connect_keys))
89 kfree(wdev->connect_keys);
90 wdev->connect_keys = connkeys;
91
04a773ad 92#ifdef CONFIG_WIRELESS_EXT
cbe8fa9c 93 wdev->wext.ibss.channel = params->channel;
04a773ad
JB
94#endif
95 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
fffd0934
JB
96 if (err) {
97 wdev->connect_keys = NULL;
04a773ad 98 return err;
fffd0934 99 }
04a773ad
JB
100
101 memcpy(wdev->ssid, params->ssid, params->ssid_len);
102 wdev->ssid_len = params->ssid_len;
103
104 return 0;
105}
106
667503dd
JB
107int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
108 struct net_device *dev,
fffd0934
JB
109 struct cfg80211_ibss_params *params,
110 struct cfg80211_cached_keys *connkeys)
667503dd
JB
111{
112 struct wireless_dev *wdev = dev->ieee80211_ptr;
113 int err;
114
115 wdev_lock(wdev);
fffd0934 116 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
667503dd
JB
117 wdev_unlock(wdev);
118
119 return err;
120}
121
122static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
04a773ad
JB
123{
124 struct wireless_dev *wdev = dev->ieee80211_ptr;
fffd0934
JB
125 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
126 int i;
04a773ad 127
667503dd
JB
128 ASSERT_WDEV_LOCK(wdev);
129
fffd0934
JB
130 kfree(wdev->connect_keys);
131 wdev->connect_keys = NULL;
132
133 /*
134 * Delete all the keys ... pairwise keys can't really
135 * exist any more anyway, but default keys might.
136 */
137 if (rdev->ops->del_key)
138 for (i = 0; i < 6; i++)
139 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
140
04a773ad
JB
141 if (wdev->current_bss) {
142 cfg80211_unhold_bss(wdev->current_bss);
19957bb3 143 cfg80211_put_bss(&wdev->current_bss->pub);
04a773ad
JB
144 }
145
146 wdev->current_bss = NULL;
147 wdev->ssid_len = 0;
9d308429
JB
148#ifdef CONFIG_WIRELESS_EXT
149 if (!nowext)
cbe8fa9c 150 wdev->wext.ibss.ssid_len = 0;
9d308429 151#endif
04a773ad
JB
152}
153
667503dd
JB
154void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
155{
156 struct wireless_dev *wdev = dev->ieee80211_ptr;
157
158 wdev_lock(wdev);
159 __cfg80211_clear_ibss(dev, nowext);
160 wdev_unlock(wdev);
161}
162
163static int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
164 struct net_device *dev, bool nowext)
04a773ad 165{
78485475 166 struct wireless_dev *wdev = dev->ieee80211_ptr;
04a773ad
JB
167 int err;
168
667503dd
JB
169 ASSERT_WDEV_LOCK(wdev);
170
78485475
JB
171 if (!wdev->ssid_len)
172 return -ENOLINK;
173
04a773ad
JB
174 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
175
176 if (err)
177 return err;
178
667503dd 179 __cfg80211_clear_ibss(dev, nowext);
04a773ad
JB
180
181 return 0;
182}
183
667503dd
JB
184int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
185 struct net_device *dev, bool nowext)
186{
187 struct wireless_dev *wdev = dev->ieee80211_ptr;
188 int err;
189
190 wdev_lock(wdev);
191 err = __cfg80211_leave_ibss(rdev, dev, nowext);
192 wdev_unlock(wdev);
193
194 return err;
195}
196
04a773ad 197#ifdef CONFIG_WIRELESS_EXT
fffd0934
JB
198int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
199 struct wireless_dev *wdev)
04a773ad 200{
fffd0934 201 struct cfg80211_cached_keys *ck = NULL;
04a773ad 202 enum ieee80211_band band;
fffd0934
JB
203 int i, err;
204
205 ASSERT_WDEV_LOCK(wdev);
04a773ad 206
cbe8fa9c
JB
207 if (!wdev->wext.ibss.beacon_interval)
208 wdev->wext.ibss.beacon_interval = 100;
8e30bc55 209
04a773ad 210 /* try to find an IBSS channel if none requested ... */
cbe8fa9c 211 if (!wdev->wext.ibss.channel) {
04a773ad
JB
212 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
213 struct ieee80211_supported_band *sband;
214 struct ieee80211_channel *chan;
215
216 sband = rdev->wiphy.bands[band];
217 if (!sband)
218 continue;
219
220 for (i = 0; i < sband->n_channels; i++) {
221 chan = &sband->channels[i];
222 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
223 continue;
224 if (chan->flags & IEEE80211_CHAN_DISABLED)
225 continue;
cbe8fa9c 226 wdev->wext.ibss.channel = chan;
04a773ad
JB
227 break;
228 }
229
cbe8fa9c 230 if (wdev->wext.ibss.channel)
04a773ad
JB
231 break;
232 }
233
cbe8fa9c 234 if (!wdev->wext.ibss.channel)
04a773ad
JB
235 return -EINVAL;
236 }
237
238 /* don't join -- SSID is not there */
cbe8fa9c 239 if (!wdev->wext.ibss.ssid_len)
04a773ad
JB
240 return 0;
241
242 if (!netif_running(wdev->netdev))
243 return 0;
244
fffd0934
JB
245 if (wdev->wext.keys)
246 wdev->wext.keys->def = wdev->wext.default_key;
247
248 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
249
250 if (wdev->wext.keys) {
251 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
252 if (!ck)
253 return -ENOMEM;
254 for (i = 0; i < 6; i++)
255 ck->params[i].key = ck->data[i];
256 }
257 err = __cfg80211_join_ibss(rdev, wdev->netdev,
258 &wdev->wext.ibss, ck);
259 if (err)
260 kfree(ck);
261
262 return err;
04a773ad
JB
263}
264
265int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
266 struct iw_request_info *info,
267 struct iw_freq *freq, char *extra)
268{
269 struct wireless_dev *wdev = dev->ieee80211_ptr;
270 struct ieee80211_channel *chan;
271 int err;
272
273 /* call only for ibss! */
274 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
275 return -EINVAL;
276
277 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
278 return -EOPNOTSUPP;
279
280 chan = cfg80211_wext_freq(wdev->wiphy, freq);
281 if (chan && IS_ERR(chan))
282 return PTR_ERR(chan);
283
284 if (chan &&
285 (chan->flags & IEEE80211_CHAN_NO_IBSS ||
286 chan->flags & IEEE80211_CHAN_DISABLED))
287 return -EINVAL;
288
cbe8fa9c 289 if (wdev->wext.ibss.channel == chan)
04a773ad
JB
290 return 0;
291
667503dd
JB
292 wdev_lock(wdev);
293 err = 0;
294 if (wdev->ssid_len)
295 err = __cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
296 dev, true);
297 wdev_unlock(wdev);
298
299 if (err)
300 return err;
04a773ad
JB
301
302 if (chan) {
cbe8fa9c
JB
303 wdev->wext.ibss.channel = chan;
304 wdev->wext.ibss.channel_fixed = true;
04a773ad
JB
305 } else {
306 /* cfg80211_ibss_wext_join will pick one if needed */
cbe8fa9c 307 wdev->wext.ibss.channel_fixed = false;
04a773ad
JB
308 }
309
fffd0934
JB
310 wdev_lock(wdev);
311 err = cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
312 wdev_unlock(wdev);
313
314 return err;
04a773ad 315}
04a773ad
JB
316
317int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
318 struct iw_request_info *info,
319 struct iw_freq *freq, char *extra)
320{
321 struct wireless_dev *wdev = dev->ieee80211_ptr;
322 struct ieee80211_channel *chan = NULL;
323
324 /* call only for ibss! */
325 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
326 return -EINVAL;
327
667503dd 328 wdev_lock(wdev);
04a773ad 329 if (wdev->current_bss)
19957bb3 330 chan = wdev->current_bss->pub.channel;
cbe8fa9c
JB
331 else if (wdev->wext.ibss.channel)
332 chan = wdev->wext.ibss.channel;
667503dd 333 wdev_unlock(wdev);
04a773ad
JB
334
335 if (chan) {
336 freq->m = chan->center_freq;
337 freq->e = 6;
338 return 0;
339 }
340
341 /* no channel if not joining */
342 return -EINVAL;
343}
04a773ad
JB
344
345int cfg80211_ibss_wext_siwessid(struct net_device *dev,
346 struct iw_request_info *info,
347 struct iw_point *data, char *ssid)
348{
349 struct wireless_dev *wdev = dev->ieee80211_ptr;
350 size_t len = data->length;
351 int err;
352
353 /* call only for ibss! */
354 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
355 return -EINVAL;
356
357 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
358 return -EOPNOTSUPP;
359
667503dd
JB
360 wdev_lock(wdev);
361 err = 0;
362 if (wdev->ssid_len)
363 err = __cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
364 dev, true);
365 wdev_unlock(wdev);
366
367 if (err)
368 return err;
04a773ad
JB
369
370 /* iwconfig uses nul termination in SSID.. */
371 if (len > 0 && ssid[len - 1] == '\0')
372 len--;
373
cbe8fa9c
JB
374 wdev->wext.ibss.ssid = wdev->ssid;
375 memcpy(wdev->wext.ibss.ssid, ssid, len);
376 wdev->wext.ibss.ssid_len = len;
04a773ad 377
fffd0934
JB
378 wdev_lock(wdev);
379 err = cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
380 wdev_unlock(wdev);
381
382 return err;
04a773ad 383}
04a773ad
JB
384
385int cfg80211_ibss_wext_giwessid(struct net_device *dev,
386 struct iw_request_info *info,
387 struct iw_point *data, char *ssid)
388{
389 struct wireless_dev *wdev = dev->ieee80211_ptr;
390
391 /* call only for ibss! */
392 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
393 return -EINVAL;
394
395 data->flags = 0;
396
667503dd 397 wdev_lock(wdev);
04a773ad
JB
398 if (wdev->ssid_len) {
399 data->flags = 1;
400 data->length = wdev->ssid_len;
401 memcpy(ssid, wdev->ssid, data->length);
cbe8fa9c 402 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
04a773ad 403 data->flags = 1;
cbe8fa9c
JB
404 data->length = wdev->wext.ibss.ssid_len;
405 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
04a773ad 406 }
667503dd 407 wdev_unlock(wdev);
04a773ad
JB
408
409 return 0;
410}
04a773ad
JB
411
412int cfg80211_ibss_wext_siwap(struct net_device *dev,
413 struct iw_request_info *info,
414 struct sockaddr *ap_addr, char *extra)
415{
416 struct wireless_dev *wdev = dev->ieee80211_ptr;
417 u8 *bssid = ap_addr->sa_data;
418 int err;
419
420 /* call only for ibss! */
421 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
422 return -EINVAL;
423
424 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
425 return -EOPNOTSUPP;
426
427 if (ap_addr->sa_family != ARPHRD_ETHER)
428 return -EINVAL;
429
430 /* automatic mode */
431 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
432 bssid = NULL;
433
434 /* both automatic */
cbe8fa9c 435 if (!bssid && !wdev->wext.ibss.bssid)
04a773ad
JB
436 return 0;
437
438 /* fixed already - and no change */
cbe8fa9c
JB
439 if (wdev->wext.ibss.bssid && bssid &&
440 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
04a773ad
JB
441 return 0;
442
667503dd
JB
443 wdev_lock(wdev);
444 err = 0;
445 if (wdev->ssid_len)
446 err = __cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
447 dev, true);
448 wdev_unlock(wdev);
449
450 if (err)
451 return err;
04a773ad
JB
452
453 if (bssid) {
cbe8fa9c
JB
454 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
455 wdev->wext.ibss.bssid = wdev->wext.bssid;
04a773ad 456 } else
cbe8fa9c 457 wdev->wext.ibss.bssid = NULL;
04a773ad 458
fffd0934
JB
459 wdev_lock(wdev);
460 err = cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
461 wdev_unlock(wdev);
462
463 return err;
04a773ad 464}
04a773ad
JB
465
466int cfg80211_ibss_wext_giwap(struct net_device *dev,
467 struct iw_request_info *info,
468 struct sockaddr *ap_addr, char *extra)
469{
470 struct wireless_dev *wdev = dev->ieee80211_ptr;
471
472 /* call only for ibss! */
473 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
474 return -EINVAL;
475
476 ap_addr->sa_family = ARPHRD_ETHER;
477
667503dd 478 wdev_lock(wdev);
7ebbe6bd 479 if (wdev->current_bss)
19957bb3 480 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
80e5b06a 481 else if (wdev->wext.ibss.bssid)
cbe8fa9c 482 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
80e5b06a
ZY
483 else
484 memset(ap_addr->sa_data, 0, ETH_ALEN);
485
667503dd
JB
486 wdev_unlock(wdev);
487
04a773ad
JB
488 return 0;
489}
04a773ad 490#endif