Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Some IBSS support code for cfg80211.
4 *
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright (C) 2020-2023 Intel Corporation
7 */
8
9#include <linux/etherdevice.h>
10#include <linux/if_arp.h>
11#include <linux/slab.h>
12#include <linux/export.h>
13#include <net/cfg80211.h>
14#include "wext-compat.h"
15#include "nl80211.h"
16#include "rdev-ops.h"
17
18
19void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
20 struct ieee80211_channel *channel)
21{
22 struct wireless_dev *wdev = dev->ieee80211_ptr;
23 struct cfg80211_bss *bss;
24#ifdef CONFIG_CFG80211_WEXT
25 union iwreq_data wrqu;
26#endif
27
28 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
29 return;
30
31 if (!wdev->u.ibss.ssid_len)
32 return;
33
34 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
35 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
36
37 if (WARN_ON(!bss))
38 return;
39
40 if (wdev->u.ibss.current_bss) {
41 cfg80211_unhold_bss(wdev->u.ibss.current_bss);
42 cfg80211_put_bss(wdev->wiphy, &wdev->u.ibss.current_bss->pub);
43 }
44
45 cfg80211_hold_bss(bss_from_pub(bss));
46 wdev->u.ibss.current_bss = bss_from_pub(bss);
47
48 cfg80211_upload_connect_keys(wdev);
49
50 nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid,
51 GFP_KERNEL);
52#ifdef CONFIG_CFG80211_WEXT
53 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}
58
59void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
60 struct ieee80211_channel *channel, gfp_t gfp)
61{
62 struct wireless_dev *wdev = dev->ieee80211_ptr;
63 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
64 struct cfg80211_event *ev;
65 unsigned long flags;
66
67 trace_cfg80211_ibss_joined(dev, bssid, channel);
68
69 if (WARN_ON(!channel))
70 return;
71
72 ev = kzalloc(sizeof(*ev), gfp);
73 if (!ev)
74 return;
75
76 ev->type = EVENT_IBSS_JOINED;
77 memcpy(ev->ij.bssid, bssid, ETH_ALEN);
78 ev->ij.channel = channel;
79
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);
83 queue_work(cfg80211_wq, &rdev->event_work);
84}
85EXPORT_SYMBOL(cfg80211_ibss_joined);
86
87int __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)
91{
92 struct wireless_dev *wdev = dev->ieee80211_ptr;
93 int err;
94
95 lockdep_assert_held(&rdev->wiphy.mtx);
96
97 if (wdev->u.ibss.ssid_len)
98 return -EALREADY;
99
100 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;
107 enum nl80211_band band;
108 u32 flag;
109 int j;
110
111 band = params->chandef.chan->band;
112 if (band == NL80211_BAND_5GHZ ||
113 band == NL80211_BAND_6GHZ)
114 flag = IEEE80211_RATE_MANDATORY_A;
115 else
116 flag = IEEE80211_RATE_MANDATORY_B;
117
118 sband = rdev->wiphy.bands[band];
119 for (j = 0; j < sband->n_bitrates; j++) {
120 if (sband->bitrates[j].flags & flag)
121 params->basic_rates |= BIT(j);
122 }
123 }
124
125 if (WARN_ON(connkeys && connkeys->def < 0))
126 return -EINVAL;
127
128 if (WARN_ON(wdev->connect_keys))
129 kfree_sensitive(wdev->connect_keys);
130 wdev->connect_keys = connkeys;
131
132 wdev->u.ibss.chandef = params->chandef;
133 if (connkeys) {
134 params->wep_keys = connkeys->params;
135 params->wep_tx_key = connkeys->def;
136 }
137
138#ifdef CONFIG_CFG80211_WEXT
139 wdev->wext.ibss.chandef = params->chandef;
140#endif
141 err = rdev_join_ibss(rdev, dev, params);
142 if (err) {
143 wdev->connect_keys = NULL;
144 return err;
145 }
146
147 memcpy(wdev->u.ibss.ssid, params->ssid, params->ssid_len);
148 wdev->u.ibss.ssid_len = params->ssid_len;
149
150 return 0;
151}
152
153void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
154{
155 struct wireless_dev *wdev = dev->ieee80211_ptr;
156 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
157 int i;
158
159 lockdep_assert_wiphy(wdev->wiphy);
160
161 kfree_sensitive(wdev->connect_keys);
162 wdev->connect_keys = NULL;
163
164 rdev_set_qos_map(rdev, dev, NULL);
165
166 /*
167 * Delete all the keys ... pairwise keys can't really
168 * exist any more anyway, but default keys might.
169 */
170 if (rdev->ops->del_key)
171 for (i = 0; i < 6; i++)
172 rdev_del_key(rdev, dev, -1, i, false, NULL);
173
174 if (wdev->u.ibss.current_bss) {
175 cfg80211_unhold_bss(wdev->u.ibss.current_bss);
176 cfg80211_put_bss(wdev->wiphy, &wdev->u.ibss.current_bss->pub);
177 }
178
179 wdev->u.ibss.current_bss = NULL;
180 wdev->u.ibss.ssid_len = 0;
181 memset(&wdev->u.ibss.chandef, 0, sizeof(wdev->u.ibss.chandef));
182#ifdef CONFIG_CFG80211_WEXT
183 if (!nowext)
184 wdev->wext.ibss.ssid_len = 0;
185#endif
186 cfg80211_sched_dfs_chan_update(rdev);
187}
188
189int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
190 struct net_device *dev, bool nowext)
191{
192 struct wireless_dev *wdev = dev->ieee80211_ptr;
193 int err;
194
195 lockdep_assert_wiphy(wdev->wiphy);
196
197 if (!wdev->u.ibss.ssid_len)
198 return -ENOLINK;
199
200 err = rdev_leave_ibss(rdev, dev);
201
202 if (err)
203 return err;
204
205 wdev->conn_owner_nlportid = 0;
206 cfg80211_clear_ibss(dev, nowext);
207
208 return 0;
209}
210
211#ifdef CONFIG_CFG80211_WEXT
212int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
213 struct wireless_dev *wdev)
214{
215 struct cfg80211_cached_keys *ck = NULL;
216 enum nl80211_band band;
217 int i, err;
218
219 lockdep_assert_wiphy(wdev->wiphy);
220
221 if (!wdev->wext.ibss.beacon_interval)
222 wdev->wext.ibss.beacon_interval = 100;
223
224 /* try to find an IBSS channel if none requested ... */
225 if (!wdev->wext.ibss.chandef.chan) {
226 struct ieee80211_channel *new_chan = NULL;
227
228 for (band = 0; band < NUM_NL80211_BANDS; band++) {
229 struct ieee80211_supported_band *sband;
230 struct ieee80211_channel *chan;
231
232 sband = rdev->wiphy.bands[band];
233 if (!sband)
234 continue;
235
236 for (i = 0; i < sband->n_channels; i++) {
237 chan = &sband->channels[i];
238 if (chan->flags & IEEE80211_CHAN_NO_IR)
239 continue;
240 if (chan->flags & IEEE80211_CHAN_DISABLED)
241 continue;
242 new_chan = chan;
243 break;
244 }
245
246 if (new_chan)
247 break;
248 }
249
250 if (!new_chan)
251 return -EINVAL;
252
253 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
254 NL80211_CHAN_NO_HT);
255 }
256
257 /* don't join -- SSID is not there */
258 if (!wdev->wext.ibss.ssid_len)
259 return 0;
260
261 if (!netif_running(wdev->netdev))
262 return 0;
263
264 if (wdev->wext.keys)
265 wdev->wext.keys->def = wdev->wext.default_key;
266
267 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
268
269 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
270 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
271 if (!ck)
272 return -ENOMEM;
273 for (i = 0; i < 4; i++)
274 ck->params[i].key = ck->data[i];
275 }
276 err = __cfg80211_join_ibss(rdev, wdev->netdev,
277 &wdev->wext.ibss, ck);
278 if (err)
279 kfree(ck);
280
281 return err;
282}
283
284int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
285 struct iw_request_info *info,
286 struct iw_freq *wextfreq, char *extra)
287{
288 struct wireless_dev *wdev = dev->ieee80211_ptr;
289 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
290 struct ieee80211_channel *chan = NULL;
291 int err, freq;
292
293 /* call only for ibss! */
294 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
295 return -EINVAL;
296
297 if (!rdev->ops->join_ibss)
298 return -EOPNOTSUPP;
299
300 freq = cfg80211_wext_freq(wextfreq);
301 if (freq < 0)
302 return freq;
303
304 if (freq) {
305 chan = ieee80211_get_channel(wdev->wiphy, freq);
306 if (!chan)
307 return -EINVAL;
308 if (chan->flags & IEEE80211_CHAN_NO_IR ||
309 chan->flags & IEEE80211_CHAN_DISABLED)
310 return -EINVAL;
311 }
312
313 if (wdev->wext.ibss.chandef.chan == chan)
314 return 0;
315
316 err = 0;
317 if (wdev->u.ibss.ssid_len)
318 err = cfg80211_leave_ibss(rdev, dev, true);
319
320 if (err)
321 return err;
322
323 if (chan) {
324 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
325 NL80211_CHAN_NO_HT);
326 wdev->wext.ibss.channel_fixed = true;
327 } else {
328 /* cfg80211_ibss_wext_join will pick one if needed */
329 wdev->wext.ibss.channel_fixed = false;
330 }
331
332 return cfg80211_ibss_wext_join(rdev, wdev);
333}
334
335int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
336 struct iw_request_info *info,
337 struct iw_freq *freq, char *extra)
338{
339 struct wireless_dev *wdev = dev->ieee80211_ptr;
340 struct ieee80211_channel *chan = NULL;
341
342 /* call only for ibss! */
343 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
344 return -EINVAL;
345
346 if (wdev->u.ibss.current_bss)
347 chan = wdev->u.ibss.current_bss->pub.channel;
348 else if (wdev->wext.ibss.chandef.chan)
349 chan = wdev->wext.ibss.chandef.chan;
350
351 if (chan) {
352 freq->m = chan->center_freq;
353 freq->e = 6;
354 return 0;
355 }
356
357 /* no channel if not joining */
358 return -EINVAL;
359}
360
361int cfg80211_ibss_wext_siwessid(struct net_device *dev,
362 struct iw_request_info *info,
363 struct iw_point *data, char *ssid)
364{
365 struct wireless_dev *wdev = dev->ieee80211_ptr;
366 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
367 size_t len = data->length;
368 int err;
369
370 /* call only for ibss! */
371 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
372 return -EINVAL;
373
374 if (!rdev->ops->join_ibss)
375 return -EOPNOTSUPP;
376
377 err = 0;
378 if (wdev->u.ibss.ssid_len)
379 err = cfg80211_leave_ibss(rdev, dev, true);
380
381 if (err)
382 return err;
383
384 /* iwconfig uses nul termination in SSID.. */
385 if (len > 0 && ssid[len - 1] == '\0')
386 len--;
387
388 memcpy(wdev->u.ibss.ssid, ssid, len);
389 wdev->wext.ibss.ssid = wdev->u.ibss.ssid;
390 wdev->wext.ibss.ssid_len = len;
391
392 return cfg80211_ibss_wext_join(rdev, wdev);
393}
394
395int cfg80211_ibss_wext_giwessid(struct net_device *dev,
396 struct iw_request_info *info,
397 struct iw_point *data, char *ssid)
398{
399 struct wireless_dev *wdev = dev->ieee80211_ptr;
400
401 /* call only for ibss! */
402 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
403 return -EINVAL;
404
405 data->flags = 0;
406
407 if (wdev->u.ibss.ssid_len) {
408 data->flags = 1;
409 data->length = wdev->u.ibss.ssid_len;
410 memcpy(ssid, wdev->u.ibss.ssid, data->length);
411 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
412 data->flags = 1;
413 data->length = wdev->wext.ibss.ssid_len;
414 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
415 }
416
417 return 0;
418}
419
420int cfg80211_ibss_wext_siwap(struct net_device *dev,
421 struct iw_request_info *info,
422 struct sockaddr *ap_addr, char *extra)
423{
424 struct wireless_dev *wdev = dev->ieee80211_ptr;
425 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
426 u8 *bssid = ap_addr->sa_data;
427 int err;
428
429 /* call only for ibss! */
430 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
431 return -EINVAL;
432
433 if (!rdev->ops->join_ibss)
434 return -EOPNOTSUPP;
435
436 if (ap_addr->sa_family != ARPHRD_ETHER)
437 return -EINVAL;
438
439 /* automatic mode */
440 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
441 bssid = NULL;
442
443 if (bssid && !is_valid_ether_addr(bssid))
444 return -EINVAL;
445
446 /* both automatic */
447 if (!bssid && !wdev->wext.ibss.bssid)
448 return 0;
449
450 /* fixed already - and no change */
451 if (wdev->wext.ibss.bssid && bssid &&
452 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
453 return 0;
454
455 err = 0;
456 if (wdev->u.ibss.ssid_len)
457 err = cfg80211_leave_ibss(rdev, dev, true);
458
459 if (err)
460 return err;
461
462 if (bssid) {
463 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
464 wdev->wext.ibss.bssid = wdev->wext.bssid;
465 } else
466 wdev->wext.ibss.bssid = NULL;
467
468 return cfg80211_ibss_wext_join(rdev, wdev);
469}
470
471int cfg80211_ibss_wext_giwap(struct net_device *dev,
472 struct iw_request_info *info,
473 struct sockaddr *ap_addr, char *extra)
474{
475 struct wireless_dev *wdev = dev->ieee80211_ptr;
476
477 /* call only for ibss! */
478 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
479 return -EINVAL;
480
481 ap_addr->sa_family = ARPHRD_ETHER;
482
483 if (wdev->u.ibss.current_bss)
484 memcpy(ap_addr->sa_data, wdev->u.ibss.current_bss->pub.bssid,
485 ETH_ALEN);
486 else if (wdev->wext.ibss.bssid)
487 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
488 else
489 eth_zero_addr(ap_addr->sa_data);
490
491 return 0;
492}
493#endif
1/*
2 * Some IBSS support code for cfg80211.
3 *
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7#include <linux/etherdevice.h>
8#include <linux/if_arp.h>
9#include <linux/slab.h>
10#include <net/cfg80211.h>
11#include "wext-compat.h"
12#include "nl80211.h"
13
14
15void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
16{
17 struct wireless_dev *wdev = dev->ieee80211_ptr;
18 struct cfg80211_bss *bss;
19#ifdef CONFIG_CFG80211_WEXT
20 union iwreq_data wrqu;
21#endif
22
23 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
24 return;
25
26 if (!wdev->ssid_len)
27 return;
28
29 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);
38 cfg80211_put_bss(&wdev->current_bss->pub);
39 }
40
41 cfg80211_hold_bss(bss_from_pub(bss));
42 wdev->current_bss = bss_from_pub(bss);
43
44 cfg80211_upload_connect_keys(wdev);
45
46 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
47 GFP_KERNEL);
48#ifdef CONFIG_CFG80211_WEXT
49 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}
54
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
62 CFG80211_DEV_WARN_ON(!wdev->ssid_len);
63
64 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);
74 queue_work(cfg80211_wq, &rdev->event_work);
75}
76EXPORT_SYMBOL(cfg80211_ibss_joined);
77
78int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
79 struct net_device *dev,
80 struct cfg80211_ibss_params *params,
81 struct cfg80211_cached_keys *connkeys)
82{
83 struct wireless_dev *wdev = dev->ieee80211_ptr;
84 int err;
85
86 ASSERT_WDEV_LOCK(wdev);
87
88 if (wdev->ssid_len)
89 return -EALREADY;
90
91 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
110 if (WARN_ON(wdev->connect_keys))
111 kfree(wdev->connect_keys);
112 wdev->connect_keys = connkeys;
113
114#ifdef CONFIG_CFG80211_WEXT
115 wdev->wext.ibss.channel = params->channel;
116#endif
117 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
118 if (err) {
119 wdev->connect_keys = NULL;
120 return err;
121 }
122
123 memcpy(wdev->ssid, params->ssid, params->ssid_len);
124 wdev->ssid_len = params->ssid_len;
125
126 return 0;
127}
128
129int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
130 struct net_device *dev,
131 struct cfg80211_ibss_params *params,
132 struct cfg80211_cached_keys *connkeys)
133{
134 struct wireless_dev *wdev = dev->ieee80211_ptr;
135 int err;
136
137 mutex_lock(&rdev->devlist_mtx);
138 wdev_lock(wdev);
139 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
140 wdev_unlock(wdev);
141 mutex_unlock(&rdev->devlist_mtx);
142
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;
149 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
150 int i;
151
152 ASSERT_WDEV_LOCK(wdev);
153
154 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++)
163 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
164
165 if (wdev->current_bss) {
166 cfg80211_unhold_bss(wdev->current_bss);
167 cfg80211_put_bss(&wdev->current_bss->pub);
168 }
169
170 wdev->current_bss = NULL;
171 wdev->ssid_len = 0;
172#ifdef CONFIG_CFG80211_WEXT
173 if (!nowext)
174 wdev->wext.ibss.ssid_len = 0;
175#endif
176}
177
178void 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
187int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
188 struct net_device *dev, bool nowext)
189{
190 struct wireless_dev *wdev = dev->ieee80211_ptr;
191 int err;
192
193 ASSERT_WDEV_LOCK(wdev);
194
195 if (!wdev->ssid_len)
196 return -ENOLINK;
197
198 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
199
200 if (err)
201 return err;
202
203 __cfg80211_clear_ibss(dev, nowext);
204
205 return 0;
206}
207
208int 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
221#ifdef CONFIG_CFG80211_WEXT
222int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
223 struct wireless_dev *wdev)
224{
225 struct cfg80211_cached_keys *ck = NULL;
226 enum ieee80211_band band;
227 int i, err;
228
229 ASSERT_WDEV_LOCK(wdev);
230
231 if (!wdev->wext.ibss.beacon_interval)
232 wdev->wext.ibss.beacon_interval = 100;
233
234 /* try to find an IBSS channel if none requested ... */
235 if (!wdev->wext.ibss.channel) {
236 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;
250 wdev->wext.ibss.channel = chan;
251 break;
252 }
253
254 if (wdev->wext.ibss.channel)
255 break;
256 }
257
258 if (!wdev->wext.ibss.channel)
259 return -EINVAL;
260 }
261
262 /* don't join -- SSID is not there */
263 if (!wdev->wext.ibss.ssid_len)
264 return 0;
265
266 if (!netif_running(wdev->netdev))
267 return 0;
268
269 if (wdev->wext.keys) {
270 wdev->wext.keys->def = wdev->wext.default_key;
271 wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
272 }
273
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;
289}
290
291int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
292 struct iw_request_info *info,
293 struct iw_freq *wextfreq, char *extra)
294{
295 struct wireless_dev *wdev = dev->ieee80211_ptr;
296 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
297 struct ieee80211_channel *chan = NULL;
298 int err, freq;
299
300 /* call only for ibss! */
301 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
302 return -EINVAL;
303
304 if (!rdev->ops->join_ibss)
305 return -EOPNOTSUPP;
306
307 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
308 if (freq < 0)
309 return freq;
310
311 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 }
319
320 if (wdev->wext.ibss.channel == chan)
321 return 0;
322
323 wdev_lock(wdev);
324 err = 0;
325 if (wdev->ssid_len)
326 err = __cfg80211_leave_ibss(rdev, dev, true);
327 wdev_unlock(wdev);
328
329 if (err)
330 return err;
331
332 if (chan) {
333 wdev->wext.ibss.channel = chan;
334 wdev->wext.ibss.channel_fixed = true;
335 } else {
336 /* cfg80211_ibss_wext_join will pick one if needed */
337 wdev->wext.ibss.channel_fixed = false;
338 }
339
340 mutex_lock(&rdev->devlist_mtx);
341 wdev_lock(wdev);
342 err = cfg80211_ibss_wext_join(rdev, wdev);
343 wdev_unlock(wdev);
344 mutex_unlock(&rdev->devlist_mtx);
345
346 return err;
347}
348
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
360 wdev_lock(wdev);
361 if (wdev->current_bss)
362 chan = wdev->current_bss->pub.channel;
363 else if (wdev->wext.ibss.channel)
364 chan = wdev->wext.ibss.channel;
365 wdev_unlock(wdev);
366
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}
376
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;
382 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
383 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
390 if (!rdev->ops->join_ibss)
391 return -EOPNOTSUPP;
392
393 wdev_lock(wdev);
394 err = 0;
395 if (wdev->ssid_len)
396 err = __cfg80211_leave_ibss(rdev, dev, true);
397 wdev_unlock(wdev);
398
399 if (err)
400 return err;
401
402 /* iwconfig uses nul termination in SSID.. */
403 if (len > 0 && ssid[len - 1] == '\0')
404 len--;
405
406 wdev->wext.ibss.ssid = wdev->ssid;
407 memcpy(wdev->wext.ibss.ssid, ssid, len);
408 wdev->wext.ibss.ssid_len = len;
409
410 mutex_lock(&rdev->devlist_mtx);
411 wdev_lock(wdev);
412 err = cfg80211_ibss_wext_join(rdev, wdev);
413 wdev_unlock(wdev);
414 mutex_unlock(&rdev->devlist_mtx);
415
416 return err;
417}
418
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
431 wdev_lock(wdev);
432 if (wdev->ssid_len) {
433 data->flags = 1;
434 data->length = wdev->ssid_len;
435 memcpy(ssid, wdev->ssid, data->length);
436 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
437 data->flags = 1;
438 data->length = wdev->wext.ibss.ssid_len;
439 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
440 }
441 wdev_unlock(wdev);
442
443 return 0;
444}
445
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;
451 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
452 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
459 if (!rdev->ops->join_ibss)
460 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 */
470 if (!bssid && !wdev->wext.ibss.bssid)
471 return 0;
472
473 /* fixed already - and no change */
474 if (wdev->wext.ibss.bssid && bssid &&
475 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
476 return 0;
477
478 wdev_lock(wdev);
479 err = 0;
480 if (wdev->ssid_len)
481 err = __cfg80211_leave_ibss(rdev, dev, true);
482 wdev_unlock(wdev);
483
484 if (err)
485 return err;
486
487 if (bssid) {
488 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
489 wdev->wext.ibss.bssid = wdev->wext.bssid;
490 } else
491 wdev->wext.ibss.bssid = NULL;
492
493 mutex_lock(&rdev->devlist_mtx);
494 wdev_lock(wdev);
495 err = cfg80211_ibss_wext_join(rdev, wdev);
496 wdev_unlock(wdev);
497 mutex_unlock(&rdev->devlist_mtx);
498
499 return err;
500}
501
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
514 wdev_lock(wdev);
515 if (wdev->current_bss)
516 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
517 else if (wdev->wext.ibss.bssid)
518 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
519 else
520 memset(ap_addr->sa_data, 0, ETH_ALEN);
521
522 wdev_unlock(wdev);
523
524 return 0;
525}
526#endif