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-2023 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);
97 if (wdev->u.ibss.ssid_len)
100 if (!params->basic_rates) {
102 * If no rates were explicitly configured,
103 * use the mandatory rate set for 11b or
104 * 11a for maximum compatibility.
106 struct ieee80211_supported_band *sband;
107 enum nl80211_band band;
111 band = params->chandef.chan->band;
112 if (band == NL80211_BAND_5GHZ ||
113 band == NL80211_BAND_6GHZ)
114 flag = IEEE80211_RATE_MANDATORY_A;
116 flag = IEEE80211_RATE_MANDATORY_B;
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);
125 if (WARN_ON(connkeys && connkeys->def < 0))
128 if (WARN_ON(wdev->connect_keys))
129 kfree_sensitive(wdev->connect_keys);
130 wdev->connect_keys = connkeys;
132 wdev->u.ibss.chandef = params->chandef;
134 params->wep_keys = connkeys->params;
135 params->wep_tx_key = connkeys->def;
138 #ifdef CONFIG_CFG80211_WEXT
139 wdev->wext.ibss.chandef = params->chandef;
141 err = rdev_join_ibss(rdev, dev, params);
143 wdev->connect_keys = NULL;
147 memcpy(wdev->u.ibss.ssid, params->ssid, params->ssid_len);
148 wdev->u.ibss.ssid_len = params->ssid_len;
153 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
155 struct wireless_dev *wdev = dev->ieee80211_ptr;
156 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
159 lockdep_assert_wiphy(wdev->wiphy);
161 kfree_sensitive(wdev->connect_keys);
162 wdev->connect_keys = NULL;
164 rdev_set_qos_map(rdev, dev, NULL);
167 * Delete all the keys ... pairwise keys can't really
168 * exist any more anyway, but default keys might.
170 if (rdev->ops->del_key)
171 for (i = 0; i < 6; i++)
172 rdev_del_key(rdev, dev, -1, i, false, NULL);
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);
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
184 wdev->wext.ibss.ssid_len = 0;
186 cfg80211_sched_dfs_chan_update(rdev);
189 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
190 struct net_device *dev, bool nowext)
192 struct wireless_dev *wdev = dev->ieee80211_ptr;
195 lockdep_assert_wiphy(wdev->wiphy);
197 if (!wdev->u.ibss.ssid_len)
200 err = rdev_leave_ibss(rdev, dev);
205 wdev->conn_owner_nlportid = 0;
206 cfg80211_clear_ibss(dev, nowext);
211 #ifdef CONFIG_CFG80211_WEXT
212 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
213 struct wireless_dev *wdev)
215 struct cfg80211_cached_keys *ck = NULL;
216 enum nl80211_band band;
219 lockdep_assert_wiphy(wdev->wiphy);
221 if (!wdev->wext.ibss.beacon_interval)
222 wdev->wext.ibss.beacon_interval = 100;
224 /* try to find an IBSS channel if none requested ... */
225 if (!wdev->wext.ibss.chandef.chan) {
226 struct ieee80211_channel *new_chan = NULL;
228 for (band = 0; band < NUM_NL80211_BANDS; band++) {
229 struct ieee80211_supported_band *sband;
230 struct ieee80211_channel *chan;
232 sband = rdev->wiphy.bands[band];
236 for (i = 0; i < sband->n_channels; i++) {
237 chan = &sband->channels[i];
238 if (chan->flags & IEEE80211_CHAN_NO_IR)
240 if (chan->flags & IEEE80211_CHAN_DISABLED)
253 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
257 /* don't join -- SSID is not there */
258 if (!wdev->wext.ibss.ssid_len)
261 if (!netif_running(wdev->netdev))
265 wdev->wext.keys->def = wdev->wext.default_key;
267 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
269 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
270 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
273 for (i = 0; i < 4; i++)
274 ck->params[i].key = ck->data[i];
276 err = __cfg80211_join_ibss(rdev, wdev->netdev,
277 &wdev->wext.ibss, ck);
284 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
285 struct iw_request_info *info,
286 struct iw_freq *wextfreq, char *extra)
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;
293 /* call only for ibss! */
294 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
297 if (!rdev->ops->join_ibss)
300 freq = cfg80211_wext_freq(wextfreq);
305 chan = ieee80211_get_channel(wdev->wiphy, freq);
308 if (chan->flags & IEEE80211_CHAN_NO_IR ||
309 chan->flags & IEEE80211_CHAN_DISABLED)
313 if (wdev->wext.ibss.chandef.chan == chan)
317 if (wdev->u.ibss.ssid_len)
318 err = cfg80211_leave_ibss(rdev, dev, true);
324 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
326 wdev->wext.ibss.channel_fixed = true;
328 /* cfg80211_ibss_wext_join will pick one if needed */
329 wdev->wext.ibss.channel_fixed = false;
332 return cfg80211_ibss_wext_join(rdev, wdev);
335 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
336 struct iw_request_info *info,
337 struct iw_freq *freq, char *extra)
339 struct wireless_dev *wdev = dev->ieee80211_ptr;
340 struct ieee80211_channel *chan = NULL;
342 /* call only for ibss! */
343 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
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;
352 freq->m = chan->center_freq;
357 /* no channel if not joining */
361 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
362 struct iw_request_info *info,
363 struct iw_point *data, char *ssid)
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;
370 /* call only for ibss! */
371 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
374 if (!rdev->ops->join_ibss)
378 if (wdev->u.ibss.ssid_len)
379 err = cfg80211_leave_ibss(rdev, dev, true);
384 /* iwconfig uses nul termination in SSID.. */
385 if (len > 0 && ssid[len - 1] == '\0')
388 memcpy(wdev->u.ibss.ssid, ssid, len);
389 wdev->wext.ibss.ssid = wdev->u.ibss.ssid;
390 wdev->wext.ibss.ssid_len = len;
392 return cfg80211_ibss_wext_join(rdev, wdev);
395 int cfg80211_ibss_wext_giwessid(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;
401 /* call only for ibss! */
402 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
407 if (wdev->u.ibss.ssid_len) {
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) {
413 data->length = wdev->wext.ibss.ssid_len;
414 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
420 int cfg80211_ibss_wext_siwap(struct net_device *dev,
421 struct iw_request_info *info,
422 struct sockaddr *ap_addr, char *extra)
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;
429 /* call only for ibss! */
430 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
433 if (!rdev->ops->join_ibss)
436 if (ap_addr->sa_family != ARPHRD_ETHER)
440 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
443 if (bssid && !is_valid_ether_addr(bssid))
447 if (!bssid && !wdev->wext.ibss.bssid)
450 /* fixed already - and no change */
451 if (wdev->wext.ibss.bssid && bssid &&
452 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
456 if (wdev->u.ibss.ssid_len)
457 err = cfg80211_leave_ibss(rdev, dev, true);
463 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
464 wdev->wext.ibss.bssid = wdev->wext.bssid;
466 wdev->wext.ibss.bssid = NULL;
468 return cfg80211_ibss_wext_join(rdev, wdev);
471 int cfg80211_ibss_wext_giwap(struct net_device *dev,
472 struct iw_request_info *info,
473 struct sockaddr *ap_addr, char *extra)
475 struct wireless_dev *wdev = dev->ieee80211_ptr;
477 /* call only for ibss! */
478 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
481 ap_addr->sa_family = ARPHRD_ETHER;
483 if (wdev->u.ibss.current_bss)
484 memcpy(ap_addr->sa_data, wdev->u.ibss.current_bss->pub.bssid,
486 else if (wdev->wext.ibss.bssid)
487 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
489 eth_zero_addr(ap_addr->sa_data);