blob: f33fbb79437c3280b980447c9f36cd8c99436dc7 [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 Berg93b05232010-09-28 12:53:14 +020091 if (!params->basic_rates) {
92 /*
93 * If no rates were explicitly configured,
94 * use the mandatory rate set for 11b or
95 * 11a for maximum compatibility.
96 */
97 struct ieee80211_supported_band *sband =
98 rdev->wiphy.bands[params->channel->band];
99 int j;
100 u32 flag = params->channel->band == IEEE80211_BAND_5GHZ ?
101 IEEE80211_RATE_MANDATORY_A :
102 IEEE80211_RATE_MANDATORY_B;
103
104 for (j = 0; j < sband->n_bitrates; j++) {
105 if (sband->bitrates[j].flags & flag)
106 params->basic_rates |= BIT(j);
107 }
108 }
109
Johannes Bergfffd0932009-07-08 14:22:54 +0200110 if (WARN_ON(wdev->connect_keys))
111 kfree(wdev->connect_keys);
112 wdev->connect_keys = connkeys;
113
Johannes Berg3d23e342009-09-29 23:27:28 +0200114#ifdef CONFIG_CFG80211_WEXT
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200115 wdev->wext.ibss.channel = params->channel;
Johannes Berg04a773a2009-04-19 21:24:32 +0200116#endif
117 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
Johannes Bergfffd0932009-07-08 14:22:54 +0200118 if (err) {
119 wdev->connect_keys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200120 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200121 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200122
123 memcpy(wdev->ssid, params->ssid, params->ssid_len);
124 wdev->ssid_len = params->ssid_len;
125
126 return 0;
127}
128
Johannes Berg667503dd2009-07-07 03:56:11 +0200129int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
130 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200131 struct cfg80211_ibss_params *params,
132 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +0200133{
134 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200135 int err;
136
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200137 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200138 wdev_lock(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200139 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
Johannes Berg667503dd2009-07-07 03:56:11 +0200140 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200141 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200142
143 return err;
144}
145
146static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
147{
148 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200149 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
150 int i;
Johannes Berg667503dd2009-07-07 03:56:11 +0200151
152 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200153
Johannes Bergfffd0932009-07-08 14:22:54 +0200154 kfree(wdev->connect_keys);
155 wdev->connect_keys = NULL;
156
157 /*
158 * Delete all the keys ... pairwise keys can't really
159 * exist any more anyway, but default keys might.
160 */
161 if (rdev->ops->del_key)
162 for (i = 0; i < 6; i++)
Johannes Berge31b8212010-10-05 19:39:30 +0200163 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200164
Johannes Berg04a773a2009-04-19 21:24:32 +0200165 if (wdev->current_bss) {
166 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +0200167 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +0200168 }
169
170 wdev->current_bss = NULL;
171 wdev->ssid_len = 0;
Johannes Berg3d23e342009-09-29 23:27:28 +0200172#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9d308422009-04-20 18:43:46 +0200173 if (!nowext)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200174 wdev->wext.ibss.ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +0200175#endif
Johannes Berg04a773a2009-04-19 21:24:32 +0200176}
177
Johannes Berg667503dd2009-07-07 03:56:11 +0200178void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
179{
180 struct wireless_dev *wdev = dev->ieee80211_ptr;
181
182 wdev_lock(wdev);
183 __cfg80211_clear_ibss(dev, nowext);
184 wdev_unlock(wdev);
185}
186
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100187int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
188 struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +0200189{
Johannes Berg78485472009-07-07 03:56:05 +0200190 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg04a773a2009-04-19 21:24:32 +0200191 int err;
192
Johannes Berg667503dd2009-07-07 03:56:11 +0200193 ASSERT_WDEV_LOCK(wdev);
194
Johannes Berg78485472009-07-07 03:56:05 +0200195 if (!wdev->ssid_len)
196 return -ENOLINK;
197
Johannes Berg04a773a2009-04-19 21:24:32 +0200198 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
199
200 if (err)
201 return err;
202
Johannes Berg667503dd2009-07-07 03:56:11 +0200203 __cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200204
205 return 0;
206}
207
Johannes Berg667503dd2009-07-07 03:56:11 +0200208int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
209 struct net_device *dev, bool nowext)
210{
211 struct wireless_dev *wdev = dev->ieee80211_ptr;
212 int err;
213
214 wdev_lock(wdev);
215 err = __cfg80211_leave_ibss(rdev, dev, nowext);
216 wdev_unlock(wdev);
217
218 return err;
219}
220
Johannes Berg3d23e342009-09-29 23:27:28 +0200221#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfffd0932009-07-08 14:22:54 +0200222int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
223 struct wireless_dev *wdev)
Johannes Berg04a773a2009-04-19 21:24:32 +0200224{
Johannes Bergfffd0932009-07-08 14:22:54 +0200225 struct cfg80211_cached_keys *ck = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200226 enum ieee80211_band band;
Johannes Bergfffd0932009-07-08 14:22:54 +0200227 int i, err;
228
229 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200230
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200231 if (!wdev->wext.ibss.beacon_interval)
232 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200233
Johannes Berg04a773a2009-04-19 21:24:32 +0200234 /* try to find an IBSS channel if none requested ... */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200235 if (!wdev->wext.ibss.channel) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200236 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
237 struct ieee80211_supported_band *sband;
238 struct ieee80211_channel *chan;
239
240 sband = rdev->wiphy.bands[band];
241 if (!sband)
242 continue;
243
244 for (i = 0; i < sband->n_channels; i++) {
245 chan = &sband->channels[i];
246 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
247 continue;
248 if (chan->flags & IEEE80211_CHAN_DISABLED)
249 continue;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200250 wdev->wext.ibss.channel = chan;
Johannes Berg04a773a2009-04-19 21:24:32 +0200251 break;
252 }
253
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200254 if (wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200255 break;
256 }
257
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200258 if (!wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200259 return -EINVAL;
260 }
261
262 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200263 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200264 return 0;
265
266 if (!netif_running(wdev->netdev))
267 return 0;
268
Johannes Berg3be61a32010-07-22 13:59:15 +0200269 if (wdev->wext.keys) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200270 wdev->wext.keys->def = wdev->wext.default_key;
Johannes Berg3be61a32010-07-22 13:59:15 +0200271 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
272 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200273
274 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
275
276 if (wdev->wext.keys) {
277 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
278 if (!ck)
279 return -ENOMEM;
280 for (i = 0; i < 6; i++)
281 ck->params[i].key = ck->data[i];
282 }
283 err = __cfg80211_join_ibss(rdev, wdev->netdev,
284 &wdev->wext.ibss, ck);
285 if (err)
286 kfree(ck);
287
288 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200289}
290
291int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
292 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200293 struct iw_freq *wextfreq, char *extra)
Johannes Berg04a773a2009-04-19 21:24:32 +0200294{
295 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200296 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
297 struct ieee80211_channel *chan = NULL;
298 int err, freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200299
300 /* call only for ibss! */
301 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
302 return -EINVAL;
303
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200304 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200305 return -EOPNOTSUPP;
306
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200307 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
308 if (freq < 0)
309 return freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200310
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200311 if (freq) {
312 chan = ieee80211_get_channel(wdev->wiphy, freq);
313 if (!chan)
314 return -EINVAL;
315 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
316 chan->flags & IEEE80211_CHAN_DISABLED)
317 return -EINVAL;
318 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200319
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200320 if (wdev->wext.ibss.channel == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200321 return 0;
322
Johannes Berg667503dd2009-07-07 03:56:11 +0200323 wdev_lock(wdev);
324 err = 0;
325 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200326 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200327 wdev_unlock(wdev);
328
329 if (err)
330 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200331
332 if (chan) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200333 wdev->wext.ibss.channel = chan;
334 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200335 } else {
336 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200337 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200338 }
339
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200340 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200341 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200342 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200343 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200344 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200345
346 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200347}
Johannes Berg04a773a2009-04-19 21:24:32 +0200348
349int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
350 struct iw_request_info *info,
351 struct iw_freq *freq, char *extra)
352{
353 struct wireless_dev *wdev = dev->ieee80211_ptr;
354 struct ieee80211_channel *chan = NULL;
355
356 /* call only for ibss! */
357 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
358 return -EINVAL;
359
Johannes Berg667503dd2009-07-07 03:56:11 +0200360 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200361 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200362 chan = wdev->current_bss->pub.channel;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200363 else if (wdev->wext.ibss.channel)
364 chan = wdev->wext.ibss.channel;
Johannes Berg667503dd2009-07-07 03:56:11 +0200365 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200366
367 if (chan) {
368 freq->m = chan->center_freq;
369 freq->e = 6;
370 return 0;
371 }
372
373 /* no channel if not joining */
374 return -EINVAL;
375}
Johannes Berg04a773a2009-04-19 21:24:32 +0200376
377int cfg80211_ibss_wext_siwessid(struct net_device *dev,
378 struct iw_request_info *info,
379 struct iw_point *data, char *ssid)
380{
381 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200382 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200383 size_t len = data->length;
384 int err;
385
386 /* call only for ibss! */
387 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
388 return -EINVAL;
389
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200390 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200391 return -EOPNOTSUPP;
392
Johannes Berg667503dd2009-07-07 03:56:11 +0200393 wdev_lock(wdev);
394 err = 0;
395 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200396 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200397 wdev_unlock(wdev);
398
399 if (err)
400 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200401
402 /* iwconfig uses nul termination in SSID.. */
403 if (len > 0 && ssid[len - 1] == '\0')
404 len--;
405
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200406 wdev->wext.ibss.ssid = wdev->ssid;
407 memcpy(wdev->wext.ibss.ssid, ssid, len);
408 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200409
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200410 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200411 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200412 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200413 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200414 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200415
416 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200417}
Johannes Berg04a773a2009-04-19 21:24:32 +0200418
419int cfg80211_ibss_wext_giwessid(struct net_device *dev,
420 struct iw_request_info *info,
421 struct iw_point *data, char *ssid)
422{
423 struct wireless_dev *wdev = dev->ieee80211_ptr;
424
425 /* call only for ibss! */
426 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
427 return -EINVAL;
428
429 data->flags = 0;
430
Johannes Berg667503dd2009-07-07 03:56:11 +0200431 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200432 if (wdev->ssid_len) {
433 data->flags = 1;
434 data->length = wdev->ssid_len;
435 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200436 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200437 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200438 data->length = wdev->wext.ibss.ssid_len;
439 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200440 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200441 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200442
443 return 0;
444}
Johannes Berg04a773a2009-04-19 21:24:32 +0200445
446int cfg80211_ibss_wext_siwap(struct net_device *dev,
447 struct iw_request_info *info,
448 struct sockaddr *ap_addr, char *extra)
449{
450 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200451 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200452 u8 *bssid = ap_addr->sa_data;
453 int err;
454
455 /* call only for ibss! */
456 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
457 return -EINVAL;
458
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200459 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200460 return -EOPNOTSUPP;
461
462 if (ap_addr->sa_family != ARPHRD_ETHER)
463 return -EINVAL;
464
465 /* automatic mode */
466 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
467 bssid = NULL;
468
469 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200470 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200471 return 0;
472
473 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200474 if (wdev->wext.ibss.bssid && bssid &&
475 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
Johannes Berg04a773a2009-04-19 21:24:32 +0200476 return 0;
477
Johannes Berg667503dd2009-07-07 03:56:11 +0200478 wdev_lock(wdev);
479 err = 0;
480 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200481 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200482 wdev_unlock(wdev);
483
484 if (err)
485 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200486
487 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200488 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
489 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200490 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200491 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200492
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200493 mutex_lock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200494 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200495 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200496 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200497 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergfffd0932009-07-08 14:22:54 +0200498
499 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200500}
Johannes Berg04a773a2009-04-19 21:24:32 +0200501
502int cfg80211_ibss_wext_giwap(struct net_device *dev,
503 struct iw_request_info *info,
504 struct sockaddr *ap_addr, char *extra)
505{
506 struct wireless_dev *wdev = dev->ieee80211_ptr;
507
508 /* call only for ibss! */
509 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
510 return -EINVAL;
511
512 ap_addr->sa_family = ARPHRD_ETHER;
513
Johannes Berg667503dd2009-07-07 03:56:11 +0200514 wdev_lock(wdev);
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200515 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200516 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800517 else if (wdev->wext.ibss.bssid)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200518 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800519 else
520 memset(ap_addr->sa_data, 0, ETH_ALEN);
521
Johannes Berg667503dd2009-07-07 03:56:11 +0200522 wdev_unlock(wdev);
523
Johannes Berg04a773a2009-04-19 21:24:32 +0200524 return 0;
525}
Johannes Berg04a773a2009-04-19 21:24:32 +0200526#endif