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->wiphy, &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 trace_cfg80211_ibss_joined(dev, bssid);
67 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
69 ev = kzalloc(sizeof(*ev), gfp);
73 ev->type = EVENT_IBSS_JOINED;
74 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
76 spin_lock_irqsave(&wdev->event_lock, flags);
77 list_add_tail(&ev->list, &wdev->event_list);
78 spin_unlock_irqrestore(&wdev->event_lock, flags);
79 queue_work(cfg80211_wq, &rdev->event_work);
81 EXPORT_SYMBOL(cfg80211_ibss_joined);
83 int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
84 struct net_device *dev,
85 struct cfg80211_ibss_params *params,
86 struct cfg80211_cached_keys *connkeys)
88 struct wireless_dev *wdev = dev->ieee80211_ptr;
91 ASSERT_WDEV_LOCK(wdev);
96 if (!params->basic_rates) {
98 * If no rates were explicitly configured,
99 * use the mandatory rate set for 11b or
100 * 11a for maximum compatibility.
102 struct ieee80211_supported_band *sband =
103 rdev->wiphy.bands[params->chandef.chan->band];
105 u32 flag = params->chandef.chan->band == IEEE80211_BAND_5GHZ ?
106 IEEE80211_RATE_MANDATORY_A :
107 IEEE80211_RATE_MANDATORY_B;
109 for (j = 0; j < sband->n_bitrates; j++) {
110 if (sband->bitrates[j].flags & flag)
111 params->basic_rates |= BIT(j);
115 if (WARN_ON(wdev->connect_keys))
116 kfree(wdev->connect_keys);
117 wdev->connect_keys = connkeys;
119 wdev->ibss_fixed = params->channel_fixed;
120 #ifdef CONFIG_CFG80211_WEXT
121 wdev->wext.ibss.chandef = params->chandef;
123 wdev->sme_state = CFG80211_SME_CONNECTING;
125 err = cfg80211_can_use_chan(rdev, wdev, params->chandef.chan,
126 params->channel_fixed
128 : CHAN_MODE_EXCLUSIVE);
130 wdev->connect_keys = NULL;
134 err = rdev_join_ibss(rdev, dev, params);
136 wdev->connect_keys = NULL;
137 wdev->sme_state = CFG80211_SME_IDLE;
141 memcpy(wdev->ssid, params->ssid, params->ssid_len);
142 wdev->ssid_len = params->ssid_len;
147 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
148 struct net_device *dev,
149 struct cfg80211_ibss_params *params,
150 struct cfg80211_cached_keys *connkeys)
152 struct wireless_dev *wdev = dev->ieee80211_ptr;
155 mutex_lock(&rdev->devlist_mtx);
157 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
159 mutex_unlock(&rdev->devlist_mtx);
164 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
166 struct wireless_dev *wdev = dev->ieee80211_ptr;
167 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
170 ASSERT_WDEV_LOCK(wdev);
172 kfree(wdev->connect_keys);
173 wdev->connect_keys = NULL;
176 * Delete all the keys ... pairwise keys can't really
177 * exist any more anyway, but default keys might.
179 if (rdev->ops->del_key)
180 for (i = 0; i < 6; i++)
181 rdev_del_key(rdev, dev, i, false, NULL);
183 if (wdev->current_bss) {
184 cfg80211_unhold_bss(wdev->current_bss);
185 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
188 wdev->current_bss = NULL;
189 wdev->sme_state = CFG80211_SME_IDLE;
191 #ifdef CONFIG_CFG80211_WEXT
193 wdev->wext.ibss.ssid_len = 0;
197 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
199 struct wireless_dev *wdev = dev->ieee80211_ptr;
202 __cfg80211_clear_ibss(dev, nowext);
206 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
207 struct net_device *dev, bool nowext)
209 struct wireless_dev *wdev = dev->ieee80211_ptr;
212 ASSERT_WDEV_LOCK(wdev);
217 err = rdev_leave_ibss(rdev, dev);
222 __cfg80211_clear_ibss(dev, nowext);
227 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
228 struct net_device *dev, bool nowext)
230 struct wireless_dev *wdev = dev->ieee80211_ptr;
234 err = __cfg80211_leave_ibss(rdev, dev, nowext);
240 #ifdef CONFIG_CFG80211_WEXT
241 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
242 struct wireless_dev *wdev)
244 struct cfg80211_cached_keys *ck = NULL;
245 enum ieee80211_band band;
248 ASSERT_WDEV_LOCK(wdev);
250 if (!wdev->wext.ibss.beacon_interval)
251 wdev->wext.ibss.beacon_interval = 100;
253 /* try to find an IBSS channel if none requested ... */
254 if (!wdev->wext.ibss.chandef.chan) {
255 wdev->wext.ibss.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
257 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
258 struct ieee80211_supported_band *sband;
259 struct ieee80211_channel *chan;
261 sband = rdev->wiphy.bands[band];
265 for (i = 0; i < sband->n_channels; i++) {
266 chan = &sband->channels[i];
267 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
269 if (chan->flags & IEEE80211_CHAN_DISABLED)
271 wdev->wext.ibss.chandef.chan = chan;
272 wdev->wext.ibss.chandef.center_freq1 =
277 if (wdev->wext.ibss.chandef.chan)
281 if (!wdev->wext.ibss.chandef.chan)
285 /* don't join -- SSID is not there */
286 if (!wdev->wext.ibss.ssid_len)
289 if (!netif_running(wdev->netdev))
292 if (wdev->wext.keys) {
293 wdev->wext.keys->def = wdev->wext.default_key;
294 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
297 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
299 if (wdev->wext.keys) {
300 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
303 for (i = 0; i < 6; i++)
304 ck->params[i].key = ck->data[i];
306 err = __cfg80211_join_ibss(rdev, wdev->netdev,
307 &wdev->wext.ibss, ck);
314 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
315 struct iw_request_info *info,
316 struct iw_freq *wextfreq, char *extra)
318 struct wireless_dev *wdev = dev->ieee80211_ptr;
319 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
320 struct ieee80211_channel *chan = NULL;
323 /* call only for ibss! */
324 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
327 if (!rdev->ops->join_ibss)
330 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
335 chan = ieee80211_get_channel(wdev->wiphy, freq);
338 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
339 chan->flags & IEEE80211_CHAN_DISABLED)
343 if (wdev->wext.ibss.chandef.chan == chan)
349 err = __cfg80211_leave_ibss(rdev, dev, true);
356 wdev->wext.ibss.chandef.chan = chan;
357 wdev->wext.ibss.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
358 wdev->wext.ibss.chandef.center_freq1 = freq;
359 wdev->wext.ibss.channel_fixed = true;
361 /* cfg80211_ibss_wext_join will pick one if needed */
362 wdev->wext.ibss.channel_fixed = false;
365 mutex_lock(&rdev->devlist_mtx);
367 err = cfg80211_ibss_wext_join(rdev, wdev);
369 mutex_unlock(&rdev->devlist_mtx);
374 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
375 struct iw_request_info *info,
376 struct iw_freq *freq, char *extra)
378 struct wireless_dev *wdev = dev->ieee80211_ptr;
379 struct ieee80211_channel *chan = NULL;
381 /* call only for ibss! */
382 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
386 if (wdev->current_bss)
387 chan = wdev->current_bss->pub.channel;
388 else if (wdev->wext.ibss.chandef.chan)
389 chan = wdev->wext.ibss.chandef.chan;
393 freq->m = chan->center_freq;
398 /* no channel if not joining */
402 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
403 struct iw_request_info *info,
404 struct iw_point *data, char *ssid)
406 struct wireless_dev *wdev = dev->ieee80211_ptr;
407 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
408 size_t len = data->length;
411 /* call only for ibss! */
412 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
415 if (!rdev->ops->join_ibss)
421 err = __cfg80211_leave_ibss(rdev, dev, true);
427 /* iwconfig uses nul termination in SSID.. */
428 if (len > 0 && ssid[len - 1] == '\0')
431 wdev->wext.ibss.ssid = wdev->ssid;
432 memcpy(wdev->wext.ibss.ssid, ssid, len);
433 wdev->wext.ibss.ssid_len = len;
435 mutex_lock(&rdev->devlist_mtx);
437 err = cfg80211_ibss_wext_join(rdev, wdev);
439 mutex_unlock(&rdev->devlist_mtx);
444 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
445 struct iw_request_info *info,
446 struct iw_point *data, char *ssid)
448 struct wireless_dev *wdev = dev->ieee80211_ptr;
450 /* call only for ibss! */
451 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
457 if (wdev->ssid_len) {
459 data->length = wdev->ssid_len;
460 memcpy(ssid, wdev->ssid, data->length);
461 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
463 data->length = wdev->wext.ibss.ssid_len;
464 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
471 int cfg80211_ibss_wext_siwap(struct net_device *dev,
472 struct iw_request_info *info,
473 struct sockaddr *ap_addr, char *extra)
475 struct wireless_dev *wdev = dev->ieee80211_ptr;
476 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
477 u8 *bssid = ap_addr->sa_data;
480 /* call only for ibss! */
481 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
484 if (!rdev->ops->join_ibss)
487 if (ap_addr->sa_family != ARPHRD_ETHER)
491 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
495 if (!bssid && !wdev->wext.ibss.bssid)
498 /* fixed already - and no change */
499 if (wdev->wext.ibss.bssid && bssid &&
500 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
506 err = __cfg80211_leave_ibss(rdev, dev, true);
513 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
514 wdev->wext.ibss.bssid = wdev->wext.bssid;
516 wdev->wext.ibss.bssid = NULL;
518 mutex_lock(&rdev->devlist_mtx);
520 err = cfg80211_ibss_wext_join(rdev, wdev);
522 mutex_unlock(&rdev->devlist_mtx);
527 int cfg80211_ibss_wext_giwap(struct net_device *dev,
528 struct iw_request_info *info,
529 struct sockaddr *ap_addr, char *extra)
531 struct wireless_dev *wdev = dev->ieee80211_ptr;
533 /* call only for ibss! */
534 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
537 ap_addr->sa_family = ARPHRD_ETHER;
540 if (wdev->current_bss)
541 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
542 else if (wdev->wext.ibss.bssid)
543 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
545 memset(ap_addr->sa_data, 0, ETH_ALEN);