Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / net / wireless / ibss.c
blob39bff7d367687fbdd4d220d76e34d66e4e586a9c
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"
14 #include "rdev-ops.h"
17 void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
19 struct wireless_dev *wdev = dev->ieee80211_ptr;
20 struct cfg80211_bss *bss;
21 #ifdef CONFIG_CFG80211_WEXT
22 union iwreq_data wrqu;
23 #endif
25 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
26 return;
28 if (!wdev->ssid_len)
29 return;
31 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
32 wdev->ssid, wdev->ssid_len,
33 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
35 if (WARN_ON(!bss))
36 return;
38 if (wdev->current_bss) {
39 cfg80211_unhold_bss(wdev->current_bss);
40 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
43 cfg80211_hold_bss(bss_from_pub(bss));
44 wdev->current_bss = bss_from_pub(bss);
46 cfg80211_upload_connect_keys(wdev);
48 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
49 GFP_KERNEL);
50 #ifdef CONFIG_CFG80211_WEXT
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
57 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
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;
64 trace_cfg80211_ibss_joined(dev, bssid);
66 ev = kzalloc(sizeof(*ev), gfp);
67 if (!ev)
68 return;
70 ev->type = EVENT_IBSS_JOINED;
71 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
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);
76 queue_work(cfg80211_wq, &rdev->event_work);
78 EXPORT_SYMBOL(cfg80211_ibss_joined);
80 int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
81 struct net_device *dev,
82 struct cfg80211_ibss_params *params,
83 struct cfg80211_cached_keys *connkeys)
85 struct wireless_dev *wdev = dev->ieee80211_ptr;
86 int err;
88 ASSERT_WDEV_LOCK(wdev);
90 if (wdev->ssid_len)
91 return -EALREADY;
93 if (!params->basic_rates) {
95 * If no rates were explicitly configured,
96 * use the mandatory rate set for 11b or
97 * 11a for maximum compatibility.
99 struct ieee80211_supported_band *sband =
100 rdev->wiphy.bands[params->chandef.chan->band];
101 int j;
102 u32 flag = params->chandef.chan->band == IEEE80211_BAND_5GHZ ?
103 IEEE80211_RATE_MANDATORY_A :
104 IEEE80211_RATE_MANDATORY_B;
106 for (j = 0; j < sband->n_bitrates; j++) {
107 if (sband->bitrates[j].flags & flag)
108 params->basic_rates |= BIT(j);
112 if (WARN_ON(wdev->connect_keys))
113 kfree(wdev->connect_keys);
114 wdev->connect_keys = connkeys;
116 wdev->ibss_fixed = params->channel_fixed;
117 #ifdef CONFIG_CFG80211_WEXT
118 wdev->wext.ibss.chandef = params->chandef;
119 #endif
121 err = cfg80211_can_use_chan(rdev, wdev, params->chandef.chan,
122 params->channel_fixed
123 ? CHAN_MODE_SHARED
124 : CHAN_MODE_EXCLUSIVE);
125 if (err) {
126 wdev->connect_keys = NULL;
127 return err;
130 err = rdev_join_ibss(rdev, dev, params);
131 if (err) {
132 wdev->connect_keys = NULL;
133 return err;
136 memcpy(wdev->ssid, params->ssid, params->ssid_len);
137 wdev->ssid_len = params->ssid_len;
139 return 0;
142 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
143 struct net_device *dev,
144 struct cfg80211_ibss_params *params,
145 struct cfg80211_cached_keys *connkeys)
147 struct wireless_dev *wdev = dev->ieee80211_ptr;
148 int err;
150 ASSERT_RTNL();
152 wdev_lock(wdev);
153 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
154 wdev_unlock(wdev);
156 return err;
159 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
161 struct wireless_dev *wdev = dev->ieee80211_ptr;
162 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
163 int i;
165 ASSERT_WDEV_LOCK(wdev);
167 kfree(wdev->connect_keys);
168 wdev->connect_keys = NULL;
171 * Delete all the keys ... pairwise keys can't really
172 * exist any more anyway, but default keys might.
174 if (rdev->ops->del_key)
175 for (i = 0; i < 6; i++)
176 rdev_del_key(rdev, dev, i, false, NULL);
178 if (wdev->current_bss) {
179 cfg80211_unhold_bss(wdev->current_bss);
180 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
183 wdev->current_bss = NULL;
184 wdev->ssid_len = 0;
185 #ifdef CONFIG_CFG80211_WEXT
186 if (!nowext)
187 wdev->wext.ibss.ssid_len = 0;
188 #endif
191 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
193 struct wireless_dev *wdev = dev->ieee80211_ptr;
195 wdev_lock(wdev);
196 __cfg80211_clear_ibss(dev, nowext);
197 wdev_unlock(wdev);
200 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
201 struct net_device *dev, bool nowext)
203 struct wireless_dev *wdev = dev->ieee80211_ptr;
204 int err;
206 ASSERT_WDEV_LOCK(wdev);
208 if (!wdev->ssid_len)
209 return -ENOLINK;
211 err = rdev_leave_ibss(rdev, dev);
213 if (err)
214 return err;
216 __cfg80211_clear_ibss(dev, nowext);
218 return 0;
221 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
222 struct net_device *dev, bool nowext)
224 struct wireless_dev *wdev = dev->ieee80211_ptr;
225 int err;
227 wdev_lock(wdev);
228 err = __cfg80211_leave_ibss(rdev, dev, nowext);
229 wdev_unlock(wdev);
231 return err;
234 #ifdef CONFIG_CFG80211_WEXT
235 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
236 struct wireless_dev *wdev)
238 struct cfg80211_cached_keys *ck = NULL;
239 enum ieee80211_band band;
240 int i, err;
242 ASSERT_WDEV_LOCK(wdev);
244 if (!wdev->wext.ibss.beacon_interval)
245 wdev->wext.ibss.beacon_interval = 100;
247 /* try to find an IBSS channel if none requested ... */
248 if (!wdev->wext.ibss.chandef.chan) {
249 wdev->wext.ibss.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
251 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
252 struct ieee80211_supported_band *sband;
253 struct ieee80211_channel *chan;
255 sband = rdev->wiphy.bands[band];
256 if (!sband)
257 continue;
259 for (i = 0; i < sband->n_channels; i++) {
260 chan = &sband->channels[i];
261 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
262 continue;
263 if (chan->flags & IEEE80211_CHAN_DISABLED)
264 continue;
265 wdev->wext.ibss.chandef.chan = chan;
266 break;
269 if (wdev->wext.ibss.chandef.chan)
270 break;
273 if (!wdev->wext.ibss.chandef.chan)
274 return -EINVAL;
277 /* don't join -- SSID is not there */
278 if (!wdev->wext.ibss.ssid_len)
279 return 0;
281 if (!netif_running(wdev->netdev))
282 return 0;
284 if (wdev->wext.keys) {
285 wdev->wext.keys->def = wdev->wext.default_key;
286 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
289 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
291 if (wdev->wext.keys) {
292 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
293 if (!ck)
294 return -ENOMEM;
295 for (i = 0; i < 6; i++)
296 ck->params[i].key = ck->data[i];
298 err = __cfg80211_join_ibss(rdev, wdev->netdev,
299 &wdev->wext.ibss, ck);
300 if (err)
301 kfree(ck);
303 return err;
306 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
307 struct iw_request_info *info,
308 struct iw_freq *wextfreq, char *extra)
310 struct wireless_dev *wdev = dev->ieee80211_ptr;
311 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
312 struct ieee80211_channel *chan = NULL;
313 int err, freq;
315 /* call only for ibss! */
316 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
317 return -EINVAL;
319 if (!rdev->ops->join_ibss)
320 return -EOPNOTSUPP;
322 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
323 if (freq < 0)
324 return freq;
326 if (freq) {
327 chan = ieee80211_get_channel(wdev->wiphy, freq);
328 if (!chan)
329 return -EINVAL;
330 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
331 chan->flags & IEEE80211_CHAN_DISABLED)
332 return -EINVAL;
335 if (wdev->wext.ibss.chandef.chan == chan)
336 return 0;
338 wdev_lock(wdev);
339 err = 0;
340 if (wdev->ssid_len)
341 err = __cfg80211_leave_ibss(rdev, dev, true);
342 wdev_unlock(wdev);
344 if (err)
345 return err;
347 if (chan) {
348 wdev->wext.ibss.chandef.chan = chan;
349 wdev->wext.ibss.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
350 wdev->wext.ibss.channel_fixed = true;
351 } else {
352 /* cfg80211_ibss_wext_join will pick one if needed */
353 wdev->wext.ibss.channel_fixed = false;
356 wdev_lock(wdev);
357 err = cfg80211_ibss_wext_join(rdev, wdev);
358 wdev_unlock(wdev);
360 return err;
363 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
364 struct iw_request_info *info,
365 struct iw_freq *freq, char *extra)
367 struct wireless_dev *wdev = dev->ieee80211_ptr;
368 struct ieee80211_channel *chan = NULL;
370 /* call only for ibss! */
371 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
372 return -EINVAL;
374 wdev_lock(wdev);
375 if (wdev->current_bss)
376 chan = wdev->current_bss->pub.channel;
377 else if (wdev->wext.ibss.chandef.chan)
378 chan = wdev->wext.ibss.chandef.chan;
379 wdev_unlock(wdev);
381 if (chan) {
382 freq->m = chan->center_freq;
383 freq->e = 6;
384 return 0;
387 /* no channel if not joining */
388 return -EINVAL;
391 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
392 struct iw_request_info *info,
393 struct iw_point *data, char *ssid)
395 struct wireless_dev *wdev = dev->ieee80211_ptr;
396 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
397 size_t len = data->length;
398 int err;
400 /* call only for ibss! */
401 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
402 return -EINVAL;
404 if (!rdev->ops->join_ibss)
405 return -EOPNOTSUPP;
407 wdev_lock(wdev);
408 err = 0;
409 if (wdev->ssid_len)
410 err = __cfg80211_leave_ibss(rdev, dev, true);
411 wdev_unlock(wdev);
413 if (err)
414 return err;
416 /* iwconfig uses nul termination in SSID.. */
417 if (len > 0 && ssid[len - 1] == '\0')
418 len--;
420 wdev->wext.ibss.ssid = wdev->ssid;
421 memcpy(wdev->wext.ibss.ssid, ssid, len);
422 wdev->wext.ibss.ssid_len = len;
424 wdev_lock(wdev);
425 err = cfg80211_ibss_wext_join(rdev, wdev);
426 wdev_unlock(wdev);
428 return err;
431 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
432 struct iw_request_info *info,
433 struct iw_point *data, char *ssid)
435 struct wireless_dev *wdev = dev->ieee80211_ptr;
437 /* call only for ibss! */
438 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
439 return -EINVAL;
441 data->flags = 0;
443 wdev_lock(wdev);
444 if (wdev->ssid_len) {
445 data->flags = 1;
446 data->length = wdev->ssid_len;
447 memcpy(ssid, wdev->ssid, data->length);
448 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
449 data->flags = 1;
450 data->length = wdev->wext.ibss.ssid_len;
451 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
453 wdev_unlock(wdev);
455 return 0;
458 int cfg80211_ibss_wext_siwap(struct net_device *dev,
459 struct iw_request_info *info,
460 struct sockaddr *ap_addr, char *extra)
462 struct wireless_dev *wdev = dev->ieee80211_ptr;
463 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
464 u8 *bssid = ap_addr->sa_data;
465 int err;
467 /* call only for ibss! */
468 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
469 return -EINVAL;
471 if (!rdev->ops->join_ibss)
472 return -EOPNOTSUPP;
474 if (ap_addr->sa_family != ARPHRD_ETHER)
475 return -EINVAL;
477 /* automatic mode */
478 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
479 bssid = NULL;
481 /* both automatic */
482 if (!bssid && !wdev->wext.ibss.bssid)
483 return 0;
485 /* fixed already - and no change */
486 if (wdev->wext.ibss.bssid && bssid &&
487 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
488 return 0;
490 wdev_lock(wdev);
491 err = 0;
492 if (wdev->ssid_len)
493 err = __cfg80211_leave_ibss(rdev, dev, true);
494 wdev_unlock(wdev);
496 if (err)
497 return err;
499 if (bssid) {
500 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
501 wdev->wext.ibss.bssid = wdev->wext.bssid;
502 } else
503 wdev->wext.ibss.bssid = NULL;
505 wdev_lock(wdev);
506 err = cfg80211_ibss_wext_join(rdev, wdev);
507 wdev_unlock(wdev);
509 return err;
512 int cfg80211_ibss_wext_giwap(struct net_device *dev,
513 struct iw_request_info *info,
514 struct sockaddr *ap_addr, char *extra)
516 struct wireless_dev *wdev = dev->ieee80211_ptr;
518 /* call only for ibss! */
519 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
520 return -EINVAL;
522 ap_addr->sa_family = ARPHRD_ETHER;
524 wdev_lock(wdev);
525 if (wdev->current_bss)
526 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
527 else if (wdev->wext.ibss.bssid)
528 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
529 else
530 memset(ap_addr->sa_data, 0, ETH_ALEN);
532 wdev_unlock(wdev);
534 return 0;
536 #endif