blob: 89737ee2669a8de9bc8f02aa1c392052369d210b [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"
Hila Gonene35e4d22012-06-27 17:19:42 +030014#include "rdev-ops.h"
Johannes Berg04a773a2009-04-19 21:24:32 +020015
16
Johannes Berg667503dd2009-07-07 03:56:11 +020017void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +020018{
19 struct wireless_dev *wdev = dev->ieee80211_ptr;
20 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +020021#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020022 union iwreq_data wrqu;
23#endif
24
25 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
26 return;
27
Johannes Bergf7969962009-08-21 12:23:49 +020028 if (!wdev->ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +020029 return;
30
Johannes Berg04a773a2009-04-19 21:24:32 +020031 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
32 wdev->ssid, wdev->ssid_len,
33 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
34
35 if (WARN_ON(!bss))
36 return;
37
38 if (wdev->current_bss) {
39 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +010040 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +020041 }
42
Johannes Berg19957bb2009-07-02 17:20:43 +020043 cfg80211_hold_bss(bss_from_pub(bss));
44 wdev->current_bss = bss_from_pub(bss);
Johannes Berg04a773a2009-04-19 21:24:32 +020045
Johannes Bergfffd0932009-07-08 14:22:54 +020046 cfg80211_upload_connect_keys(wdev);
47
Johannes Berg667503dd2009-07-07 03:56:11 +020048 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
49 GFP_KERNEL);
Johannes Berg3d23e342009-09-29 23:27:28 +020050#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020051 memset(&wrqu, 0, sizeof(wrqu));
52 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
53 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
54#endif
55}
Johannes Berg667503dd2009-07-07 03:56:11 +020056
57void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
58{
59 struct wireless_dev *wdev = dev->ieee80211_ptr;
60 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
61 struct cfg80211_event *ev;
62 unsigned long flags;
63
Beni Lev4ee3e062012-08-27 12:49:39 +030064 trace_cfg80211_ibss_joined(dev, bssid);
65
Johannes Berg667503dd2009-07-07 03:56:11 +020066 ev = kzalloc(sizeof(*ev), gfp);
67 if (!ev)
68 return;
69
70 ev->type = EVENT_IBSS_JOINED;
71 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
72
73 spin_lock_irqsave(&wdev->event_lock, flags);
74 list_add_tail(&ev->list, &wdev->event_list);
75 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +010076 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +020077}
Johannes Berg04a773a2009-04-19 21:24:32 +020078EXPORT_SYMBOL(cfg80211_ibss_joined);
79
Johannes Berg667503dd2009-07-07 03:56:11 +020080int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
81 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +020082 struct cfg80211_ibss_params *params,
83 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +020084{
85 struct wireless_dev *wdev = dev->ieee80211_ptr;
Simon Wunderlich5336fa82013-10-07 18:41:05 +020086 struct ieee80211_channel *check_chan;
87 u8 radar_detect_width = 0;
Johannes Berg04a773a2009-04-19 21:24:32 +020088 int err;
89
Johannes Berg667503dd2009-07-07 03:56:11 +020090 ASSERT_WDEV_LOCK(wdev);
91
Johannes Berg04a773a2009-04-19 21:24:32 +020092 if (wdev->ssid_len)
93 return -EALREADY;
94
Johannes Berg93b05232010-09-28 12:53:14 +020095 if (!params->basic_rates) {
96 /*
97 * If no rates were explicitly configured,
98 * use the mandatory rate set for 11b or
99 * 11a for maximum compatibility.
100 */
101 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +0100102 rdev->wiphy.bands[params->chandef.chan->band];
Johannes Berg93b05232010-09-28 12:53:14 +0200103 int j;
Johannes Berg683b6d32012-11-08 21:25:48 +0100104 u32 flag = params->chandef.chan->band == IEEE80211_BAND_5GHZ ?
Johannes Berg93b05232010-09-28 12:53:14 +0200105 IEEE80211_RATE_MANDATORY_A :
106 IEEE80211_RATE_MANDATORY_B;
107
108 for (j = 0; j < sband->n_bitrates; j++) {
109 if (sband->bitrates[j].flags & flag)
110 params->basic_rates |= BIT(j);
111 }
112 }
113
Johannes Bergfffd0932009-07-08 14:22:54 +0200114 if (WARN_ON(wdev->connect_keys))
115 kfree(wdev->connect_keys);
116 wdev->connect_keys = connkeys;
117
Michal Kaziorc30a3d32012-06-29 12:46:59 +0200118 wdev->ibss_fixed = params->channel_fixed;
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200119 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
Johannes Berg3d23e342009-09-29 23:27:28 +0200120#ifdef CONFIG_CFG80211_WEXT
Johannes Berg683b6d32012-11-08 21:25:48 +0100121 wdev->wext.ibss.chandef = params->chandef;
Johannes Berg04a773a2009-04-19 21:24:32 +0200122#endif
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200123 check_chan = params->chandef.chan;
124 if (params->userspace_handles_dfs) {
125 /* use channel NULL to check for radar even if the current
126 * channel is not a radar channel - it might decide to change
127 * to DFS channel later.
128 */
129 radar_detect_width = BIT(params->chandef.width);
130 check_chan = NULL;
131 }
Michal Kaziore4e32452012-06-29 12:47:08 +0200132
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200133 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
134 check_chan,
135 (params->channel_fixed &&
136 !radar_detect_width)
137 ? CHAN_MODE_SHARED
138 : CHAN_MODE_EXCLUSIVE,
139 radar_detect_width);
140
Michal Kaziore4e32452012-06-29 12:47:08 +0200141 if (err) {
142 wdev->connect_keys = NULL;
143 return err;
144 }
145
Hila Gonene35e4d22012-06-27 17:19:42 +0300146 err = rdev_join_ibss(rdev, dev, params);
Johannes Bergfffd0932009-07-08 14:22:54 +0200147 if (err) {
148 wdev->connect_keys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200149 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200150 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200151
152 memcpy(wdev->ssid, params->ssid, params->ssid_len);
153 wdev->ssid_len = params->ssid_len;
154
155 return 0;
156}
157
Johannes Berg667503dd2009-07-07 03:56:11 +0200158int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
159 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200160 struct cfg80211_ibss_params *params,
161 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +0200162{
163 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200164 int err;
165
Johannes Berg5fe231e2013-05-08 21:45:15 +0200166 ASSERT_RTNL();
167
Johannes Berg667503dd2009-07-07 03:56:11 +0200168 wdev_lock(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200169 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
Johannes Berg667503dd2009-07-07 03:56:11 +0200170 wdev_unlock(wdev);
171
172 return err;
173}
174
175static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
176{
177 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200178 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
179 int i;
Johannes Berg667503dd2009-07-07 03:56:11 +0200180
181 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200182
Johannes Bergfffd0932009-07-08 14:22:54 +0200183 kfree(wdev->connect_keys);
184 wdev->connect_keys = NULL;
185
186 /*
187 * Delete all the keys ... pairwise keys can't really
188 * exist any more anyway, but default keys might.
189 */
190 if (rdev->ops->del_key)
191 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300192 rdev_del_key(rdev, dev, i, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200193
Johannes Berg04a773a2009-04-19 21:24:32 +0200194 if (wdev->current_bss) {
195 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100196 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +0200197 }
198
199 wdev->current_bss = NULL;
200 wdev->ssid_len = 0;
Johannes Berg3d23e342009-09-29 23:27:28 +0200201#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9d308422009-04-20 18:43:46 +0200202 if (!nowext)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200203 wdev->wext.ibss.ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +0200204#endif
Johannes Berg04a773a2009-04-19 21:24:32 +0200205}
206
Johannes Berg667503dd2009-07-07 03:56:11 +0200207void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
208{
209 struct wireless_dev *wdev = dev->ieee80211_ptr;
210
211 wdev_lock(wdev);
212 __cfg80211_clear_ibss(dev, nowext);
213 wdev_unlock(wdev);
214}
215
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100216int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
217 struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +0200218{
Johannes Berg78485472009-07-07 03:56:05 +0200219 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg04a773a2009-04-19 21:24:32 +0200220 int err;
221
Johannes Berg667503dd2009-07-07 03:56:11 +0200222 ASSERT_WDEV_LOCK(wdev);
223
Johannes Berg78485472009-07-07 03:56:05 +0200224 if (!wdev->ssid_len)
225 return -ENOLINK;
226
Hila Gonene35e4d22012-06-27 17:19:42 +0300227 err = rdev_leave_ibss(rdev, dev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200228
229 if (err)
230 return err;
231
Johannes Berg667503dd2009-07-07 03:56:11 +0200232 __cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200233
234 return 0;
235}
236
Johannes Berg667503dd2009-07-07 03:56:11 +0200237int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
238 struct net_device *dev, bool nowext)
239{
240 struct wireless_dev *wdev = dev->ieee80211_ptr;
241 int err;
242
243 wdev_lock(wdev);
244 err = __cfg80211_leave_ibss(rdev, dev, nowext);
245 wdev_unlock(wdev);
246
247 return err;
248}
249
Johannes Berg3d23e342009-09-29 23:27:28 +0200250#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfffd0932009-07-08 14:22:54 +0200251int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
252 struct wireless_dev *wdev)
Johannes Berg04a773a2009-04-19 21:24:32 +0200253{
Johannes Bergfffd0932009-07-08 14:22:54 +0200254 struct cfg80211_cached_keys *ck = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200255 enum ieee80211_band band;
Johannes Bergfffd0932009-07-08 14:22:54 +0200256 int i, err;
257
258 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200259
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200260 if (!wdev->wext.ibss.beacon_interval)
261 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200262
Johannes Berg04a773a2009-04-19 21:24:32 +0200263 /* try to find an IBSS channel if none requested ... */
Johannes Berg683b6d32012-11-08 21:25:48 +0100264 if (!wdev->wext.ibss.chandef.chan) {
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100265 struct ieee80211_channel *new_chan = NULL;
Johannes Berg683b6d32012-11-08 21:25:48 +0100266
Johannes Berg04a773a2009-04-19 21:24:32 +0200267 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
268 struct ieee80211_supported_band *sband;
269 struct ieee80211_channel *chan;
270
271 sband = rdev->wiphy.bands[band];
272 if (!sband)
273 continue;
274
275 for (i = 0; i < sband->n_channels; i++) {
276 chan = &sband->channels[i];
277 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
278 continue;
279 if (chan->flags & IEEE80211_CHAN_DISABLED)
280 continue;
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100281 new_chan = chan;
Johannes Berg04a773a2009-04-19 21:24:32 +0200282 break;
283 }
284
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100285 if (new_chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200286 break;
287 }
288
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100289 if (!new_chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200290 return -EINVAL;
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100291
292 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
293 NL80211_CHAN_NO_HT);
Johannes Berg04a773a2009-04-19 21:24:32 +0200294 }
295
296 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200297 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200298 return 0;
299
300 if (!netif_running(wdev->netdev))
301 return 0;
302
Johannes Berg3be61a32010-07-22 13:59:15 +0200303 if (wdev->wext.keys) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200304 wdev->wext.keys->def = wdev->wext.default_key;
Johannes Berg3be61a32010-07-22 13:59:15 +0200305 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
306 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200307
308 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
309
310 if (wdev->wext.keys) {
311 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
312 if (!ck)
313 return -ENOMEM;
314 for (i = 0; i < 6; i++)
315 ck->params[i].key = ck->data[i];
316 }
317 err = __cfg80211_join_ibss(rdev, wdev->netdev,
318 &wdev->wext.ibss, ck);
319 if (err)
320 kfree(ck);
321
322 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200323}
324
325int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
326 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200327 struct iw_freq *wextfreq, char *extra)
Johannes Berg04a773a2009-04-19 21:24:32 +0200328{
329 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200330 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
331 struct ieee80211_channel *chan = NULL;
332 int err, freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200333
334 /* call only for ibss! */
335 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
336 return -EINVAL;
337
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200338 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200339 return -EOPNOTSUPP;
340
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200341 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
342 if (freq < 0)
343 return freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200344
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200345 if (freq) {
346 chan = ieee80211_get_channel(wdev->wiphy, freq);
347 if (!chan)
348 return -EINVAL;
349 if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
350 chan->flags & IEEE80211_CHAN_DISABLED)
351 return -EINVAL;
352 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200353
Johannes Berg683b6d32012-11-08 21:25:48 +0100354 if (wdev->wext.ibss.chandef.chan == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200355 return 0;
356
Johannes Berg667503dd2009-07-07 03:56:11 +0200357 wdev_lock(wdev);
358 err = 0;
359 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200360 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200361 wdev_unlock(wdev);
362
363 if (err)
364 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200365
366 if (chan) {
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100367 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
368 NL80211_CHAN_NO_HT);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200369 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200370 } else {
371 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200372 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200373 }
374
Johannes Bergfffd0932009-07-08 14:22:54 +0200375 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200376 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200377 wdev_unlock(wdev);
378
379 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200380}
Johannes Berg04a773a2009-04-19 21:24:32 +0200381
382int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
383 struct iw_request_info *info,
384 struct iw_freq *freq, char *extra)
385{
386 struct wireless_dev *wdev = dev->ieee80211_ptr;
387 struct ieee80211_channel *chan = NULL;
388
389 /* call only for ibss! */
390 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
391 return -EINVAL;
392
Johannes Berg667503dd2009-07-07 03:56:11 +0200393 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200394 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200395 chan = wdev->current_bss->pub.channel;
Johannes Berg683b6d32012-11-08 21:25:48 +0100396 else if (wdev->wext.ibss.chandef.chan)
397 chan = wdev->wext.ibss.chandef.chan;
Johannes Berg667503dd2009-07-07 03:56:11 +0200398 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200399
400 if (chan) {
401 freq->m = chan->center_freq;
402 freq->e = 6;
403 return 0;
404 }
405
406 /* no channel if not joining */
407 return -EINVAL;
408}
Johannes Berg04a773a2009-04-19 21:24:32 +0200409
410int cfg80211_ibss_wext_siwessid(struct net_device *dev,
411 struct iw_request_info *info,
412 struct iw_point *data, char *ssid)
413{
414 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200415 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200416 size_t len = data->length;
417 int err;
418
419 /* call only for ibss! */
420 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
421 return -EINVAL;
422
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200423 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200424 return -EOPNOTSUPP;
425
Johannes Berg667503dd2009-07-07 03:56:11 +0200426 wdev_lock(wdev);
427 err = 0;
428 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200429 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200430 wdev_unlock(wdev);
431
432 if (err)
433 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200434
435 /* iwconfig uses nul termination in SSID.. */
436 if (len > 0 && ssid[len - 1] == '\0')
437 len--;
438
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200439 wdev->wext.ibss.ssid = wdev->ssid;
440 memcpy(wdev->wext.ibss.ssid, ssid, len);
441 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200442
Johannes Bergfffd0932009-07-08 14:22:54 +0200443 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200444 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200445 wdev_unlock(wdev);
446
447 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200448}
Johannes Berg04a773a2009-04-19 21:24:32 +0200449
450int cfg80211_ibss_wext_giwessid(struct net_device *dev,
451 struct iw_request_info *info,
452 struct iw_point *data, char *ssid)
453{
454 struct wireless_dev *wdev = dev->ieee80211_ptr;
455
456 /* call only for ibss! */
457 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
458 return -EINVAL;
459
460 data->flags = 0;
461
Johannes Berg667503dd2009-07-07 03:56:11 +0200462 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200463 if (wdev->ssid_len) {
464 data->flags = 1;
465 data->length = wdev->ssid_len;
466 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200467 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200468 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200469 data->length = wdev->wext.ibss.ssid_len;
470 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200471 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200472 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200473
474 return 0;
475}
Johannes Berg04a773a2009-04-19 21:24:32 +0200476
477int cfg80211_ibss_wext_siwap(struct net_device *dev,
478 struct iw_request_info *info,
479 struct sockaddr *ap_addr, char *extra)
480{
481 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200482 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200483 u8 *bssid = ap_addr->sa_data;
484 int err;
485
486 /* call only for ibss! */
487 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
488 return -EINVAL;
489
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200490 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200491 return -EOPNOTSUPP;
492
493 if (ap_addr->sa_family != ARPHRD_ETHER)
494 return -EINVAL;
495
496 /* automatic mode */
497 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
498 bssid = NULL;
499
500 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200501 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200502 return 0;
503
504 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200505 if (wdev->wext.ibss.bssid && bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000506 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
Johannes Berg04a773a2009-04-19 21:24:32 +0200507 return 0;
508
Johannes Berg667503dd2009-07-07 03:56:11 +0200509 wdev_lock(wdev);
510 err = 0;
511 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200512 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200513 wdev_unlock(wdev);
514
515 if (err)
516 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200517
518 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200519 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
520 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200521 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200522 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200523
Johannes Bergfffd0932009-07-08 14:22:54 +0200524 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200525 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200526 wdev_unlock(wdev);
527
528 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200529}
Johannes Berg04a773a2009-04-19 21:24:32 +0200530
531int cfg80211_ibss_wext_giwap(struct net_device *dev,
532 struct iw_request_info *info,
533 struct sockaddr *ap_addr, char *extra)
534{
535 struct wireless_dev *wdev = dev->ieee80211_ptr;
536
537 /* call only for ibss! */
538 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
539 return -EINVAL;
540
541 ap_addr->sa_family = ARPHRD_ETHER;
542
Johannes Berg667503dd2009-07-07 03:56:11 +0200543 wdev_lock(wdev);
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200544 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200545 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800546 else if (wdev->wext.ibss.bssid)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200547 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800548 else
549 memset(ap_addr->sa_data, 0, ETH_ALEN);
550
Johannes Berg667503dd2009-07-07 03:56:11 +0200551 wdev_unlock(wdev);
552
Johannes Berg04a773a2009-04-19 21:24:32 +0200553 return 0;
554}
Johannes Berg04a773a2009-04-19 21:24:32 +0200555#endif