2 * Some IBSS support code for cfg80211.
7 #include <linux/etherdevice.h>
8 #include <linux/if_arp.h>
9 #include <linux/slab.h>
10 #include <net/cfg80211.h>
11 #include "wext-compat.h"
15 void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
17 struct wireless_dev *wdev = dev->ieee80211_ptr;
18 struct cfg80211_bss *bss;
19 #ifdef CONFIG_CFG80211_WEXT
20 union iwreq_data wrqu;
23 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
29 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
30 wdev->ssid, wdev->ssid_len,
31 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
36 if (wdev->current_bss) {
37 cfg80211_unhold_bss(wdev->current_bss);
38 cfg80211_put_bss(&wdev->current_bss->pub);
41 cfg80211_hold_bss(bss_from_pub(bss));
42 wdev->current_bss = bss_from_pub(bss);
44 cfg80211_upload_connect_keys(wdev);
46 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
48 #ifdef CONFIG_CFG80211_WEXT
49 memset(&wrqu, 0, sizeof(wrqu));
50 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
51 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
55 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
57 struct wireless_dev *wdev = dev->ieee80211_ptr;
58 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
59 struct cfg80211_event *ev;
62 CFG80211_DEV_WARN_ON(!wdev->ssid_len);
64 ev = kzalloc(sizeof(*ev), gfp);
68 ev->type = EVENT_IBSS_JOINED;
69 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
71 spin_lock_irqsave(&wdev->event_lock, flags);
72 list_add_tail(&ev->list, &wdev->event_list);
73 spin_unlock_irqrestore(&wdev->event_lock, flags);
74 queue_work(cfg80211_wq, &rdev->event_work);
76 EXPORT_SYMBOL(cfg80211_ibss_joined);
78 int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
79 struct net_device *dev,
80 struct cfg80211_ibss_params *params,
81 struct cfg80211_cached_keys *connkeys)
83 struct wireless_dev *wdev = dev->ieee80211_ptr;
86 ASSERT_WDEV_LOCK(wdev);
91 if (!params->basic_rates) {
93 * If no rates were explicitly configured,
94 * use the mandatory rate set for 11b or
95 * 11a for maximum compatibility.
97 struct ieee80211_supported_band *sband =
98 rdev->wiphy.bands[params->channel->band];
100 u32 flag = params->channel->band == IEEE80211_BAND_5GHZ ?
101 IEEE80211_RATE_MANDATORY_A :
102 IEEE80211_RATE_MANDATORY_B;
104 for (j = 0; j < sband->n_bitrates; j++) {
105 if (sband->bitrates[j].flags & flag)
106 params->basic_rates |= BIT(j);
110 if (WARN_ON(wdev->connect_keys))
111 kfree(wdev->connect_keys);
112 wdev->connect_keys = connkeys;
114 #ifdef CONFIG_CFG80211_WEXT
115 wdev->wext.ibss.channel = params->channel;
117 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
119 wdev->connect_keys = NULL;
123 memcpy(wdev->ssid, params->ssid, params->ssid_len);
124 wdev->ssid_len = params->ssid_len;
129 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
130 struct net_device *dev,
131 struct cfg80211_ibss_params *params,
132 struct cfg80211_cached_keys *connkeys)
134 struct wireless_dev *wdev = dev->ieee80211_ptr;
137 mutex_lock(&rdev->devlist_mtx);
139 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
141 mutex_unlock(&rdev->devlist_mtx);
146 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
148 struct wireless_dev *wdev = dev->ieee80211_ptr;
149 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
152 ASSERT_WDEV_LOCK(wdev);
154 kfree(wdev->connect_keys);
155 wdev->connect_keys = NULL;
158 * Delete all the keys ... pairwise keys can't really
159 * exist any more anyway, but default keys might.
161 if (rdev->ops->del_key)
162 for (i = 0; i < 6; i++)
163 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
165 if (wdev->current_bss) {
166 cfg80211_unhold_bss(wdev->current_bss);
167 cfg80211_put_bss(&wdev->current_bss->pub);
170 wdev->current_bss = NULL;
172 #ifdef CONFIG_CFG80211_WEXT
174 wdev->wext.ibss.ssid_len = 0;
178 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
180 struct wireless_dev *wdev = dev->ieee80211_ptr;
183 __cfg80211_clear_ibss(dev, nowext);
187 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
188 struct net_device *dev, bool nowext)
190 struct wireless_dev *wdev = dev->ieee80211_ptr;
193 ASSERT_WDEV_LOCK(wdev);
198 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
203 __cfg80211_clear_ibss(dev, nowext);
208 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
209 struct net_device *dev, bool nowext)
211 struct wireless_dev *wdev = dev->ieee80211_ptr;
215 err = __cfg80211_leave_ibss(rdev, dev, nowext);
221 #ifdef CONFIG_CFG80211_WEXT
222 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
223 struct wireless_dev *wdev)
225 struct cfg80211_cached_keys *ck = NULL;
226 enum ieee80211_band band;
229 ASSERT_WDEV_LOCK(wdev);
231 if (!wdev->wext.ibss.beacon_interval)
232 wdev->wext.ibss.beacon_interval = 100;
234 /* try to find an IBSS channel if none requested ... */
235 if (!wdev->wext.ibss.channel) {
236 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
237 struct ieee80211_supported_band *sband;
238 struct ieee80211_channel *chan;
240 sband = rdev->wiphy.bands[band];
244 for (i = 0; i < sband->n_channels; i++) {
245 chan = &sband->channels[i];
246 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
248 if (chan->flags & IEEE80211_CHAN_DISABLED)
250 wdev->wext.ibss.channel = chan;
254 if (wdev->wext.ibss.channel)
258 if (!wdev->wext.ibss.channel)
262 /* don't join -- SSID is not there */
263 if (!wdev->wext.ibss.ssid_len)
266 if (!netif_running(wdev->netdev))
269 if (wdev->wext.keys) {
270 wdev->wext.keys->def = wdev->wext.default_key;
271 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
274 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
276 if (wdev->wext.keys) {
277 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
280 for (i = 0; i < 6; i++)
281 ck->params[i].key = ck->data[i];
283 err = __cfg80211_join_ibss(rdev, wdev->netdev,
284 &wdev->wext.ibss, ck);
291 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
292 struct iw_request_info *info,
293 struct iw_freq *wextfreq, char *extra)
295 struct wireless_dev *wdev = dev->ieee80211_ptr;
296 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
297 struct ieee80211_channel *chan = NULL;
300 /* call only for ibss! */
301 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
304 if (!rdev->ops->join_ibss)
307 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
312 chan = ieee80211_get_channel(wdev->wiphy, freq);
315 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
316 chan->flags & IEEE80211_CHAN_DISABLED)
320 if (wdev->wext.ibss.channel == chan)
326 err = __cfg80211_leave_ibss(rdev, dev, true);
333 wdev->wext.ibss.channel = chan;
334 wdev->wext.ibss.channel_fixed = true;
336 /* cfg80211_ibss_wext_join will pick one if needed */
337 wdev->wext.ibss.channel_fixed = false;
340 mutex_lock(&rdev->devlist_mtx);
342 err = cfg80211_ibss_wext_join(rdev, wdev);
344 mutex_unlock(&rdev->devlist_mtx);
349 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
350 struct iw_request_info *info,
351 struct iw_freq *freq, char *extra)
353 struct wireless_dev *wdev = dev->ieee80211_ptr;
354 struct ieee80211_channel *chan = NULL;
356 /* call only for ibss! */
357 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
361 if (wdev->current_bss)
362 chan = wdev->current_bss->pub.channel;
363 else if (wdev->wext.ibss.channel)
364 chan = wdev->wext.ibss.channel;
368 freq->m = chan->center_freq;
373 /* no channel if not joining */
377 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
378 struct iw_request_info *info,
379 struct iw_point *data, char *ssid)
381 struct wireless_dev *wdev = dev->ieee80211_ptr;
382 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
383 size_t len = data->length;
386 /* call only for ibss! */
387 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
390 if (!rdev->ops->join_ibss)
396 err = __cfg80211_leave_ibss(rdev, dev, true);
402 /* iwconfig uses nul termination in SSID.. */
403 if (len > 0 && ssid[len - 1] == '\0')
406 wdev->wext.ibss.ssid = wdev->ssid;
407 memcpy(wdev->wext.ibss.ssid, ssid, len);
408 wdev->wext.ibss.ssid_len = len;
410 mutex_lock(&rdev->devlist_mtx);
412 err = cfg80211_ibss_wext_join(rdev, wdev);
414 mutex_unlock(&rdev->devlist_mtx);
419 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
420 struct iw_request_info *info,
421 struct iw_point *data, char *ssid)
423 struct wireless_dev *wdev = dev->ieee80211_ptr;
425 /* call only for ibss! */
426 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
432 if (wdev->ssid_len) {
434 data->length = wdev->ssid_len;
435 memcpy(ssid, wdev->ssid, data->length);
436 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
438 data->length = wdev->wext.ibss.ssid_len;
439 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
446 int cfg80211_ibss_wext_siwap(struct net_device *dev,
447 struct iw_request_info *info,
448 struct sockaddr *ap_addr, char *extra)
450 struct wireless_dev *wdev = dev->ieee80211_ptr;
451 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
452 u8 *bssid = ap_addr->sa_data;
455 /* call only for ibss! */
456 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
459 if (!rdev->ops->join_ibss)
462 if (ap_addr->sa_family != ARPHRD_ETHER)
466 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
470 if (!bssid && !wdev->wext.ibss.bssid)
473 /* fixed already - and no change */
474 if (wdev->wext.ibss.bssid && bssid &&
475 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
481 err = __cfg80211_leave_ibss(rdev, dev, true);
488 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
489 wdev->wext.ibss.bssid = wdev->wext.bssid;
491 wdev->wext.ibss.bssid = NULL;
493 mutex_lock(&rdev->devlist_mtx);
495 err = cfg80211_ibss_wext_join(rdev, wdev);
497 mutex_unlock(&rdev->devlist_mtx);
502 int cfg80211_ibss_wext_giwap(struct net_device *dev,
503 struct iw_request_info *info,
504 struct sockaddr *ap_addr, char *extra)
506 struct wireless_dev *wdev = dev->ieee80211_ptr;
508 /* call only for ibss! */
509 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
512 ap_addr->sa_family = ARPHRD_ETHER;
515 if (wdev->current_bss)
516 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
517 else if (wdev->wext.ibss.bssid)
518 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
520 memset(ap_addr->sa_data, 0, ETH_ALEN);