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-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))
31 if (!wdev->u.ibss.ssid_len)
34 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
35 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
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);
45 cfg80211_hold_bss(bss_from_pub(bss));
46 wdev->u.ibss.current_bss = bss_from_pub(bss);
48 cfg80211_upload_connect_keys(wdev);
50 nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid,
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);
59 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
60 struct ieee80211_channel *channel, gfp_t gfp)
62 struct wireless_dev *wdev = dev->ieee80211_ptr;
63 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
64 struct cfg80211_event *ev;
67 trace_cfg80211_ibss_joined(dev, bssid, channel);
69 if (WARN_ON(!channel))
72 ev = kzalloc(sizeof(*ev), gfp);
76 ev->type = EVENT_IBSS_JOINED;
77 memcpy(ev->ij.bssid, bssid, ETH_ALEN);
78 ev->ij.channel = channel;
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);
85 EXPORT_SYMBOL(cfg80211_ibss_joined);
87 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)
92 struct wireless_dev *wdev = dev->ieee80211_ptr;
95 lockdep_assert_held(&rdev->wiphy.mtx);
96 ASSERT_WDEV_LOCK(wdev);
98 if (wdev->u.ibss.ssid_len)
101 if (!params->basic_rates) {
103 * If no rates were explicitly configured,
104 * use the mandatory rate set for 11b or
105 * 11a for maximum compatibility.
107 struct ieee80211_supported_band *sband;
108 enum nl80211_band band;
112 band = params->chandef.chan->band;
113 if (band == NL80211_BAND_5GHZ ||
114 band == NL80211_BAND_6GHZ)
115 flag = IEEE80211_RATE_MANDATORY_A;
117 flag = IEEE80211_RATE_MANDATORY_B;
119 sband = rdev->wiphy.bands[band];
120 for (j = 0; j < sband->n_bitrates; j++) {
121 if (sband->bitrates[j].flags & flag)
122 params->basic_rates |= BIT(j);
126 if (WARN_ON(connkeys && connkeys->def < 0))
129 if (WARN_ON(wdev->connect_keys))
130 kfree_sensitive(wdev->connect_keys);
131 wdev->connect_keys = connkeys;
133 wdev->u.ibss.chandef = params->chandef;
135 params->wep_keys = connkeys->params;
136 params->wep_tx_key = connkeys->def;
139 #ifdef CONFIG_CFG80211_WEXT
140 wdev->wext.ibss.chandef = params->chandef;
142 err = rdev_join_ibss(rdev, dev, params);
144 wdev->connect_keys = NULL;
148 memcpy(wdev->u.ibss.ssid, params->ssid, params->ssid_len);
149 wdev->u.ibss.ssid_len = params->ssid_len;
154 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
156 struct wireless_dev *wdev = dev->ieee80211_ptr;
157 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
160 ASSERT_WDEV_LOCK(wdev);
162 kfree_sensitive(wdev->connect_keys);
163 wdev->connect_keys = NULL;
165 rdev_set_qos_map(rdev, dev, NULL);
168 * Delete all the keys ... pairwise keys can't really
169 * exist any more anyway, but default keys might.
171 if (rdev->ops->del_key)
172 for (i = 0; i < 6; i++)
173 rdev_del_key(rdev, dev, -1, i, false, NULL);
175 if (wdev->u.ibss.current_bss) {
176 cfg80211_unhold_bss(wdev->u.ibss.current_bss);
177 cfg80211_put_bss(wdev->wiphy, &wdev->u.ibss.current_bss->pub);
180 wdev->u.ibss.current_bss = NULL;
181 wdev->u.ibss.ssid_len = 0;
182 memset(&wdev->u.ibss.chandef, 0, sizeof(wdev->u.ibss.chandef));
183 #ifdef CONFIG_CFG80211_WEXT
185 wdev->wext.ibss.ssid_len = 0;
187 cfg80211_sched_dfs_chan_update(rdev);
190 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
192 struct wireless_dev *wdev = dev->ieee80211_ptr;
195 __cfg80211_clear_ibss(dev, nowext);
199 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
200 struct net_device *dev, bool nowext)
202 struct wireless_dev *wdev = dev->ieee80211_ptr;
205 ASSERT_WDEV_LOCK(wdev);
207 if (!wdev->u.ibss.ssid_len)
210 err = rdev_leave_ibss(rdev, dev);
215 wdev->conn_owner_nlportid = 0;
216 __cfg80211_clear_ibss(dev, nowext);
221 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
222 struct net_device *dev, bool nowext)
224 struct wireless_dev *wdev = dev->ieee80211_ptr;
228 err = __cfg80211_leave_ibss(rdev, dev, nowext);
234 #ifdef CONFIG_CFG80211_WEXT
235 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
236 struct wireless_dev *wdev)
238 struct cfg80211_cached_keys *ck = NULL;
239 enum nl80211_band band;
242 ASSERT_WDEV_LOCK(wdev);
244 if (!wdev->wext.ibss.beacon_interval)
245 wdev->wext.ibss.beacon_interval = 100;
247 /* try to find an IBSS channel if none requested ... */
248 if (!wdev->wext.ibss.chandef.chan) {
249 struct ieee80211_channel *new_chan = NULL;
251 for (band = 0; band < NUM_NL80211_BANDS; band++) {
252 struct ieee80211_supported_band *sband;
253 struct ieee80211_channel *chan;
255 sband = rdev->wiphy.bands[band];
259 for (i = 0; i < sband->n_channels; i++) {
260 chan = &sband->channels[i];
261 if (chan->flags & IEEE80211_CHAN_NO_IR)
263 if (chan->flags & IEEE80211_CHAN_DISABLED)
276 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
280 /* don't join -- SSID is not there */
281 if (!wdev->wext.ibss.ssid_len)
284 if (!netif_running(wdev->netdev))
288 wdev->wext.keys->def = wdev->wext.default_key;
290 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
292 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
293 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
296 for (i = 0; i < 4; i++)
297 ck->params[i].key = ck->data[i];
299 err = __cfg80211_join_ibss(rdev, wdev->netdev,
300 &wdev->wext.ibss, ck);
307 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
308 struct iw_request_info *info,
309 struct iw_freq *wextfreq, char *extra)
311 struct wireless_dev *wdev = dev->ieee80211_ptr;
312 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
313 struct ieee80211_channel *chan = NULL;
316 /* call only for ibss! */
317 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
320 if (!rdev->ops->join_ibss)
323 freq = cfg80211_wext_freq(wextfreq);
328 chan = ieee80211_get_channel(wdev->wiphy, freq);
331 if (chan->flags & IEEE80211_CHAN_NO_IR ||
332 chan->flags & IEEE80211_CHAN_DISABLED)
336 if (wdev->wext.ibss.chandef.chan == chan)
341 if (wdev->u.ibss.ssid_len)
342 err = __cfg80211_leave_ibss(rdev, dev, true);
349 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
351 wdev->wext.ibss.channel_fixed = true;
353 /* cfg80211_ibss_wext_join will pick one if needed */
354 wdev->wext.ibss.channel_fixed = false;
358 err = cfg80211_ibss_wext_join(rdev, wdev);
364 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
365 struct iw_request_info *info,
366 struct iw_freq *freq, char *extra)
368 struct wireless_dev *wdev = dev->ieee80211_ptr;
369 struct ieee80211_channel *chan = NULL;
371 /* call only for ibss! */
372 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
376 if (wdev->u.ibss.current_bss)
377 chan = wdev->u.ibss.current_bss->pub.channel;
378 else if (wdev->wext.ibss.chandef.chan)
379 chan = wdev->wext.ibss.chandef.chan;
383 freq->m = chan->center_freq;
388 /* no channel if not joining */
392 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
393 struct iw_request_info *info,
394 struct iw_point *data, char *ssid)
396 struct wireless_dev *wdev = dev->ieee80211_ptr;
397 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
398 size_t len = data->length;
401 /* call only for ibss! */
402 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
405 if (!rdev->ops->join_ibss)
410 if (wdev->u.ibss.ssid_len)
411 err = __cfg80211_leave_ibss(rdev, dev, true);
417 /* iwconfig uses nul termination in SSID.. */
418 if (len > 0 && ssid[len - 1] == '\0')
421 memcpy(wdev->u.ibss.ssid, ssid, len);
422 wdev->wext.ibss.ssid = wdev->u.ibss.ssid;
423 wdev->wext.ibss.ssid_len = len;
426 err = cfg80211_ibss_wext_join(rdev, wdev);
432 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
433 struct iw_request_info *info,
434 struct iw_point *data, char *ssid)
436 struct wireless_dev *wdev = dev->ieee80211_ptr;
438 /* call only for ibss! */
439 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
445 if (wdev->u.ibss.ssid_len) {
447 data->length = wdev->u.ibss.ssid_len;
448 memcpy(ssid, wdev->u.ibss.ssid, data->length);
449 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
451 data->length = wdev->wext.ibss.ssid_len;
452 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
459 int cfg80211_ibss_wext_siwap(struct net_device *dev,
460 struct iw_request_info *info,
461 struct sockaddr *ap_addr, char *extra)
463 struct wireless_dev *wdev = dev->ieee80211_ptr;
464 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
465 u8 *bssid = ap_addr->sa_data;
468 /* call only for ibss! */
469 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
472 if (!rdev->ops->join_ibss)
475 if (ap_addr->sa_family != ARPHRD_ETHER)
479 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
482 if (bssid && !is_valid_ether_addr(bssid))
486 if (!bssid && !wdev->wext.ibss.bssid)
489 /* fixed already - and no change */
490 if (wdev->wext.ibss.bssid && bssid &&
491 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
496 if (wdev->u.ibss.ssid_len)
497 err = __cfg80211_leave_ibss(rdev, dev, true);
504 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
505 wdev->wext.ibss.bssid = wdev->wext.bssid;
507 wdev->wext.ibss.bssid = NULL;
510 err = cfg80211_ibss_wext_join(rdev, wdev);
516 int cfg80211_ibss_wext_giwap(struct net_device *dev,
517 struct iw_request_info *info,
518 struct sockaddr *ap_addr, char *extra)
520 struct wireless_dev *wdev = dev->ieee80211_ptr;
522 /* call only for ibss! */
523 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
526 ap_addr->sa_family = ARPHRD_ETHER;
529 if (wdev->u.ibss.current_bss)
530 memcpy(ap_addr->sa_data, wdev->u.ibss.current_bss->pub.bssid,
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);