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 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 trace_cfg80211_ibss_joined(dev, bssid);
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;
86 struct ieee80211_channel *check_chan;
87 u8 radar_detect_width = 0;
90 ASSERT_WDEV_LOCK(wdev);
95 if (!params->basic_rates) {
97 * If no rates were explicitly configured,
98 * use the mandatory rate set for 11b or
99 * 11a for maximum compatibility.
101 struct ieee80211_supported_band *sband =
102 rdev->wiphy.bands[params->chandef.chan->band];
104 u32 flag = params->chandef.chan->band == IEEE80211_BAND_5GHZ ?
105 IEEE80211_RATE_MANDATORY_A :
106 IEEE80211_RATE_MANDATORY_B;
108 for (j = 0; j < sband->n_bitrates; j++) {
109 if (sband->bitrates[j].flags & flag)
110 params->basic_rates |= BIT(j);
114 if (WARN_ON(wdev->connect_keys))
115 kfree(wdev->connect_keys);
116 wdev->connect_keys = connkeys;
118 wdev->ibss_fixed = params->channel_fixed;
119 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
120 #ifdef CONFIG_CFG80211_WEXT
121 wdev->wext.ibss.chandef = params->chandef;
123 check_chan = params->chandef.chan;
124 if (params->userspace_handles_dfs) {
125 /* use channel NULL to check for radar even if the current
126 * channel is not a radar channel - it might decide to change
127 * to DFS channel later.
129 radar_detect_width = BIT(params->chandef.width);
133 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
135 (params->channel_fixed &&
138 : CHAN_MODE_EXCLUSIVE,
142 wdev->connect_keys = NULL;
146 err = rdev_join_ibss(rdev, dev, params);
148 wdev->connect_keys = NULL;
152 memcpy(wdev->ssid, params->ssid, params->ssid_len);
153 wdev->ssid_len = params->ssid_len;
158 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
159 struct net_device *dev,
160 struct cfg80211_ibss_params *params,
161 struct cfg80211_cached_keys *connkeys)
163 struct wireless_dev *wdev = dev->ieee80211_ptr;
169 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
175 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
177 struct wireless_dev *wdev = dev->ieee80211_ptr;
178 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
181 ASSERT_WDEV_LOCK(wdev);
183 kfree(wdev->connect_keys);
184 wdev->connect_keys = NULL;
186 rdev_set_qos_map(rdev, dev, NULL);
189 * Delete all the keys ... pairwise keys can't really
190 * exist any more anyway, but default keys might.
192 if (rdev->ops->del_key)
193 for (i = 0; i < 6; i++)
194 rdev_del_key(rdev, dev, i, false, NULL);
196 if (wdev->current_bss) {
197 cfg80211_unhold_bss(wdev->current_bss);
198 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
201 wdev->current_bss = NULL;
203 #ifdef CONFIG_CFG80211_WEXT
205 wdev->wext.ibss.ssid_len = 0;
209 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
211 struct wireless_dev *wdev = dev->ieee80211_ptr;
214 __cfg80211_clear_ibss(dev, nowext);
218 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
219 struct net_device *dev, bool nowext)
221 struct wireless_dev *wdev = dev->ieee80211_ptr;
224 ASSERT_WDEV_LOCK(wdev);
229 err = rdev_leave_ibss(rdev, dev);
234 __cfg80211_clear_ibss(dev, nowext);
239 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
240 struct net_device *dev, bool nowext)
242 struct wireless_dev *wdev = dev->ieee80211_ptr;
246 err = __cfg80211_leave_ibss(rdev, dev, nowext);
252 #ifdef CONFIG_CFG80211_WEXT
253 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
254 struct wireless_dev *wdev)
256 struct cfg80211_cached_keys *ck = NULL;
257 enum ieee80211_band band;
260 ASSERT_WDEV_LOCK(wdev);
262 if (!wdev->wext.ibss.beacon_interval)
263 wdev->wext.ibss.beacon_interval = 100;
265 /* try to find an IBSS channel if none requested ... */
266 if (!wdev->wext.ibss.chandef.chan) {
267 struct ieee80211_channel *new_chan = NULL;
269 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
270 struct ieee80211_supported_band *sband;
271 struct ieee80211_channel *chan;
273 sband = rdev->wiphy.bands[band];
277 for (i = 0; i < sband->n_channels; i++) {
278 chan = &sband->channels[i];
279 if (chan->flags & IEEE80211_CHAN_NO_IR)
281 if (chan->flags & IEEE80211_CHAN_DISABLED)
294 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
298 /* don't join -- SSID is not there */
299 if (!wdev->wext.ibss.ssid_len)
302 if (!netif_running(wdev->netdev))
305 if (wdev->wext.keys) {
306 wdev->wext.keys->def = wdev->wext.default_key;
307 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
310 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
312 if (wdev->wext.keys) {
313 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
316 for (i = 0; i < 6; i++)
317 ck->params[i].key = ck->data[i];
319 err = __cfg80211_join_ibss(rdev, wdev->netdev,
320 &wdev->wext.ibss, ck);
327 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
328 struct iw_request_info *info,
329 struct iw_freq *wextfreq, char *extra)
331 struct wireless_dev *wdev = dev->ieee80211_ptr;
332 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
333 struct ieee80211_channel *chan = NULL;
336 /* call only for ibss! */
337 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
340 if (!rdev->ops->join_ibss)
343 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
348 chan = ieee80211_get_channel(wdev->wiphy, freq);
351 if (chan->flags & IEEE80211_CHAN_NO_IR ||
352 chan->flags & IEEE80211_CHAN_DISABLED)
356 if (wdev->wext.ibss.chandef.chan == chan)
362 err = __cfg80211_leave_ibss(rdev, dev, true);
369 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
371 wdev->wext.ibss.channel_fixed = true;
373 /* cfg80211_ibss_wext_join will pick one if needed */
374 wdev->wext.ibss.channel_fixed = false;
378 err = cfg80211_ibss_wext_join(rdev, wdev);
384 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
385 struct iw_request_info *info,
386 struct iw_freq *freq, char *extra)
388 struct wireless_dev *wdev = dev->ieee80211_ptr;
389 struct ieee80211_channel *chan = NULL;
391 /* call only for ibss! */
392 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
396 if (wdev->current_bss)
397 chan = wdev->current_bss->pub.channel;
398 else if (wdev->wext.ibss.chandef.chan)
399 chan = wdev->wext.ibss.chandef.chan;
403 freq->m = chan->center_freq;
408 /* no channel if not joining */
412 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
413 struct iw_request_info *info,
414 struct iw_point *data, char *ssid)
416 struct wireless_dev *wdev = dev->ieee80211_ptr;
417 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
418 size_t len = data->length;
421 /* call only for ibss! */
422 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
425 if (!rdev->ops->join_ibss)
431 err = __cfg80211_leave_ibss(rdev, dev, true);
437 /* iwconfig uses nul termination in SSID.. */
438 if (len > 0 && ssid[len - 1] == '\0')
441 wdev->wext.ibss.ssid = wdev->ssid;
442 memcpy(wdev->wext.ibss.ssid, ssid, len);
443 wdev->wext.ibss.ssid_len = len;
446 err = cfg80211_ibss_wext_join(rdev, wdev);
452 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
453 struct iw_request_info *info,
454 struct iw_point *data, char *ssid)
456 struct wireless_dev *wdev = dev->ieee80211_ptr;
458 /* call only for ibss! */
459 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
465 if (wdev->ssid_len) {
467 data->length = wdev->ssid_len;
468 memcpy(ssid, wdev->ssid, data->length);
469 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
471 data->length = wdev->wext.ibss.ssid_len;
472 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
479 int cfg80211_ibss_wext_siwap(struct net_device *dev,
480 struct iw_request_info *info,
481 struct sockaddr *ap_addr, char *extra)
483 struct wireless_dev *wdev = dev->ieee80211_ptr;
484 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
485 u8 *bssid = ap_addr->sa_data;
488 /* call only for ibss! */
489 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
492 if (!rdev->ops->join_ibss)
495 if (ap_addr->sa_family != ARPHRD_ETHER)
499 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
503 if (!bssid && !wdev->wext.ibss.bssid)
506 /* fixed already - and no change */
507 if (wdev->wext.ibss.bssid && bssid &&
508 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
514 err = __cfg80211_leave_ibss(rdev, dev, true);
521 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
522 wdev->wext.ibss.bssid = wdev->wext.bssid;
524 wdev->wext.ibss.bssid = NULL;
527 err = cfg80211_ibss_wext_join(rdev, wdev);
533 int cfg80211_ibss_wext_giwap(struct net_device *dev,
534 struct iw_request_info *info,
535 struct sockaddr *ap_addr, char *extra)
537 struct wireless_dev *wdev = dev->ieee80211_ptr;
539 /* call only for ibss! */
540 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
543 ap_addr->sa_family = ARPHRD_ETHER;
546 if (wdev->current_bss)
547 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
548 else if (wdev->wext.ibss.bssid)
549 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
551 memset(ap_addr->sa_data, 0, ETH_ALEN);