blob: adcabba02e20c78199d3913a626fe74f209e002d [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>
Johannes Berg04a773a2009-04-19 21:24:32 +020010#include <net/cfg80211.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020011#include "wext-compat.h"
Johannes Berg04a773a2009-04-19 21:24:32 +020012#include "nl80211.h"
13
14
Johannes Berg667503dd2009-07-07 03:56:11 +020015void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +020016{
17 struct wireless_dev *wdev = dev->ieee80211_ptr;
18 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +020019#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020020 union iwreq_data wrqu;
21#endif
22
23 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
24 return;
25
Johannes Bergf7969962009-08-21 12:23:49 +020026 if (!wdev->ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +020027 return;
28
Johannes Berg04a773a2009-04-19 21:24:32 +020029 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
30 wdev->ssid, wdev->ssid_len,
31 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
32
33 if (WARN_ON(!bss))
34 return;
35
36 if (wdev->current_bss) {
37 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +020038 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +020039 }
40
Johannes Berg19957bb2009-07-02 17:20:43 +020041 cfg80211_hold_bss(bss_from_pub(bss));
42 wdev->current_bss = bss_from_pub(bss);
Johannes Berg04a773a2009-04-19 21:24:32 +020043
Johannes Bergfffd0932009-07-08 14:22:54 +020044 cfg80211_upload_connect_keys(wdev);
45
Johannes Berg667503dd2009-07-07 03:56:11 +020046 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
47 GFP_KERNEL);
Johannes Berg3d23e342009-09-29 23:27:28 +020048#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020049 memset(&wrqu, 0, sizeof(wrqu));
50 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
51 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
52#endif
53}
Johannes Berg667503dd2009-07-07 03:56:11 +020054
55void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
56{
57 struct wireless_dev *wdev = dev->ieee80211_ptr;
58 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
59 struct cfg80211_event *ev;
60 unsigned long flags;
61
Johannes Bergf7969962009-08-21 12:23:49 +020062 CFG80211_DEV_WARN_ON(!wdev->ssid_len);
63
Johannes Berg667503dd2009-07-07 03:56:11 +020064 ev = kzalloc(sizeof(*ev), gfp);
65 if (!ev)
66 return;
67
68 ev->type = EVENT_IBSS_JOINED;
69 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
70
71 spin_lock_irqsave(&wdev->event_lock, flags);
72 list_add_tail(&ev->list, &wdev->event_list);
73 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +010074 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +020075}
Johannes Berg04a773a2009-04-19 21:24:32 +020076EXPORT_SYMBOL(cfg80211_ibss_joined);
77
Johannes Berg667503dd2009-07-07 03:56:11 +020078int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
79 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +020080 struct cfg80211_ibss_params *params,
81 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +020082{
83 struct wireless_dev *wdev = dev->ieee80211_ptr;
84 int err;
85
Johannes Berg667503dd2009-07-07 03:56:11 +020086 ASSERT_WDEV_LOCK(wdev);
87
Johannes Berg04a773a2009-04-19 21:24:32 +020088 if (wdev->ssid_len)
89 return -EALREADY;
90
Johannes Bergfffd0932009-07-08 14:22:54 +020091 if (WARN_ON(wdev->connect_keys))
92 kfree(wdev->connect_keys);
93 wdev->connect_keys = connkeys;
94
Johannes Berg3d23e342009-09-29 23:27:28 +020095#ifdef CONFIG_CFG80211_WEXT
Johannes Bergcbe8fa92009-05-09 20:09:03 +020096 wdev->wext.ibss.channel = params->channel;
Johannes Berg04a773a2009-04-19 21:24:32 +020097#endif
98 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
Johannes Bergfffd0932009-07-08 14:22:54 +020099 if (err) {
100 wdev->connect_keys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200101 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200102 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200103
104 memcpy(wdev->ssid, params->ssid, params->ssid_len);
105 wdev->ssid_len = params->ssid_len;
106
107 return 0;
108}
109
Johannes Berg667503dd2009-07-07 03:56:11 +0200110int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
111 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200112 struct cfg80211_ibss_params *params,
113 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +0200114{
115 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200116 int err;
117
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200118 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200119 wdev_lock(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200120 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
Johannes Berg667503dd2009-07-07 03:56:11 +0200121 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200122 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200123
124 return err;
125}
126
127static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
128{
129 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200130 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
131 int i;
Johannes Berg667503dd2009-07-07 03:56:11 +0200132
133 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200134
Johannes Bergfffd0932009-07-08 14:22:54 +0200135 kfree(wdev->connect_keys);
136 wdev->connect_keys = NULL;
137
138 /*
139 * Delete all the keys ... pairwise keys can't really
140 * exist any more anyway, but default keys might.
141 */
142 if (rdev->ops->del_key)
143 for (i = 0; i < 6; i++)
144 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
145
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 if (wdev->current_bss) {
147 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200148 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +0200149 }
150
151 wdev->current_bss = NULL;
152 wdev->ssid_len = 0;
Johannes Berg3d23e342009-09-29 23:27:28 +0200153#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9d308422009-04-20 18:43:46 +0200154 if (!nowext)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200155 wdev->wext.ibss.ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +0200156#endif
Johannes Berg04a773a2009-04-19 21:24:32 +0200157}
158
Johannes Berg667503dd2009-07-07 03:56:11 +0200159void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
160{
161 struct wireless_dev *wdev = dev->ieee80211_ptr;
162
163 wdev_lock(wdev);
164 __cfg80211_clear_ibss(dev, nowext);
165 wdev_unlock(wdev);
166}
167
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100168int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
169 struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +0200170{
Johannes Berg78485472009-07-07 03:56:05 +0200171 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg04a773a2009-04-19 21:24:32 +0200172 int err;
173
Johannes Berg667503dd2009-07-07 03:56:11 +0200174 ASSERT_WDEV_LOCK(wdev);
175
Johannes Berg78485472009-07-07 03:56:05 +0200176 if (!wdev->ssid_len)
177 return -ENOLINK;
178
Johannes Berg04a773a2009-04-19 21:24:32 +0200179 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
180
181 if (err)
182 return err;
183
Johannes Berg667503dd2009-07-07 03:56:11 +0200184 __cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200185
186 return 0;
187}
188
Johannes Berg667503dd2009-07-07 03:56:11 +0200189int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
190 struct net_device *dev, bool nowext)
191{
192 struct wireless_dev *wdev = dev->ieee80211_ptr;
193 int err;
194
195 wdev_lock(wdev);
196 err = __cfg80211_leave_ibss(rdev, dev, nowext);
197 wdev_unlock(wdev);
198
199 return err;
200}
201
Johannes Berg3d23e342009-09-29 23:27:28 +0200202#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfffd0932009-07-08 14:22:54 +0200203int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
204 struct wireless_dev *wdev)
Johannes Berg04a773a2009-04-19 21:24:32 +0200205{
Johannes Bergfffd0932009-07-08 14:22:54 +0200206 struct cfg80211_cached_keys *ck = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200207 enum ieee80211_band band;
Johannes Bergfffd0932009-07-08 14:22:54 +0200208 int i, err;
209
210 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200211
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200212 if (!wdev->wext.ibss.beacon_interval)
213 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200214
Johannes Berg04a773a2009-04-19 21:24:32 +0200215 /* try to find an IBSS channel if none requested ... */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200216 if (!wdev->wext.ibss.channel) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200217 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
218 struct ieee80211_supported_band *sband;
219 struct ieee80211_channel *chan;
220
221 sband = rdev->wiphy.bands[band];
222 if (!sband)
223 continue;
224
225 for (i = 0; i < sband->n_channels; i++) {
226 chan = &sband->channels[i];
227 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
228 continue;
229 if (chan->flags & IEEE80211_CHAN_DISABLED)
230 continue;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200231 wdev->wext.ibss.channel = chan;
Johannes Berg04a773a2009-04-19 21:24:32 +0200232 break;
233 }
234
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200235 if (wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200236 break;
237 }
238
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200239 if (!wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200240 return -EINVAL;
241 }
242
243 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200244 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200245 return 0;
246
247 if (!netif_running(wdev->netdev))
248 return 0;
249
Johannes Bergfffd0932009-07-08 14:22:54 +0200250 if (wdev->wext.keys)
251 wdev->wext.keys->def = wdev->wext.default_key;
252
253 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
254
255 if (wdev->wext.keys) {
256 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
257 if (!ck)
258 return -ENOMEM;
259 for (i = 0; i < 6; i++)
260 ck->params[i].key = ck->data[i];
261 }
262 err = __cfg80211_join_ibss(rdev, wdev->netdev,
263 &wdev->wext.ibss, ck);
264 if (err)
265 kfree(ck);
266
267 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200268}
269
270int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
271 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200272 struct iw_freq *wextfreq, char *extra)
Johannes Berg04a773a2009-04-19 21:24:32 +0200273{
274 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200275 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
276 struct ieee80211_channel *chan = NULL;
277 int err, freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200278
279 /* call only for ibss! */
280 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
281 return -EINVAL;
282
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200283 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200284 return -EOPNOTSUPP;
285
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200286 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
287 if (freq < 0)
288 return freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200289
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200290 if (freq) {
291 chan = ieee80211_get_channel(wdev->wiphy, freq);
292 if (!chan)
293 return -EINVAL;
294 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
295 chan->flags & IEEE80211_CHAN_DISABLED)
296 return -EINVAL;
297 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200298
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200299 if (wdev->wext.ibss.channel == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200300 return 0;
301
Johannes Berg667503dd2009-07-07 03:56:11 +0200302 wdev_lock(wdev);
303 err = 0;
304 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200305 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200306 wdev_unlock(wdev);
307
308 if (err)
309 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200310
311 if (chan) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200312 wdev->wext.ibss.channel = chan;
313 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200314 } else {
315 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200316 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200317 }
318
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200319 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200320 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200321 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200322 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200323 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200324
325 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200326}
Johannes Berg04a773a2009-04-19 21:24:32 +0200327
328int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
329 struct iw_request_info *info,
330 struct iw_freq *freq, char *extra)
331{
332 struct wireless_dev *wdev = dev->ieee80211_ptr;
333 struct ieee80211_channel *chan = NULL;
334
335 /* call only for ibss! */
336 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
337 return -EINVAL;
338
Johannes Berg667503dd2009-07-07 03:56:11 +0200339 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200340 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200341 chan = wdev->current_bss->pub.channel;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200342 else if (wdev->wext.ibss.channel)
343 chan = wdev->wext.ibss.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200344 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200345
346 if (chan) {
347 freq->m = chan->center_freq;
348 freq->e = 6;
349 return 0;
350 }
351
352 /* no channel if not joining */
353 return -EINVAL;
354}
Johannes Berg04a773a2009-04-19 21:24:32 +0200355
356int cfg80211_ibss_wext_siwessid(struct net_device *dev,
357 struct iw_request_info *info,
358 struct iw_point *data, char *ssid)
359{
360 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200361 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200362 size_t len = data->length;
363 int err;
364
365 /* call only for ibss! */
366 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
367 return -EINVAL;
368
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200369 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200370 return -EOPNOTSUPP;
371
Johannes Berg667503dd2009-07-07 03:56:11 +0200372 wdev_lock(wdev);
373 err = 0;
374 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200375 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200376 wdev_unlock(wdev);
377
378 if (err)
379 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200380
381 /* iwconfig uses nul termination in SSID.. */
382 if (len > 0 && ssid[len - 1] == '\0')
383 len--;
384
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200385 wdev->wext.ibss.ssid = wdev->ssid;
386 memcpy(wdev->wext.ibss.ssid, ssid, len);
387 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200388
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200389 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200390 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200391 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200392 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200393 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200394
395 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200396}
Johannes Berg04a773a2009-04-19 21:24:32 +0200397
398int cfg80211_ibss_wext_giwessid(struct net_device *dev,
399 struct iw_request_info *info,
400 struct iw_point *data, char *ssid)
401{
402 struct wireless_dev *wdev = dev->ieee80211_ptr;
403
404 /* call only for ibss! */
405 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
406 return -EINVAL;
407
408 data->flags = 0;
409
Johannes Berg667503dd2009-07-07 03:56:11 +0200410 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200411 if (wdev->ssid_len) {
412 data->flags = 1;
413 data->length = wdev->ssid_len;
414 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200415 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200416 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200417 data->length = wdev->wext.ibss.ssid_len;
418 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200419 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200420 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200421
422 return 0;
423}
Johannes Berg04a773a2009-04-19 21:24:32 +0200424
425int cfg80211_ibss_wext_siwap(struct net_device *dev,
426 struct iw_request_info *info,
427 struct sockaddr *ap_addr, char *extra)
428{
429 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200430 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200431 u8 *bssid = ap_addr->sa_data;
432 int err;
433
434 /* call only for ibss! */
435 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
436 return -EINVAL;
437
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200438 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200439 return -EOPNOTSUPP;
440
441 if (ap_addr->sa_family != ARPHRD_ETHER)
442 return -EINVAL;
443
444 /* automatic mode */
445 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
446 bssid = NULL;
447
448 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200449 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200450 return 0;
451
452 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200453 if (wdev->wext.ibss.bssid && bssid &&
454 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
Johannes Berg04a773a2009-04-19 21:24:32 +0200455 return 0;
456
Johannes Berg667503dd2009-07-07 03:56:11 +0200457 wdev_lock(wdev);
458 err = 0;
459 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200460 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200461 wdev_unlock(wdev);
462
463 if (err)
464 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200465
466 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200467 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
468 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200469 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200470 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200471
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200472 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200473 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200474 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200475 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200476 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200477
478 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200479}
Johannes Berg04a773a2009-04-19 21:24:32 +0200480
481int cfg80211_ibss_wext_giwap(struct net_device *dev,
482 struct iw_request_info *info,
483 struct sockaddr *ap_addr, char *extra)
484{
485 struct wireless_dev *wdev = dev->ieee80211_ptr;
486
487 /* call only for ibss! */
488 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
489 return -EINVAL;
490
491 ap_addr->sa_family = ARPHRD_ETHER;
492
Johannes Berg667503dd2009-07-07 03:56:11 +0200493 wdev_lock(wdev);
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200494 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200495 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800496 else if (wdev->wext.ibss.bssid)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200497 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800498 else
499 memset(ap_addr->sa_data, 0, ETH_ALEN);
500
Johannes Berg667503dd2009-07-07 03:56:11 +0200501 wdev_unlock(wdev);
502
Johannes Berg04a773a2009-04-19 21:24:32 +0200503 return 0;
504}
Johannes Berg04a773a2009-04-19 21:24:32 +0200505#endif