Merge branches 'pm-cpufreq-fixes' and 'pm-cpuidle-fixes'
[linux/fpc-iii.git] / net / wireless / ibss.c
blob364f900a3dc4d2810c90fa3f613d1b9a08cb5e71
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,
18 struct ieee80211_channel *channel)
20 struct wireless_dev *wdev = dev->ieee80211_ptr;
21 struct cfg80211_bss *bss;
22 #ifdef CONFIG_CFG80211_WEXT
23 union iwreq_data wrqu;
24 #endif
26 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
27 return;
29 if (!wdev->ssid_len)
30 return;
32 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
33 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
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 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
47 cfg80211_upload_connect_keys(wdev);
49 nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid,
50 GFP_KERNEL);
51 #ifdef CONFIG_CFG80211_WEXT
52 memset(&wrqu, 0, sizeof(wrqu));
53 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
54 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
55 #endif
58 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
59 struct ieee80211_channel *channel, gfp_t gfp)
61 struct wireless_dev *wdev = dev->ieee80211_ptr;
62 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
63 struct cfg80211_event *ev;
64 unsigned long flags;
66 trace_cfg80211_ibss_joined(dev, bssid, channel);
68 if (WARN_ON(!channel))
69 return;
71 ev = kzalloc(sizeof(*ev), gfp);
72 if (!ev)
73 return;
75 ev->type = EVENT_IBSS_JOINED;
76 memcpy(ev->ij.bssid, bssid, ETH_ALEN);
77 ev->ij.channel = channel;
79 spin_lock_irqsave(&wdev->event_lock, flags);
80 list_add_tail(&ev->list, &wdev->event_list);
81 spin_unlock_irqrestore(&wdev->event_lock, flags);
82 queue_work(cfg80211_wq, &rdev->event_work);
84 EXPORT_SYMBOL(cfg80211_ibss_joined);
86 static int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
87 struct net_device *dev,
88 struct cfg80211_ibss_params *params,
89 struct cfg80211_cached_keys *connkeys)
91 struct wireless_dev *wdev = dev->ieee80211_ptr;
92 int err;
94 ASSERT_WDEV_LOCK(wdev);
96 if (wdev->ssid_len)
97 return -EALREADY;
99 if (!params->basic_rates) {
101 * If no rates were explicitly configured,
102 * use the mandatory rate set for 11b or
103 * 11a for maximum compatibility.
105 struct ieee80211_supported_band *sband =
106 rdev->wiphy.bands[params->chandef.chan->band];
107 int j;
108 u32 flag = params->chandef.chan->band == NL80211_BAND_5GHZ ?
109 IEEE80211_RATE_MANDATORY_A :
110 IEEE80211_RATE_MANDATORY_B;
112 for (j = 0; j < sband->n_bitrates; j++) {
113 if (sband->bitrates[j].flags & flag)
114 params->basic_rates |= BIT(j);
118 if (WARN_ON(connkeys && connkeys->def < 0))
119 return -EINVAL;
121 if (WARN_ON(wdev->connect_keys))
122 kzfree(wdev->connect_keys);
123 wdev->connect_keys = connkeys;
125 wdev->ibss_fixed = params->channel_fixed;
126 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
127 wdev->chandef = params->chandef;
128 #ifdef CONFIG_CFG80211_WEXT
129 wdev->wext.ibss.chandef = params->chandef;
130 #endif
131 err = rdev_join_ibss(rdev, dev, params);
132 if (err) {
133 wdev->connect_keys = NULL;
134 return err;
137 memcpy(wdev->ssid, params->ssid, params->ssid_len);
138 wdev->ssid_len = params->ssid_len;
140 return 0;
143 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
144 struct net_device *dev,
145 struct cfg80211_ibss_params *params,
146 struct cfg80211_cached_keys *connkeys)
148 struct wireless_dev *wdev = dev->ieee80211_ptr;
149 int err;
151 ASSERT_RTNL();
153 wdev_lock(wdev);
154 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
155 wdev_unlock(wdev);
157 return err;
160 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
162 struct wireless_dev *wdev = dev->ieee80211_ptr;
163 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
164 int i;
166 ASSERT_WDEV_LOCK(wdev);
168 kzfree(wdev->connect_keys);
169 wdev->connect_keys = NULL;
171 rdev_set_qos_map(rdev, dev, NULL);
174 * Delete all the keys ... pairwise keys can't really
175 * exist any more anyway, but default keys might.
177 if (rdev->ops->del_key)
178 for (i = 0; i < 6; i++)
179 rdev_del_key(rdev, dev, i, false, NULL);
181 if (wdev->current_bss) {
182 cfg80211_unhold_bss(wdev->current_bss);
183 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
186 wdev->current_bss = NULL;
187 wdev->ssid_len = 0;
188 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
189 #ifdef CONFIG_CFG80211_WEXT
190 if (!nowext)
191 wdev->wext.ibss.ssid_len = 0;
192 #endif
195 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
197 struct wireless_dev *wdev = dev->ieee80211_ptr;
199 wdev_lock(wdev);
200 __cfg80211_clear_ibss(dev, nowext);
201 wdev_unlock(wdev);
204 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
205 struct net_device *dev, bool nowext)
207 struct wireless_dev *wdev = dev->ieee80211_ptr;
208 int err;
210 ASSERT_WDEV_LOCK(wdev);
212 if (!wdev->ssid_len)
213 return -ENOLINK;
215 err = rdev_leave_ibss(rdev, dev);
217 if (err)
218 return err;
220 __cfg80211_clear_ibss(dev, nowext);
222 return 0;
225 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
226 struct net_device *dev, bool nowext)
228 struct wireless_dev *wdev = dev->ieee80211_ptr;
229 int err;
231 wdev_lock(wdev);
232 err = __cfg80211_leave_ibss(rdev, dev, nowext);
233 wdev_unlock(wdev);
235 return err;
238 #ifdef CONFIG_CFG80211_WEXT
239 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
240 struct wireless_dev *wdev)
242 struct cfg80211_cached_keys *ck = NULL;
243 enum nl80211_band band;
244 int i, err;
246 ASSERT_WDEV_LOCK(wdev);
248 if (!wdev->wext.ibss.beacon_interval)
249 wdev->wext.ibss.beacon_interval = 100;
251 /* try to find an IBSS channel if none requested ... */
252 if (!wdev->wext.ibss.chandef.chan) {
253 struct ieee80211_channel *new_chan = NULL;
255 for (band = 0; band < NUM_NL80211_BANDS; band++) {
256 struct ieee80211_supported_band *sband;
257 struct ieee80211_channel *chan;
259 sband = rdev->wiphy.bands[band];
260 if (!sband)
261 continue;
263 for (i = 0; i < sband->n_channels; i++) {
264 chan = &sband->channels[i];
265 if (chan->flags & IEEE80211_CHAN_NO_IR)
266 continue;
267 if (chan->flags & IEEE80211_CHAN_DISABLED)
268 continue;
269 new_chan = chan;
270 break;
273 if (new_chan)
274 break;
277 if (!new_chan)
278 return -EINVAL;
280 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
281 NL80211_CHAN_NO_HT);
284 /* don't join -- SSID is not there */
285 if (!wdev->wext.ibss.ssid_len)
286 return 0;
288 if (!netif_running(wdev->netdev))
289 return 0;
291 if (wdev->wext.keys)
292 wdev->wext.keys->def = wdev->wext.default_key;
294 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
296 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
297 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
298 if (!ck)
299 return -ENOMEM;
300 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
301 ck->params[i].key = ck->data[i];
303 err = __cfg80211_join_ibss(rdev, wdev->netdev,
304 &wdev->wext.ibss, ck);
305 if (err)
306 kfree(ck);
308 return err;
311 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
312 struct iw_request_info *info,
313 struct iw_freq *wextfreq, char *extra)
315 struct wireless_dev *wdev = dev->ieee80211_ptr;
316 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
317 struct ieee80211_channel *chan = NULL;
318 int err, freq;
320 /* call only for ibss! */
321 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
322 return -EINVAL;
324 if (!rdev->ops->join_ibss)
325 return -EOPNOTSUPP;
327 freq = cfg80211_wext_freq(wextfreq);
328 if (freq < 0)
329 return freq;
331 if (freq) {
332 chan = ieee80211_get_channel(wdev->wiphy, freq);
333 if (!chan)
334 return -EINVAL;
335 if (chan->flags & IEEE80211_CHAN_NO_IR ||
336 chan->flags & IEEE80211_CHAN_DISABLED)
337 return -EINVAL;
340 if (wdev->wext.ibss.chandef.chan == chan)
341 return 0;
343 wdev_lock(wdev);
344 err = 0;
345 if (wdev->ssid_len)
346 err = __cfg80211_leave_ibss(rdev, dev, true);
347 wdev_unlock(wdev);
349 if (err)
350 return err;
352 if (chan) {
353 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
354 NL80211_CHAN_NO_HT);
355 wdev->wext.ibss.channel_fixed = true;
356 } else {
357 /* cfg80211_ibss_wext_join will pick one if needed */
358 wdev->wext.ibss.channel_fixed = false;
361 wdev_lock(wdev);
362 err = cfg80211_ibss_wext_join(rdev, wdev);
363 wdev_unlock(wdev);
365 return err;
368 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
369 struct iw_request_info *info,
370 struct iw_freq *freq, char *extra)
372 struct wireless_dev *wdev = dev->ieee80211_ptr;
373 struct ieee80211_channel *chan = NULL;
375 /* call only for ibss! */
376 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
377 return -EINVAL;
379 wdev_lock(wdev);
380 if (wdev->current_bss)
381 chan = wdev->current_bss->pub.channel;
382 else if (wdev->wext.ibss.chandef.chan)
383 chan = wdev->wext.ibss.chandef.chan;
384 wdev_unlock(wdev);
386 if (chan) {
387 freq->m = chan->center_freq;
388 freq->e = 6;
389 return 0;
392 /* no channel if not joining */
393 return -EINVAL;
396 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
397 struct iw_request_info *info,
398 struct iw_point *data, char *ssid)
400 struct wireless_dev *wdev = dev->ieee80211_ptr;
401 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
402 size_t len = data->length;
403 int err;
405 /* call only for ibss! */
406 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
407 return -EINVAL;
409 if (!rdev->ops->join_ibss)
410 return -EOPNOTSUPP;
412 wdev_lock(wdev);
413 err = 0;
414 if (wdev->ssid_len)
415 err = __cfg80211_leave_ibss(rdev, dev, true);
416 wdev_unlock(wdev);
418 if (err)
419 return err;
421 /* iwconfig uses nul termination in SSID.. */
422 if (len > 0 && ssid[len - 1] == '\0')
423 len--;
425 memcpy(wdev->ssid, ssid, len);
426 wdev->wext.ibss.ssid = wdev->ssid;
427 wdev->wext.ibss.ssid_len = len;
429 wdev_lock(wdev);
430 err = cfg80211_ibss_wext_join(rdev, wdev);
431 wdev_unlock(wdev);
433 return err;
436 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
437 struct iw_request_info *info,
438 struct iw_point *data, char *ssid)
440 struct wireless_dev *wdev = dev->ieee80211_ptr;
442 /* call only for ibss! */
443 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
444 return -EINVAL;
446 data->flags = 0;
448 wdev_lock(wdev);
449 if (wdev->ssid_len) {
450 data->flags = 1;
451 data->length = wdev->ssid_len;
452 memcpy(ssid, wdev->ssid, data->length);
453 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
454 data->flags = 1;
455 data->length = wdev->wext.ibss.ssid_len;
456 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
458 wdev_unlock(wdev);
460 return 0;
463 int cfg80211_ibss_wext_siwap(struct net_device *dev,
464 struct iw_request_info *info,
465 struct sockaddr *ap_addr, char *extra)
467 struct wireless_dev *wdev = dev->ieee80211_ptr;
468 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
469 u8 *bssid = ap_addr->sa_data;
470 int err;
472 /* call only for ibss! */
473 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
474 return -EINVAL;
476 if (!rdev->ops->join_ibss)
477 return -EOPNOTSUPP;
479 if (ap_addr->sa_family != ARPHRD_ETHER)
480 return -EINVAL;
482 /* automatic mode */
483 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
484 bssid = NULL;
486 if (bssid && !is_valid_ether_addr(bssid))
487 return -EINVAL;
489 /* both automatic */
490 if (!bssid && !wdev->wext.ibss.bssid)
491 return 0;
493 /* fixed already - and no change */
494 if (wdev->wext.ibss.bssid && bssid &&
495 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
496 return 0;
498 wdev_lock(wdev);
499 err = 0;
500 if (wdev->ssid_len)
501 err = __cfg80211_leave_ibss(rdev, dev, true);
502 wdev_unlock(wdev);
504 if (err)
505 return err;
507 if (bssid) {
508 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
509 wdev->wext.ibss.bssid = wdev->wext.bssid;
510 } else
511 wdev->wext.ibss.bssid = NULL;
513 wdev_lock(wdev);
514 err = cfg80211_ibss_wext_join(rdev, wdev);
515 wdev_unlock(wdev);
517 return err;
520 int cfg80211_ibss_wext_giwap(struct net_device *dev,
521 struct iw_request_info *info,
522 struct sockaddr *ap_addr, char *extra)
524 struct wireless_dev *wdev = dev->ieee80211_ptr;
526 /* call only for ibss! */
527 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
528 return -EINVAL;
530 ap_addr->sa_family = ARPHRD_ETHER;
532 wdev_lock(wdev);
533 if (wdev->current_bss)
534 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
535 else if (wdev->wext.ibss.bssid)
536 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
537 else
538 eth_zero_addr(ap_addr->sa_data);
540 wdev_unlock(wdev);
542 return 0;
544 #endif