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 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
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_rdev(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_rdev(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 static 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;
93 ASSERT_WDEV_LOCK(wdev);
98 if (!params->basic_rates) {
100 * If no rates were explicitly configured,
101 * use the mandatory rate set for 11b or
102 * 11a for maximum compatibility.
104 struct ieee80211_supported_band *sband =
105 rdev->wiphy.bands[params->chandef.chan->band];
107 u32 flag = params->chandef.chan->band == IEEE80211_BAND_5GHZ ?
108 IEEE80211_RATE_MANDATORY_A :
109 IEEE80211_RATE_MANDATORY_B;
111 for (j = 0; j < sband->n_bitrates; j++) {
112 if (sband->bitrates[j].flags & flag)
113 params->basic_rates |= BIT(j);
117 if (WARN_ON(wdev->connect_keys))
118 kzfree(wdev->connect_keys);
119 wdev->connect_keys = connkeys;
121 wdev->ibss_fixed = params->channel_fixed;
122 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
123 wdev->chandef = params->chandef;
124 #ifdef CONFIG_CFG80211_WEXT
125 wdev->wext.ibss.chandef = params->chandef;
127 err = rdev_join_ibss(rdev, dev, params);
129 wdev->connect_keys = NULL;
133 memcpy(wdev->ssid, params->ssid, params->ssid_len);
134 wdev->ssid_len = params->ssid_len;
139 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
140 struct net_device *dev,
141 struct cfg80211_ibss_params *params,
142 struct cfg80211_cached_keys *connkeys)
144 struct wireless_dev *wdev = dev->ieee80211_ptr;
150 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
156 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
158 struct wireless_dev *wdev = dev->ieee80211_ptr;
159 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
162 ASSERT_WDEV_LOCK(wdev);
164 kzfree(wdev->connect_keys);
165 wdev->connect_keys = NULL;
167 rdev_set_qos_map(rdev, dev, NULL);
170 * Delete all the keys ... pairwise keys can't really
171 * exist any more anyway, but default keys might.
173 if (rdev->ops->del_key)
174 for (i = 0; i < 6; i++)
175 rdev_del_key(rdev, dev, i, false, NULL);
177 if (wdev->current_bss) {
178 cfg80211_unhold_bss(wdev->current_bss);
179 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
182 wdev->current_bss = NULL;
184 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
185 #ifdef CONFIG_CFG80211_WEXT
187 wdev->wext.ibss.ssid_len = 0;
189 cfg80211_sched_dfs_chan_update(rdev);
192 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
194 struct wireless_dev *wdev = dev->ieee80211_ptr;
197 __cfg80211_clear_ibss(dev, nowext);
201 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
202 struct net_device *dev, bool nowext)
204 struct wireless_dev *wdev = dev->ieee80211_ptr;
207 ASSERT_WDEV_LOCK(wdev);
212 err = rdev_leave_ibss(rdev, dev);
217 __cfg80211_clear_ibss(dev, nowext);
222 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
223 struct net_device *dev, bool nowext)
225 struct wireless_dev *wdev = dev->ieee80211_ptr;
229 err = __cfg80211_leave_ibss(rdev, dev, nowext);
235 #ifdef CONFIG_CFG80211_WEXT
236 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
237 struct wireless_dev *wdev)
239 struct cfg80211_cached_keys *ck = NULL;
240 enum ieee80211_band band;
243 ASSERT_WDEV_LOCK(wdev);
245 if (!wdev->wext.ibss.beacon_interval)
246 wdev->wext.ibss.beacon_interval = 100;
248 /* try to find an IBSS channel if none requested ... */
249 if (!wdev->wext.ibss.chandef.chan) {
250 struct ieee80211_channel *new_chan = NULL;
252 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
253 struct ieee80211_supported_band *sband;
254 struct ieee80211_channel *chan;
256 sband = rdev->wiphy.bands[band];
260 for (i = 0; i < sband->n_channels; i++) {
261 chan = &sband->channels[i];
262 if (chan->flags & IEEE80211_CHAN_NO_IR)
264 if (chan->flags & IEEE80211_CHAN_DISABLED)
277 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
281 /* don't join -- SSID is not there */
282 if (!wdev->wext.ibss.ssid_len)
285 if (!netif_running(wdev->netdev))
288 if (wdev->wext.keys) {
289 wdev->wext.keys->def = wdev->wext.default_key;
290 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
293 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
295 if (wdev->wext.keys) {
296 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
299 for (i = 0; i < 6; i++)
300 ck->params[i].key = ck->data[i];
302 err = __cfg80211_join_ibss(rdev, wdev->netdev,
303 &wdev->wext.ibss, ck);
310 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
311 struct iw_request_info *info,
312 struct iw_freq *wextfreq, char *extra)
314 struct wireless_dev *wdev = dev->ieee80211_ptr;
315 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
316 struct ieee80211_channel *chan = NULL;
319 /* call only for ibss! */
320 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
323 if (!rdev->ops->join_ibss)
326 freq = cfg80211_wext_freq(wextfreq);
331 chan = ieee80211_get_channel(wdev->wiphy, freq);
334 if (chan->flags & IEEE80211_CHAN_NO_IR ||
335 chan->flags & IEEE80211_CHAN_DISABLED)
339 if (wdev->wext.ibss.chandef.chan == chan)
345 err = __cfg80211_leave_ibss(rdev, dev, true);
352 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
354 wdev->wext.ibss.channel_fixed = true;
356 /* cfg80211_ibss_wext_join will pick one if needed */
357 wdev->wext.ibss.channel_fixed = false;
361 err = cfg80211_ibss_wext_join(rdev, wdev);
367 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
368 struct iw_request_info *info,
369 struct iw_freq *freq, char *extra)
371 struct wireless_dev *wdev = dev->ieee80211_ptr;
372 struct ieee80211_channel *chan = NULL;
374 /* call only for ibss! */
375 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
379 if (wdev->current_bss)
380 chan = wdev->current_bss->pub.channel;
381 else if (wdev->wext.ibss.chandef.chan)
382 chan = wdev->wext.ibss.chandef.chan;
386 freq->m = chan->center_freq;
391 /* no channel if not joining */
395 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
396 struct iw_request_info *info,
397 struct iw_point *data, char *ssid)
399 struct wireless_dev *wdev = dev->ieee80211_ptr;
400 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
401 size_t len = data->length;
404 /* call only for ibss! */
405 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
408 if (!rdev->ops->join_ibss)
414 err = __cfg80211_leave_ibss(rdev, dev, true);
420 /* iwconfig uses nul termination in SSID.. */
421 if (len > 0 && ssid[len - 1] == '\0')
424 memcpy(wdev->ssid, ssid, len);
425 wdev->wext.ibss.ssid = wdev->ssid;
426 wdev->wext.ibss.ssid_len = len;
429 err = cfg80211_ibss_wext_join(rdev, wdev);
435 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
436 struct iw_request_info *info,
437 struct iw_point *data, char *ssid)
439 struct wireless_dev *wdev = dev->ieee80211_ptr;
441 /* call only for ibss! */
442 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
448 if (wdev->ssid_len) {
450 data->length = wdev->ssid_len;
451 memcpy(ssid, wdev->ssid, data->length);
452 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
454 data->length = wdev->wext.ibss.ssid_len;
455 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
462 int cfg80211_ibss_wext_siwap(struct net_device *dev,
463 struct iw_request_info *info,
464 struct sockaddr *ap_addr, char *extra)
466 struct wireless_dev *wdev = dev->ieee80211_ptr;
467 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
468 u8 *bssid = ap_addr->sa_data;
471 /* call only for ibss! */
472 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
475 if (!rdev->ops->join_ibss)
478 if (ap_addr->sa_family != ARPHRD_ETHER)
482 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
485 if (bssid && !is_valid_ether_addr(bssid))
489 if (!bssid && !wdev->wext.ibss.bssid)
492 /* fixed already - and no change */
493 if (wdev->wext.ibss.bssid && bssid &&
494 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
500 err = __cfg80211_leave_ibss(rdev, dev, true);
507 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
508 wdev->wext.ibss.bssid = wdev->wext.bssid;
510 wdev->wext.ibss.bssid = NULL;
513 err = cfg80211_ibss_wext_join(rdev, wdev);
519 int cfg80211_ibss_wext_giwap(struct net_device *dev,
520 struct iw_request_info *info,
521 struct sockaddr *ap_addr, char *extra)
523 struct wireless_dev *wdev = dev->ieee80211_ptr;
525 /* call only for ibss! */
526 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
529 ap_addr->sa_family = ARPHRD_ETHER;
532 if (wdev->current_bss)
533 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
534 else if (wdev->wext.ibss.bssid)
535 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
537 eth_zero_addr(ap_addr->sa_data);