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