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 wdev->sme_state = CFG80211_SME_CONNECTED;
46 cfg80211_upload_connect_keys(wdev);
48 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
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);
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;
64 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
66 ev = kzalloc(sizeof(*ev), gfp);
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;
88 ASSERT_WDEV_LOCK(wdev);
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->channel->band];
102 u32 flag = params->channel->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 #ifdef CONFIG_CFG80211_WEXT
117 wdev->wext.ibss.channel = params->channel;
119 wdev->sme_state = CFG80211_SME_CONNECTING;
120 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
122 wdev->connect_keys = NULL;
123 wdev->sme_state = CFG80211_SME_IDLE;
127 memcpy(wdev->ssid, params->ssid, params->ssid_len);
128 wdev->ssid_len = params->ssid_len;
133 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
134 struct net_device *dev,
135 struct cfg80211_ibss_params *params,
136 struct cfg80211_cached_keys *connkeys)
138 struct wireless_dev *wdev = dev->ieee80211_ptr;
141 mutex_lock(&rdev->devlist_mtx);
143 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
145 mutex_unlock(&rdev->devlist_mtx);
150 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
152 struct wireless_dev *wdev = dev->ieee80211_ptr;
153 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
156 ASSERT_WDEV_LOCK(wdev);
158 kfree(wdev->connect_keys);
159 wdev->connect_keys = NULL;
162 * Delete all the keys ... pairwise keys can't really
163 * exist any more anyway, but default keys might.
165 if (rdev->ops->del_key)
166 for (i = 0; i < 6; i++)
167 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
169 if (wdev->current_bss) {
170 cfg80211_unhold_bss(wdev->current_bss);
171 cfg80211_put_bss(&wdev->current_bss->pub);
174 wdev->current_bss = NULL;
175 wdev->sme_state = CFG80211_SME_IDLE;
177 #ifdef CONFIG_CFG80211_WEXT
179 wdev->wext.ibss.ssid_len = 0;
183 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
185 struct wireless_dev *wdev = dev->ieee80211_ptr;
188 __cfg80211_clear_ibss(dev, nowext);
192 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
193 struct net_device *dev, bool nowext)
195 struct wireless_dev *wdev = dev->ieee80211_ptr;
198 ASSERT_WDEV_LOCK(wdev);
203 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
208 __cfg80211_clear_ibss(dev, nowext);
213 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
214 struct net_device *dev, bool nowext)
216 struct wireless_dev *wdev = dev->ieee80211_ptr;
220 err = __cfg80211_leave_ibss(rdev, dev, nowext);
226 #ifdef CONFIG_CFG80211_WEXT
227 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
228 struct wireless_dev *wdev)
230 struct cfg80211_cached_keys *ck = NULL;
231 enum ieee80211_band band;
234 ASSERT_WDEV_LOCK(wdev);
236 if (!wdev->wext.ibss.beacon_interval)
237 wdev->wext.ibss.beacon_interval = 100;
239 /* try to find an IBSS channel if none requested ... */
240 if (!wdev->wext.ibss.channel) {
241 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
242 struct ieee80211_supported_band *sband;
243 struct ieee80211_channel *chan;
245 sband = rdev->wiphy.bands[band];
249 for (i = 0; i < sband->n_channels; i++) {
250 chan = &sband->channels[i];
251 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
253 if (chan->flags & IEEE80211_CHAN_DISABLED)
255 wdev->wext.ibss.channel = chan;
259 if (wdev->wext.ibss.channel)
263 if (!wdev->wext.ibss.channel)
267 /* don't join -- SSID is not there */
268 if (!wdev->wext.ibss.ssid_len)
271 if (!netif_running(wdev->netdev))
274 if (wdev->wext.keys) {
275 wdev->wext.keys->def = wdev->wext.default_key;
276 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
279 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
281 if (wdev->wext.keys) {
282 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
285 for (i = 0; i < 6; i++)
286 ck->params[i].key = ck->data[i];
288 err = __cfg80211_join_ibss(rdev, wdev->netdev,
289 &wdev->wext.ibss, ck);
296 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
297 struct iw_request_info *info,
298 struct iw_freq *wextfreq, char *extra)
300 struct wireless_dev *wdev = dev->ieee80211_ptr;
301 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
302 struct ieee80211_channel *chan = NULL;
305 /* call only for ibss! */
306 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
309 if (!rdev->ops->join_ibss)
312 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
317 chan = ieee80211_get_channel(wdev->wiphy, freq);
320 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
321 chan->flags & IEEE80211_CHAN_DISABLED)
325 if (wdev->wext.ibss.channel == chan)
331 err = __cfg80211_leave_ibss(rdev, dev, true);
338 wdev->wext.ibss.channel = chan;
339 wdev->wext.ibss.channel_fixed = true;
341 /* cfg80211_ibss_wext_join will pick one if needed */
342 wdev->wext.ibss.channel_fixed = false;
345 mutex_lock(&rdev->devlist_mtx);
347 err = cfg80211_ibss_wext_join(rdev, wdev);
349 mutex_unlock(&rdev->devlist_mtx);
354 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
355 struct iw_request_info *info,
356 struct iw_freq *freq, char *extra)
358 struct wireless_dev *wdev = dev->ieee80211_ptr;
359 struct ieee80211_channel *chan = NULL;
361 /* call only for ibss! */
362 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
366 if (wdev->current_bss)
367 chan = wdev->current_bss->pub.channel;
368 else if (wdev->wext.ibss.channel)
369 chan = wdev->wext.ibss.channel;
373 freq->m = chan->center_freq;
378 /* no channel if not joining */
382 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
383 struct iw_request_info *info,
384 struct iw_point *data, char *ssid)
386 struct wireless_dev *wdev = dev->ieee80211_ptr;
387 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
388 size_t len = data->length;
391 /* call only for ibss! */
392 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
395 if (!rdev->ops->join_ibss)
401 err = __cfg80211_leave_ibss(rdev, dev, true);
407 /* iwconfig uses nul termination in SSID.. */
408 if (len > 0 && ssid[len - 1] == '\0')
411 wdev->wext.ibss.ssid = wdev->ssid;
412 memcpy(wdev->wext.ibss.ssid, ssid, len);
413 wdev->wext.ibss.ssid_len = len;
415 mutex_lock(&rdev->devlist_mtx);
417 err = cfg80211_ibss_wext_join(rdev, wdev);
419 mutex_unlock(&rdev->devlist_mtx);
424 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
425 struct iw_request_info *info,
426 struct iw_point *data, char *ssid)
428 struct wireless_dev *wdev = dev->ieee80211_ptr;
430 /* call only for ibss! */
431 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
437 if (wdev->ssid_len) {
439 data->length = wdev->ssid_len;
440 memcpy(ssid, wdev->ssid, data->length);
441 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
443 data->length = wdev->wext.ibss.ssid_len;
444 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
451 int cfg80211_ibss_wext_siwap(struct net_device *dev,
452 struct iw_request_info *info,
453 struct sockaddr *ap_addr, char *extra)
455 struct wireless_dev *wdev = dev->ieee80211_ptr;
456 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
457 u8 *bssid = ap_addr->sa_data;
460 /* call only for ibss! */
461 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
464 if (!rdev->ops->join_ibss)
467 if (ap_addr->sa_family != ARPHRD_ETHER)
471 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
475 if (!bssid && !wdev->wext.ibss.bssid)
478 /* fixed already - and no change */
479 if (wdev->wext.ibss.bssid && bssid &&
480 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
486 err = __cfg80211_leave_ibss(rdev, dev, true);
493 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
494 wdev->wext.ibss.bssid = wdev->wext.bssid;
496 wdev->wext.ibss.bssid = NULL;
498 mutex_lock(&rdev->devlist_mtx);
500 err = cfg80211_ibss_wext_join(rdev, wdev);
502 mutex_unlock(&rdev->devlist_mtx);
507 int cfg80211_ibss_wext_giwap(struct net_device *dev,
508 struct iw_request_info *info,
509 struct sockaddr *ap_addr, char *extra)
511 struct wireless_dev *wdev = dev->ieee80211_ptr;
513 /* call only for ibss! */
514 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
517 ap_addr->sa_family = ARPHRD_ETHER;
520 if (wdev->current_bss)
521 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
522 else if (wdev->wext.ibss.bssid)
523 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
525 memset(ap_addr->sa_data, 0, ETH_ALEN);