nl80211/cfg80211: enable DFS for IBSS mode
[deliverable/linux.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>
5a0e3ad6 9#include <linux/slab.h>
bc3b2d7f 10#include <linux/export.h>
04a773ad 11#include <net/cfg80211.h>
0e82ffe3 12#include "wext-compat.h"
04a773ad 13#include "nl80211.h"
e35e4d28 14#include "rdev-ops.h"
04a773ad
JB
15
16
667503dd 17void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
04a773ad
JB
18{
19 struct wireless_dev *wdev = dev->ieee80211_ptr;
20 struct cfg80211_bss *bss;
3d23e349 21#ifdef CONFIG_CFG80211_WEXT
04a773ad
JB
22 union iwreq_data wrqu;
23#endif
24
25 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
26 return;
27
f7969969 28 if (!wdev->ssid_len)
04a773ad
JB
29 return;
30
04a773ad
JB
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);
5b112d3d 40 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
04a773ad
JB
41 }
42
19957bb3
JB
43 cfg80211_hold_bss(bss_from_pub(bss));
44 wdev->current_bss = bss_from_pub(bss);
04a773ad 45
fffd0934
JB
46 cfg80211_upload_connect_keys(wdev);
47
667503dd
JB
48 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
49 GFP_KERNEL);
3d23e349 50#ifdef CONFIG_CFG80211_WEXT
04a773ad
JB
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}
667503dd
JB
56
57void 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
4ee3e063
BL
64 trace_cfg80211_ibss_joined(dev, bssid);
65
667503dd
JB
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);
e60d7443 76 queue_work(cfg80211_wq, &rdev->event_work);
667503dd 77}
04a773ad
JB
78EXPORT_SYMBOL(cfg80211_ibss_joined);
79
667503dd
JB
80int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
81 struct net_device *dev,
fffd0934
JB
82 struct cfg80211_ibss_params *params,
83 struct cfg80211_cached_keys *connkeys)
04a773ad
JB
84{
85 struct wireless_dev *wdev = dev->ieee80211_ptr;
5336fa88
SW
86 struct ieee80211_channel *check_chan;
87 u8 radar_detect_width = 0;
04a773ad
JB
88 int err;
89
667503dd
JB
90 ASSERT_WDEV_LOCK(wdev);
91
04a773ad
JB
92 if (wdev->ssid_len)
93 return -EALREADY;
94
93b05238
JB
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 =
683b6d3b 102 rdev->wiphy.bands[params->chandef.chan->band];
93b05238 103 int j;
683b6d3b 104 u32 flag = params->chandef.chan->band == IEEE80211_BAND_5GHZ ?
93b05238
JB
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
fffd0934
JB
114 if (WARN_ON(wdev->connect_keys))
115 kfree(wdev->connect_keys);
116 wdev->connect_keys = connkeys;
117
c30a3d38 118 wdev->ibss_fixed = params->channel_fixed;
5336fa88 119 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
3d23e349 120#ifdef CONFIG_CFG80211_WEXT
683b6d3b 121 wdev->wext.ibss.chandef = params->chandef;
04a773ad 122#endif
5336fa88
SW
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);
e4e32459 140
e4e32459
MK
141 if (err) {
142 wdev->connect_keys = NULL;
143 return err;
144 }
145
e35e4d28 146 err = rdev_join_ibss(rdev, dev, params);
fffd0934
JB
147 if (err) {
148 wdev->connect_keys = NULL;
04a773ad 149 return err;
fffd0934 150 }
04a773ad
JB
151
152 memcpy(wdev->ssid, params->ssid, params->ssid_len);
153 wdev->ssid_len = params->ssid_len;
154
155 return 0;
156}
157
667503dd
JB
158int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
159 struct net_device *dev,
fffd0934
JB
160 struct cfg80211_ibss_params *params,
161 struct cfg80211_cached_keys *connkeys)
667503dd
JB
162{
163 struct wireless_dev *wdev = dev->ieee80211_ptr;
164 int err;
165
5fe231e8
JB
166 ASSERT_RTNL();
167
667503dd 168 wdev_lock(wdev);
fffd0934 169 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
667503dd
JB
170 wdev_unlock(wdev);
171
172 return err;
173}
174
175static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
04a773ad
JB
176{
177 struct wireless_dev *wdev = dev->ieee80211_ptr;
fffd0934
JB
178 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
179 int i;
04a773ad 180
667503dd
JB
181 ASSERT_WDEV_LOCK(wdev);
182
fffd0934
JB
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++)
e35e4d28 192 rdev_del_key(rdev, dev, i, false, NULL);
fffd0934 193
04a773ad
JB
194 if (wdev->current_bss) {
195 cfg80211_unhold_bss(wdev->current_bss);
5b112d3d 196 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
04a773ad
JB
197 }
198
199 wdev->current_bss = NULL;
200 wdev->ssid_len = 0;
3d23e349 201#ifdef CONFIG_CFG80211_WEXT
9d308429 202 if (!nowext)
cbe8fa9c 203 wdev->wext.ibss.ssid_len = 0;
9d308429 204#endif
04a773ad
JB
205}
206
667503dd
JB
207void 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
98d3a7ca
JB
216int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
217 struct net_device *dev, bool nowext)
04a773ad 218{
78485475 219 struct wireless_dev *wdev = dev->ieee80211_ptr;
04a773ad
JB
220 int err;
221
667503dd
JB
222 ASSERT_WDEV_LOCK(wdev);
223
78485475
JB
224 if (!wdev->ssid_len)
225 return -ENOLINK;
226
e35e4d28 227 err = rdev_leave_ibss(rdev, dev);
04a773ad
JB
228
229 if (err)
230 return err;
231
667503dd 232 __cfg80211_clear_ibss(dev, nowext);
04a773ad
JB
233
234 return 0;
235}
236
667503dd
JB
237int 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
3d23e349 250#ifdef CONFIG_CFG80211_WEXT
fffd0934
JB
251int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
252 struct wireless_dev *wdev)
04a773ad 253{
fffd0934 254 struct cfg80211_cached_keys *ck = NULL;
04a773ad 255 enum ieee80211_band band;
fffd0934
JB
256 int i, err;
257
258 ASSERT_WDEV_LOCK(wdev);
04a773ad 259
cbe8fa9c
JB
260 if (!wdev->wext.ibss.beacon_interval)
261 wdev->wext.ibss.beacon_interval = 100;
8e30bc55 262
04a773ad 263 /* try to find an IBSS channel if none requested ... */
683b6d3b 264 if (!wdev->wext.ibss.chandef.chan) {
3d9d1d66 265 wdev->wext.ibss.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
683b6d3b 266
04a773ad
JB
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;
683b6d3b 281 wdev->wext.ibss.chandef.chan = chan;
04a773ad
JB
282 break;
283 }
284
683b6d3b 285 if (wdev->wext.ibss.chandef.chan)
04a773ad
JB
286 break;
287 }
288
683b6d3b 289 if (!wdev->wext.ibss.chandef.chan)
04a773ad
JB
290 return -EINVAL;
291 }
292
293 /* don't join -- SSID is not there */
cbe8fa9c 294 if (!wdev->wext.ibss.ssid_len)
04a773ad
JB
295 return 0;
296
297 if (!netif_running(wdev->netdev))
298 return 0;
299
3be61a38 300 if (wdev->wext.keys) {
fffd0934 301 wdev->wext.keys->def = wdev->wext.default_key;
3be61a38
JB
302 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
303 }
fffd0934
JB
304
305 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
306
307 if (wdev->wext.keys) {
308 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
309 if (!ck)
310 return -ENOMEM;
311 for (i = 0; i < 6; i++)
312 ck->params[i].key = ck->data[i];
313 }
314 err = __cfg80211_join_ibss(rdev, wdev->netdev,
315 &wdev->wext.ibss, ck);
316 if (err)
317 kfree(ck);
318
319 return err;
04a773ad
JB
320}
321
322int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
323 struct iw_request_info *info,
59bbb6f7 324 struct iw_freq *wextfreq, char *extra)
04a773ad
JB
325{
326 struct wireless_dev *wdev = dev->ieee80211_ptr;
59bbb6f7
JB
327 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
328 struct ieee80211_channel *chan = NULL;
329 int err, freq;
04a773ad
JB
330
331 /* call only for ibss! */
332 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
333 return -EINVAL;
334
59bbb6f7 335 if (!rdev->ops->join_ibss)
04a773ad
JB
336 return -EOPNOTSUPP;
337
59bbb6f7
JB
338 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
339 if (freq < 0)
340 return freq;
04a773ad 341
59bbb6f7
JB
342 if (freq) {
343 chan = ieee80211_get_channel(wdev->wiphy, freq);
344 if (!chan)
345 return -EINVAL;
346 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
347 chan->flags & IEEE80211_CHAN_DISABLED)
348 return -EINVAL;
349 }
04a773ad 350
683b6d3b 351 if (wdev->wext.ibss.chandef.chan == chan)
04a773ad
JB
352 return 0;
353
667503dd
JB
354 wdev_lock(wdev);
355 err = 0;
356 if (wdev->ssid_len)
59bbb6f7 357 err = __cfg80211_leave_ibss(rdev, dev, true);
667503dd
JB
358 wdev_unlock(wdev);
359
360 if (err)
361 return err;
04a773ad
JB
362
363 if (chan) {
683b6d3b 364 wdev->wext.ibss.chandef.chan = chan;
3d9d1d66 365 wdev->wext.ibss.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
cbe8fa9c 366 wdev->wext.ibss.channel_fixed = true;
04a773ad
JB
367 } else {
368 /* cfg80211_ibss_wext_join will pick one if needed */
cbe8fa9c 369 wdev->wext.ibss.channel_fixed = false;
04a773ad
JB
370 }
371
fffd0934 372 wdev_lock(wdev);
59bbb6f7 373 err = cfg80211_ibss_wext_join(rdev, wdev);
fffd0934
JB
374 wdev_unlock(wdev);
375
376 return err;
04a773ad 377}
04a773ad
JB
378
379int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
380 struct iw_request_info *info,
381 struct iw_freq *freq, char *extra)
382{
383 struct wireless_dev *wdev = dev->ieee80211_ptr;
384 struct ieee80211_channel *chan = NULL;
385
386 /* call only for ibss! */
387 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
388 return -EINVAL;
389
667503dd 390 wdev_lock(wdev);
04a773ad 391 if (wdev->current_bss)
19957bb3 392 chan = wdev->current_bss->pub.channel;
683b6d3b
JB
393 else if (wdev->wext.ibss.chandef.chan)
394 chan = wdev->wext.ibss.chandef.chan;
667503dd 395 wdev_unlock(wdev);
04a773ad
JB
396
397 if (chan) {
398 freq->m = chan->center_freq;
399 freq->e = 6;
400 return 0;
401 }
402
403 /* no channel if not joining */
404 return -EINVAL;
405}
04a773ad
JB
406
407int cfg80211_ibss_wext_siwessid(struct net_device *dev,
408 struct iw_request_info *info,
409 struct iw_point *data, char *ssid)
410{
411 struct wireless_dev *wdev = dev->ieee80211_ptr;
59bbb6f7 412 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
04a773ad
JB
413 size_t len = data->length;
414 int err;
415
416 /* call only for ibss! */
417 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
418 return -EINVAL;
419
59bbb6f7 420 if (!rdev->ops->join_ibss)
04a773ad
JB
421 return -EOPNOTSUPP;
422
667503dd
JB
423 wdev_lock(wdev);
424 err = 0;
425 if (wdev->ssid_len)
59bbb6f7 426 err = __cfg80211_leave_ibss(rdev, dev, true);
667503dd
JB
427 wdev_unlock(wdev);
428
429 if (err)
430 return err;
04a773ad
JB
431
432 /* iwconfig uses nul termination in SSID.. */
433 if (len > 0 && ssid[len - 1] == '\0')
434 len--;
435
cbe8fa9c
JB
436 wdev->wext.ibss.ssid = wdev->ssid;
437 memcpy(wdev->wext.ibss.ssid, ssid, len);
438 wdev->wext.ibss.ssid_len = len;
04a773ad 439
fffd0934 440 wdev_lock(wdev);
59bbb6f7 441 err = cfg80211_ibss_wext_join(rdev, wdev);
fffd0934
JB
442 wdev_unlock(wdev);
443
444 return err;
04a773ad 445}
04a773ad
JB
446
447int cfg80211_ibss_wext_giwessid(struct net_device *dev,
448 struct iw_request_info *info,
449 struct iw_point *data, char *ssid)
450{
451 struct wireless_dev *wdev = dev->ieee80211_ptr;
452
453 /* call only for ibss! */
454 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
455 return -EINVAL;
456
457 data->flags = 0;
458
667503dd 459 wdev_lock(wdev);
04a773ad
JB
460 if (wdev->ssid_len) {
461 data->flags = 1;
462 data->length = wdev->ssid_len;
463 memcpy(ssid, wdev->ssid, data->length);
cbe8fa9c 464 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
04a773ad 465 data->flags = 1;
cbe8fa9c
JB
466 data->length = wdev->wext.ibss.ssid_len;
467 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
04a773ad 468 }
667503dd 469 wdev_unlock(wdev);
04a773ad
JB
470
471 return 0;
472}
04a773ad
JB
473
474int cfg80211_ibss_wext_siwap(struct net_device *dev,
475 struct iw_request_info *info,
476 struct sockaddr *ap_addr, char *extra)
477{
478 struct wireless_dev *wdev = dev->ieee80211_ptr;
59bbb6f7 479 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
04a773ad
JB
480 u8 *bssid = ap_addr->sa_data;
481 int err;
482
483 /* call only for ibss! */
484 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
485 return -EINVAL;
486
59bbb6f7 487 if (!rdev->ops->join_ibss)
04a773ad
JB
488 return -EOPNOTSUPP;
489
490 if (ap_addr->sa_family != ARPHRD_ETHER)
491 return -EINVAL;
492
493 /* automatic mode */
494 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
495 bssid = NULL;
496
497 /* both automatic */
cbe8fa9c 498 if (!bssid && !wdev->wext.ibss.bssid)
04a773ad
JB
499 return 0;
500
501 /* fixed already - and no change */
cbe8fa9c 502 if (wdev->wext.ibss.bssid && bssid &&
ac422d3c 503 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
04a773ad
JB
504 return 0;
505
667503dd
JB
506 wdev_lock(wdev);
507 err = 0;
508 if (wdev->ssid_len)
59bbb6f7 509 err = __cfg80211_leave_ibss(rdev, dev, true);
667503dd
JB
510 wdev_unlock(wdev);
511
512 if (err)
513 return err;
04a773ad
JB
514
515 if (bssid) {
cbe8fa9c
JB
516 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
517 wdev->wext.ibss.bssid = wdev->wext.bssid;
04a773ad 518 } else
cbe8fa9c 519 wdev->wext.ibss.bssid = NULL;
04a773ad 520
fffd0934 521 wdev_lock(wdev);
59bbb6f7 522 err = cfg80211_ibss_wext_join(rdev, wdev);
fffd0934
JB
523 wdev_unlock(wdev);
524
525 return err;
04a773ad 526}
04a773ad
JB
527
528int cfg80211_ibss_wext_giwap(struct net_device *dev,
529 struct iw_request_info *info,
530 struct sockaddr *ap_addr, char *extra)
531{
532 struct wireless_dev *wdev = dev->ieee80211_ptr;
533
534 /* call only for ibss! */
535 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
536 return -EINVAL;
537
538 ap_addr->sa_family = ARPHRD_ETHER;
539
667503dd 540 wdev_lock(wdev);
7ebbe6bd 541 if (wdev->current_bss)
19957bb3 542 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
80e5b06a 543 else if (wdev->wext.ibss.bssid)
cbe8fa9c 544 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
80e5b06a
ZY
545 else
546 memset(ap_addr->sa_data, 0, ETH_ALEN);
547
667503dd
JB
548 wdev_unlock(wdev);
549
04a773ad
JB
550 return 0;
551}
04a773ad 552#endif
This page took 0.305574 seconds and 5 git commands to generate.