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 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 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
->ibss_fixed
= params
->channel_fixed
;
134 wdev
->ibss_dfs_possible
= params
->userspace_handles_dfs
;
135 wdev
->chandef
= params
->chandef
;
137 params
->wep_keys
= connkeys
->params
;
138 params
->wep_tx_key
= connkeys
->def
;
141 #ifdef CONFIG_CFG80211_WEXT
142 wdev
->wext
.ibss
.chandef
= params
->chandef
;
144 err
= rdev_join_ibss(rdev
, dev
, params
);
146 wdev
->connect_keys
= NULL
;
150 memcpy(wdev
->ssid
, params
->ssid
, params
->ssid_len
);
151 wdev
->ssid_len
= params
->ssid_len
;
156 static void __cfg80211_clear_ibss(struct net_device
*dev
, bool nowext
)
158 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
159 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
162 ASSERT_WDEV_LOCK(wdev
);
164 kfree_sensitive(wdev
->connect_keys
);
165 wdev
->connect_keys
= NULL
;
167 rdev_set_qos_map(rdev
, dev
, NULL
);
170 * Delete all the keys ... pairwise keys can't really
171 * exist any more anyway, but default keys might.
173 if (rdev
->ops
->del_key
)
174 for (i
= 0; i
< 6; i
++)
175 rdev_del_key(rdev
, dev
, i
, false, NULL
);
177 if (wdev
->current_bss
) {
178 cfg80211_unhold_bss(wdev
->current_bss
);
179 cfg80211_put_bss(wdev
->wiphy
, &wdev
->current_bss
->pub
);
182 wdev
->current_bss
= NULL
;
184 memset(&wdev
->chandef
, 0, sizeof(wdev
->chandef
));
185 #ifdef CONFIG_CFG80211_WEXT
187 wdev
->wext
.ibss
.ssid_len
= 0;
189 cfg80211_sched_dfs_chan_update(rdev
);
192 void cfg80211_clear_ibss(struct net_device
*dev
, bool nowext
)
194 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
197 __cfg80211_clear_ibss(dev
, nowext
);
201 int __cfg80211_leave_ibss(struct cfg80211_registered_device
*rdev
,
202 struct net_device
*dev
, bool nowext
)
204 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
207 ASSERT_WDEV_LOCK(wdev
);
212 err
= rdev_leave_ibss(rdev
, dev
);
217 wdev
->conn_owner_nlportid
= 0;
218 __cfg80211_clear_ibss(dev
, nowext
);
223 int cfg80211_leave_ibss(struct cfg80211_registered_device
*rdev
,
224 struct net_device
*dev
, bool nowext
)
226 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
230 err
= __cfg80211_leave_ibss(rdev
, dev
, nowext
);
236 #ifdef CONFIG_CFG80211_WEXT
237 int cfg80211_ibss_wext_join(struct cfg80211_registered_device
*rdev
,
238 struct wireless_dev
*wdev
)
240 struct cfg80211_cached_keys
*ck
= NULL
;
241 enum nl80211_band band
;
244 ASSERT_WDEV_LOCK(wdev
);
246 if (!wdev
->wext
.ibss
.beacon_interval
)
247 wdev
->wext
.ibss
.beacon_interval
= 100;
249 /* try to find an IBSS channel if none requested ... */
250 if (!wdev
->wext
.ibss
.chandef
.chan
) {
251 struct ieee80211_channel
*new_chan
= NULL
;
253 for (band
= 0; band
< NUM_NL80211_BANDS
; band
++) {
254 struct ieee80211_supported_band
*sband
;
255 struct ieee80211_channel
*chan
;
257 sband
= rdev
->wiphy
.bands
[band
];
261 for (i
= 0; i
< sband
->n_channels
; i
++) {
262 chan
= &sband
->channels
[i
];
263 if (chan
->flags
& IEEE80211_CHAN_NO_IR
)
265 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
278 cfg80211_chandef_create(&wdev
->wext
.ibss
.chandef
, new_chan
,
282 /* don't join -- SSID is not there */
283 if (!wdev
->wext
.ibss
.ssid_len
)
286 if (!netif_running(wdev
->netdev
))
290 wdev
->wext
.keys
->def
= wdev
->wext
.default_key
;
292 wdev
->wext
.ibss
.privacy
= wdev
->wext
.default_key
!= -1;
294 if (wdev
->wext
.keys
&& wdev
->wext
.keys
->def
!= -1) {
295 ck
= kmemdup(wdev
->wext
.keys
, sizeof(*ck
), GFP_KERNEL
);
298 for (i
= 0; i
< CFG80211_MAX_WEP_KEYS
; i
++)
299 ck
->params
[i
].key
= ck
->data
[i
];
301 err
= __cfg80211_join_ibss(rdev
, wdev
->netdev
,
302 &wdev
->wext
.ibss
, ck
);
309 int cfg80211_ibss_wext_siwfreq(struct net_device
*dev
,
310 struct iw_request_info
*info
,
311 struct iw_freq
*wextfreq
, char *extra
)
313 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
314 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
315 struct ieee80211_channel
*chan
= NULL
;
318 /* call only for ibss! */
319 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
322 if (!rdev
->ops
->join_ibss
)
325 freq
= cfg80211_wext_freq(wextfreq
);
330 chan
= ieee80211_get_channel(wdev
->wiphy
, freq
);
333 if (chan
->flags
& IEEE80211_CHAN_NO_IR
||
334 chan
->flags
& IEEE80211_CHAN_DISABLED
)
338 if (wdev
->wext
.ibss
.chandef
.chan
== chan
)
344 err
= __cfg80211_leave_ibss(rdev
, dev
, true);
351 cfg80211_chandef_create(&wdev
->wext
.ibss
.chandef
, chan
,
353 wdev
->wext
.ibss
.channel_fixed
= true;
355 /* cfg80211_ibss_wext_join will pick one if needed */
356 wdev
->wext
.ibss
.channel_fixed
= false;
360 err
= cfg80211_ibss_wext_join(rdev
, wdev
);
366 int cfg80211_ibss_wext_giwfreq(struct net_device
*dev
,
367 struct iw_request_info
*info
,
368 struct iw_freq
*freq
, char *extra
)
370 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
371 struct ieee80211_channel
*chan
= NULL
;
373 /* call only for ibss! */
374 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
378 if (wdev
->current_bss
)
379 chan
= wdev
->current_bss
->pub
.channel
;
380 else if (wdev
->wext
.ibss
.chandef
.chan
)
381 chan
= wdev
->wext
.ibss
.chandef
.chan
;
385 freq
->m
= chan
->center_freq
;
390 /* no channel if not joining */
394 int cfg80211_ibss_wext_siwessid(struct net_device
*dev
,
395 struct iw_request_info
*info
,
396 struct iw_point
*data
, char *ssid
)
398 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
399 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
400 size_t len
= data
->length
;
403 /* call only for ibss! */
404 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
407 if (!rdev
->ops
->join_ibss
)
413 err
= __cfg80211_leave_ibss(rdev
, dev
, true);
419 /* iwconfig uses nul termination in SSID.. */
420 if (len
> 0 && ssid
[len
- 1] == '\0')
423 memcpy(wdev
->ssid
, ssid
, len
);
424 wdev
->wext
.ibss
.ssid
= wdev
->ssid
;
425 wdev
->wext
.ibss
.ssid_len
= len
;
428 err
= cfg80211_ibss_wext_join(rdev
, wdev
);
434 int cfg80211_ibss_wext_giwessid(struct net_device
*dev
,
435 struct iw_request_info
*info
,
436 struct iw_point
*data
, char *ssid
)
438 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
440 /* call only for ibss! */
441 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
447 if (wdev
->ssid_len
) {
449 data
->length
= wdev
->ssid_len
;
450 memcpy(ssid
, wdev
->ssid
, data
->length
);
451 } else if (wdev
->wext
.ibss
.ssid
&& wdev
->wext
.ibss
.ssid_len
) {
453 data
->length
= wdev
->wext
.ibss
.ssid_len
;
454 memcpy(ssid
, wdev
->wext
.ibss
.ssid
, data
->length
);
461 int cfg80211_ibss_wext_siwap(struct net_device
*dev
,
462 struct iw_request_info
*info
,
463 struct sockaddr
*ap_addr
, char *extra
)
465 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
466 struct cfg80211_registered_device
*rdev
= wiphy_to_rdev(wdev
->wiphy
);
467 u8
*bssid
= ap_addr
->sa_data
;
470 /* call only for ibss! */
471 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
474 if (!rdev
->ops
->join_ibss
)
477 if (ap_addr
->sa_family
!= ARPHRD_ETHER
)
481 if (is_zero_ether_addr(bssid
) || is_broadcast_ether_addr(bssid
))
484 if (bssid
&& !is_valid_ether_addr(bssid
))
488 if (!bssid
&& !wdev
->wext
.ibss
.bssid
)
491 /* fixed already - and no change */
492 if (wdev
->wext
.ibss
.bssid
&& bssid
&&
493 ether_addr_equal(bssid
, wdev
->wext
.ibss
.bssid
))
499 err
= __cfg80211_leave_ibss(rdev
, dev
, true);
506 memcpy(wdev
->wext
.bssid
, bssid
, ETH_ALEN
);
507 wdev
->wext
.ibss
.bssid
= wdev
->wext
.bssid
;
509 wdev
->wext
.ibss
.bssid
= NULL
;
512 err
= cfg80211_ibss_wext_join(rdev
, wdev
);
518 int cfg80211_ibss_wext_giwap(struct net_device
*dev
,
519 struct iw_request_info
*info
,
520 struct sockaddr
*ap_addr
, char *extra
)
522 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
524 /* call only for ibss! */
525 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
528 ap_addr
->sa_family
= ARPHRD_ETHER
;
531 if (wdev
->current_bss
)
532 memcpy(ap_addr
->sa_data
, wdev
->current_bss
->pub
.bssid
, ETH_ALEN
);
533 else if (wdev
->wext
.ibss
.bssid
)
534 memcpy(ap_addr
->sa_data
, wdev
->wext
.ibss
.bssid
, ETH_ALEN
);
536 eth_zero_addr(ap_addr
->sa_data
);