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 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
47 cfg80211_upload_connect_keys(wdev);
49 nl80211_send_ibss_bssid(wiphy_to_rdev(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,
59 struct ieee80211_channel *channel, gfp_t gfp)
61 struct wireless_dev *wdev = dev->ieee80211_ptr;
62 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
63 struct cfg80211_event *ev;
66 trace_cfg80211_ibss_joined(dev, bssid, channel);
68 if (WARN_ON(!channel))
71 ev = kzalloc(sizeof(*ev), gfp);
75 ev->type = EVENT_IBSS_JOINED;
76 memcpy(ev->ij.bssid, bssid, ETH_ALEN);
77 ev->ij.channel = channel;
79 spin_lock_irqsave(&wdev->event_lock, flags);
80 list_add_tail(&ev->list, &wdev->event_list);
81 spin_unlock_irqrestore(&wdev->event_lock, flags);
82 queue_work(cfg80211_wq, &rdev->event_work);
84 EXPORT_SYMBOL(cfg80211_ibss_joined);
86 static int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
87 struct net_device *dev,
88 struct cfg80211_ibss_params *params,
89 struct cfg80211_cached_keys *connkeys)
91 struct wireless_dev *wdev = dev->ieee80211_ptr;
94 ASSERT_WDEV_LOCK(wdev);
99 if (!params->basic_rates) {
101 * If no rates were explicitly configured,
102 * use the mandatory rate set for 11b or
103 * 11a for maximum compatibility.
105 struct ieee80211_supported_band *sband =
106 rdev->wiphy.bands[params->chandef.chan->band];
108 u32 flag = params->chandef.chan->band == NL80211_BAND_5GHZ ?
109 IEEE80211_RATE_MANDATORY_A :
110 IEEE80211_RATE_MANDATORY_B;
112 for (j = 0; j < sband->n_bitrates; j++) {
113 if (sband->bitrates[j].flags & flag)
114 params->basic_rates |= BIT(j);
118 if (WARN_ON(connkeys && connkeys->def < 0))
121 if (WARN_ON(wdev->connect_keys))
122 kzfree(wdev->connect_keys);
123 wdev->connect_keys = connkeys;
125 wdev->ibss_fixed = params->channel_fixed;
126 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
127 wdev->chandef = params->chandef;
128 #ifdef CONFIG_CFG80211_WEXT
129 wdev->wext.ibss.chandef = params->chandef;
131 err = rdev_join_ibss(rdev, dev, params);
133 wdev->connect_keys = NULL;
137 memcpy(wdev->ssid, params->ssid, params->ssid_len);
138 wdev->ssid_len = params->ssid_len;
143 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
144 struct net_device *dev,
145 struct cfg80211_ibss_params *params,
146 struct cfg80211_cached_keys *connkeys)
148 struct wireless_dev *wdev = dev->ieee80211_ptr;
154 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
160 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
162 struct wireless_dev *wdev = dev->ieee80211_ptr;
163 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
166 ASSERT_WDEV_LOCK(wdev);
168 kzfree(wdev->connect_keys);
169 wdev->connect_keys = NULL;
171 rdev_set_qos_map(rdev, dev, NULL);
174 * Delete all the keys ... pairwise keys can't really
175 * exist any more anyway, but default keys might.
177 if (rdev->ops->del_key)
178 for (i = 0; i < 6; i++)
179 rdev_del_key(rdev, dev, i, false, NULL);
181 if (wdev->current_bss) {
182 cfg80211_unhold_bss(wdev->current_bss);
183 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
186 wdev->current_bss = NULL;
188 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
189 #ifdef CONFIG_CFG80211_WEXT
191 wdev->wext.ibss.ssid_len = 0;
195 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
197 struct wireless_dev *wdev = dev->ieee80211_ptr;
200 __cfg80211_clear_ibss(dev, nowext);
204 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
205 struct net_device *dev, bool nowext)
207 struct wireless_dev *wdev = dev->ieee80211_ptr;
210 ASSERT_WDEV_LOCK(wdev);
215 err = rdev_leave_ibss(rdev, dev);
220 __cfg80211_clear_ibss(dev, nowext);
225 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
226 struct net_device *dev, bool nowext)
228 struct wireless_dev *wdev = dev->ieee80211_ptr;
232 err = __cfg80211_leave_ibss(rdev, dev, nowext);
238 #ifdef CONFIG_CFG80211_WEXT
239 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
240 struct wireless_dev *wdev)
242 struct cfg80211_cached_keys *ck = NULL;
243 enum nl80211_band band;
246 ASSERT_WDEV_LOCK(wdev);
248 if (!wdev->wext.ibss.beacon_interval)
249 wdev->wext.ibss.beacon_interval = 100;
251 /* try to find an IBSS channel if none requested ... */
252 if (!wdev->wext.ibss.chandef.chan) {
253 struct ieee80211_channel *new_chan = NULL;
255 for (band = 0; band < NUM_NL80211_BANDS; band++) {
256 struct ieee80211_supported_band *sband;
257 struct ieee80211_channel *chan;
259 sband = rdev->wiphy.bands[band];
263 for (i = 0; i < sband->n_channels; i++) {
264 chan = &sband->channels[i];
265 if (chan->flags & IEEE80211_CHAN_NO_IR)
267 if (chan->flags & IEEE80211_CHAN_DISABLED)
280 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
284 /* don't join -- SSID is not there */
285 if (!wdev->wext.ibss.ssid_len)
288 if (!netif_running(wdev->netdev))
292 wdev->wext.keys->def = wdev->wext.default_key;
294 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
296 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
297 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
300 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
301 ck->params[i].key = ck->data[i];
303 err = __cfg80211_join_ibss(rdev, wdev->netdev,
304 &wdev->wext.ibss, ck);
311 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
312 struct iw_request_info *info,
313 struct iw_freq *wextfreq, char *extra)
315 struct wireless_dev *wdev = dev->ieee80211_ptr;
316 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
317 struct ieee80211_channel *chan = NULL;
320 /* call only for ibss! */
321 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
324 if (!rdev->ops->join_ibss)
327 freq = cfg80211_wext_freq(wextfreq);
332 chan = ieee80211_get_channel(wdev->wiphy, freq);
335 if (chan->flags & IEEE80211_CHAN_NO_IR ||
336 chan->flags & IEEE80211_CHAN_DISABLED)
340 if (wdev->wext.ibss.chandef.chan == chan)
346 err = __cfg80211_leave_ibss(rdev, dev, true);
353 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
355 wdev->wext.ibss.channel_fixed = true;
357 /* cfg80211_ibss_wext_join will pick one if needed */
358 wdev->wext.ibss.channel_fixed = false;
362 err = cfg80211_ibss_wext_join(rdev, wdev);
368 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
369 struct iw_request_info *info,
370 struct iw_freq *freq, char *extra)
372 struct wireless_dev *wdev = dev->ieee80211_ptr;
373 struct ieee80211_channel *chan = NULL;
375 /* call only for ibss! */
376 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
380 if (wdev->current_bss)
381 chan = wdev->current_bss->pub.channel;
382 else if (wdev->wext.ibss.chandef.chan)
383 chan = wdev->wext.ibss.chandef.chan;
387 freq->m = chan->center_freq;
392 /* no channel if not joining */
396 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
397 struct iw_request_info *info,
398 struct iw_point *data, char *ssid)
400 struct wireless_dev *wdev = dev->ieee80211_ptr;
401 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
402 size_t len = data->length;
405 /* call only for ibss! */
406 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
409 if (!rdev->ops->join_ibss)
415 err = __cfg80211_leave_ibss(rdev, dev, true);
421 /* iwconfig uses nul termination in SSID.. */
422 if (len > 0 && ssid[len - 1] == '\0')
425 memcpy(wdev->ssid, ssid, len);
426 wdev->wext.ibss.ssid = wdev->ssid;
427 wdev->wext.ibss.ssid_len = len;
430 err = cfg80211_ibss_wext_join(rdev, wdev);
436 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
437 struct iw_request_info *info,
438 struct iw_point *data, char *ssid)
440 struct wireless_dev *wdev = dev->ieee80211_ptr;
442 /* call only for ibss! */
443 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
449 if (wdev->ssid_len) {
451 data->length = wdev->ssid_len;
452 memcpy(ssid, wdev->ssid, data->length);
453 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
455 data->length = wdev->wext.ibss.ssid_len;
456 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
463 int cfg80211_ibss_wext_siwap(struct net_device *dev,
464 struct iw_request_info *info,
465 struct sockaddr *ap_addr, char *extra)
467 struct wireless_dev *wdev = dev->ieee80211_ptr;
468 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
469 u8 *bssid = ap_addr->sa_data;
472 /* call only for ibss! */
473 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
476 if (!rdev->ops->join_ibss)
479 if (ap_addr->sa_family != ARPHRD_ETHER)
483 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
486 if (bssid && !is_valid_ether_addr(bssid))
490 if (!bssid && !wdev->wext.ibss.bssid)
493 /* fixed already - and no change */
494 if (wdev->wext.ibss.bssid && bssid &&
495 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
501 err = __cfg80211_leave_ibss(rdev, dev, true);
508 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
509 wdev->wext.ibss.bssid = wdev->wext.bssid;
511 wdev->wext.ibss.bssid = NULL;
514 err = cfg80211_ibss_wext_join(rdev, wdev);
520 int cfg80211_ibss_wext_giwap(struct net_device *dev,
521 struct iw_request_info *info,
522 struct sockaddr *ap_addr, char *extra)
524 struct wireless_dev *wdev = dev->ieee80211_ptr;
526 /* call only for ibss! */
527 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
530 ap_addr->sa_family = ARPHRD_ETHER;
533 if (wdev->current_bss)
534 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
535 else if (wdev->wext.ibss.bssid)
536 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
538 eth_zero_addr(ap_addr->sa_data);