blob: 9825317e653aaf748c7511083c64ea62c5bb2417 [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>
9#include <net/cfg80211.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020010#include "wext-compat.h"
Johannes Berg04a773a2009-04-19 21:24:32 +020011#include "nl80211.h"
12
13
Johannes Berg667503d2009-07-07 03:56:11 +020014void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +020015{
16 struct wireless_dev *wdev = dev->ieee80211_ptr;
17 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +020018#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020019 union iwreq_data wrqu;
20#endif
21
22 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
23 return;
24
Johannes Bergf7969962009-08-21 12:23:49 +020025 if (!wdev->ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +020026 return;
27
Johannes Berg04a773a2009-04-19 21:24:32 +020028 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
29 wdev->ssid, wdev->ssid_len,
30 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
31
32 if (WARN_ON(!bss))
33 return;
34
35 if (wdev->current_bss) {
36 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +020037 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +020038 }
39
Johannes Berg19957bb2009-07-02 17:20:43 +020040 cfg80211_hold_bss(bss_from_pub(bss));
41 wdev->current_bss = bss_from_pub(bss);
Johannes Berg04a773a2009-04-19 21:24:32 +020042
Johannes Bergfffd0932009-07-08 14:22:54 +020043 cfg80211_upload_connect_keys(wdev);
44
Johannes Berg667503d2009-07-07 03:56:11 +020045 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
46 GFP_KERNEL);
Johannes Berg3d23e342009-09-29 23:27:28 +020047#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020048 memset(&wrqu, 0, sizeof(wrqu));
49 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
50 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
51#endif
52}
Johannes Berg667503d2009-07-07 03:56:11 +020053
54void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
55{
56 struct wireless_dev *wdev = dev->ieee80211_ptr;
57 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
58 struct cfg80211_event *ev;
59 unsigned long flags;
60
Johannes Bergf7969962009-08-21 12:23:49 +020061 CFG80211_DEV_WARN_ON(!wdev->ssid_len);
62
Johannes Berg667503d2009-07-07 03:56:11 +020063 ev = kzalloc(sizeof(*ev), gfp);
64 if (!ev)
65 return;
66
67 ev->type = EVENT_IBSS_JOINED;
68 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
69
70 spin_lock_irqsave(&wdev->event_lock, flags);
71 list_add_tail(&ev->list, &wdev->event_list);
72 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +010073 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +020074}
Johannes Berg04a773a2009-04-19 21:24:32 +020075EXPORT_SYMBOL(cfg80211_ibss_joined);
76
Johannes Berg667503d2009-07-07 03:56:11 +020077int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
78 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +020079 struct cfg80211_ibss_params *params,
80 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +020081{
82 struct wireless_dev *wdev = dev->ieee80211_ptr;
83 int err;
84
Johannes Berg667503d2009-07-07 03:56:11 +020085 ASSERT_WDEV_LOCK(wdev);
86
Johannes Berg04a773a2009-04-19 21:24:32 +020087 if (wdev->ssid_len)
88 return -EALREADY;
89
Johannes Bergfffd0932009-07-08 14:22:54 +020090 if (WARN_ON(wdev->connect_keys))
91 kfree(wdev->connect_keys);
92 wdev->connect_keys = connkeys;
93
Johannes Berg3d23e342009-09-29 23:27:28 +020094#ifdef CONFIG_CFG80211_WEXT
Johannes Bergcbe8fa92009-05-09 20:09:03 +020095 wdev->wext.ibss.channel = params->channel;
Johannes Berg04a773a2009-04-19 21:24:32 +020096#endif
97 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
Johannes Bergfffd0932009-07-08 14:22:54 +020098 if (err) {
99 wdev->connect_keys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200100 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200101 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200102
103 memcpy(wdev->ssid, params->ssid, params->ssid_len);
104 wdev->ssid_len = params->ssid_len;
105
106 return 0;
107}
108
Johannes Berg667503d2009-07-07 03:56:11 +0200109int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
110 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200111 struct cfg80211_ibss_params *params,
112 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +0200113{
114 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200115 int err;
116
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200117 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200118 wdev_lock(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200119 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
Johannes Berg667503d2009-07-07 03:56:11 +0200120 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200121 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503d2009-07-07 03:56:11 +0200122
123 return err;
124}
125
126static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
127{
128 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200129 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
130 int i;
Johannes Berg667503d2009-07-07 03:56:11 +0200131
132 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200133
Johannes Bergfffd0932009-07-08 14:22:54 +0200134 kfree(wdev->connect_keys);
135 wdev->connect_keys = NULL;
136
137 /*
138 * Delete all the keys ... pairwise keys can't really
139 * exist any more anyway, but default keys might.
140 */
141 if (rdev->ops->del_key)
142 for (i = 0; i < 6; i++)
143 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
144
Johannes Berg04a773a2009-04-19 21:24:32 +0200145 if (wdev->current_bss) {
146 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200147 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +0200148 }
149
150 wdev->current_bss = NULL;
151 wdev->ssid_len = 0;
Johannes Berg3d23e342009-09-29 23:27:28 +0200152#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9d308422009-04-20 18:43:46 +0200153 if (!nowext)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200154 wdev->wext.ibss.ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +0200155#endif
Johannes Berg04a773a2009-04-19 21:24:32 +0200156}
157
Johannes Berg667503d2009-07-07 03:56:11 +0200158void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
159{
160 struct wireless_dev *wdev = dev->ieee80211_ptr;
161
162 wdev_lock(wdev);
163 __cfg80211_clear_ibss(dev, nowext);
164 wdev_unlock(wdev);
165}
166
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100167int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
168 struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +0200169{
Johannes Berg78485472009-07-07 03:56:05 +0200170 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg04a773a2009-04-19 21:24:32 +0200171 int err;
172
Johannes Berg667503d2009-07-07 03:56:11 +0200173 ASSERT_WDEV_LOCK(wdev);
174
Johannes Berg78485472009-07-07 03:56:05 +0200175 if (!wdev->ssid_len)
176 return -ENOLINK;
177
Johannes Berg04a773a2009-04-19 21:24:32 +0200178 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
179
180 if (err)
181 return err;
182
Johannes Berg667503d2009-07-07 03:56:11 +0200183 __cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200184
185 return 0;
186}
187
Johannes Berg667503d2009-07-07 03:56:11 +0200188int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
189 struct net_device *dev, bool nowext)
190{
191 struct wireless_dev *wdev = dev->ieee80211_ptr;
192 int err;
193
194 wdev_lock(wdev);
195 err = __cfg80211_leave_ibss(rdev, dev, nowext);
196 wdev_unlock(wdev);
197
198 return err;
199}
200
Johannes Berg3d23e342009-09-29 23:27:28 +0200201#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfffd0932009-07-08 14:22:54 +0200202int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
203 struct wireless_dev *wdev)
Johannes Berg04a773a2009-04-19 21:24:32 +0200204{
Johannes Bergfffd0932009-07-08 14:22:54 +0200205 struct cfg80211_cached_keys *ck = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200206 enum ieee80211_band band;
Johannes Bergfffd0932009-07-08 14:22:54 +0200207 int i, err;
208
209 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200210
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200211 if (!wdev->wext.ibss.beacon_interval)
212 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200213
Johannes Berg04a773a2009-04-19 21:24:32 +0200214 /* try to find an IBSS channel if none requested ... */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200215 if (!wdev->wext.ibss.channel) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200216 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
217 struct ieee80211_supported_band *sband;
218 struct ieee80211_channel *chan;
219
220 sband = rdev->wiphy.bands[band];
221 if (!sband)
222 continue;
223
224 for (i = 0; i < sband->n_channels; i++) {
225 chan = &sband->channels[i];
226 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
227 continue;
228 if (chan->flags & IEEE80211_CHAN_DISABLED)
229 continue;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200230 wdev->wext.ibss.channel = chan;
Johannes Berg04a773a2009-04-19 21:24:32 +0200231 break;
232 }
233
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200234 if (wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200235 break;
236 }
237
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200238 if (!wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200239 return -EINVAL;
240 }
241
242 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200243 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200244 return 0;
245
246 if (!netif_running(wdev->netdev))
247 return 0;
248
Johannes Bergfffd0932009-07-08 14:22:54 +0200249 if (wdev->wext.keys)
250 wdev->wext.keys->def = wdev->wext.default_key;
251
252 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
253
254 if (wdev->wext.keys) {
255 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
256 if (!ck)
257 return -ENOMEM;
258 for (i = 0; i < 6; i++)
259 ck->params[i].key = ck->data[i];
260 }
261 err = __cfg80211_join_ibss(rdev, wdev->netdev,
262 &wdev->wext.ibss, ck);
263 if (err)
264 kfree(ck);
265
266 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200267}
268
269int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
270 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200271 struct iw_freq *wextfreq, char *extra)
Johannes Berg04a773a2009-04-19 21:24:32 +0200272{
273 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200274 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
275 struct ieee80211_channel *chan = NULL;
276 int err, freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200277
278 /* call only for ibss! */
279 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
280 return -EINVAL;
281
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200282 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200283 return -EOPNOTSUPP;
284
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200285 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
286 if (freq < 0)
287 return freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200288
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200289 if (freq) {
290 chan = ieee80211_get_channel(wdev->wiphy, freq);
291 if (!chan)
292 return -EINVAL;
293 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
294 chan->flags & IEEE80211_CHAN_DISABLED)
295 return -EINVAL;
296 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200297
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200298 if (wdev->wext.ibss.channel == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200299 return 0;
300
Johannes Berg667503d2009-07-07 03:56:11 +0200301 wdev_lock(wdev);
302 err = 0;
303 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200304 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503d2009-07-07 03:56:11 +0200305 wdev_unlock(wdev);
306
307 if (err)
308 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200309
310 if (chan) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200311 wdev->wext.ibss.channel = chan;
312 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200313 } else {
314 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200315 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200316 }
317
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200318 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200319 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200320 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200321 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200322 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200323
324 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200325}
Johannes Berg04a773a2009-04-19 21:24:32 +0200326
327int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
328 struct iw_request_info *info,
329 struct iw_freq *freq, char *extra)
330{
331 struct wireless_dev *wdev = dev->ieee80211_ptr;
332 struct ieee80211_channel *chan = NULL;
333
334 /* call only for ibss! */
335 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
336 return -EINVAL;
337
Johannes Berg667503d2009-07-07 03:56:11 +0200338 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200339 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200340 chan = wdev->current_bss->pub.channel;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200341 else if (wdev->wext.ibss.channel)
342 chan = wdev->wext.ibss.channel;
Johannes Berg667503d2009-07-07 03:56:11 +0200343 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200344
345 if (chan) {
346 freq->m = chan->center_freq;
347 freq->e = 6;
348 return 0;
349 }
350
351 /* no channel if not joining */
352 return -EINVAL;
353}
Johannes Berg04a773a2009-04-19 21:24:32 +0200354
355int cfg80211_ibss_wext_siwessid(struct net_device *dev,
356 struct iw_request_info *info,
357 struct iw_point *data, char *ssid)
358{
359 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200360 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200361 size_t len = data->length;
362 int err;
363
364 /* call only for ibss! */
365 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
366 return -EINVAL;
367
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200368 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200369 return -EOPNOTSUPP;
370
Johannes Berg667503d2009-07-07 03:56:11 +0200371 wdev_lock(wdev);
372 err = 0;
373 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200374 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503d2009-07-07 03:56:11 +0200375 wdev_unlock(wdev);
376
377 if (err)
378 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200379
380 /* iwconfig uses nul termination in SSID.. */
381 if (len > 0 && ssid[len - 1] == '\0')
382 len--;
383
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200384 wdev->wext.ibss.ssid = wdev->ssid;
385 memcpy(wdev->wext.ibss.ssid, ssid, len);
386 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200387
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200388 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200389 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200390 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200391 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200392 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200393
394 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200395}
Johannes Berg04a773a2009-04-19 21:24:32 +0200396
397int cfg80211_ibss_wext_giwessid(struct net_device *dev,
398 struct iw_request_info *info,
399 struct iw_point *data, char *ssid)
400{
401 struct wireless_dev *wdev = dev->ieee80211_ptr;
402
403 /* call only for ibss! */
404 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
405 return -EINVAL;
406
407 data->flags = 0;
408
Johannes Berg667503d2009-07-07 03:56:11 +0200409 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200410 if (wdev->ssid_len) {
411 data->flags = 1;
412 data->length = wdev->ssid_len;
413 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200414 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200415 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200416 data->length = wdev->wext.ibss.ssid_len;
417 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200418 }
Johannes Berg667503d2009-07-07 03:56:11 +0200419 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200420
421 return 0;
422}
Johannes Berg04a773a2009-04-19 21:24:32 +0200423
424int cfg80211_ibss_wext_siwap(struct net_device *dev,
425 struct iw_request_info *info,
426 struct sockaddr *ap_addr, char *extra)
427{
428 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200429 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200430 u8 *bssid = ap_addr->sa_data;
431 int err;
432
433 /* call only for ibss! */
434 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
435 return -EINVAL;
436
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200437 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200438 return -EOPNOTSUPP;
439
440 if (ap_addr->sa_family != ARPHRD_ETHER)
441 return -EINVAL;
442
443 /* automatic mode */
444 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
445 bssid = NULL;
446
447 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200448 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200449 return 0;
450
451 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200452 if (wdev->wext.ibss.bssid && bssid &&
453 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
Johannes Berg04a773a2009-04-19 21:24:32 +0200454 return 0;
455
Johannes Berg667503d2009-07-07 03:56:11 +0200456 wdev_lock(wdev);
457 err = 0;
458 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200459 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503d2009-07-07 03:56:11 +0200460 wdev_unlock(wdev);
461
462 if (err)
463 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200464
465 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200466 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
467 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200468 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200469 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200470
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200471 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200472 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200473 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200474 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200475 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200476
477 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200478}
Johannes Berg04a773a2009-04-19 21:24:32 +0200479
480int cfg80211_ibss_wext_giwap(struct net_device *dev,
481 struct iw_request_info *info,
482 struct sockaddr *ap_addr, char *extra)
483{
484 struct wireless_dev *wdev = dev->ieee80211_ptr;
485
486 /* call only for ibss! */
487 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
488 return -EINVAL;
489
490 ap_addr->sa_family = ARPHRD_ETHER;
491
Johannes Berg667503d2009-07-07 03:56:11 +0200492 wdev_lock(wdev);
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200493 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200494 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800495 else if (wdev->wext.ibss.bssid)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200496 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800497 else
498 memset(ap_addr->sa_data, 0, ETH_ALEN);
499
Johannes Berg667503d2009-07-07 03:56:11 +0200500 wdev_unlock(wdev);
501
Johannes Berg04a773a2009-04-19 21:24:32 +0200502 return 0;
503}
Johannes Berg04a773a2009-04-19 21:24:32 +0200504#endif