2 * Some IBSS support code for cfg80211.
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
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"
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;
24 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
30 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
31 wdev->ssid, wdev->ssid_len,
32 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
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,
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);
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;
63 CFG80211_DEV_WARN_ON(!wdev->ssid_len);
65 ev = kzalloc(sizeof(*ev), gfp);
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;
87 ASSERT_WDEV_LOCK(wdev);
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];
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;
118 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
120 wdev->connect_keys = NULL;
124 memcpy(wdev->ssid, params->ssid, params->ssid_len);
125 wdev->ssid_len = params->ssid_len;
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;
138 mutex_lock(&rdev->devlist_mtx);
140 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
142 mutex_unlock(&rdev->devlist_mtx);
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);
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;
173 #ifdef CONFIG_CFG80211_WEXT
175 wdev->wext.ibss.ssid_len = 0;
179 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
181 struct wireless_dev *wdev = dev->ieee80211_ptr;
184 __cfg80211_clear_ibss(dev, nowext);
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;
194 ASSERT_WDEV_LOCK(wdev);
199 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
204 __cfg80211_clear_ibss(dev, nowext);
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;
216 err = __cfg80211_leave_ibss(rdev, dev, nowext);
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;
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];
245 for (i = 0; i < sband->n_channels; i++) {
246 chan = &sband->channels[i];
247 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
249 if (chan->flags & IEEE80211_CHAN_DISABLED)
251 wdev->wext.ibss.channel = chan;
255 if (wdev->wext.ibss.channel)
259 if (!wdev->wext.ibss.channel)
263 /* don't join -- SSID is not there */
264 if (!wdev->wext.ibss.ssid_len)
267 if (!netif_running(wdev->netdev))
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);
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);
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;
301 /* call only for ibss! */
302 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
305 if (!rdev->ops->join_ibss)
308 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
313 chan = ieee80211_get_channel(wdev->wiphy, freq);
316 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
317 chan->flags & IEEE80211_CHAN_DISABLED)
321 if (wdev->wext.ibss.channel == chan)
327 err = __cfg80211_leave_ibss(rdev, dev, true);
334 wdev->wext.ibss.channel = chan;
335 wdev->wext.ibss.channel_fixed = true;
337 /* cfg80211_ibss_wext_join will pick one if needed */
338 wdev->wext.ibss.channel_fixed = false;
341 mutex_lock(&rdev->devlist_mtx);
343 err = cfg80211_ibss_wext_join(rdev, wdev);
345 mutex_unlock(&rdev->devlist_mtx);
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))
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;
369 freq->m = chan->center_freq;
374 /* no channel if not joining */
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;
387 /* call only for ibss! */
388 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
391 if (!rdev->ops->join_ibss)
397 err = __cfg80211_leave_ibss(rdev, dev, true);
403 /* iwconfig uses nul termination in SSID.. */
404 if (len > 0 && ssid[len - 1] == '\0')
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);
413 err = cfg80211_ibss_wext_join(rdev, wdev);
415 mutex_unlock(&rdev->devlist_mtx);
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))
433 if (wdev->ssid_len) {
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) {
439 data->length = wdev->wext.ibss.ssid_len;
440 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
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;
456 /* call only for ibss! */
457 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
460 if (!rdev->ops->join_ibss)
463 if (ap_addr->sa_family != ARPHRD_ETHER)
467 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
471 if (!bssid && !wdev->wext.ibss.bssid)
474 /* fixed already - and no change */
475 if (wdev->wext.ibss.bssid && bssid &&
476 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
482 err = __cfg80211_leave_ibss(rdev, dev, true);
489 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
490 wdev->wext.ibss.bssid = wdev->wext.bssid;
492 wdev->wext.ibss.bssid = NULL;
494 mutex_lock(&rdev->devlist_mtx);
496 err = cfg80211_ibss_wext_join(rdev, wdev);
498 mutex_unlock(&rdev->devlist_mtx);
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))
513 ap_addr->sa_family = ARPHRD_ETHER;
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);
521 memset(ap_addr->sa_data, 0, ETH_ALEN);