projects/linux·blob master · refs

500 lines · 11.7 KiB

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