blob: d2a19b0ff71f134544ac1afc65dd5d3fe3bc4891 [file] [log] [blame]
Johannes Berg04a773a2009-04-19 21:24:32 +02001/*
2 * Some IBSS support code for cfg80211.
3 *
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7#include <linux/etherdevice.h>
8#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040010#include <linux/export.h>
Johannes Berg04a773a2009-04-19 21:24:32 +020011#include <net/cfg80211.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020012#include "wext-compat.h"
Johannes Berg04a773a2009-04-19 21:24:32 +020013#include "nl80211.h"
14
15
Johannes Berg667503dd2009-07-07 03:56:11 +020016void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +020017{
18 struct wireless_dev *wdev = dev->ieee80211_ptr;
19 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +020020#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020021 union iwreq_data wrqu;
22#endif
23
24 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
25 return;
26
Johannes Bergf7969962009-08-21 12:23:49 +020027 if (!wdev->ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +020028 return;
29
Johannes Berg04a773a2009-04-19 21:24:32 +020030 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
31 wdev->ssid, wdev->ssid_len,
32 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
33
34 if (WARN_ON(!bss))
35 return;
36
37 if (wdev->current_bss) {
38 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +020039 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +020040 }
41
Johannes Berg19957bb2009-07-02 17:20:43 +020042 cfg80211_hold_bss(bss_from_pub(bss));
43 wdev->current_bss = bss_from_pub(bss);
Johannes Berg04a773a2009-04-19 21:24:32 +020044
Johannes Bergfffd0932009-07-08 14:22:54 +020045 cfg80211_upload_connect_keys(wdev);
46
Johannes Berg667503dd2009-07-07 03:56:11 +020047 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
48 GFP_KERNEL);
Johannes Berg3d23e342009-09-29 23:27:28 +020049#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020050 memset(&wrqu, 0, sizeof(wrqu));
51 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
52 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
53#endif
54}
Johannes Berg667503dd2009-07-07 03:56:11 +020055
56void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
57{
58 struct wireless_dev *wdev = dev->ieee80211_ptr;
59 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
60 struct cfg80211_event *ev;
61 unsigned long flags;
62
Johannes Bergf7969962009-08-21 12:23:49 +020063 CFG80211_DEV_WARN_ON(!wdev->ssid_len);
64
Johannes Berg667503dd2009-07-07 03:56:11 +020065 ev = kzalloc(sizeof(*ev), gfp);
66 if (!ev)
67 return;
68
69 ev->type = EVENT_IBSS_JOINED;
70 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
71
72 spin_lock_irqsave(&wdev->event_lock, flags);
73 list_add_tail(&ev->list, &wdev->event_list);
74 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +010075 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +020076}
Johannes Berg04a773a2009-04-19 21:24:32 +020077EXPORT_SYMBOL(cfg80211_ibss_joined);
78
Johannes Berg667503dd2009-07-07 03:56:11 +020079int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
80 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +020081 struct cfg80211_ibss_params *params,
82 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +020083{
84 struct wireless_dev *wdev = dev->ieee80211_ptr;
85 int err;
86
Johannes Berg667503dd2009-07-07 03:56:11 +020087 ASSERT_WDEV_LOCK(wdev);
88
Johannes Berg04a773a2009-04-19 21:24:32 +020089 if (wdev->ssid_len)
90 return -EALREADY;
91
Johannes Berg93b05232010-09-28 12:53:14 +020092 if (!params->basic_rates) {
93 /*
94 * If no rates were explicitly configured,
95 * use the mandatory rate set for 11b or
96 * 11a for maximum compatibility.
97 */
98 struct ieee80211_supported_band *sband =
99 rdev->wiphy.bands[params->channel->band];
100 int j;
101 u32 flag = params->channel->band == IEEE80211_BAND_5GHZ ?
102 IEEE80211_RATE_MANDATORY_A :
103 IEEE80211_RATE_MANDATORY_B;
104
105 for (j = 0; j < sband->n_bitrates; j++) {
106 if (sband->bitrates[j].flags & flag)
107 params->basic_rates |= BIT(j);
108 }
109 }
110
Johannes Bergfffd0932009-07-08 14:22:54 +0200111 if (WARN_ON(wdev->connect_keys))
112 kfree(wdev->connect_keys);
113 wdev->connect_keys = connkeys;
114
Johannes Berg3d23e342009-09-29 23:27:28 +0200115#ifdef CONFIG_CFG80211_WEXT
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200116 wdev->wext.ibss.channel = params->channel;
Johannes Berg04a773a2009-04-19 21:24:32 +0200117#endif
118 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
Johannes Bergfffd0932009-07-08 14:22:54 +0200119 if (err) {
120 wdev->connect_keys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200121 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200122 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200123
124 memcpy(wdev->ssid, params->ssid, params->ssid_len);
125 wdev->ssid_len = params->ssid_len;
126
127 return 0;
128}
129
Johannes Berg667503dd2009-07-07 03:56:11 +0200130int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
131 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200132 struct cfg80211_ibss_params *params,
133 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +0200134{
135 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200136 int err;
137
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200138 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200139 wdev_lock(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200140 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
Johannes Berg667503dd2009-07-07 03:56:11 +0200141 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200142 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200143
144 return err;
145}
146
147static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
148{
149 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200150 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
151 int i;
Johannes Berg667503dd2009-07-07 03:56:11 +0200152
153 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200154
Johannes Bergfffd0932009-07-08 14:22:54 +0200155 kfree(wdev->connect_keys);
156 wdev->connect_keys = NULL;
157
158 /*
159 * Delete all the keys ... pairwise keys can't really
160 * exist any more anyway, but default keys might.
161 */
162 if (rdev->ops->del_key)
163 for (i = 0; i < 6; i++)
Johannes Berge31b8212010-10-05 19:39:30 +0200164 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200165
Johannes Berg04a773a2009-04-19 21:24:32 +0200166 if (wdev->current_bss) {
167 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200168 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +0200169 }
170
171 wdev->current_bss = NULL;
172 wdev->ssid_len = 0;
Johannes Berg3d23e342009-09-29 23:27:28 +0200173#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9d308422009-04-20 18:43:46 +0200174 if (!nowext)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200175 wdev->wext.ibss.ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +0200176#endif
Johannes Berg04a773a2009-04-19 21:24:32 +0200177}
178
Johannes Berg667503dd2009-07-07 03:56:11 +0200179void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
180{
181 struct wireless_dev *wdev = dev->ieee80211_ptr;
182
183 wdev_lock(wdev);
184 __cfg80211_clear_ibss(dev, nowext);
185 wdev_unlock(wdev);
186}
187
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100188int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
189 struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +0200190{
Johannes Berg78485472009-07-07 03:56:05 +0200191 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg04a773a2009-04-19 21:24:32 +0200192 int err;
193
Johannes Berg667503dd2009-07-07 03:56:11 +0200194 ASSERT_WDEV_LOCK(wdev);
195
Johannes Berg78485472009-07-07 03:56:05 +0200196 if (!wdev->ssid_len)
197 return -ENOLINK;
198
Johannes Berg04a773a2009-04-19 21:24:32 +0200199 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
200
201 if (err)
202 return err;
203
Johannes Berg667503dd2009-07-07 03:56:11 +0200204 __cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200205
206 return 0;
207}
208
Johannes Berg667503dd2009-07-07 03:56:11 +0200209int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
210 struct net_device *dev, bool nowext)
211{
212 struct wireless_dev *wdev = dev->ieee80211_ptr;
213 int err;
214
215 wdev_lock(wdev);
216 err = __cfg80211_leave_ibss(rdev, dev, nowext);
217 wdev_unlock(wdev);
218
219 return err;
220}
221
Johannes Berg3d23e342009-09-29 23:27:28 +0200222#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfffd0932009-07-08 14:22:54 +0200223int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
224 struct wireless_dev *wdev)
Johannes Berg04a773a2009-04-19 21:24:32 +0200225{
Johannes Bergfffd0932009-07-08 14:22:54 +0200226 struct cfg80211_cached_keys *ck = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200227 enum ieee80211_band band;
Johannes Bergfffd0932009-07-08 14:22:54 +0200228 int i, err;
229
230 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200231
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200232 if (!wdev->wext.ibss.beacon_interval)
233 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200234
Johannes Berg04a773a2009-04-19 21:24:32 +0200235 /* try to find an IBSS channel if none requested ... */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200236 if (!wdev->wext.ibss.channel) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200237 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
238 struct ieee80211_supported_band *sband;
239 struct ieee80211_channel *chan;
240
241 sband = rdev->wiphy.bands[band];
242 if (!sband)
243 continue;
244
245 for (i = 0; i < sband->n_channels; i++) {
246 chan = &sband->channels[i];
247 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
248 continue;
249 if (chan->flags & IEEE80211_CHAN_DISABLED)
250 continue;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200251 wdev->wext.ibss.channel = chan;
Johannes Berg04a773a2009-04-19 21:24:32 +0200252 break;
253 }
254
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200255 if (wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200256 break;
257 }
258
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200259 if (!wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200260 return -EINVAL;
261 }
262
263 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200264 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200265 return 0;
266
267 if (!netif_running(wdev->netdev))
268 return 0;
269
Johannes Berg3be61a32010-07-22 13:59:15 +0200270 if (wdev->wext.keys) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200271 wdev->wext.keys->def = wdev->wext.default_key;
Johannes Berg3be61a32010-07-22 13:59:15 +0200272 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
273 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200274
275 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
276
277 if (wdev->wext.keys) {
278 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
279 if (!ck)
280 return -ENOMEM;
281 for (i = 0; i < 6; i++)
282 ck->params[i].key = ck->data[i];
283 }
284 err = __cfg80211_join_ibss(rdev, wdev->netdev,
285 &wdev->wext.ibss, ck);
286 if (err)
287 kfree(ck);
288
289 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200290}
291
292int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
293 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200294 struct iw_freq *wextfreq, char *extra)
Johannes Berg04a773a2009-04-19 21:24:32 +0200295{
296 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200297 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
298 struct ieee80211_channel *chan = NULL;
299 int err, freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200300
301 /* call only for ibss! */
302 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
303 return -EINVAL;
304
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200305 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200306 return -EOPNOTSUPP;
307
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200308 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
309 if (freq < 0)
310 return freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200311
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200312 if (freq) {
313 chan = ieee80211_get_channel(wdev->wiphy, freq);
314 if (!chan)
315 return -EINVAL;
316 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
317 chan->flags & IEEE80211_CHAN_DISABLED)
318 return -EINVAL;
319 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200320
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200321 if (wdev->wext.ibss.channel == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200322 return 0;
323
Johannes Berg667503dd2009-07-07 03:56:11 +0200324 wdev_lock(wdev);
325 err = 0;
326 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200327 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200328 wdev_unlock(wdev);
329
330 if (err)
331 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200332
333 if (chan) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200334 wdev->wext.ibss.channel = chan;
335 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200336 } else {
337 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200338 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200339 }
340
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200341 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200342 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200343 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200344 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200345 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200346
347 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200348}
Johannes Berg04a773a2009-04-19 21:24:32 +0200349
350int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
351 struct iw_request_info *info,
352 struct iw_freq *freq, char *extra)
353{
354 struct wireless_dev *wdev = dev->ieee80211_ptr;
355 struct ieee80211_channel *chan = NULL;
356
357 /* call only for ibss! */
358 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
359 return -EINVAL;
360
Johannes Berg667503dd2009-07-07 03:56:11 +0200361 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200362 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200363 chan = wdev->current_bss->pub.channel;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200364 else if (wdev->wext.ibss.channel)
365 chan = wdev->wext.ibss.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200366 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200367
368 if (chan) {
369 freq->m = chan->center_freq;
370 freq->e = 6;
371 return 0;
372 }
373
374 /* no channel if not joining */
375 return -EINVAL;
376}
Johannes Berg04a773a2009-04-19 21:24:32 +0200377
378int cfg80211_ibss_wext_siwessid(struct net_device *dev,
379 struct iw_request_info *info,
380 struct iw_point *data, char *ssid)
381{
382 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200383 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200384 size_t len = data->length;
385 int err;
386
387 /* call only for ibss! */
388 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
389 return -EINVAL;
390
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200391 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200392 return -EOPNOTSUPP;
393
Johannes Berg667503dd2009-07-07 03:56:11 +0200394 wdev_lock(wdev);
395 err = 0;
396 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200397 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200398 wdev_unlock(wdev);
399
400 if (err)
401 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200402
403 /* iwconfig uses nul termination in SSID.. */
404 if (len > 0 && ssid[len - 1] == '\0')
405 len--;
406
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200407 wdev->wext.ibss.ssid = wdev->ssid;
408 memcpy(wdev->wext.ibss.ssid, ssid, len);
409 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200410
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200411 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200412 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200413 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200414 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200415 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200416
417 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200418}
Johannes Berg04a773a2009-04-19 21:24:32 +0200419
420int cfg80211_ibss_wext_giwessid(struct net_device *dev,
421 struct iw_request_info *info,
422 struct iw_point *data, char *ssid)
423{
424 struct wireless_dev *wdev = dev->ieee80211_ptr;
425
426 /* call only for ibss! */
427 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
428 return -EINVAL;
429
430 data->flags = 0;
431
Johannes Berg667503dd2009-07-07 03:56:11 +0200432 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200433 if (wdev->ssid_len) {
434 data->flags = 1;
435 data->length = wdev->ssid_len;
436 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200437 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200438 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200439 data->length = wdev->wext.ibss.ssid_len;
440 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200441 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200442 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200443
444 return 0;
445}
Johannes Berg04a773a2009-04-19 21:24:32 +0200446
447int cfg80211_ibss_wext_siwap(struct net_device *dev,
448 struct iw_request_info *info,
449 struct sockaddr *ap_addr, char *extra)
450{
451 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200452 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200453 u8 *bssid = ap_addr->sa_data;
454 int err;
455
456 /* call only for ibss! */
457 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
458 return -EINVAL;
459
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200460 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200461 return -EOPNOTSUPP;
462
463 if (ap_addr->sa_family != ARPHRD_ETHER)
464 return -EINVAL;
465
466 /* automatic mode */
467 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
468 bssid = NULL;
469
470 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200471 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200472 return 0;
473
474 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200475 if (wdev->wext.ibss.bssid && bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000476 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
Johannes Berg04a773a2009-04-19 21:24:32 +0200477 return 0;
478
Johannes Berg667503dd2009-07-07 03:56:11 +0200479 wdev_lock(wdev);
480 err = 0;
481 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200482 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200483 wdev_unlock(wdev);
484
485 if (err)
486 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200487
488 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200489 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
490 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200491 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200492 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200493
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200494 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200495 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200496 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200497 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200498 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200499
500 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200501}
Johannes Berg04a773a2009-04-19 21:24:32 +0200502
503int cfg80211_ibss_wext_giwap(struct net_device *dev,
504 struct iw_request_info *info,
505 struct sockaddr *ap_addr, char *extra)
506{
507 struct wireless_dev *wdev = dev->ieee80211_ptr;
508
509 /* call only for ibss! */
510 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
511 return -EINVAL;
512
513 ap_addr->sa_family = ARPHRD_ETHER;
514
Johannes Berg667503dd2009-07-07 03:56:11 +0200515 wdev_lock(wdev);
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200516 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200517 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800518 else if (wdev->wext.ibss.bssid)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200519 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800520 else
521 memset(ap_addr->sa_data, 0, ETH_ALEN);
522
Johannes Berg667503dd2009-07-07 03:56:11 +0200523 wdev_unlock(wdev);
524
Johannes Berg04a773a2009-04-19 21:24:32 +0200525 return 0;
526}
Johannes Berg04a773a2009-04-19 21:24:32 +0200527#endif