Merge branch 'for-3.2-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[zen-stable.git] / net / wireless / ibss.c
blob30f20fe4a5fe406316fb55140b35371caa390572
1 /*
2 * Some IBSS support code for cfg80211.
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
5 */
7 #include <linux/etherdevice.h>
8 #include <linux/if_arp.h>
9 #include <linux/slab.h>
10 #include <linux/export.h>
11 #include <net/cfg80211.h>
12 #include "wext-compat.h"
13 #include "nl80211.h"
16 void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
18 struct wireless_dev *wdev = dev->ieee80211_ptr;
19 struct cfg80211_bss *bss;
20 #ifdef CONFIG_CFG80211_WEXT
21 union iwreq_data wrqu;
22 #endif
24 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
25 return;
27 if (!wdev->ssid_len)
28 return;
30 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
31 wdev->ssid, wdev->ssid_len,
32 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
34 if (WARN_ON(!bss))
35 return;
37 if (wdev->current_bss) {
38 cfg80211_unhold_bss(wdev->current_bss);
39 cfg80211_put_bss(&wdev->current_bss->pub);
42 cfg80211_hold_bss(bss_from_pub(bss));
43 wdev->current_bss = bss_from_pub(bss);
45 cfg80211_upload_connect_keys(wdev);
47 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
48 GFP_KERNEL);
49 #ifdef CONFIG_CFG80211_WEXT
50 memset(&wrqu, 0, sizeof(wrqu));
51 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
52 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
53 #endif
56 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
58 struct wireless_dev *wdev = dev->ieee80211_ptr;
59 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
60 struct cfg80211_event *ev;
61 unsigned long flags;
63 CFG80211_DEV_WARN_ON(!wdev->ssid_len);
65 ev = kzalloc(sizeof(*ev), gfp);
66 if (!ev)
67 return;
69 ev->type = EVENT_IBSS_JOINED;
70 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
72 spin_lock_irqsave(&wdev->event_lock, flags);
73 list_add_tail(&ev->list, &wdev->event_list);
74 spin_unlock_irqrestore(&wdev->event_lock, flags);
75 queue_work(cfg80211_wq, &rdev->event_work);
77 EXPORT_SYMBOL(cfg80211_ibss_joined);
79 int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
80 struct net_device *dev,
81 struct cfg80211_ibss_params *params,
82 struct cfg80211_cached_keys *connkeys)
84 struct wireless_dev *wdev = dev->ieee80211_ptr;
85 int err;
87 ASSERT_WDEV_LOCK(wdev);
89 if (wdev->ssid_len)
90 return -EALREADY;
92 if (!params->basic_rates) {
94 * If no rates were explicitly configured,
95 * use the mandatory rate set for 11b or
96 * 11a for maximum compatibility.
98 struct ieee80211_supported_band *sband =
99 rdev->wiphy.bands[params->channel->band];
100 int j;
101 u32 flag = params->channel->band == IEEE80211_BAND_5GHZ ?
102 IEEE80211_RATE_MANDATORY_A :
103 IEEE80211_RATE_MANDATORY_B;
105 for (j = 0; j < sband->n_bitrates; j++) {
106 if (sband->bitrates[j].flags & flag)
107 params->basic_rates |= BIT(j);
111 if (WARN_ON(wdev->connect_keys))
112 kfree(wdev->connect_keys);
113 wdev->connect_keys = connkeys;
115 #ifdef CONFIG_CFG80211_WEXT
116 wdev->wext.ibss.channel = params->channel;
117 #endif
118 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
119 if (err) {
120 wdev->connect_keys = NULL;
121 return err;
124 memcpy(wdev->ssid, params->ssid, params->ssid_len);
125 wdev->ssid_len = params->ssid_len;
127 return 0;
130 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
131 struct net_device *dev,
132 struct cfg80211_ibss_params *params,
133 struct cfg80211_cached_keys *connkeys)
135 struct wireless_dev *wdev = dev->ieee80211_ptr;
136 int err;
138 mutex_lock(&rdev->devlist_mtx);
139 wdev_lock(wdev);
140 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
141 wdev_unlock(wdev);
142 mutex_unlock(&rdev->devlist_mtx);
144 return err;
147 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
149 struct wireless_dev *wdev = dev->ieee80211_ptr;
150 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
151 int i;
153 ASSERT_WDEV_LOCK(wdev);
155 kfree(wdev->connect_keys);
156 wdev->connect_keys = NULL;
159 * Delete all the keys ... pairwise keys can't really
160 * exist any more anyway, but default keys might.
162 if (rdev->ops->del_key)
163 for (i = 0; i < 6; i++)
164 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
166 if (wdev->current_bss) {
167 cfg80211_unhold_bss(wdev->current_bss);
168 cfg80211_put_bss(&wdev->current_bss->pub);
171 wdev->current_bss = NULL;
172 wdev->ssid_len = 0;
173 #ifdef CONFIG_CFG80211_WEXT
174 if (!nowext)
175 wdev->wext.ibss.ssid_len = 0;
176 #endif
179 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
181 struct wireless_dev *wdev = dev->ieee80211_ptr;
183 wdev_lock(wdev);
184 __cfg80211_clear_ibss(dev, nowext);
185 wdev_unlock(wdev);
188 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
189 struct net_device *dev, bool nowext)
191 struct wireless_dev *wdev = dev->ieee80211_ptr;
192 int err;
194 ASSERT_WDEV_LOCK(wdev);
196 if (!wdev->ssid_len)
197 return -ENOLINK;
199 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
201 if (err)
202 return err;
204 __cfg80211_clear_ibss(dev, nowext);
206 return 0;
209 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
210 struct net_device *dev, bool nowext)
212 struct wireless_dev *wdev = dev->ieee80211_ptr;
213 int err;
215 wdev_lock(wdev);
216 err = __cfg80211_leave_ibss(rdev, dev, nowext);
217 wdev_unlock(wdev);
219 return err;
222 #ifdef CONFIG_CFG80211_WEXT
223 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
224 struct wireless_dev *wdev)
226 struct cfg80211_cached_keys *ck = NULL;
227 enum ieee80211_band band;
228 int i, err;
230 ASSERT_WDEV_LOCK(wdev);
232 if (!wdev->wext.ibss.beacon_interval)
233 wdev->wext.ibss.beacon_interval = 100;
235 /* try to find an IBSS channel if none requested ... */
236 if (!wdev->wext.ibss.channel) {
237 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
238 struct ieee80211_supported_band *sband;
239 struct ieee80211_channel *chan;
241 sband = rdev->wiphy.bands[band];
242 if (!sband)
243 continue;
245 for (i = 0; i < sband->n_channels; i++) {
246 chan = &sband->channels[i];
247 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
248 continue;
249 if (chan->flags & IEEE80211_CHAN_DISABLED)
250 continue;
251 wdev->wext.ibss.channel = chan;
252 break;
255 if (wdev->wext.ibss.channel)
256 break;
259 if (!wdev->wext.ibss.channel)
260 return -EINVAL;
263 /* don't join -- SSID is not there */
264 if (!wdev->wext.ibss.ssid_len)
265 return 0;
267 if (!netif_running(wdev->netdev))
268 return 0;
270 if (wdev->wext.keys) {
271 wdev->wext.keys->def = wdev->wext.default_key;
272 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
275 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
277 if (wdev->wext.keys) {
278 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
279 if (!ck)
280 return -ENOMEM;
281 for (i = 0; i < 6; i++)
282 ck->params[i].key = ck->data[i];
284 err = __cfg80211_join_ibss(rdev, wdev->netdev,
285 &wdev->wext.ibss, ck);
286 if (err)
287 kfree(ck);
289 return err;
292 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
293 struct iw_request_info *info,
294 struct iw_freq *wextfreq, char *extra)
296 struct wireless_dev *wdev = dev->ieee80211_ptr;
297 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
298 struct ieee80211_channel *chan = NULL;
299 int err, freq;
301 /* call only for ibss! */
302 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
303 return -EINVAL;
305 if (!rdev->ops->join_ibss)
306 return -EOPNOTSUPP;
308 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
309 if (freq < 0)
310 return freq;
312 if (freq) {
313 chan = ieee80211_get_channel(wdev->wiphy, freq);
314 if (!chan)
315 return -EINVAL;
316 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
317 chan->flags & IEEE80211_CHAN_DISABLED)
318 return -EINVAL;
321 if (wdev->wext.ibss.channel == chan)
322 return 0;
324 wdev_lock(wdev);
325 err = 0;
326 if (wdev->ssid_len)
327 err = __cfg80211_leave_ibss(rdev, dev, true);
328 wdev_unlock(wdev);
330 if (err)
331 return err;
333 if (chan) {
334 wdev->wext.ibss.channel = chan;
335 wdev->wext.ibss.channel_fixed = true;
336 } else {
337 /* cfg80211_ibss_wext_join will pick one if needed */
338 wdev->wext.ibss.channel_fixed = false;
341 mutex_lock(&rdev->devlist_mtx);
342 wdev_lock(wdev);
343 err = cfg80211_ibss_wext_join(rdev, wdev);
344 wdev_unlock(wdev);
345 mutex_unlock(&rdev->devlist_mtx);
347 return err;
350 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
351 struct iw_request_info *info,
352 struct iw_freq *freq, char *extra)
354 struct wireless_dev *wdev = dev->ieee80211_ptr;
355 struct ieee80211_channel *chan = NULL;
357 /* call only for ibss! */
358 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
359 return -EINVAL;
361 wdev_lock(wdev);
362 if (wdev->current_bss)
363 chan = wdev->current_bss->pub.channel;
364 else if (wdev->wext.ibss.channel)
365 chan = wdev->wext.ibss.channel;
366 wdev_unlock(wdev);
368 if (chan) {
369 freq->m = chan->center_freq;
370 freq->e = 6;
371 return 0;
374 /* no channel if not joining */
375 return -EINVAL;
378 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
379 struct iw_request_info *info,
380 struct iw_point *data, char *ssid)
382 struct wireless_dev *wdev = dev->ieee80211_ptr;
383 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
384 size_t len = data->length;
385 int err;
387 /* call only for ibss! */
388 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
389 return -EINVAL;
391 if (!rdev->ops->join_ibss)
392 return -EOPNOTSUPP;
394 wdev_lock(wdev);
395 err = 0;
396 if (wdev->ssid_len)
397 err = __cfg80211_leave_ibss(rdev, dev, true);
398 wdev_unlock(wdev);
400 if (err)
401 return err;
403 /* iwconfig uses nul termination in SSID.. */
404 if (len > 0 && ssid[len - 1] == '\0')
405 len--;
407 wdev->wext.ibss.ssid = wdev->ssid;
408 memcpy(wdev->wext.ibss.ssid, ssid, len);
409 wdev->wext.ibss.ssid_len = len;
411 mutex_lock(&rdev->devlist_mtx);
412 wdev_lock(wdev);
413 err = cfg80211_ibss_wext_join(rdev, wdev);
414 wdev_unlock(wdev);
415 mutex_unlock(&rdev->devlist_mtx);
417 return err;
420 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
421 struct iw_request_info *info,
422 struct iw_point *data, char *ssid)
424 struct wireless_dev *wdev = dev->ieee80211_ptr;
426 /* call only for ibss! */
427 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
428 return -EINVAL;
430 data->flags = 0;
432 wdev_lock(wdev);
433 if (wdev->ssid_len) {
434 data->flags = 1;
435 data->length = wdev->ssid_len;
436 memcpy(ssid, wdev->ssid, data->length);
437 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
438 data->flags = 1;
439 data->length = wdev->wext.ibss.ssid_len;
440 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
442 wdev_unlock(wdev);
444 return 0;
447 int cfg80211_ibss_wext_siwap(struct net_device *dev,
448 struct iw_request_info *info,
449 struct sockaddr *ap_addr, char *extra)
451 struct wireless_dev *wdev = dev->ieee80211_ptr;
452 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
453 u8 *bssid = ap_addr->sa_data;
454 int err;
456 /* call only for ibss! */
457 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
458 return -EINVAL;
460 if (!rdev->ops->join_ibss)
461 return -EOPNOTSUPP;
463 if (ap_addr->sa_family != ARPHRD_ETHER)
464 return -EINVAL;
466 /* automatic mode */
467 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
468 bssid = NULL;
470 /* both automatic */
471 if (!bssid && !wdev->wext.ibss.bssid)
472 return 0;
474 /* fixed already - and no change */
475 if (wdev->wext.ibss.bssid && bssid &&
476 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
477 return 0;
479 wdev_lock(wdev);
480 err = 0;
481 if (wdev->ssid_len)
482 err = __cfg80211_leave_ibss(rdev, dev, true);
483 wdev_unlock(wdev);
485 if (err)
486 return err;
488 if (bssid) {
489 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
490 wdev->wext.ibss.bssid = wdev->wext.bssid;
491 } else
492 wdev->wext.ibss.bssid = NULL;
494 mutex_lock(&rdev->devlist_mtx);
495 wdev_lock(wdev);
496 err = cfg80211_ibss_wext_join(rdev, wdev);
497 wdev_unlock(wdev);
498 mutex_unlock(&rdev->devlist_mtx);
500 return err;
503 int cfg80211_ibss_wext_giwap(struct net_device *dev,
504 struct iw_request_info *info,
505 struct sockaddr *ap_addr, char *extra)
507 struct wireless_dev *wdev = dev->ieee80211_ptr;
509 /* call only for ibss! */
510 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
511 return -EINVAL;
513 ap_addr->sa_family = ARPHRD_ETHER;
515 wdev_lock(wdev);
516 if (wdev->current_bss)
517 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
518 else if (wdev->wext.ibss.bssid)
519 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
520 else
521 memset(ap_addr->sa_data, 0, ETH_ALEN);
523 wdev_unlock(wdev);
525 return 0;
527 #endif