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"
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;
25 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
31 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
32 wdev->ssid, wdev->ssid_len,
33 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
38 if (wdev->current_bss) {
39 cfg80211_unhold_bss(wdev->current_bss);
40 cfg80211_put_bss(&wdev->current_bss->pub);
43 cfg80211_hold_bss(bss_from_pub(bss));
44 wdev->current_bss = bss_from_pub(bss);
46 wdev->sme_state = CFG80211_SME_CONNECTED;
47 cfg80211_upload_connect_keys(wdev);
49 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
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);
58 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
60 struct wireless_dev *wdev = dev->ieee80211_ptr;
61 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
62 struct cfg80211_event *ev;
65 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
67 ev = kzalloc(sizeof(*ev), gfp);
71 ev->type = EVENT_IBSS_JOINED;
72 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
74 spin_lock_irqsave(&wdev->event_lock, flags);
75 list_add_tail(&ev->list, &wdev->event_list);
76 spin_unlock_irqrestore(&wdev->event_lock, flags);
77 queue_work(cfg80211_wq, &rdev->event_work);
79 EXPORT_SYMBOL(cfg80211_ibss_joined);
81 int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
82 struct net_device *dev,
83 struct cfg80211_ibss_params *params,
84 struct cfg80211_cached_keys *connkeys)
86 struct wireless_dev *wdev = dev->ieee80211_ptr;
89 ASSERT_WDEV_LOCK(wdev);
94 if (!params->basic_rates) {
96 * If no rates were explicitly configured,
97 * use the mandatory rate set for 11b or
98 * 11a for maximum compatibility.
100 struct ieee80211_supported_band *sband =
101 rdev->wiphy.bands[params->channel->band];
103 u32 flag = params->channel->band == IEEE80211_BAND_5GHZ ?
104 IEEE80211_RATE_MANDATORY_A :
105 IEEE80211_RATE_MANDATORY_B;
107 for (j = 0; j < sband->n_bitrates; j++) {
108 if (sband->bitrates[j].flags & flag)
109 params->basic_rates |= BIT(j);
113 if (WARN_ON(wdev->connect_keys))
114 kfree(wdev->connect_keys);
115 wdev->connect_keys = connkeys;
117 wdev->ibss_fixed = params->channel_fixed;
118 #ifdef CONFIG_CFG80211_WEXT
119 wdev->wext.ibss.channel = params->channel;
121 wdev->sme_state = CFG80211_SME_CONNECTING;
123 err = cfg80211_can_use_chan(rdev, wdev, params->channel,
124 params->channel_fixed
126 : CHAN_MODE_EXCLUSIVE);
128 wdev->connect_keys = NULL;
132 err = rdev_join_ibss(rdev, dev, params);
134 wdev->connect_keys = NULL;
135 wdev->sme_state = CFG80211_SME_IDLE;
139 memcpy(wdev->ssid, params->ssid, params->ssid_len);
140 wdev->ssid_len = params->ssid_len;
145 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
146 struct net_device *dev,
147 struct cfg80211_ibss_params *params,
148 struct cfg80211_cached_keys *connkeys)
150 struct wireless_dev *wdev = dev->ieee80211_ptr;
153 mutex_lock(&rdev->devlist_mtx);
155 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
157 mutex_unlock(&rdev->devlist_mtx);
162 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
164 struct wireless_dev *wdev = dev->ieee80211_ptr;
165 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
168 ASSERT_WDEV_LOCK(wdev);
170 kfree(wdev->connect_keys);
171 wdev->connect_keys = 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->current_bss->pub);
186 wdev->current_bss = NULL;
187 wdev->sme_state = CFG80211_SME_IDLE;
189 #ifdef CONFIG_CFG80211_WEXT
191 wdev->wext.ibss.ssid_len = 0;
195 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
197 struct wireless_dev *wdev = dev->ieee80211_ptr;
200 __cfg80211_clear_ibss(dev, nowext);
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;
210 ASSERT_WDEV_LOCK(wdev);
215 err = rdev_leave_ibss(rdev, dev);
220 __cfg80211_clear_ibss(dev, nowext);
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;
232 err = __cfg80211_leave_ibss(rdev, dev, nowext);
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 ieee80211_band band;
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.channel) {
253 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
254 struct ieee80211_supported_band *sband;
255 struct ieee80211_channel *chan;
257 sband = rdev->wiphy.bands[band];
261 for (i = 0; i < sband->n_channels; i++) {
262 chan = &sband->channels[i];
263 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
265 if (chan->flags & IEEE80211_CHAN_DISABLED)
267 wdev->wext.ibss.channel = chan;
271 if (wdev->wext.ibss.channel)
275 if (!wdev->wext.ibss.channel)
279 /* don't join -- SSID is not there */
280 if (!wdev->wext.ibss.ssid_len)
283 if (!netif_running(wdev->netdev))
286 if (wdev->wext.keys) {
287 wdev->wext.keys->def = wdev->wext.default_key;
288 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
291 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
293 if (wdev->wext.keys) {
294 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
297 for (i = 0; i < 6; i++)
298 ck->params[i].key = ck->data[i];
300 err = __cfg80211_join_ibss(rdev, wdev->netdev,
301 &wdev->wext.ibss, ck);
308 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
309 struct iw_request_info *info,
310 struct iw_freq *wextfreq, char *extra)
312 struct wireless_dev *wdev = dev->ieee80211_ptr;
313 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
314 struct ieee80211_channel *chan = NULL;
317 /* call only for ibss! */
318 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
321 if (!rdev->ops->join_ibss)
324 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
329 chan = ieee80211_get_channel(wdev->wiphy, freq);
332 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
333 chan->flags & IEEE80211_CHAN_DISABLED)
337 if (wdev->wext.ibss.channel == chan)
343 err = __cfg80211_leave_ibss(rdev, dev, true);
350 wdev->wext.ibss.channel = chan;
351 wdev->wext.ibss.channel_fixed = true;
353 /* cfg80211_ibss_wext_join will pick one if needed */
354 wdev->wext.ibss.channel_fixed = false;
357 mutex_lock(&rdev->devlist_mtx);
359 err = cfg80211_ibss_wext_join(rdev, wdev);
361 mutex_unlock(&rdev->devlist_mtx);
366 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
367 struct iw_request_info *info,
368 struct iw_freq *freq, char *extra)
370 struct wireless_dev *wdev = dev->ieee80211_ptr;
371 struct ieee80211_channel *chan = NULL;
373 /* call only for ibss! */
374 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
378 if (wdev->current_bss)
379 chan = wdev->current_bss->pub.channel;
380 else if (wdev->wext.ibss.channel)
381 chan = wdev->wext.ibss.channel;
385 freq->m = chan->center_freq;
390 /* no channel if not joining */
394 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
395 struct iw_request_info *info,
396 struct iw_point *data, char *ssid)
398 struct wireless_dev *wdev = dev->ieee80211_ptr;
399 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
400 size_t len = data->length;
403 /* call only for ibss! */
404 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
407 if (!rdev->ops->join_ibss)
413 err = __cfg80211_leave_ibss(rdev, dev, true);
419 /* iwconfig uses nul termination in SSID.. */
420 if (len > 0 && ssid[len - 1] == '\0')
423 wdev->wext.ibss.ssid = wdev->ssid;
424 memcpy(wdev->wext.ibss.ssid, ssid, len);
425 wdev->wext.ibss.ssid_len = len;
427 mutex_lock(&rdev->devlist_mtx);
429 err = cfg80211_ibss_wext_join(rdev, wdev);
431 mutex_unlock(&rdev->devlist_mtx);
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))
449 if (wdev->ssid_len) {
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) {
455 data->length = wdev->wext.ibss.ssid_len;
456 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
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_dev(wdev->wiphy);
469 u8 *bssid = ap_addr->sa_data;
472 /* call only for ibss! */
473 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
476 if (!rdev->ops->join_ibss)
479 if (ap_addr->sa_family != ARPHRD_ETHER)
483 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
487 if (!bssid && !wdev->wext.ibss.bssid)
490 /* fixed already - and no change */
491 if (wdev->wext.ibss.bssid && bssid &&
492 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
498 err = __cfg80211_leave_ibss(rdev, dev, true);
505 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
506 wdev->wext.ibss.bssid = wdev->wext.bssid;
508 wdev->wext.ibss.bssid = NULL;
510 mutex_lock(&rdev->devlist_mtx);
512 err = cfg80211_ibss_wext_join(rdev, wdev);
514 mutex_unlock(&rdev->devlist_mtx);
519 int cfg80211_ibss_wext_giwap(struct net_device *dev,
520 struct iw_request_info *info,
521 struct sockaddr *ap_addr, char *extra)
523 struct wireless_dev *wdev = dev->ieee80211_ptr;
525 /* call only for ibss! */
526 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
529 ap_addr->sa_family = ARPHRD_ETHER;
532 if (wdev->current_bss)
533 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
534 else if (wdev->wext.ibss.bssid)
535 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
537 memset(ap_addr->sa_data, 0, ETH_ALEN);