1 // SPDX-License-Identifier: GPL-2.0
3 * Some IBSS support code for cfg80211.
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
6 * Copyright (C) 2020-2021 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->ibss_fixed = params->channel_fixed;
135 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
136 wdev->chandef = params->chandef;
138 params->wep_keys = connkeys->params;
139 params->wep_tx_key = connkeys->def;
142 #ifdef CONFIG_CFG80211_WEXT
143 wdev->wext.ibss.chandef = params->chandef;
145 err = rdev_join_ibss(rdev, dev, params);
147 wdev->connect_keys = NULL;
151 memcpy(wdev->ssid, params->ssid, params->ssid_len);
152 wdev->ssid_len = params->ssid_len;
157 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
159 struct wireless_dev *wdev = dev->ieee80211_ptr;
160 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
163 ASSERT_WDEV_LOCK(wdev);
165 kfree_sensitive(wdev->connect_keys);
166 wdev->connect_keys = NULL;
168 rdev_set_qos_map(rdev, dev, NULL);
171 * Delete all the keys ... pairwise keys can't really
172 * exist any more anyway, but default keys might.
174 if (rdev->ops->del_key)
175 for (i = 0; i < 6; i++)
176 rdev_del_key(rdev, dev, i, false, NULL);
178 if (wdev->current_bss) {
179 cfg80211_unhold_bss(wdev->current_bss);
180 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
183 wdev->current_bss = NULL;
185 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
186 #ifdef CONFIG_CFG80211_WEXT
188 wdev->wext.ibss.ssid_len = 0;
190 cfg80211_sched_dfs_chan_update(rdev);
193 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
195 struct wireless_dev *wdev = dev->ieee80211_ptr;
198 __cfg80211_clear_ibss(dev, nowext);
202 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
203 struct net_device *dev, bool nowext)
205 struct wireless_dev *wdev = dev->ieee80211_ptr;
208 ASSERT_WDEV_LOCK(wdev);
213 err = rdev_leave_ibss(rdev, dev);
218 wdev->conn_owner_nlportid = 0;
219 __cfg80211_clear_ibss(dev, nowext);
224 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
225 struct net_device *dev, bool nowext)
227 struct wireless_dev *wdev = dev->ieee80211_ptr;
231 err = __cfg80211_leave_ibss(rdev, dev, nowext);
237 #ifdef CONFIG_CFG80211_WEXT
238 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
239 struct wireless_dev *wdev)
241 struct cfg80211_cached_keys *ck = NULL;
242 enum nl80211_band band;
245 ASSERT_WDEV_LOCK(wdev);
247 if (!wdev->wext.ibss.beacon_interval)
248 wdev->wext.ibss.beacon_interval = 100;
250 /* try to find an IBSS channel if none requested ... */
251 if (!wdev->wext.ibss.chandef.chan) {
252 struct ieee80211_channel *new_chan = NULL;
254 for (band = 0; band < NUM_NL80211_BANDS; band++) {
255 struct ieee80211_supported_band *sband;
256 struct ieee80211_channel *chan;
258 sband = rdev->wiphy.bands[band];
262 for (i = 0; i < sband->n_channels; i++) {
263 chan = &sband->channels[i];
264 if (chan->flags & IEEE80211_CHAN_NO_IR)
266 if (chan->flags & IEEE80211_CHAN_DISABLED)
279 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
283 /* don't join -- SSID is not there */
284 if (!wdev->wext.ibss.ssid_len)
287 if (!netif_running(wdev->netdev))
291 wdev->wext.keys->def = wdev->wext.default_key;
293 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
295 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
296 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
299 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
300 ck->params[i].key = ck->data[i];
302 err = __cfg80211_join_ibss(rdev, wdev->netdev,
303 &wdev->wext.ibss, ck);
310 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
311 struct iw_request_info *info,
312 struct iw_freq *wextfreq, char *extra)
314 struct wireless_dev *wdev = dev->ieee80211_ptr;
315 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
316 struct ieee80211_channel *chan = NULL;
319 /* call only for ibss! */
320 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
323 if (!rdev->ops->join_ibss)
326 freq = cfg80211_wext_freq(wextfreq);
331 chan = ieee80211_get_channel(wdev->wiphy, freq);
334 if (chan->flags & IEEE80211_CHAN_NO_IR ||
335 chan->flags & IEEE80211_CHAN_DISABLED)
339 if (wdev->wext.ibss.chandef.chan == chan)
345 err = __cfg80211_leave_ibss(rdev, dev, true);
352 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
354 wdev->wext.ibss.channel_fixed = true;
356 /* cfg80211_ibss_wext_join will pick one if needed */
357 wdev->wext.ibss.channel_fixed = false;
361 err = cfg80211_ibss_wext_join(rdev, wdev);
367 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
368 struct iw_request_info *info,
369 struct iw_freq *freq, char *extra)
371 struct wireless_dev *wdev = dev->ieee80211_ptr;
372 struct ieee80211_channel *chan = NULL;
374 /* call only for ibss! */
375 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
379 if (wdev->current_bss)
380 chan = wdev->current_bss->pub.channel;
381 else if (wdev->wext.ibss.chandef.chan)
382 chan = wdev->wext.ibss.chandef.chan;
386 freq->m = chan->center_freq;
391 /* no channel if not joining */
395 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
396 struct iw_request_info *info,
397 struct iw_point *data, char *ssid)
399 struct wireless_dev *wdev = dev->ieee80211_ptr;
400 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
401 size_t len = data->length;
404 /* call only for ibss! */
405 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
408 if (!rdev->ops->join_ibss)
414 err = __cfg80211_leave_ibss(rdev, dev, true);
420 /* iwconfig uses nul termination in SSID.. */
421 if (len > 0 && ssid[len - 1] == '\0')
424 memcpy(wdev->ssid, ssid, len);
425 wdev->wext.ibss.ssid = wdev->ssid;
426 wdev->wext.ibss.ssid_len = len;
429 err = cfg80211_ibss_wext_join(rdev, wdev);
435 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
436 struct iw_request_info *info,
437 struct iw_point *data, char *ssid)
439 struct wireless_dev *wdev = dev->ieee80211_ptr;
441 /* call only for ibss! */
442 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
448 if (wdev->ssid_len) {
450 data->length = wdev->ssid_len;
451 memcpy(ssid, wdev->ssid, data->length);
452 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
454 data->length = wdev->wext.ibss.ssid_len;
455 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
462 int cfg80211_ibss_wext_siwap(struct net_device *dev,
463 struct iw_request_info *info,
464 struct sockaddr *ap_addr, char *extra)
466 struct wireless_dev *wdev = dev->ieee80211_ptr;
467 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
468 u8 *bssid = ap_addr->sa_data;
471 /* call only for ibss! */
472 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
475 if (!rdev->ops->join_ibss)
478 if (ap_addr->sa_family != ARPHRD_ETHER)
482 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
485 if (bssid && !is_valid_ether_addr(bssid))
489 if (!bssid && !wdev->wext.ibss.bssid)
492 /* fixed already - and no change */
493 if (wdev->wext.ibss.bssid && bssid &&
494 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
500 err = __cfg80211_leave_ibss(rdev, dev, true);
507 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
508 wdev->wext.ibss.bssid = wdev->wext.bssid;
510 wdev->wext.ibss.bssid = NULL;
513 err = cfg80211_ibss_wext_join(rdev, wdev);
519 int cfg80211_ibss_wext_giwap(struct net_device *dev,
520 struct iw_request_info *info,
521 struct sockaddr *ap_addr, char *extra)
523 struct wireless_dev *wdev = dev->ieee80211_ptr;
525 /* call only for ibss! */
526 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
529 ap_addr->sa_family = ARPHRD_ETHER;
532 if (wdev->current_bss)
533 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
534 else if (wdev->wext.ibss.bssid)
535 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
537 eth_zero_addr(ap_addr->sa_data);