blob: 413d4f4e63345bd561b6c157ea7bdd2bc0c64504 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Johannes Berg04a773a2009-04-19 21:24:32 +02002/*
3 * Some IBSS support code for cfg80211.
4 *
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
6 */
7
8#include <linux/etherdevice.h>
9#include <linux/if_arp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040011#include <linux/export.h>
Johannes Berg04a773a2009-04-19 21:24:32 +020012#include <net/cfg80211.h>
Johannes Berg0e82ffe2009-07-27 12:01:50 +020013#include "wext-compat.h"
Johannes Berg04a773a2009-04-19 21:24:32 +020014#include "nl80211.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030015#include "rdev-ops.h"
Johannes Berg04a773a2009-04-19 21:24:32 +020016
17
Antonio Quartullife94f3a2014-01-29 17:53:43 +010018void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
19 struct ieee80211_channel *channel)
Johannes Berg04a773a2009-04-19 21:24:32 +020020{
21 struct wireless_dev *wdev = dev->ieee80211_ptr;
22 struct cfg80211_bss *bss;
Johannes Berg3d23e342009-09-29 23:27:28 +020023#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020024 union iwreq_data wrqu;
25#endif
26
27 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
28 return;
29
Johannes Bergf7969962009-08-21 12:23:49 +020030 if (!wdev->ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +020031 return;
32
Antonio Quartullife94f3a2014-01-29 17:53:43 +010033 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
Dedy Lansky6eb18132015-02-08 15:52:03 +020034 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
Johannes Berg04a773a2009-04-19 21:24:32 +020035
36 if (WARN_ON(!bss))
37 return;
38
39 if (wdev->current_bss) {
40 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +010041 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +020042 }
43
Johannes Berg19957bb2009-07-02 17:20:43 +020044 cfg80211_hold_bss(bss_from_pub(bss));
45 wdev->current_bss = bss_from_pub(bss);
Johannes Berg04a773a2009-04-19 21:24:32 +020046
David Spinadelb8676222016-09-22 23:16:50 +030047 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
48 cfg80211_upload_connect_keys(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +020049
Zhao, Gangf26cbf42014-04-21 12:53:03 +080050 nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid,
Johannes Berg667503d2009-07-07 03:56:11 +020051 GFP_KERNEL);
Johannes Berg3d23e342009-09-29 23:27:28 +020052#ifdef CONFIG_CFG80211_WEXT
Johannes Berg04a773a2009-04-19 21:24:32 +020053 memset(&wrqu, 0, sizeof(wrqu));
54 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
55 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
56#endif
57}
Johannes Berg667503d2009-07-07 03:56:11 +020058
Antonio Quartullife94f3a2014-01-29 17:53:43 +010059void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
60 struct ieee80211_channel *channel, gfp_t gfp)
Johannes Berg667503d2009-07-07 03:56:11 +020061{
62 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +080063 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg667503d2009-07-07 03:56:11 +020064 struct cfg80211_event *ev;
65 unsigned long flags;
66
Antonio Quartullife94f3a2014-01-29 17:53:43 +010067 trace_cfg80211_ibss_joined(dev, bssid, channel);
68
69 if (WARN_ON(!channel))
70 return;
Beni Lev4ee3e062012-08-27 12:49:39 +030071
Johannes Berg667503d2009-07-07 03:56:11 +020072 ev = kzalloc(sizeof(*ev), gfp);
73 if (!ev)
74 return;
75
76 ev->type = EVENT_IBSS_JOINED;
Antonio Quartullife94f3a2014-01-29 17:53:43 +010077 memcpy(ev->ij.bssid, bssid, ETH_ALEN);
78 ev->ij.channel = channel;
Johannes Berg667503d2009-07-07 03:56:11 +020079
80 spin_lock_irqsave(&wdev->event_lock, flags);
81 list_add_tail(&ev->list, &wdev->event_list);
82 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +010083 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503d2009-07-07 03:56:11 +020084}
Johannes Berg04a773a2009-04-19 21:24:32 +020085EXPORT_SYMBOL(cfg80211_ibss_joined);
86
Zhao, Gang2316d7b2014-03-08 11:01:24 +080087static int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
88 struct net_device *dev,
89 struct cfg80211_ibss_params *params,
90 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +020091{
92 struct wireless_dev *wdev = dev->ieee80211_ptr;
93 int err;
94
Johannes Berg667503d2009-07-07 03:56:11 +020095 ASSERT_WDEV_LOCK(wdev);
96
Johannes Berg04a773a2009-04-19 21:24:32 +020097 if (wdev->ssid_len)
98 return -EALREADY;
99
Johannes Berg93b05232010-09-28 12:53:14 +0200100 if (!params->basic_rates) {
101 /*
102 * If no rates were explicitly configured,
103 * use the mandatory rate set for 11b or
104 * 11a for maximum compatibility.
105 */
106 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +0100107 rdev->wiphy.bands[params->chandef.chan->band];
Johannes Berg93b05232010-09-28 12:53:14 +0200108 int j;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200109 u32 flag = params->chandef.chan->band == NL80211_BAND_5GHZ ?
Johannes Berg93b05232010-09-28 12:53:14 +0200110 IEEE80211_RATE_MANDATORY_A :
111 IEEE80211_RATE_MANDATORY_B;
112
113 for (j = 0; j < sband->n_bitrates; j++) {
114 if (sband->bitrates[j].flags & flag)
115 params->basic_rates |= BIT(j);
116 }
117 }
118
Johannes Bergf1c1f172016-09-13 17:08:23 +0200119 if (WARN_ON(connkeys && connkeys->def < 0))
120 return -EINVAL;
121
Johannes Bergfffd0932009-07-08 14:22:54 +0200122 if (WARN_ON(wdev->connect_keys))
Johannes Bergb47f6102014-09-10 13:39:54 +0300123 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200124 wdev->connect_keys = connkeys;
125
Michal Kaziorc30a3d32012-06-29 12:46:59 +0200126 wdev->ibss_fixed = params->channel_fixed;
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200127 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
Michal Kazior9e0e2962014-01-29 14:22:27 +0100128 wdev->chandef = params->chandef;
Johannes Berg3d23e342009-09-29 23:27:28 +0200129#ifdef CONFIG_CFG80211_WEXT
Johannes Berg683b6d32012-11-08 21:25:48 +0100130 wdev->wext.ibss.chandef = params->chandef;
Johannes Berg04a773a2009-04-19 21:24:32 +0200131#endif
Hila Gonene35e4d22012-06-27 17:19:42 +0300132 err = rdev_join_ibss(rdev, dev, params);
Johannes Bergfffd0932009-07-08 14:22:54 +0200133 if (err) {
134 wdev->connect_keys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200135 return err;
Johannes Bergfffd0932009-07-08 14:22:54 +0200136 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200137
138 memcpy(wdev->ssid, params->ssid, params->ssid_len);
139 wdev->ssid_len = params->ssid_len;
140
141 return 0;
142}
143
Johannes Berg667503d2009-07-07 03:56:11 +0200144int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
145 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200146 struct cfg80211_ibss_params *params,
147 struct cfg80211_cached_keys *connkeys)
Johannes Berg04a773a2009-04-19 21:24:32 +0200148{
149 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503d2009-07-07 03:56:11 +0200150 int err;
151
Johannes Berg5fe231e2013-05-08 21:45:15 +0200152 ASSERT_RTNL();
153
Johannes Berg667503d2009-07-07 03:56:11 +0200154 wdev_lock(wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200155 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
Johannes Berg667503d2009-07-07 03:56:11 +0200156 wdev_unlock(wdev);
157
158 return err;
159}
160
161static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
162{
163 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800164 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Bergfffd0932009-07-08 14:22:54 +0200165 int i;
Johannes Berg667503d2009-07-07 03:56:11 +0200166
167 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200168
Johannes Bergb47f6102014-09-10 13:39:54 +0300169 kzfree(wdev->connect_keys);
Johannes Bergfffd0932009-07-08 14:22:54 +0200170 wdev->connect_keys = NULL;
171
Kyeyoon Parkfa9ffc72013-12-16 23:01:30 -0800172 rdev_set_qos_map(rdev, dev, NULL);
173
Johannes Bergfffd0932009-07-08 14:22:54 +0200174 /*
175 * Delete all the keys ... pairwise keys can't really
176 * exist any more anyway, but default keys might.
177 */
178 if (rdev->ops->del_key)
179 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300180 rdev_del_key(rdev, dev, i, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200181
Johannes Berg04a773a2009-04-19 21:24:32 +0200182 if (wdev->current_bss) {
183 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100184 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +0200185 }
186
187 wdev->current_bss = NULL;
188 wdev->ssid_len = 0;
Michal Kazior9e0e2962014-01-29 14:22:27 +0100189 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
Johannes Berg3d23e342009-09-29 23:27:28 +0200190#ifdef CONFIG_CFG80211_WEXT
Johannes Berg9d308422009-04-20 18:43:46 +0200191 if (!nowext)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200192 wdev->wext.ibss.ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +0200193#endif
Vasanthakumar Thiagarajanb35a51c2017-02-27 17:04:33 +0530194 cfg80211_sched_dfs_chan_update(rdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200195}
196
Johannes Berg667503d2009-07-07 03:56:11 +0200197void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
198{
199 struct wireless_dev *wdev = dev->ieee80211_ptr;
200
201 wdev_lock(wdev);
202 __cfg80211_clear_ibss(dev, nowext);
203 wdev_unlock(wdev);
204}
205
Johannes Berg98d3a7c2009-11-18 13:03:43 +0100206int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
207 struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +0200208{
Johannes Berg78485472009-07-07 03:56:05 +0200209 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg04a773a2009-04-19 21:24:32 +0200210 int err;
211
Johannes Berg667503d2009-07-07 03:56:11 +0200212 ASSERT_WDEV_LOCK(wdev);
213
Johannes Berg78485472009-07-07 03:56:05 +0200214 if (!wdev->ssid_len)
215 return -ENOLINK;
216
Hila Gonene35e4d22012-06-27 17:19:42 +0300217 err = rdev_leave_ibss(rdev, dev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200218
219 if (err)
220 return err;
221
Johannes Berg667503d2009-07-07 03:56:11 +0200222 __cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200223
224 return 0;
225}
226
Johannes Berg667503d2009-07-07 03:56:11 +0200227int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
228 struct net_device *dev, bool nowext)
229{
230 struct wireless_dev *wdev = dev->ieee80211_ptr;
231 int err;
232
233 wdev_lock(wdev);
234 err = __cfg80211_leave_ibss(rdev, dev, nowext);
235 wdev_unlock(wdev);
236
237 return err;
238}
239
Johannes Berg3d23e342009-09-29 23:27:28 +0200240#ifdef CONFIG_CFG80211_WEXT
Johannes Bergfffd0932009-07-08 14:22:54 +0200241int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
242 struct wireless_dev *wdev)
Johannes Berg04a773a2009-04-19 21:24:32 +0200243{
Johannes Bergfffd0932009-07-08 14:22:54 +0200244 struct cfg80211_cached_keys *ck = NULL;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200245 enum nl80211_band band;
Johannes Bergfffd0932009-07-08 14:22:54 +0200246 int i, err;
247
248 ASSERT_WDEV_LOCK(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200249
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200250 if (!wdev->wext.ibss.beacon_interval)
251 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200252
Johannes Berg04a773a2009-04-19 21:24:32 +0200253 /* try to find an IBSS channel if none requested ... */
Johannes Berg683b6d32012-11-08 21:25:48 +0100254 if (!wdev->wext.ibss.chandef.chan) {
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100255 struct ieee80211_channel *new_chan = NULL;
Johannes Berg683b6d32012-11-08 21:25:48 +0100256
Johannes Berg57fbcce2016-04-12 15:56:15 +0200257 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200258 struct ieee80211_supported_band *sband;
259 struct ieee80211_channel *chan;
260
261 sband = rdev->wiphy.bands[band];
262 if (!sband)
263 continue;
264
265 for (i = 0; i < sband->n_channels; i++) {
266 chan = &sband->channels[i];
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200267 if (chan->flags & IEEE80211_CHAN_NO_IR)
Johannes Berg04a773a2009-04-19 21:24:32 +0200268 continue;
269 if (chan->flags & IEEE80211_CHAN_DISABLED)
270 continue;
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100271 new_chan = chan;
Johannes Berg04a773a2009-04-19 21:24:32 +0200272 break;
273 }
274
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100275 if (new_chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200276 break;
277 }
278
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100279 if (!new_chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200280 return -EINVAL;
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100281
282 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
283 NL80211_CHAN_NO_HT);
Johannes Berg04a773a2009-04-19 21:24:32 +0200284 }
285
286 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200287 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200288 return 0;
289
290 if (!netif_running(wdev->netdev))
291 return 0;
292
Johannes Berg89b706f2016-09-13 16:39:38 +0200293 if (wdev->wext.keys)
Johannes Bergfffd0932009-07-08 14:22:54 +0200294 wdev->wext.keys->def = wdev->wext.default_key;
295
296 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
297
Johannes Bergf1c1f172016-09-13 17:08:23 +0200298 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200299 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
300 if (!ck)
301 return -ENOMEM;
David Spinadelb8676222016-09-22 23:16:50 +0300302 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
Johannes Bergfffd0932009-07-08 14:22:54 +0200303 ck->params[i].key = ck->data[i];
304 }
305 err = __cfg80211_join_ibss(rdev, wdev->netdev,
306 &wdev->wext.ibss, ck);
307 if (err)
308 kfree(ck);
309
310 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200311}
312
313int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
314 struct iw_request_info *info,
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200315 struct iw_freq *wextfreq, char *extra)
Johannes Berg04a773a2009-04-19 21:24:32 +0200316{
317 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800318 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200319 struct ieee80211_channel *chan = NULL;
320 int err, freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200321
322 /* call only for ibss! */
323 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
324 return -EINVAL;
325
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200326 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200327 return -EOPNOTSUPP;
328
Zhao, Gang96998e32014-04-09 09:28:06 +0800329 freq = cfg80211_wext_freq(wextfreq);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200330 if (freq < 0)
331 return freq;
Johannes Berg04a773a2009-04-19 21:24:32 +0200332
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200333 if (freq) {
334 chan = ieee80211_get_channel(wdev->wiphy, freq);
335 if (!chan)
336 return -EINVAL;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200337 if (chan->flags & IEEE80211_CHAN_NO_IR ||
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200338 chan->flags & IEEE80211_CHAN_DISABLED)
339 return -EINVAL;
340 }
Johannes Berg04a773a2009-04-19 21:24:32 +0200341
Johannes Berg683b6d32012-11-08 21:25:48 +0100342 if (wdev->wext.ibss.chandef.chan == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200343 return 0;
344
Johannes Berg667503d2009-07-07 03:56:11 +0200345 wdev_lock(wdev);
346 err = 0;
347 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200348 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503d2009-07-07 03:56:11 +0200349 wdev_unlock(wdev);
350
351 if (err)
352 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200353
354 if (chan) {
Simon Wunderlich1fe45172013-10-30 16:09:33 +0100355 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
356 NL80211_CHAN_NO_HT);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200357 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200358 } else {
359 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200360 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200361 }
362
Johannes Bergfffd0932009-07-08 14:22:54 +0200363 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200364 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200365 wdev_unlock(wdev);
366
367 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200368}
Johannes Berg04a773a2009-04-19 21:24:32 +0200369
370int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
371 struct iw_request_info *info,
372 struct iw_freq *freq, char *extra)
373{
374 struct wireless_dev *wdev = dev->ieee80211_ptr;
375 struct ieee80211_channel *chan = NULL;
376
377 /* call only for ibss! */
378 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
379 return -EINVAL;
380
Johannes Berg667503d2009-07-07 03:56:11 +0200381 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200382 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200383 chan = wdev->current_bss->pub.channel;
Johannes Berg683b6d32012-11-08 21:25:48 +0100384 else if (wdev->wext.ibss.chandef.chan)
385 chan = wdev->wext.ibss.chandef.chan;
Johannes Berg667503d2009-07-07 03:56:11 +0200386 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200387
388 if (chan) {
389 freq->m = chan->center_freq;
390 freq->e = 6;
391 return 0;
392 }
393
394 /* no channel if not joining */
395 return -EINVAL;
396}
Johannes Berg04a773a2009-04-19 21:24:32 +0200397
398int cfg80211_ibss_wext_siwessid(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;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800403 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200404 size_t len = data->length;
405 int err;
406
407 /* call only for ibss! */
408 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
409 return -EINVAL;
410
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200411 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200412 return -EOPNOTSUPP;
413
Johannes Berg667503d2009-07-07 03:56:11 +0200414 wdev_lock(wdev);
415 err = 0;
416 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200417 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503d2009-07-07 03:56:11 +0200418 wdev_unlock(wdev);
419
420 if (err)
421 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200422
423 /* iwconfig uses nul termination in SSID.. */
424 if (len > 0 && ssid[len - 1] == '\0')
425 len--;
426
Johannes Bergc1e5f472014-05-19 17:53:16 +0200427 memcpy(wdev->ssid, ssid, len);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200428 wdev->wext.ibss.ssid = wdev->ssid;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200429 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200430
Johannes Bergfffd0932009-07-08 14:22:54 +0200431 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200432 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200433 wdev_unlock(wdev);
434
435 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200436}
Johannes Berg04a773a2009-04-19 21:24:32 +0200437
438int cfg80211_ibss_wext_giwessid(struct net_device *dev,
439 struct iw_request_info *info,
440 struct iw_point *data, char *ssid)
441{
442 struct wireless_dev *wdev = dev->ieee80211_ptr;
443
444 /* call only for ibss! */
445 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
446 return -EINVAL;
447
448 data->flags = 0;
449
Johannes Berg667503d2009-07-07 03:56:11 +0200450 wdev_lock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200451 if (wdev->ssid_len) {
452 data->flags = 1;
453 data->length = wdev->ssid_len;
454 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200455 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200456 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200457 data->length = wdev->wext.ibss.ssid_len;
458 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200459 }
Johannes Berg667503d2009-07-07 03:56:11 +0200460 wdev_unlock(wdev);
Johannes Berg04a773a2009-04-19 21:24:32 +0200461
462 return 0;
463}
Johannes Berg04a773a2009-04-19 21:24:32 +0200464
465int cfg80211_ibss_wext_siwap(struct net_device *dev,
466 struct iw_request_info *info,
467 struct sockaddr *ap_addr, char *extra)
468{
469 struct wireless_dev *wdev = dev->ieee80211_ptr;
Zhao, Gangf26cbf42014-04-21 12:53:03 +0800470 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
Johannes Berg04a773a2009-04-19 21:24:32 +0200471 u8 *bssid = ap_addr->sa_data;
472 int err;
473
474 /* call only for ibss! */
475 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
476 return -EINVAL;
477
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200478 if (!rdev->ops->join_ibss)
Johannes Berg04a773a2009-04-19 21:24:32 +0200479 return -EOPNOTSUPP;
480
481 if (ap_addr->sa_family != ARPHRD_ETHER)
482 return -EINVAL;
483
484 /* automatic mode */
485 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
486 bssid = NULL;
487
Johannes Berg74f82742014-04-09 22:32:16 +0200488 if (bssid && !is_valid_ether_addr(bssid))
489 return -EINVAL;
490
Johannes Berg04a773a2009-04-19 21:24:32 +0200491 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200492 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200493 return 0;
494
495 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200496 if (wdev->wext.ibss.bssid && bssid &&
Joe Perchesac422d32012-05-08 18:56:55 +0000497 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
Johannes Berg04a773a2009-04-19 21:24:32 +0200498 return 0;
499
Johannes Berg667503d2009-07-07 03:56:11 +0200500 wdev_lock(wdev);
501 err = 0;
502 if (wdev->ssid_len)
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200503 err = __cfg80211_leave_ibss(rdev, dev, true);
Johannes Berg667503d2009-07-07 03:56:11 +0200504 wdev_unlock(wdev);
505
506 if (err)
507 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200508
509 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200510 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
511 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200512 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200513 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200514
Johannes Bergfffd0932009-07-08 14:22:54 +0200515 wdev_lock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200516 err = cfg80211_ibss_wext_join(rdev, wdev);
Johannes Bergfffd0932009-07-08 14:22:54 +0200517 wdev_unlock(wdev);
518
519 return err;
Johannes Berg04a773a2009-04-19 21:24:32 +0200520}
Johannes Berg04a773a2009-04-19 21:24:32 +0200521
522int cfg80211_ibss_wext_giwap(struct net_device *dev,
523 struct iw_request_info *info,
524 struct sockaddr *ap_addr, char *extra)
525{
526 struct wireless_dev *wdev = dev->ieee80211_ptr;
527
528 /* call only for ibss! */
529 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
530 return -EINVAL;
531
532 ap_addr->sa_family = ARPHRD_ETHER;
533
Johannes Berg667503d2009-07-07 03:56:11 +0200534 wdev_lock(wdev);
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200535 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200536 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800537 else if (wdev->wext.ibss.bssid)
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200538 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Zhu Yi80e5b062009-07-09 16:59:49 +0800539 else
Joe Perchesd2beae12015-03-02 19:54:58 -0800540 eth_zero_addr(ap_addr->sa_data);
Zhu Yi80e5b062009-07-09 16:59:49 +0800541
Johannes Berg667503d2009-07-07 03:56:11 +0200542 wdev_unlock(wdev);
543
Johannes Berg04a773a2009-04-19 21:24:32 +0200544 return 0;
545}
Johannes Berg04a773a2009-04-19 21:24:32 +0200546#endif