1 // SPDX-License-Identifier: GPL-2.0
3 * Some IBSS support code for cfg80211.
8 #include <linux/etherdevice.h>
9 #include <linux/if_arp.h>
10 #include <linux/slab.h>
11 #include <linux/export.h>
12 #include <net/cfg80211.h>
13 #include "wext-compat.h"
18 void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
19 struct ieee80211_channel *channel)
21 struct wireless_dev *wdev = dev->ieee80211_ptr;
22 struct cfg80211_bss *bss;
23 #ifdef CONFIG_CFG80211_WEXT
24 union iwreq_data wrqu;
27 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
33 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
34 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
39 if (wdev->current_bss) {
40 cfg80211_unhold_bss(wdev->current_bss);
41 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
44 cfg80211_hold_bss(bss_from_pub(bss));
45 wdev->current_bss = bss_from_pub(bss);
47 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
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;
96 ASSERT_WDEV_LOCK(wdev);
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 rdev->wiphy.bands[params->chandef.chan->band];
110 u32 flag = params->chandef.chan->band == NL80211_BAND_5GHZ ?
111 IEEE80211_RATE_MANDATORY_A :
112 IEEE80211_RATE_MANDATORY_B;
114 for (j = 0; j < sband->n_bitrates; j++) {
115 if (sband->bitrates[j].flags & flag)
116 params->basic_rates |= BIT(j);
120 if (WARN_ON(connkeys && connkeys->def < 0))
123 if (WARN_ON(wdev->connect_keys))
124 kzfree(wdev->connect_keys);
125 wdev->connect_keys = connkeys;
127 wdev->ibss_fixed = params->channel_fixed;
128 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
129 wdev->chandef = params->chandef;
131 params->wep_keys = connkeys->params;
132 params->wep_tx_key = connkeys->def;
135 #ifdef CONFIG_CFG80211_WEXT
136 wdev->wext.ibss.chandef = params->chandef;
138 err = rdev_join_ibss(rdev, dev, params);
140 wdev->connect_keys = NULL;
144 memcpy(wdev->ssid, params->ssid, params->ssid_len);
145 wdev->ssid_len = params->ssid_len;
150 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
152 struct wireless_dev *wdev = dev->ieee80211_ptr;
153 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
156 ASSERT_WDEV_LOCK(wdev);
158 kzfree(wdev->connect_keys);
159 wdev->connect_keys = NULL;
161 rdev_set_qos_map(rdev, dev, NULL);
164 * Delete all the keys ... pairwise keys can't really
165 * exist any more anyway, but default keys might.
167 if (rdev->ops->del_key)
168 for (i = 0; i < 6; i++)
169 rdev_del_key(rdev, dev, i, false, NULL);
171 if (wdev->current_bss) {
172 cfg80211_unhold_bss(wdev->current_bss);
173 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
176 wdev->current_bss = NULL;
178 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
179 #ifdef CONFIG_CFG80211_WEXT
181 wdev->wext.ibss.ssid_len = 0;
183 cfg80211_sched_dfs_chan_update(rdev);
186 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
188 struct wireless_dev *wdev = dev->ieee80211_ptr;
191 __cfg80211_clear_ibss(dev, nowext);
195 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
196 struct net_device *dev, bool nowext)
198 struct wireless_dev *wdev = dev->ieee80211_ptr;
201 ASSERT_WDEV_LOCK(wdev);
206 err = rdev_leave_ibss(rdev, dev);
211 wdev->conn_owner_nlportid = 0;
212 __cfg80211_clear_ibss(dev, nowext);
217 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
218 struct net_device *dev, bool nowext)
220 struct wireless_dev *wdev = dev->ieee80211_ptr;
224 err = __cfg80211_leave_ibss(rdev, dev, nowext);
230 #ifdef CONFIG_CFG80211_WEXT
231 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
232 struct wireless_dev *wdev)
234 struct cfg80211_cached_keys *ck = NULL;
235 enum nl80211_band band;
238 ASSERT_WDEV_LOCK(wdev);
240 if (!wdev->wext.ibss.beacon_interval)
241 wdev->wext.ibss.beacon_interval = 100;
243 /* try to find an IBSS channel if none requested ... */
244 if (!wdev->wext.ibss.chandef.chan) {
245 struct ieee80211_channel *new_chan = NULL;
247 for (band = 0; band < NUM_NL80211_BANDS; band++) {
248 struct ieee80211_supported_band *sband;
249 struct ieee80211_channel *chan;
251 sband = rdev->wiphy.bands[band];
255 for (i = 0; i < sband->n_channels; i++) {
256 chan = &sband->channels[i];
257 if (chan->flags & IEEE80211_CHAN_NO_IR)
259 if (chan->flags & IEEE80211_CHAN_DISABLED)
272 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
276 /* don't join -- SSID is not there */
277 if (!wdev->wext.ibss.ssid_len)
280 if (!netif_running(wdev->netdev))
284 wdev->wext.keys->def = wdev->wext.default_key;
286 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
288 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
289 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
292 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
293 ck->params[i].key = ck->data[i];
295 err = __cfg80211_join_ibss(rdev, wdev->netdev,
296 &wdev->wext.ibss, ck);
303 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
304 struct iw_request_info *info,
305 struct iw_freq *wextfreq, char *extra)
307 struct wireless_dev *wdev = dev->ieee80211_ptr;
308 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
309 struct ieee80211_channel *chan = NULL;
312 /* call only for ibss! */
313 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
316 if (!rdev->ops->join_ibss)
319 freq = cfg80211_wext_freq(wextfreq);
324 chan = ieee80211_get_channel(wdev->wiphy, freq);
327 if (chan->flags & IEEE80211_CHAN_NO_IR ||
328 chan->flags & IEEE80211_CHAN_DISABLED)
332 if (wdev->wext.ibss.chandef.chan == chan)
338 err = __cfg80211_leave_ibss(rdev, dev, true);
345 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
347 wdev->wext.ibss.channel_fixed = true;
349 /* cfg80211_ibss_wext_join will pick one if needed */
350 wdev->wext.ibss.channel_fixed = false;
354 err = cfg80211_ibss_wext_join(rdev, wdev);
360 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
361 struct iw_request_info *info,
362 struct iw_freq *freq, char *extra)
364 struct wireless_dev *wdev = dev->ieee80211_ptr;
365 struct ieee80211_channel *chan = NULL;
367 /* call only for ibss! */
368 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
372 if (wdev->current_bss)
373 chan = wdev->current_bss->pub.channel;
374 else if (wdev->wext.ibss.chandef.chan)
375 chan = wdev->wext.ibss.chandef.chan;
379 freq->m = chan->center_freq;
384 /* no channel if not joining */
388 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
389 struct iw_request_info *info,
390 struct iw_point *data, char *ssid)
392 struct wireless_dev *wdev = dev->ieee80211_ptr;
393 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
394 size_t len = data->length;
397 /* call only for ibss! */
398 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
401 if (!rdev->ops->join_ibss)
407 err = __cfg80211_leave_ibss(rdev, dev, true);
413 /* iwconfig uses nul termination in SSID.. */
414 if (len > 0 && ssid[len - 1] == '\0')
417 memcpy(wdev->ssid, ssid, len);
418 wdev->wext.ibss.ssid = wdev->ssid;
419 wdev->wext.ibss.ssid_len = len;
422 err = cfg80211_ibss_wext_join(rdev, wdev);
428 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
429 struct iw_request_info *info,
430 struct iw_point *data, char *ssid)
432 struct wireless_dev *wdev = dev->ieee80211_ptr;
434 /* call only for ibss! */
435 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
441 if (wdev->ssid_len) {
443 data->length = wdev->ssid_len;
444 memcpy(ssid, wdev->ssid, data->length);
445 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
447 data->length = wdev->wext.ibss.ssid_len;
448 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
455 int cfg80211_ibss_wext_siwap(struct net_device *dev,
456 struct iw_request_info *info,
457 struct sockaddr *ap_addr, char *extra)
459 struct wireless_dev *wdev = dev->ieee80211_ptr;
460 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
461 u8 *bssid = ap_addr->sa_data;
464 /* call only for ibss! */
465 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
468 if (!rdev->ops->join_ibss)
471 if (ap_addr->sa_family != ARPHRD_ETHER)
475 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
478 if (bssid && !is_valid_ether_addr(bssid))
482 if (!bssid && !wdev->wext.ibss.bssid)
485 /* fixed already - and no change */
486 if (wdev->wext.ibss.bssid && bssid &&
487 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
493 err = __cfg80211_leave_ibss(rdev, dev, true);
500 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
501 wdev->wext.ibss.bssid = wdev->wext.bssid;
503 wdev->wext.ibss.bssid = NULL;
506 err = cfg80211_ibss_wext_join(rdev, wdev);
512 int cfg80211_ibss_wext_giwap(struct net_device *dev,
513 struct iw_request_info *info,
514 struct sockaddr *ap_addr, char *extra)
516 struct wireless_dev *wdev = dev->ieee80211_ptr;
518 /* call only for ibss! */
519 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
522 ap_addr->sa_family = ARPHRD_ETHER;
525 if (wdev->current_bss)
526 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
527 else if (wdev->wext.ibss.bssid)
528 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
530 eth_zero_addr(ap_addr->sa_data);