blob: c88338911979363d646ef54c8da9394478e45290 [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 Berg667503dd2009-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;
18#ifdef CONFIG_WIRELESS_EXT
19 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 Berg667503dd2009-07-07 03:56:11 +020045 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
46 GFP_KERNEL);
Johannes Berg04a773a2009-04-19 21:24:32 +020047#ifdef CONFIG_WIRELESS_EXT
48 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 Berg667503dd2009-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 Berg667503dd2009-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);
73 schedule_work(&rdev->event_work);
74}
Johannes Berg04a773a2009-04-19 21:24:32 +020075EXPORT_SYMBOL(cfg80211_ibss_joined);
76
Johannes Berg667503dd2009-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;
Johannes Berg59bbb6f2009-08-07 17:22:35 +020083 struct ieee80211_channel *chan;
Johannes Berg04a773a2009-04-19 21:24:32 +020084 int err;
85
Johannes Berg667503dd2009-07-07 03:56:11 +020086 ASSERT_WDEV_LOCK(wdev);
87
Johannes Berg59bbb6f2009-08-07 17:22:35 +020088 chan = rdev_fixed_channel(rdev, wdev);
89 if (chan && chan != params->channel)
90 return -EBUSY;
91
Johannes Berg04a773a2009-04-19 21:24:32 +020092 if (wdev->ssid_len)
93 return -EALREADY;
94
Johannes Bergfffd0932009-07-08 14:22:54 +020095 if (WARN_ON(wdev->connect_keys))
96 kfree(wdev->connect_keys);
97 wdev->connect_keys = connkeys;
98
Johannes Berg04a773a2009-04-19 21:24:32 +020099#ifdef CONFIG_WIRELESS_EXT
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200100 wdev->wext.ibss.channel = params->channel;
Johannes Berg04a773a2009-04-19 21:24:32 +0200101#endif
102 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
Johannes Bergfffd0932009-07-08 14:22:54 +0200103 if (err) {
104 wdev->connect_keys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200105 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200106 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200107
108 memcpy(wdev->ssid, params->ssid, params->ssid_len);
109 wdev->ssid_len = params->ssid_len;
110
111 return 0;
112}
113
Johannes Berg667503dd2009-07-07 03:56:11 +0200114int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
115 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200116 struct cfg80211_ibss_params *params,
117 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +0200118{
119 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200120 int err;
121
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200122 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200123 wdev_lock(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200124 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
Johannes Berg667503dd2009-07-07 03:56:11 +0200125 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200126 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200127
128 return err;
129}
130
131static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
132{
133 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200134 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
135 int i;
Johannes Berg667503dd2009-07-07 03:56:11 +0200136
137 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200138
Johannes Bergfffd0932009-07-08 14:22:54 +0200139 kfree(wdev->connect_keys);
140 wdev->connect_keys = NULL;
141
142 /*
143 * Delete all the keys ... pairwise keys can't really
144 * exist any more anyway, but default keys might.
145 */
146 if (rdev->ops->del_key)
147 for (i = 0; i < 6; i++)
148 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
149
Johannes Berg04a773a2009-04-19 21:24:32 +0200150 if (wdev->current_bss) {
151 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200152 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +0200153 }
154
155 wdev->current_bss = NULL;
156 wdev->ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +0200157#ifdef CONFIG_WIRELESS_EXT
158 if (!nowext)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200159 wdev->wext.ibss.ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +0200160#endif
Johannes Berg04a773a2009-04-19 21:24:32 +0200161}
162
Johannes Berg667503dd2009-07-07 03:56:11 +0200163void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
164{
165 struct wireless_dev *wdev = dev->ieee80211_ptr;
166
167 wdev_lock(wdev);
168 __cfg80211_clear_ibss(dev, nowext);
169 wdev_unlock(wdev);
170}
171
172static int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
173 struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +0200174{
Johannes Berg78485472009-07-07 03:56:05 +0200175 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg04a773a2009-04-19 21:24:32 +0200176 int err;
177
Johannes Berg667503dd2009-07-07 03:56:11 +0200178 ASSERT_WDEV_LOCK(wdev);
179
Johannes Berg78485472009-07-07 03:56:05 +0200180 if (!wdev->ssid_len)
181 return -ENOLINK;
182
Johannes Berg04a773a2009-04-19 21:24:32 +0200183 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
184
185 if (err)
186 return err;
187
Johannes Berg667503dd2009-07-07 03:56:11 +0200188 __cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200189
190 return 0;
191}
192
Johannes Berg667503dd2009-07-07 03:56:11 +0200193int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
194 struct net_device *dev, bool nowext)
195{
196 struct wireless_dev *wdev = dev->ieee80211_ptr;
197 int err;
198
199 wdev_lock(wdev);
200 err = __cfg80211_leave_ibss(rdev, dev, nowext);
201 wdev_unlock(wdev);
202
203 return err;
204}
205
Johannes Berg04a773a2009-04-19 21:24:32 +0200206#ifdef CONFIG_WIRELESS_EXT
Johannes Bergfffd0932009-07-08 14:22:54 +0200207int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
208 struct wireless_dev *wdev)
Johannes Berg04a773a2009-04-19 21:24:32 +0200209{
Johannes Bergfffd0932009-07-08 14:22:54 +0200210 struct cfg80211_cached_keys *ck = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200211 enum ieee80211_band band;
Johannes Bergfffd0932009-07-08 14:22:54 +0200212 int i, err;
213
214 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200215
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200216 if (!wdev->wext.ibss.beacon_interval)
217 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200218
Johannes Berg04a773a2009-04-19 21:24:32 +0200219 /* try to find an IBSS channel if none requested ... */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200220 if (!wdev->wext.ibss.channel) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200221 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
222 struct ieee80211_supported_band *sband;
223 struct ieee80211_channel *chan;
224
225 sband = rdev->wiphy.bands[band];
226 if (!sband)
227 continue;
228
229 for (i = 0; i < sband->n_channels; i++) {
230 chan = &sband->channels[i];
231 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
232 continue;
233 if (chan->flags & IEEE80211_CHAN_DISABLED)
234 continue;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200235 wdev->wext.ibss.channel = chan;
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 break;
241 }
242
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200243 if (!wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200244 return -EINVAL;
245 }
246
247 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200248 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200249 return 0;
250
251 if (!netif_running(wdev->netdev))
252 return 0;
253
Johannes Bergfffd0932009-07-08 14:22:54 +0200254 if (wdev->wext.keys)
255 wdev->wext.keys->def = wdev->wext.default_key;
256
257 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
258
259 if (wdev->wext.keys) {
260 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
261 if (!ck)
262 return -ENOMEM;
263 for (i = 0; i < 6; i++)
264 ck->params[i].key = ck->data[i];
265 }
266 err = __cfg80211_join_ibss(rdev, wdev->netdev,
267 &wdev->wext.ibss, ck);
268 if (err)
269 kfree(ck);
270
271 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200272}
273
274int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
275 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200276 struct iw_freq *wextfreq, char *extra)
Johannes Berg04a773a2009-04-19 21:24:32 +0200277{
278 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200279 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
280 struct ieee80211_channel *chan = NULL;
281 int err, freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200282
283 /* call only for ibss! */
284 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
285 return -EINVAL;
286
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200287 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200288 return -EOPNOTSUPP;
289
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200290 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
291 if (freq < 0)
292 return freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200293
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200294 if (freq) {
295 chan = ieee80211_get_channel(wdev->wiphy, freq);
296 if (!chan)
297 return -EINVAL;
298 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
299 chan->flags & IEEE80211_CHAN_DISABLED)
300 return -EINVAL;
301 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200302
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200303 if (wdev->wext.ibss.channel == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200304 return 0;
305
Johannes Berg667503dd2009-07-07 03:56:11 +0200306 wdev_lock(wdev);
307 err = 0;
308 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200309 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200310 wdev_unlock(wdev);
311
312 if (err)
313 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200314
315 if (chan) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200316 wdev->wext.ibss.channel = chan;
317 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200318 } else {
319 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200320 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200321 }
322
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200323 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200324 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200325 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200326 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200327 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200328
329 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200330}
Johannes Berg04a773a2009-04-19 21:24:32 +0200331
332int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
333 struct iw_request_info *info,
334 struct iw_freq *freq, char *extra)
335{
336 struct wireless_dev *wdev = dev->ieee80211_ptr;
337 struct ieee80211_channel *chan = NULL;
338
339 /* call only for ibss! */
340 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
341 return -EINVAL;
342
Johannes Berg667503dd2009-07-07 03:56:11 +0200343 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200344 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200345 chan = wdev->current_bss->pub.channel;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200346 else if (wdev->wext.ibss.channel)
347 chan = wdev->wext.ibss.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200348 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200349
350 if (chan) {
351 freq->m = chan->center_freq;
352 freq->e = 6;
353 return 0;
354 }
355
356 /* no channel if not joining */
357 return -EINVAL;
358}
Johannes Berg04a773a2009-04-19 21:24:32 +0200359
360int cfg80211_ibss_wext_siwessid(struct net_device *dev,
361 struct iw_request_info *info,
362 struct iw_point *data, char *ssid)
363{
364 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200365 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200366 size_t len = data->length;
367 int err;
368
369 /* call only for ibss! */
370 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
371 return -EINVAL;
372
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200373 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200374 return -EOPNOTSUPP;
375
Johannes Berg667503dd2009-07-07 03:56:11 +0200376 wdev_lock(wdev);
377 err = 0;
378 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200379 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200380 wdev_unlock(wdev);
381
382 if (err)
383 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200384
385 /* iwconfig uses nul termination in SSID.. */
386 if (len > 0 && ssid[len - 1] == '\0')
387 len--;
388
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200389 wdev->wext.ibss.ssid = wdev->ssid;
390 memcpy(wdev->wext.ibss.ssid, ssid, len);
391 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200392
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200393 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200394 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200395 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200396 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200397 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200398
399 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200400}
Johannes Berg04a773a2009-04-19 21:24:32 +0200401
402int cfg80211_ibss_wext_giwessid(struct net_device *dev,
403 struct iw_request_info *info,
404 struct iw_point *data, char *ssid)
405{
406 struct wireless_dev *wdev = dev->ieee80211_ptr;
407
408 /* call only for ibss! */
409 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
410 return -EINVAL;
411
412 data->flags = 0;
413
Johannes Berg667503dd2009-07-07 03:56:11 +0200414 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200415 if (wdev->ssid_len) {
416 data->flags = 1;
417 data->length = wdev->ssid_len;
418 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200419 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200420 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200421 data->length = wdev->wext.ibss.ssid_len;
422 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200423 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200424 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200425
426 return 0;
427}
Johannes Berg04a773a2009-04-19 21:24:32 +0200428
429int cfg80211_ibss_wext_siwap(struct net_device *dev,
430 struct iw_request_info *info,
431 struct sockaddr *ap_addr, char *extra)
432{
433 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200434 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200435 u8 *bssid = ap_addr->sa_data;
436 int err;
437
438 /* call only for ibss! */
439 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
440 return -EINVAL;
441
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200442 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200443 return -EOPNOTSUPP;
444
445 if (ap_addr->sa_family != ARPHRD_ETHER)
446 return -EINVAL;
447
448 /* automatic mode */
449 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
450 bssid = NULL;
451
452 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200453 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200454 return 0;
455
456 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200457 if (wdev->wext.ibss.bssid && bssid &&
458 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
Johannes Berg04a773a2009-04-19 21:24:32 +0200459 return 0;
460
Johannes Berg667503dd2009-07-07 03:56:11 +0200461 wdev_lock(wdev);
462 err = 0;
463 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200464 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200465 wdev_unlock(wdev);
466
467 if (err)
468 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200469
470 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200471 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
472 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200473 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200474 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200475
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200476 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200477 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200478 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200479 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200480 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200481
482 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200483}
Johannes Berg04a773a2009-04-19 21:24:32 +0200484
485int cfg80211_ibss_wext_giwap(struct net_device *dev,
486 struct iw_request_info *info,
487 struct sockaddr *ap_addr, char *extra)
488{
489 struct wireless_dev *wdev = dev->ieee80211_ptr;
490
491 /* call only for ibss! */
492 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
493 return -EINVAL;
494
495 ap_addr->sa_family = ARPHRD_ETHER;
496
Johannes Berg667503dd2009-07-07 03:56:11 +0200497 wdev_lock(wdev);
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200498 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200499 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800500 else if (wdev->wext.ibss.bssid)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200501 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800502 else
503 memset(ap_addr->sa_data, 0, ETH_ALEN);
504
Johannes Berg667503dd2009-07-07 03:56:11 +0200505 wdev_unlock(wdev);
506
Johannes Berg04a773a2009-04-19 21:24:32 +0200507 return 0;
508}
Johannes Berg04a773a2009-04-19 21:24:32 +0200509#endif