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,
18 struct ieee80211_channel *channel)
20 struct wireless_dev *wdev = dev->ieee80211_ptr;
21 struct cfg80211_bss *bss;
22 #ifdef CONFIG_CFG80211_WEXT
23 union iwreq_data wrqu;
26 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
32 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
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,
58 struct ieee80211_channel *channel, 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, channel);
67 if (WARN_ON(!channel))
70 ev = kzalloc(sizeof(*ev), gfp);
74 ev->type = EVENT_IBSS_JOINED;
75 memcpy(ev->ij.bssid, bssid, ETH_ALEN);
76 ev->ij.channel = channel;
78 spin_lock_irqsave(&wdev->event_lock, flags);
79 list_add_tail(&ev->list, &wdev->event_list);
80 spin_unlock_irqrestore(&wdev->event_lock, flags);
81 queue_work(cfg80211_wq, &rdev->event_work);
83 EXPORT_SYMBOL(cfg80211_ibss_joined);
85 int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
86 struct net_device *dev,
87 struct cfg80211_ibss_params *params,
88 struct cfg80211_cached_keys *connkeys)
90 struct wireless_dev *wdev = dev->ieee80211_ptr;
91 struct ieee80211_channel *check_chan;
92 u8 radar_detect_width = 0;
95 ASSERT_WDEV_LOCK(wdev);
100 if (!params->basic_rates) {
102 * If no rates were explicitly configured,
103 * use the mandatory rate set for 11b or
104 * 11a for maximum compatibility.
106 struct ieee80211_supported_band *sband =
107 rdev->wiphy.bands[params->chandef.chan->band];
109 u32 flag = params->chandef.chan->band == IEEE80211_BAND_5GHZ ?
110 IEEE80211_RATE_MANDATORY_A :
111 IEEE80211_RATE_MANDATORY_B;
113 for (j = 0; j < sband->n_bitrates; j++) {
114 if (sband->bitrates[j].flags & flag)
115 params->basic_rates |= BIT(j);
119 if (WARN_ON(wdev->connect_keys))
120 kfree(wdev->connect_keys);
121 wdev->connect_keys = connkeys;
123 wdev->ibss_fixed = params->channel_fixed;
124 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
125 #ifdef CONFIG_CFG80211_WEXT
126 wdev->wext.ibss.chandef = params->chandef;
128 check_chan = params->chandef.chan;
129 if (params->userspace_handles_dfs) {
130 /* use channel NULL to check for radar even if the current
131 * channel is not a radar channel - it might decide to change
132 * to DFS channel later.
134 radar_detect_width = BIT(params->chandef.width);
138 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
140 (params->channel_fixed &&
143 : CHAN_MODE_EXCLUSIVE,
147 wdev->connect_keys = NULL;
151 err = rdev_join_ibss(rdev, dev, params);
153 wdev->connect_keys = NULL;
157 memcpy(wdev->ssid, params->ssid, params->ssid_len);
158 wdev->ssid_len = params->ssid_len;
163 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
164 struct net_device *dev,
165 struct cfg80211_ibss_params *params,
166 struct cfg80211_cached_keys *connkeys)
168 struct wireless_dev *wdev = dev->ieee80211_ptr;
174 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
180 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
182 struct wireless_dev *wdev = dev->ieee80211_ptr;
183 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
186 ASSERT_WDEV_LOCK(wdev);
188 kfree(wdev->connect_keys);
189 wdev->connect_keys = NULL;
191 rdev_set_qos_map(rdev, dev, NULL);
194 * Delete all the keys ... pairwise keys can't really
195 * exist any more anyway, but default keys might.
197 if (rdev->ops->del_key)
198 for (i = 0; i < 6; i++)
199 rdev_del_key(rdev, dev, i, false, NULL);
201 if (wdev->current_bss) {
202 cfg80211_unhold_bss(wdev->current_bss);
203 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
206 wdev->current_bss = NULL;
208 #ifdef CONFIG_CFG80211_WEXT
210 wdev->wext.ibss.ssid_len = 0;
214 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
216 struct wireless_dev *wdev = dev->ieee80211_ptr;
219 __cfg80211_clear_ibss(dev, nowext);
223 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
224 struct net_device *dev, bool nowext)
226 struct wireless_dev *wdev = dev->ieee80211_ptr;
229 ASSERT_WDEV_LOCK(wdev);
234 err = rdev_leave_ibss(rdev, dev);
239 __cfg80211_clear_ibss(dev, nowext);
244 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
245 struct net_device *dev, bool nowext)
247 struct wireless_dev *wdev = dev->ieee80211_ptr;
251 err = __cfg80211_leave_ibss(rdev, dev, nowext);
257 #ifdef CONFIG_CFG80211_WEXT
258 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
259 struct wireless_dev *wdev)
261 struct cfg80211_cached_keys *ck = NULL;
262 enum ieee80211_band band;
265 ASSERT_WDEV_LOCK(wdev);
267 if (!wdev->wext.ibss.beacon_interval)
268 wdev->wext.ibss.beacon_interval = 100;
270 /* try to find an IBSS channel if none requested ... */
271 if (!wdev->wext.ibss.chandef.chan) {
272 struct ieee80211_channel *new_chan = NULL;
274 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
275 struct ieee80211_supported_band *sband;
276 struct ieee80211_channel *chan;
278 sband = rdev->wiphy.bands[band];
282 for (i = 0; i < sband->n_channels; i++) {
283 chan = &sband->channels[i];
284 if (chan->flags & IEEE80211_CHAN_NO_IR)
286 if (chan->flags & IEEE80211_CHAN_DISABLED)
299 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
303 /* don't join -- SSID is not there */
304 if (!wdev->wext.ibss.ssid_len)
307 if (!netif_running(wdev->netdev))
310 if (wdev->wext.keys) {
311 wdev->wext.keys->def = wdev->wext.default_key;
312 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
315 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
317 if (wdev->wext.keys) {
318 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
321 for (i = 0; i < 6; i++)
322 ck->params[i].key = ck->data[i];
324 err = __cfg80211_join_ibss(rdev, wdev->netdev,
325 &wdev->wext.ibss, ck);
332 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
333 struct iw_request_info *info,
334 struct iw_freq *wextfreq, char *extra)
336 struct wireless_dev *wdev = dev->ieee80211_ptr;
337 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
338 struct ieee80211_channel *chan = NULL;
341 /* call only for ibss! */
342 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
345 if (!rdev->ops->join_ibss)
348 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
353 chan = ieee80211_get_channel(wdev->wiphy, freq);
356 if (chan->flags & IEEE80211_CHAN_NO_IR ||
357 chan->flags & IEEE80211_CHAN_DISABLED)
361 if (wdev->wext.ibss.chandef.chan == chan)
367 err = __cfg80211_leave_ibss(rdev, dev, true);
374 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
376 wdev->wext.ibss.channel_fixed = true;
378 /* cfg80211_ibss_wext_join will pick one if needed */
379 wdev->wext.ibss.channel_fixed = false;
383 err = cfg80211_ibss_wext_join(rdev, wdev);
389 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
390 struct iw_request_info *info,
391 struct iw_freq *freq, char *extra)
393 struct wireless_dev *wdev = dev->ieee80211_ptr;
394 struct ieee80211_channel *chan = NULL;
396 /* call only for ibss! */
397 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
401 if (wdev->current_bss)
402 chan = wdev->current_bss->pub.channel;
403 else if (wdev->wext.ibss.chandef.chan)
404 chan = wdev->wext.ibss.chandef.chan;
408 freq->m = chan->center_freq;
413 /* no channel if not joining */
417 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
418 struct iw_request_info *info,
419 struct iw_point *data, char *ssid)
421 struct wireless_dev *wdev = dev->ieee80211_ptr;
422 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
423 size_t len = data->length;
426 /* call only for ibss! */
427 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
430 if (!rdev->ops->join_ibss)
436 err = __cfg80211_leave_ibss(rdev, dev, true);
442 /* iwconfig uses nul termination in SSID.. */
443 if (len > 0 && ssid[len - 1] == '\0')
446 wdev->wext.ibss.ssid = wdev->ssid;
447 memcpy(wdev->wext.ibss.ssid, ssid, len);
448 wdev->wext.ibss.ssid_len = len;
451 err = cfg80211_ibss_wext_join(rdev, wdev);
457 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
458 struct iw_request_info *info,
459 struct iw_point *data, char *ssid)
461 struct wireless_dev *wdev = dev->ieee80211_ptr;
463 /* call only for ibss! */
464 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
470 if (wdev->ssid_len) {
472 data->length = wdev->ssid_len;
473 memcpy(ssid, wdev->ssid, data->length);
474 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
476 data->length = wdev->wext.ibss.ssid_len;
477 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
484 int cfg80211_ibss_wext_siwap(struct net_device *dev,
485 struct iw_request_info *info,
486 struct sockaddr *ap_addr, char *extra)
488 struct wireless_dev *wdev = dev->ieee80211_ptr;
489 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
490 u8 *bssid = ap_addr->sa_data;
493 /* call only for ibss! */
494 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
497 if (!rdev->ops->join_ibss)
500 if (ap_addr->sa_family != ARPHRD_ETHER)
504 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
508 if (!bssid && !wdev->wext.ibss.bssid)
511 /* fixed already - and no change */
512 if (wdev->wext.ibss.bssid && bssid &&
513 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
519 err = __cfg80211_leave_ibss(rdev, dev, true);
526 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
527 wdev->wext.ibss.bssid = wdev->wext.bssid;
529 wdev->wext.ibss.bssid = NULL;
532 err = cfg80211_ibss_wext_join(rdev, wdev);
538 int cfg80211_ibss_wext_giwap(struct net_device *dev,
539 struct iw_request_info *info,
540 struct sockaddr *ap_addr, char *extra)
542 struct wireless_dev *wdev = dev->ieee80211_ptr;
544 /* call only for ibss! */
545 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
548 ap_addr->sa_family = ARPHRD_ETHER;
551 if (wdev->current_bss)
552 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
553 else if (wdev->wext.ibss.bssid)
554 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
556 memset(ap_addr->sa_data, 0, ETH_ALEN);