blob: 4a4dda53bdf140e1142987410033ab5a4d61a497 [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
Antonio Quartullife94f3a2014-01-29 17:53:43 +010017void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
18 struct ieee80211_channel *channel)
Johannes Berg04a773a2009-04-19 21:24:32 +020019{
20 struct wireless_dev *wdev = dev->ieee80211_ptr;
21 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +020022#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020023 union iwreq_data wrqu;
24#endif
25
26 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
27 return;
28
Johannes Bergf7969962009-08-21 12:23:49 +020029 if (!wdev->ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +020030 return;
31
Antonio Quartullife94f3a2014-01-29 17:53:43 +010032 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
Dedy Lansky6eb18132015-02-08 15:52:03 +020033 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
Johannes Berg04a773a2009-04-19 21:24:32 +020034
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
Zhao, Gangf26cbf42014-04-21 12:53:03 +080048 nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +020049 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
Antonio Quartullife94f3a2014-01-29 17:53:43 +010057void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
58 struct ieee80211_channel *channel, gfp_t gfp)
Johannes Berg667503dd2009-07-07 03:56:11 +020059{
60 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080061 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503dd2009-07-07 03:56:11 +020062 struct cfg80211_event *ev;
63 unsigned long flags;
64
Antonio Quartullife94f3a2014-01-29 17:53:43 +010065 trace_cfg80211_ibss_joined(dev, bssid, channel);
66
67 if (WARN_ON(!channel))
68 return;
Beni Lev4ee3e062012-08-27 12:49:39 +030069
Johannes Berg667503dd2009-07-07 03:56:11 +020070 ev = kzalloc(sizeof(*ev), gfp);
71 if (!ev)
72 return;
73
74 ev->type = EVENT_IBSS_JOINED;
Antonio Quartullife94f3a2014-01-29 17:53:43 +010075 memcpy(ev->ij.bssid, bssid, ETH_ALEN);
76 ev->ij.channel = channel;
Johannes Berg667503dd2009-07-07 03:56:11 +020077
78 spin_lock_irqsave(&wdev->event_lock, flags);
79 list_add_tail(&ev->list, &wdev->event_list);
80 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +010081 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +020082}
Johannes Berg04a773a2009-04-19 21:24:32 +020083EXPORT_SYMBOL(cfg80211_ibss_joined);
84
Zhao, Gang2316d7b2014-03-08 11:01:24 +080085static int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
86 struct net_device *dev,
87 struct cfg80211_ibss_params *params,
88 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +020089{
90 struct wireless_dev *wdev = dev->ieee80211_ptr;
91 int err;
92
Johannes Berg667503dd2009-07-07 03:56:11 +020093 ASSERT_WDEV_LOCK(wdev);
94
Johannes Berg04a773a2009-04-19 21:24:32 +020095 if (wdev->ssid_len)
96 return -EALREADY;
97
Johannes Berg93b05232010-09-28 12:53:14 +020098 if (!params->basic_rates) {
99 /*
100 * If no rates were explicitly configured,
101 * use the mandatory rate set for 11b or
102 * 11a for maximum compatibility.
103 */
104 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +0100105 rdev->wiphy.bands[params->chandef.chan->band];
Johannes Berg93b05232010-09-28 12:53:14 +0200106 int j;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200107 u32 flag = params->chandef.chan->band == NL80211_BAND_5GHZ ?
Johannes Berg93b05232010-09-28 12:53:14 +0200108 IEEE80211_RATE_MANDATORY_A :
109 IEEE80211_RATE_MANDATORY_B;
110
111 for (j = 0; j < sband->n_bitrates; j++) {
112 if (sband->bitrates[j].flags & flag)
113 params->basic_rates |= BIT(j);
114 }
115 }
116
Johannes Bergfffd0932009-07-08 14:22:54 +0200117 if (WARN_ON(wdev->connect_keys))
Johannes Bergb47f6102014-09-10 13:39:54 +0300118 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200119 wdev->connect_keys = connkeys;
120
Michal Kaziorc30a3d32012-06-29 12:46:59 +0200121 wdev->ibss_fixed = params->channel_fixed;
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200122 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
Michal Kazior9e0e2962014-01-29 14:22:27 +0100123 wdev->chandef = params->chandef;
Johannes Berg3d23e342009-09-29 23:27:28 +0200124#ifdef CONFIG_CFG80211_WEXT
Johannes Berg683b6d32012-11-08 21:25:48 +0100125 wdev->wext.ibss.chandef = params->chandef;
Johannes Berg04a773a2009-04-19 21:24:32 +0200126#endif
Hila Gonene35e4d22012-06-27 17:19:42 +0300127 err = rdev_join_ibss(rdev, dev, params);
Johannes Bergfffd0932009-07-08 14:22:54 +0200128 if (err) {
129 wdev->connect_keys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200130 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200131 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200132
133 memcpy(wdev->ssid, params->ssid, params->ssid_len);
134 wdev->ssid_len = params->ssid_len;
135
136 return 0;
137}
138
Johannes Berg667503dd2009-07-07 03:56:11 +0200139int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
140 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200141 struct cfg80211_ibss_params *params,
142 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +0200143{
144 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200145 int err;
146
Johannes Berg5fe231e2013-05-08 21:45:15 +0200147 ASSERT_RTNL();
148
Johannes Berg667503dd2009-07-07 03:56:11 +0200149 wdev_lock(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200150 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
Johannes Berg667503dd2009-07-07 03:56:11 +0200151 wdev_unlock(wdev);
152
153 return err;
154}
155
156static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
157{
158 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800159 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200160 int i;
Johannes Berg667503dd2009-07-07 03:56:11 +0200161
162 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200163
Johannes Bergb47f6102014-09-10 13:39:54 +0300164 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200165 wdev->connect_keys = NULL;
166
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800167 rdev_set_qos_map(rdev, dev, NULL);
168
Johannes Bergfffd0932009-07-08 14:22:54 +0200169 /*
170 * Delete all the keys ... pairwise keys can't really
171 * exist any more anyway, but default keys might.
172 */
173 if (rdev->ops->del_key)
174 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300175 rdev_del_key(rdev, dev, i, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200176
Johannes Berg04a773a2009-04-19 21:24:32 +0200177 if (wdev->current_bss) {
178 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100179 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +0200180 }
181
182 wdev->current_bss = NULL;
183 wdev->ssid_len = 0;
Michal Kazior9e0e2962014-01-29 14:22:27 +0100184 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
Johannes Berg3d23e342009-09-29 23:27:28 +0200185#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9d308422009-04-20 18:43:46 +0200186 if (!nowext)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200187 wdev->wext.ibss.ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +0200188#endif
Johannes Berg04a773a2009-04-19 21:24:32 +0200189}
190
Johannes Berg667503dd2009-07-07 03:56:11 +0200191void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
192{
193 struct wireless_dev *wdev = dev->ieee80211_ptr;
194
195 wdev_lock(wdev);
196 __cfg80211_clear_ibss(dev, nowext);
197 wdev_unlock(wdev);
198}
199
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100200int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
201 struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +0200202{
Johannes Berg78485472009-07-07 03:56:05 +0200203 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg04a773a2009-04-19 21:24:32 +0200204 int err;
205
Johannes Berg667503dd2009-07-07 03:56:11 +0200206 ASSERT_WDEV_LOCK(wdev);
207
Johannes Berg78485472009-07-07 03:56:05 +0200208 if (!wdev->ssid_len)
209 return -ENOLINK;
210
Hila Gonene35e4d22012-06-27 17:19:42 +0300211 err = rdev_leave_ibss(rdev, dev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200212
213 if (err)
214 return err;
215
Johannes Berg667503dd2009-07-07 03:56:11 +0200216 __cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200217
218 return 0;
219}
220
Johannes Berg667503dd2009-07-07 03:56:11 +0200221int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
222 struct net_device *dev, bool nowext)
223{
224 struct wireless_dev *wdev = dev->ieee80211_ptr;
225 int err;
226
227 wdev_lock(wdev);
228 err = __cfg80211_leave_ibss(rdev, dev, nowext);
229 wdev_unlock(wdev);
230
231 return err;
232}
233
Johannes Berg3d23e342009-09-29 23:27:28 +0200234#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfffd0932009-07-08 14:22:54 +0200235int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
236 struct wireless_dev *wdev)
Johannes Berg04a773a2009-04-19 21:24:32 +0200237{
Johannes Bergfffd0932009-07-08 14:22:54 +0200238 struct cfg80211_cached_keys *ck = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200239 enum nl80211_band band;
Johannes Bergfffd0932009-07-08 14:22:54 +0200240 int i, err;
241
242 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200243
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200244 if (!wdev->wext.ibss.beacon_interval)
245 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200246
Johannes Berg04a773a2009-04-19 21:24:32 +0200247 /* try to find an IBSS channel if none requested ... */
Johannes Berg683b6d32012-11-08 21:25:48 +0100248 if (!wdev->wext.ibss.chandef.chan) {
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100249 struct ieee80211_channel *new_chan = NULL;
Johannes Berg683b6d32012-11-08 21:25:48 +0100250
Johannes Berg57fbcce2016-04-12 15:56:15 +0200251 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200252 struct ieee80211_supported_band *sband;
253 struct ieee80211_channel *chan;
254
255 sband = rdev->wiphy.bands[band];
256 if (!sband)
257 continue;
258
259 for (i = 0; i < sband->n_channels; i++) {
260 chan = &sband->channels[i];
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200261 if (chan->flags & IEEE80211_CHAN_NO_IR)
Johannes Berg04a773a2009-04-19 21:24:32 +0200262 continue;
263 if (chan->flags & IEEE80211_CHAN_DISABLED)
264 continue;
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100265 new_chan = chan;
Johannes Berg04a773a2009-04-19 21:24:32 +0200266 break;
267 }
268
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100269 if (new_chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200270 break;
271 }
272
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100273 if (!new_chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200274 return -EINVAL;
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100275
276 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
277 NL80211_CHAN_NO_HT);
Johannes Berg04a773a2009-04-19 21:24:32 +0200278 }
279
280 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200281 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200282 return 0;
283
284 if (!netif_running(wdev->netdev))
285 return 0;
286
Johannes Berg3be61a32010-07-22 13:59:15 +0200287 if (wdev->wext.keys) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200288 wdev->wext.keys->def = wdev->wext.default_key;
Johannes Berg3be61a32010-07-22 13:59:15 +0200289 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
290 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200291
292 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
293
294 if (wdev->wext.keys) {
295 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
296 if (!ck)
297 return -ENOMEM;
298 for (i = 0; i < 6; i++)
299 ck->params[i].key = ck->data[i];
300 }
301 err = __cfg80211_join_ibss(rdev, wdev->netdev,
302 &wdev->wext.ibss, ck);
303 if (err)
304 kfree(ck);
305
306 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200307}
308
309int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
310 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200311 struct iw_freq *wextfreq, char *extra)
Johannes Berg04a773a2009-04-19 21:24:32 +0200312{
313 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800314 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200315 struct ieee80211_channel *chan = NULL;
316 int err, freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200317
318 /* call only for ibss! */
319 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
320 return -EINVAL;
321
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200322 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200323 return -EOPNOTSUPP;
324
Zhao, Gang96998e32014-04-09 09:28:06 +0800325 freq = cfg80211_wext_freq(wextfreq);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200326 if (freq < 0)
327 return freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200328
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200329 if (freq) {
330 chan = ieee80211_get_channel(wdev->wiphy, freq);
331 if (!chan)
332 return -EINVAL;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200333 if (chan->flags & IEEE80211_CHAN_NO_IR ||
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200334 chan->flags & IEEE80211_CHAN_DISABLED)
335 return -EINVAL;
336 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200337
Johannes Berg683b6d32012-11-08 21:25:48 +0100338 if (wdev->wext.ibss.chandef.chan == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200339 return 0;
340
Johannes Berg667503dd2009-07-07 03:56:11 +0200341 wdev_lock(wdev);
342 err = 0;
343 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200344 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200345 wdev_unlock(wdev);
346
347 if (err)
348 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200349
350 if (chan) {
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100351 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
352 NL80211_CHAN_NO_HT);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200353 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200354 } else {
355 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200356 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200357 }
358
Johannes Bergfffd0932009-07-08 14:22:54 +0200359 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200360 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200361 wdev_unlock(wdev);
362
363 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200364}
Johannes Berg04a773a2009-04-19 21:24:32 +0200365
366int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
367 struct iw_request_info *info,
368 struct iw_freq *freq, char *extra)
369{
370 struct wireless_dev *wdev = dev->ieee80211_ptr;
371 struct ieee80211_channel *chan = NULL;
372
373 /* call only for ibss! */
374 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
375 return -EINVAL;
376
Johannes Berg667503dd2009-07-07 03:56:11 +0200377 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200378 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200379 chan = wdev->current_bss->pub.channel;
Johannes Berg683b6d32012-11-08 21:25:48 +0100380 else if (wdev->wext.ibss.chandef.chan)
381 chan = wdev->wext.ibss.chandef.chan;
Johannes Berg667503dd2009-07-07 03:56:11 +0200382 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200383
384 if (chan) {
385 freq->m = chan->center_freq;
386 freq->e = 6;
387 return 0;
388 }
389
390 /* no channel if not joining */
391 return -EINVAL;
392}
Johannes Berg04a773a2009-04-19 21:24:32 +0200393
394int cfg80211_ibss_wext_siwessid(struct net_device *dev,
395 struct iw_request_info *info,
396 struct iw_point *data, char *ssid)
397{
398 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800399 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200400 size_t len = data->length;
401 int err;
402
403 /* call only for ibss! */
404 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
405 return -EINVAL;
406
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200407 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200408 return -EOPNOTSUPP;
409
Johannes Berg667503dd2009-07-07 03:56:11 +0200410 wdev_lock(wdev);
411 err = 0;
412 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200413 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200414 wdev_unlock(wdev);
415
416 if (err)
417 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200418
419 /* iwconfig uses nul termination in SSID.. */
420 if (len > 0 && ssid[len - 1] == '\0')
421 len--;
422
Johannes Bergc1e5f472014-05-19 17:53:16 +0200423 memcpy(wdev->ssid, ssid, len);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200424 wdev->wext.ibss.ssid = wdev->ssid;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200425 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200426
Johannes Bergfffd0932009-07-08 14:22:54 +0200427 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200428 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200429 wdev_unlock(wdev);
430
431 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200432}
Johannes Berg04a773a2009-04-19 21:24:32 +0200433
434int cfg80211_ibss_wext_giwessid(struct net_device *dev,
435 struct iw_request_info *info,
436 struct iw_point *data, char *ssid)
437{
438 struct wireless_dev *wdev = dev->ieee80211_ptr;
439
440 /* call only for ibss! */
441 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
442 return -EINVAL;
443
444 data->flags = 0;
445
Johannes Berg667503dd2009-07-07 03:56:11 +0200446 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200447 if (wdev->ssid_len) {
448 data->flags = 1;
449 data->length = wdev->ssid_len;
450 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200451 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200452 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200453 data->length = wdev->wext.ibss.ssid_len;
454 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200455 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200456 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200457
458 return 0;
459}
Johannes Berg04a773a2009-04-19 21:24:32 +0200460
461int cfg80211_ibss_wext_siwap(struct net_device *dev,
462 struct iw_request_info *info,
463 struct sockaddr *ap_addr, char *extra)
464{
465 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800466 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200467 u8 *bssid = ap_addr->sa_data;
468 int err;
469
470 /* call only for ibss! */
471 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
472 return -EINVAL;
473
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200474 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200475 return -EOPNOTSUPP;
476
477 if (ap_addr->sa_family != ARPHRD_ETHER)
478 return -EINVAL;
479
480 /* automatic mode */
481 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
482 bssid = NULL;
483
Johannes Berg74f82742014-04-09 22:32:16 +0200484 if (bssid && !is_valid_ether_addr(bssid))
485 return -EINVAL;
486
Johannes Berg04a773a2009-04-19 21:24:32 +0200487 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200488 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200489 return 0;
490
491 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200492 if (wdev->wext.ibss.bssid && bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000493 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
Johannes Berg04a773a2009-04-19 21:24:32 +0200494 return 0;
495
Johannes Berg667503dd2009-07-07 03:56:11 +0200496 wdev_lock(wdev);
497 err = 0;
498 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200499 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503dd2009-07-07 03:56:11 +0200500 wdev_unlock(wdev);
501
502 if (err)
503 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200504
505 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200506 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
507 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200508 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200509 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200510
Johannes Bergfffd0932009-07-08 14:22:54 +0200511 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200512 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200513 wdev_unlock(wdev);
514
515 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200516}
Johannes Berg04a773a2009-04-19 21:24:32 +0200517
518int cfg80211_ibss_wext_giwap(struct net_device *dev,
519 struct iw_request_info *info,
520 struct sockaddr *ap_addr, char *extra)
521{
522 struct wireless_dev *wdev = dev->ieee80211_ptr;
523
524 /* call only for ibss! */
525 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
526 return -EINVAL;
527
528 ap_addr->sa_family = ARPHRD_ETHER;
529
Johannes Berg667503dd2009-07-07 03:56:11 +0200530 wdev_lock(wdev);
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200531 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200532 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800533 else if (wdev->wext.ibss.bssid)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200534 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800535 else
Joe Perchesd2beae12015-03-02 19:54:58 -0800536 eth_zero_addr(ap_addr->sa_data);
Zhu Yi80e5b062009-07-09 16:59:49 +0800537
Johannes Berg667503dd2009-07-07 03:56:11 +0200538 wdev_unlock(wdev);
539
Johannes Berg04a773a2009-04-19 21:24:32 +0200540 return 0;
541}
Johannes Berg04a773a2009-04-19 21:24:32 +0200542#endif