1 // SPDX-License-Identifier: GPL-2.0
3 * Some IBSS support code for cfg80211.
5 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
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 static 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 ASSERT_WDEV_LOCK(wdev);
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 rdev->wiphy.bands[params->chandef.chan->band];
109 u32 flag = params->chandef.chan->band == NL80211_BAND_5GHZ ?
110 IEEE80211_RATE_MANDATORY_A :
111 IEEE80211_RATE_MANDATORY_B;
113 for (j = 0; j < sband->n_bitrates; j++) {
114 if (sband->bitrates[j].flags & flag)
115 params->basic_rates |= BIT(j);
119 if (WARN_ON(connkeys && connkeys->def < 0))
122 if (WARN_ON(wdev->connect_keys))
123 kzfree(wdev->connect_keys);
124 wdev->connect_keys = connkeys;
126 wdev->ibss_fixed = params->channel_fixed;
127 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
128 wdev->chandef = params->chandef;
129 #ifdef CONFIG_CFG80211_WEXT
130 wdev->wext.ibss.chandef = params->chandef;
132 err = rdev_join_ibss(rdev, dev, params);
134 wdev->connect_keys = NULL;
138 memcpy(wdev->ssid, params->ssid, params->ssid_len);
139 wdev->ssid_len = params->ssid_len;
144 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
145 struct net_device *dev,
146 struct cfg80211_ibss_params *params,
147 struct cfg80211_cached_keys *connkeys)
149 struct wireless_dev *wdev = dev->ieee80211_ptr;
155 err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
161 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
163 struct wireless_dev *wdev = dev->ieee80211_ptr;
164 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
167 ASSERT_WDEV_LOCK(wdev);
169 kzfree(wdev->connect_keys);
170 wdev->connect_keys = NULL;
172 rdev_set_qos_map(rdev, dev, NULL);
175 * Delete all the keys ... pairwise keys can't really
176 * exist any more anyway, but default keys might.
178 if (rdev->ops->del_key)
179 for (i = 0; i < 6; i++)
180 rdev_del_key(rdev, dev, i, false, NULL);
182 if (wdev->current_bss) {
183 cfg80211_unhold_bss(wdev->current_bss);
184 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
187 wdev->current_bss = NULL;
189 memset(&wdev->chandef, 0, sizeof(wdev->chandef));
190 #ifdef CONFIG_CFG80211_WEXT
192 wdev->wext.ibss.ssid_len = 0;
194 cfg80211_sched_dfs_chan_update(rdev);
197 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
199 struct wireless_dev *wdev = dev->ieee80211_ptr;
202 __cfg80211_clear_ibss(dev, nowext);
206 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
207 struct net_device *dev, bool nowext)
209 struct wireless_dev *wdev = dev->ieee80211_ptr;
212 ASSERT_WDEV_LOCK(wdev);
217 err = rdev_leave_ibss(rdev, dev);
222 __cfg80211_clear_ibss(dev, nowext);
227 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
228 struct net_device *dev, bool nowext)
230 struct wireless_dev *wdev = dev->ieee80211_ptr;
234 err = __cfg80211_leave_ibss(rdev, dev, nowext);
240 #ifdef CONFIG_CFG80211_WEXT
241 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
242 struct wireless_dev *wdev)
244 struct cfg80211_cached_keys *ck = NULL;
245 enum nl80211_band band;
248 ASSERT_WDEV_LOCK(wdev);
250 if (!wdev->wext.ibss.beacon_interval)
251 wdev->wext.ibss.beacon_interval = 100;
253 /* try to find an IBSS channel if none requested ... */
254 if (!wdev->wext.ibss.chandef.chan) {
255 struct ieee80211_channel *new_chan = NULL;
257 for (band = 0; band < NUM_NL80211_BANDS; band++) {
258 struct ieee80211_supported_band *sband;
259 struct ieee80211_channel *chan;
261 sband = rdev->wiphy.bands[band];
265 for (i = 0; i < sband->n_channels; i++) {
266 chan = &sband->channels[i];
267 if (chan->flags & IEEE80211_CHAN_NO_IR)
269 if (chan->flags & IEEE80211_CHAN_DISABLED)
282 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
286 /* don't join -- SSID is not there */
287 if (!wdev->wext.ibss.ssid_len)
290 if (!netif_running(wdev->netdev))
294 wdev->wext.keys->def = wdev->wext.default_key;
296 wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
298 if (wdev->wext.keys && wdev->wext.keys->def != -1) {
299 ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
302 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
303 ck->params[i].key = ck->data[i];
305 err = __cfg80211_join_ibss(rdev, wdev->netdev,
306 &wdev->wext.ibss, ck);
313 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
314 struct iw_request_info *info,
315 struct iw_freq *wextfreq, char *extra)
317 struct wireless_dev *wdev = dev->ieee80211_ptr;
318 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
319 struct ieee80211_channel *chan = NULL;
322 /* call only for ibss! */
323 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
326 if (!rdev->ops->join_ibss)
329 freq = cfg80211_wext_freq(wextfreq);
334 chan = ieee80211_get_channel(wdev->wiphy, freq);
337 if (chan->flags & IEEE80211_CHAN_NO_IR ||
338 chan->flags & IEEE80211_CHAN_DISABLED)
342 if (wdev->wext.ibss.chandef.chan == chan)
348 err = __cfg80211_leave_ibss(rdev, dev, true);
355 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
357 wdev->wext.ibss.channel_fixed = true;
359 /* cfg80211_ibss_wext_join will pick one if needed */
360 wdev->wext.ibss.channel_fixed = false;
364 err = cfg80211_ibss_wext_join(rdev, wdev);
370 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
371 struct iw_request_info *info,
372 struct iw_freq *freq, char *extra)
374 struct wireless_dev *wdev = dev->ieee80211_ptr;
375 struct ieee80211_channel *chan = NULL;
377 /* call only for ibss! */
378 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
382 if (wdev->current_bss)
383 chan = wdev->current_bss->pub.channel;
384 else if (wdev->wext.ibss.chandef.chan)
385 chan = wdev->wext.ibss.chandef.chan;
389 freq->m = chan->center_freq;
394 /* no channel if not joining */
398 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
399 struct iw_request_info *info,
400 struct iw_point *data, char *ssid)
402 struct wireless_dev *wdev = dev->ieee80211_ptr;
403 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
404 size_t len = data->length;
407 /* call only for ibss! */
408 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
411 if (!rdev->ops->join_ibss)
417 err = __cfg80211_leave_ibss(rdev, dev, true);
423 /* iwconfig uses nul termination in SSID.. */
424 if (len > 0 && ssid[len - 1] == '\0')
427 memcpy(wdev->ssid, ssid, len);
428 wdev->wext.ibss.ssid = wdev->ssid;
429 wdev->wext.ibss.ssid_len = len;
432 err = cfg80211_ibss_wext_join(rdev, wdev);
438 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
439 struct iw_request_info *info,
440 struct iw_point *data, char *ssid)
442 struct wireless_dev *wdev = dev->ieee80211_ptr;
444 /* call only for ibss! */
445 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
451 if (wdev->ssid_len) {
453 data->length = wdev->ssid_len;
454 memcpy(ssid, wdev->ssid, data->length);
455 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
457 data->length = wdev->wext.ibss.ssid_len;
458 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
465 int cfg80211_ibss_wext_siwap(struct net_device *dev,
466 struct iw_request_info *info,
467 struct sockaddr *ap_addr, char *extra)
469 struct wireless_dev *wdev = dev->ieee80211_ptr;
470 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
471 u8 *bssid = ap_addr->sa_data;
474 /* call only for ibss! */
475 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
478 if (!rdev->ops->join_ibss)
481 if (ap_addr->sa_family != ARPHRD_ETHER)
485 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
488 if (bssid && !is_valid_ether_addr(bssid))
492 if (!bssid && !wdev->wext.ibss.bssid)
495 /* fixed already - and no change */
496 if (wdev->wext.ibss.bssid && bssid &&
497 ether_addr_equal(bssid, wdev->wext.ibss.bssid))
503 err = __cfg80211_leave_ibss(rdev, dev, true);
510 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
511 wdev->wext.ibss.bssid = wdev->wext.bssid;
513 wdev->wext.ibss.bssid = NULL;
516 err = cfg80211_ibss_wext_join(rdev, wdev);
522 int cfg80211_ibss_wext_giwap(struct net_device *dev,
523 struct iw_request_info *info,
524 struct sockaddr *ap_addr, char *extra)
526 struct wireless_dev *wdev = dev->ieee80211_ptr;
528 /* call only for ibss! */
529 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
532 ap_addr->sa_family = ARPHRD_ETHER;
535 if (wdev->current_bss)
536 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
537 else if (wdev->wext.ibss.bssid)
538 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
540 eth_zero_addr(ap_addr->sa_data);