1 // SPDX-License-Identifier: GPL-2.0
3 * Some IBSS support code for cfg80211.
6 * Copyright (C) 2020-2022 Intel Corporation
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"
19 void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
20 struct ieee80211_channel *channel)
22 struct wireless_dev *wdev = dev->ieee80211_ptr;
23 struct cfg80211_bss *bss;
24 #ifdef CONFIG_CFG80211_WEXT
25 union iwreq_data wrqu;
28 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
34 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
35 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
40 if (wdev->current_bss) {
41 cfg80211_unhold_bss(wdev->current_bss);
42 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
45 cfg80211_hold_bss(bss_from_pub(bss));
46 wdev->current_bss = bss_from_pub(bss);
48 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
49 cfg80211_upload_connect_keys(wdev);
51 nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid,
53 #ifdef CONFIG_CFG80211_WEXT
54 memset(&wrqu, 0, sizeof(wrqu));
55 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
56 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
60 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
61 struct ieee80211_channel *channel, gfp_t gfp)
63 struct wireless_dev *wdev = dev->ieee80211_ptr;
64 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
65 struct cfg80211_event *ev;
68 trace_cfg80211_ibss_joined(dev, bssid, channel);
70 if (WARN_ON(!channel))
73 ev = kzalloc(sizeof(*ev), gfp);
77 ev->type = EVENT_IBSS_JOINED;
78 memcpy(ev->ij.bssid, bssid, ETH_ALEN);
79 ev->ij.channel = channel;
81 spin_lock_irqsave(&wdev->event_lock, flags);
82 list_add_tail(&ev->list, &wdev->event_list);
83 spin_unlock_irqrestore(&wdev->event_lock, flags);
84 queue_work(cfg80211_wq, &rdev->event_work);
86 EXPORT_SYMBOL(cfg80211_ibss_joined);
88 int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
89 struct net_device *dev,
90 struct cfg80211_ibss_params *params,
91 struct cfg80211_cached_keys *connkeys)
93 struct wireless_dev *wdev = dev->ieee80211_ptr;
96 lockdep_assert_held(&rdev->wiphy.mtx);
97 ASSERT_WDEV_LOCK(wdev);
102 if (!params->basic_rates) {
104 * If no rates were explicitly configured,
105 * use the mandatory rate set for 11b or
106 * 11a for maximum compatibility.
108 struct ieee80211_supported_band *sband;
109 enum nl80211_band band;
113 band = params->chandef.chan->band;
114 if (band == NL80211_BAND_5GHZ ||
115 band == NL80211_BAND_6GHZ)
116 flag = IEEE80211_RATE_MANDATORY_A;
118 flag = IEEE80211_RATE_MANDATORY_B;
120 sband = rdev->wiphy.bands[band];
121 for (j = 0; j < sband->n_bitrates; j++) {
122 if (sband->bitrates[j].flags & flag)
123 params->basic_rates |= BIT(j);
127 if (WARN_ON(connkeys && connkeys->def < 0))
130 if (WARN_ON(wdev->connect_keys))
131 kfree_sensitive(wdev->connect_keys);
132 wdev->connect_keys = connkeys;
134 wdev->chandef = params->chandef;
136 params->wep_keys = connkeys->params;
137 params->wep_tx_key = connkeys->def;
140 #ifdef CONFIG_CFG80211_WEXT
141 wdev->wext.ibss.chandef = params->chandef;
143 err = rdev_join_ibss(rdev, dev, params);
145 wdev->connect_keys = NULL;
149 memcpy(wdev->ssid, params->ssid, params->ssid_len);
150 wdev->ssid_len = params->ssid_len;
155 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
157 struct wireless_dev *wdev = dev->ieee80211_ptr;
158 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
161 ASSERT_WDEV_LOCK(wdev);
163 kfree_sensitive(wdev->connect_keys);
164 wdev->connect_keys = NULL;
166 rdev_set_qos_map(rdev, dev, NULL);
169 * Delete all the keys ... pairwise keys can't really
170 * exist any more anyway, but default keys might.
172 if (rdev->ops->del_key)
173 for (i = 0; i < 6; i++)
174 rdev_del_key(rdev, dev, i, false, NULL);
176 if (wdev->current_bss) {
177 cfg80211_unhold_bss(wdev->current_bss);
178 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
181 wdev->current_bss = NULL;
183 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
184 #ifdef CONFIG_CFG80211_WEXT
186 wdev->wext.ibss.ssid_len = 0;
188 cfg80211_sched_dfs_chan_update(rdev);
191 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
193 struct wireless_dev *wdev = dev->ieee80211_ptr;
196 __cfg80211_clear_ibss(dev, nowext);
200 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
201 struct net_device *dev, bool nowext)
203 struct wireless_dev *wdev = dev->ieee80211_ptr;
206 ASSERT_WDEV_LOCK(wdev);
211 err = rdev_leave_ibss(rdev, dev);
216 wdev->conn_owner_nlportid = 0;
217 __cfg80211_clear_ibss(dev, nowext);
222 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
223 struct net_device *dev, bool nowext)
225 struct wireless_dev *wdev = dev->ieee80211_ptr;
229 err = __cfg80211_leave_ibss(rdev, dev, nowext);
235 #ifdef CONFIG_CFG80211_WEXT
236 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
237 struct wireless_dev *wdev)
239 struct cfg80211_cached_keys *ck = NULL;
240 enum nl80211_band band;
243 ASSERT_WDEV_LOCK(wdev);
245 if (!wdev->wext.ibss.beacon_interval)
246 wdev->wext.ibss.beacon_interval = 100;
248 /* try to find an IBSS channel if none requested ... */
249 if (!wdev->wext.ibss.chandef.chan) {
250 struct ieee80211_channel *new_chan = NULL;
252 for (band = 0; band < NUM_NL80211_BANDS; band++) {
253 struct ieee80211_supported_band *sband;
254 struct ieee80211_channel *chan;
256 sband = rdev->wiphy.bands[band];
260 for (i = 0; i < sband->n_channels; i++) {
261 chan = &sband->channels[i];
262 if (chan->flags & IEEE80211_CHAN_NO_IR)
264 if (chan->flags & IEEE80211_CHAN_DISABLED)
277 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
281 /* don't join -- SSID is not there */
282 if (!wdev->wext.ibss.ssid_len)
285 if (!netif_running(wdev->netdev))
289 wdev->wext.keys->def = wdev->wext.default_key;
291 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
293 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
294 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
297 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
298 ck->params[i].key = ck->data[i];
300 err = __cfg80211_join_ibss(rdev, wdev->netdev,
301 &wdev->wext.ibss, ck);
308 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
309 struct iw_request_info *info,
310 struct iw_freq *wextfreq, char *extra)
312 struct wireless_dev *wdev = dev->ieee80211_ptr;
313 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
314 struct ieee80211_channel *chan = NULL;
317 /* call only for ibss! */
318 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
321 if (!rdev->ops->join_ibss)
324 freq = cfg80211_wext_freq(wextfreq);
329 chan = ieee80211_get_channel(wdev->wiphy, freq);
332 if (chan->flags & IEEE80211_CHAN_NO_IR ||
333 chan->flags & IEEE80211_CHAN_DISABLED)
337 if (wdev->wext.ibss.chandef.chan == chan)
343 err = __cfg80211_leave_ibss(rdev, dev, true);
350 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
352 wdev->wext.ibss.channel_fixed = true;
354 /* cfg80211_ibss_wext_join will pick one if needed */
355 wdev->wext.ibss.channel_fixed = false;
359 err = cfg80211_ibss_wext_join(rdev, wdev);
365 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
366 struct iw_request_info *info,
367 struct iw_freq *freq, char *extra)
369 struct wireless_dev *wdev = dev->ieee80211_ptr;
370 struct ieee80211_channel *chan = NULL;
372 /* call only for ibss! */
373 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
377 if (wdev->current_bss)
378 chan = wdev->current_bss->pub.channel;
379 else if (wdev->wext.ibss.chandef.chan)
380 chan = wdev->wext.ibss.chandef.chan;
384 freq->m = chan->center_freq;
389 /* no channel if not joining */
393 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
394 struct iw_request_info *info,
395 struct iw_point *data, char *ssid)
397 struct wireless_dev *wdev = dev->ieee80211_ptr;
398 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
399 size_t len = data->length;
402 /* call only for ibss! */
403 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
406 if (!rdev->ops->join_ibss)
412 err = __cfg80211_leave_ibss(rdev, dev, true);
418 /* iwconfig uses nul termination in SSID.. */
419 if (len > 0 && ssid[len - 1] == '\0')
422 memcpy(wdev->ssid, ssid, len);
423 wdev->wext.ibss.ssid = wdev->ssid;
424 wdev->wext.ibss.ssid_len = len;
427 err = cfg80211_ibss_wext_join(rdev, wdev);
433 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
434 struct iw_request_info *info,
435 struct iw_point *data, char *ssid)
437 struct wireless_dev *wdev = dev->ieee80211_ptr;
439 /* call only for ibss! */
440 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
446 if (wdev->ssid_len) {
448 data->length = wdev->ssid_len;
449 memcpy(ssid, wdev->ssid, data->length);
450 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
452 data->length = wdev->wext.ibss.ssid_len;
453 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
460 int cfg80211_ibss_wext_siwap(struct net_device *dev,
461 struct iw_request_info *info,
462 struct sockaddr *ap_addr, char *extra)
464 struct wireless_dev *wdev = dev->ieee80211_ptr;
465 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
466 u8 *bssid = ap_addr->sa_data;
469 /* call only for ibss! */
470 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
473 if (!rdev->ops->join_ibss)
476 if (ap_addr->sa_family != ARPHRD_ETHER)
480 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
483 if (bssid && !is_valid_ether_addr(bssid))
487 if (!bssid && !wdev->wext.ibss.bssid)
490 /* fixed already - and no change */
491 if (wdev->wext.ibss.bssid && bssid &&
492 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
498 err = __cfg80211_leave_ibss(rdev, dev, true);
505 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
506 wdev->wext.ibss.bssid = wdev->wext.bssid;
508 wdev->wext.ibss.bssid = NULL;
511 err = cfg80211_ibss_wext_join(rdev, wdev);
517 int cfg80211_ibss_wext_giwap(struct net_device *dev,
518 struct iw_request_info *info,
519 struct sockaddr *ap_addr, char *extra)
521 struct wireless_dev *wdev = dev->ieee80211_ptr;
523 /* call only for ibss! */
524 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
527 ap_addr->sa_family = ARPHRD_ETHER;
530 if (wdev->current_bss)
531 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
532 else if (wdev->wext.ibss.bssid)
533 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
535 eth_zero_addr(ap_addr->sa_data);