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 <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;
193 cfg80211_sched_dfs_chan_update(rdev);
196 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
198 struct wireless_dev *wdev = dev->ieee80211_ptr;
201 __cfg80211_clear_ibss(dev, nowext);
205 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
206 struct net_device *dev, bool nowext)
208 struct wireless_dev *wdev = dev->ieee80211_ptr;
211 ASSERT_WDEV_LOCK(wdev);
216 err = rdev_leave_ibss(rdev, dev);
221 __cfg80211_clear_ibss(dev, nowext);
226 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
227 struct net_device *dev, bool nowext)
229 struct wireless_dev *wdev = dev->ieee80211_ptr;
233 err = __cfg80211_leave_ibss(rdev, dev, nowext);
239 #ifdef CONFIG_CFG80211_WEXT
240 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
241 struct wireless_dev *wdev)
243 struct cfg80211_cached_keys *ck = NULL;
244 enum nl80211_band band;
247 ASSERT_WDEV_LOCK(wdev);
249 if (!wdev->wext.ibss.beacon_interval)
250 wdev->wext.ibss.beacon_interval = 100;
252 /* try to find an IBSS channel if none requested ... */
253 if (!wdev->wext.ibss.chandef.chan) {
254 struct ieee80211_channel *new_chan = NULL;
256 for (band = 0; band < NUM_NL80211_BANDS; band++) {
257 struct ieee80211_supported_band *sband;
258 struct ieee80211_channel *chan;
260 sband = rdev->wiphy.bands[band];
264 for (i = 0; i < sband->n_channels; i++) {
265 chan = &sband->channels[i];
266 if (chan->flags & IEEE80211_CHAN_NO_IR)
268 if (chan->flags & IEEE80211_CHAN_DISABLED)
281 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
285 /* don't join -- SSID is not there */
286 if (!wdev->wext.ibss.ssid_len)
289 if (!netif_running(wdev->netdev))
293 wdev->wext.keys->def = wdev->wext.default_key;
295 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
297 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
298 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
301 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
302 ck->params[i].key = ck->data[i];
304 err = __cfg80211_join_ibss(rdev, wdev->netdev,
305 &wdev->wext.ibss, ck);
312 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
313 struct iw_request_info *info,
314 struct iw_freq *wextfreq, char *extra)
316 struct wireless_dev *wdev = dev->ieee80211_ptr;
317 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
318 struct ieee80211_channel *chan = NULL;
321 /* call only for ibss! */
322 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
325 if (!rdev->ops->join_ibss)
328 freq = cfg80211_wext_freq(wextfreq);
333 chan = ieee80211_get_channel(wdev->wiphy, freq);
336 if (chan->flags & IEEE80211_CHAN_NO_IR ||
337 chan->flags & IEEE80211_CHAN_DISABLED)
341 if (wdev->wext.ibss.chandef.chan == chan)
347 err = __cfg80211_leave_ibss(rdev, dev, true);
354 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
356 wdev->wext.ibss.channel_fixed = true;
358 /* cfg80211_ibss_wext_join will pick one if needed */
359 wdev->wext.ibss.channel_fixed = false;
363 err = cfg80211_ibss_wext_join(rdev, wdev);
369 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
370 struct iw_request_info *info,
371 struct iw_freq *freq, char *extra)
373 struct wireless_dev *wdev = dev->ieee80211_ptr;
374 struct ieee80211_channel *chan = NULL;
376 /* call only for ibss! */
377 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
381 if (wdev->current_bss)
382 chan = wdev->current_bss->pub.channel;
383 else if (wdev->wext.ibss.chandef.chan)
384 chan = wdev->wext.ibss.chandef.chan;
388 freq->m = chan->center_freq;
393 /* no channel if not joining */
397 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
398 struct iw_request_info *info,
399 struct iw_point *data, char *ssid)
401 struct wireless_dev *wdev = dev->ieee80211_ptr;
402 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
403 size_t len = data->length;
406 /* call only for ibss! */
407 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
410 if (!rdev->ops->join_ibss)
416 err = __cfg80211_leave_ibss(rdev, dev, true);
422 /* iwconfig uses nul termination in SSID.. */
423 if (len > 0 && ssid[len - 1] == '\0')
426 memcpy(wdev->ssid, ssid, len);
427 wdev->wext.ibss.ssid = wdev->ssid;
428 wdev->wext.ibss.ssid_len = len;
431 err = cfg80211_ibss_wext_join(rdev, wdev);
437 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
438 struct iw_request_info *info,
439 struct iw_point *data, char *ssid)
441 struct wireless_dev *wdev = dev->ieee80211_ptr;
443 /* call only for ibss! */
444 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
450 if (wdev->ssid_len) {
452 data->length = wdev->ssid_len;
453 memcpy(ssid, wdev->ssid, data->length);
454 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
456 data->length = wdev->wext.ibss.ssid_len;
457 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
464 int cfg80211_ibss_wext_siwap(struct net_device *dev,
465 struct iw_request_info *info,
466 struct sockaddr *ap_addr, char *extra)
468 struct wireless_dev *wdev = dev->ieee80211_ptr;
469 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
470 u8 *bssid = ap_addr->sa_data;
473 /* call only for ibss! */
474 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
477 if (!rdev->ops->join_ibss)
480 if (ap_addr->sa_family != ARPHRD_ETHER)
484 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
487 if (bssid && !is_valid_ether_addr(bssid))
491 if (!bssid && !wdev->wext.ibss.bssid)
494 /* fixed already - and no change */
495 if (wdev->wext.ibss.bssid && bssid &&
496 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
502 err = __cfg80211_leave_ibss(rdev, dev, true);
509 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
510 wdev->wext.ibss.bssid = wdev->wext.bssid;
512 wdev->wext.ibss.bssid = NULL;
515 err = cfg80211_ibss_wext_join(rdev, wdev);
521 int cfg80211_ibss_wext_giwap(struct net_device *dev,
522 struct iw_request_info *info,
523 struct sockaddr *ap_addr, char *extra)
525 struct wireless_dev *wdev = dev->ieee80211_ptr;
527 /* call only for ibss! */
528 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
531 ap_addr->sa_family = ARPHRD_ETHER;
534 if (wdev->current_bss)
535 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
536 else if (wdev->wext.ibss.bssid)
537 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
539 eth_zero_addr(ap_addr->sa_data);