2 * Some IBSS support code for cfg80211.
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
7 #include <linux/etherdevice.h>
8 #include <linux/if_arp.h>
9 #include <linux/slab.h>
10 #include <net/cfg80211.h>
11 #include "wext-compat.h"
15 void __cfg80211_ibss_joined(struct net_device
*dev
, const u8
*bssid
)
17 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
18 struct cfg80211_bss
*bss
;
19 #ifdef CONFIG_CFG80211_WEXT
20 union iwreq_data wrqu
;
23 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
29 bss
= cfg80211_get_bss(wdev
->wiphy
, NULL
, bssid
,
30 wdev
->ssid
, wdev
->ssid_len
,
31 WLAN_CAPABILITY_IBSS
, WLAN_CAPABILITY_IBSS
);
36 if (wdev
->current_bss
) {
37 cfg80211_unhold_bss(wdev
->current_bss
);
38 cfg80211_put_bss(&wdev
->current_bss
->pub
);
41 cfg80211_hold_bss(bss_from_pub(bss
));
42 wdev
->current_bss
= bss_from_pub(bss
);
44 cfg80211_upload_connect_keys(wdev
);
46 nl80211_send_ibss_bssid(wiphy_to_dev(wdev
->wiphy
), dev
, bssid
,
48 #ifdef CONFIG_CFG80211_WEXT
49 memset(&wrqu
, 0, sizeof(wrqu
));
50 memcpy(wrqu
.ap_addr
.sa_data
, bssid
, ETH_ALEN
);
51 wireless_send_event(dev
, SIOCGIWAP
, &wrqu
, NULL
);
55 void cfg80211_ibss_joined(struct net_device
*dev
, const u8
*bssid
, gfp_t gfp
)
57 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
58 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
59 struct cfg80211_event
*ev
;
62 CFG80211_DEV_WARN_ON(!wdev
->ssid_len
);
64 ev
= kzalloc(sizeof(*ev
), gfp
);
68 ev
->type
= EVENT_IBSS_JOINED
;
69 memcpy(ev
->cr
.bssid
, bssid
, ETH_ALEN
);
71 spin_lock_irqsave(&wdev
->event_lock
, flags
);
72 list_add_tail(&ev
->list
, &wdev
->event_list
);
73 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
74 queue_work(cfg80211_wq
, &rdev
->event_work
);
76 EXPORT_SYMBOL(cfg80211_ibss_joined
);
78 int __cfg80211_join_ibss(struct cfg80211_registered_device
*rdev
,
79 struct net_device
*dev
,
80 struct cfg80211_ibss_params
*params
,
81 struct cfg80211_cached_keys
*connkeys
)
83 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
86 ASSERT_WDEV_LOCK(wdev
);
91 if (WARN_ON(wdev
->connect_keys
))
92 kfree(wdev
->connect_keys
);
93 wdev
->connect_keys
= connkeys
;
95 #ifdef CONFIG_CFG80211_WEXT
96 wdev
->wext
.ibss
.channel
= params
->channel
;
98 err
= rdev
->ops
->join_ibss(&rdev
->wiphy
, dev
, params
);
100 wdev
->connect_keys
= NULL
;
104 memcpy(wdev
->ssid
, params
->ssid
, params
->ssid_len
);
105 wdev
->ssid_len
= params
->ssid_len
;
110 int cfg80211_join_ibss(struct cfg80211_registered_device
*rdev
,
111 struct net_device
*dev
,
112 struct cfg80211_ibss_params
*params
,
113 struct cfg80211_cached_keys
*connkeys
)
115 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
118 mutex_lock(&rdev
->devlist_mtx
);
120 err
= __cfg80211_join_ibss(rdev
, dev
, params
, connkeys
);
122 mutex_unlock(&rdev
->devlist_mtx
);
127 static void __cfg80211_clear_ibss(struct net_device
*dev
, bool nowext
)
129 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
130 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
133 ASSERT_WDEV_LOCK(wdev
);
135 kfree(wdev
->connect_keys
);
136 wdev
->connect_keys
= NULL
;
139 * Delete all the keys ... pairwise keys can't really
140 * exist any more anyway, but default keys might.
142 if (rdev
->ops
->del_key
)
143 for (i
= 0; i
< 6; i
++)
144 rdev
->ops
->del_key(wdev
->wiphy
, dev
, i
, NULL
);
146 if (wdev
->current_bss
) {
147 cfg80211_unhold_bss(wdev
->current_bss
);
148 cfg80211_put_bss(&wdev
->current_bss
->pub
);
151 wdev
->current_bss
= NULL
;
153 #ifdef CONFIG_CFG80211_WEXT
155 wdev
->wext
.ibss
.ssid_len
= 0;
159 void cfg80211_clear_ibss(struct net_device
*dev
, bool nowext
)
161 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
164 __cfg80211_clear_ibss(dev
, nowext
);
168 int __cfg80211_leave_ibss(struct cfg80211_registered_device
*rdev
,
169 struct net_device
*dev
, bool nowext
)
171 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
174 ASSERT_WDEV_LOCK(wdev
);
179 err
= rdev
->ops
->leave_ibss(&rdev
->wiphy
, dev
);
184 __cfg80211_clear_ibss(dev
, nowext
);
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
;
196 err
= __cfg80211_leave_ibss(rdev
, dev
, nowext
);
202 #ifdef CONFIG_CFG80211_WEXT
203 int cfg80211_ibss_wext_join(struct cfg80211_registered_device
*rdev
,
204 struct wireless_dev
*wdev
)
206 struct cfg80211_cached_keys
*ck
= NULL
;
207 enum ieee80211_band band
;
210 ASSERT_WDEV_LOCK(wdev
);
212 if (!wdev
->wext
.ibss
.beacon_interval
)
213 wdev
->wext
.ibss
.beacon_interval
= 100;
215 /* try to find an IBSS channel if none requested ... */
216 if (!wdev
->wext
.ibss
.channel
) {
217 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
218 struct ieee80211_supported_band
*sband
;
219 struct ieee80211_channel
*chan
;
221 sband
= rdev
->wiphy
.bands
[band
];
225 for (i
= 0; i
< sband
->n_channels
; i
++) {
226 chan
= &sband
->channels
[i
];
227 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
)
229 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
231 wdev
->wext
.ibss
.channel
= chan
;
235 if (wdev
->wext
.ibss
.channel
)
239 if (!wdev
->wext
.ibss
.channel
)
243 /* don't join -- SSID is not there */
244 if (!wdev
->wext
.ibss
.ssid_len
)
247 if (!netif_running(wdev
->netdev
))
250 if (wdev
->wext
.keys
) {
251 wdev
->wext
.keys
->def
= wdev
->wext
.default_key
;
252 wdev
->wext
.keys
->defmgmt
= wdev
->wext
.default_mgmt_key
;
255 wdev
->wext
.ibss
.privacy
= wdev
->wext
.default_key
!= -1;
257 if (wdev
->wext
.keys
) {
258 ck
= kmemdup(wdev
->wext
.keys
, sizeof(*ck
), GFP_KERNEL
);
261 for (i
= 0; i
< 6; i
++)
262 ck
->params
[i
].key
= ck
->data
[i
];
264 err
= __cfg80211_join_ibss(rdev
, wdev
->netdev
,
265 &wdev
->wext
.ibss
, ck
);
272 int cfg80211_ibss_wext_siwfreq(struct net_device
*dev
,
273 struct iw_request_info
*info
,
274 struct iw_freq
*wextfreq
, char *extra
)
276 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
277 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
278 struct ieee80211_channel
*chan
= NULL
;
281 /* call only for ibss! */
282 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
285 if (!rdev
->ops
->join_ibss
)
288 freq
= cfg80211_wext_freq(wdev
->wiphy
, wextfreq
);
293 chan
= ieee80211_get_channel(wdev
->wiphy
, freq
);
296 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
||
297 chan
->flags
& IEEE80211_CHAN_DISABLED
)
301 if (wdev
->wext
.ibss
.channel
== chan
)
307 err
= __cfg80211_leave_ibss(rdev
, dev
, true);
314 wdev
->wext
.ibss
.channel
= chan
;
315 wdev
->wext
.ibss
.channel_fixed
= true;
317 /* cfg80211_ibss_wext_join will pick one if needed */
318 wdev
->wext
.ibss
.channel_fixed
= false;
321 mutex_lock(&rdev
->devlist_mtx
);
323 err
= cfg80211_ibss_wext_join(rdev
, wdev
);
325 mutex_unlock(&rdev
->devlist_mtx
);
330 int cfg80211_ibss_wext_giwfreq(struct net_device
*dev
,
331 struct iw_request_info
*info
,
332 struct iw_freq
*freq
, char *extra
)
334 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
335 struct ieee80211_channel
*chan
= NULL
;
337 /* call only for ibss! */
338 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
342 if (wdev
->current_bss
)
343 chan
= wdev
->current_bss
->pub
.channel
;
344 else if (wdev
->wext
.ibss
.channel
)
345 chan
= wdev
->wext
.ibss
.channel
;
349 freq
->m
= chan
->center_freq
;
354 /* no channel if not joining */
358 int cfg80211_ibss_wext_siwessid(struct net_device
*dev
,
359 struct iw_request_info
*info
,
360 struct iw_point
*data
, char *ssid
)
362 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
363 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
364 size_t len
= data
->length
;
367 /* call only for ibss! */
368 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
371 if (!rdev
->ops
->join_ibss
)
377 err
= __cfg80211_leave_ibss(rdev
, dev
, true);
383 /* iwconfig uses nul termination in SSID.. */
384 if (len
> 0 && ssid
[len
- 1] == '\0')
387 wdev
->wext
.ibss
.ssid
= wdev
->ssid
;
388 memcpy(wdev
->wext
.ibss
.ssid
, ssid
, len
);
389 wdev
->wext
.ibss
.ssid_len
= len
;
391 mutex_lock(&rdev
->devlist_mtx
);
393 err
= cfg80211_ibss_wext_join(rdev
, wdev
);
395 mutex_unlock(&rdev
->devlist_mtx
);
400 int cfg80211_ibss_wext_giwessid(struct net_device
*dev
,
401 struct iw_request_info
*info
,
402 struct iw_point
*data
, char *ssid
)
404 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
406 /* call only for ibss! */
407 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
413 if (wdev
->ssid_len
) {
415 data
->length
= wdev
->ssid_len
;
416 memcpy(ssid
, wdev
->ssid
, data
->length
);
417 } else if (wdev
->wext
.ibss
.ssid
&& wdev
->wext
.ibss
.ssid_len
) {
419 data
->length
= wdev
->wext
.ibss
.ssid_len
;
420 memcpy(ssid
, wdev
->wext
.ibss
.ssid
, data
->length
);
427 int cfg80211_ibss_wext_siwap(struct net_device
*dev
,
428 struct iw_request_info
*info
,
429 struct sockaddr
*ap_addr
, char *extra
)
431 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
432 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
433 u8
*bssid
= ap_addr
->sa_data
;
436 /* call only for ibss! */
437 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
440 if (!rdev
->ops
->join_ibss
)
443 if (ap_addr
->sa_family
!= ARPHRD_ETHER
)
447 if (is_zero_ether_addr(bssid
) || is_broadcast_ether_addr(bssid
))
451 if (!bssid
&& !wdev
->wext
.ibss
.bssid
)
454 /* fixed already - and no change */
455 if (wdev
->wext
.ibss
.bssid
&& bssid
&&
456 compare_ether_addr(bssid
, wdev
->wext
.ibss
.bssid
) == 0)
462 err
= __cfg80211_leave_ibss(rdev
, dev
, true);
469 memcpy(wdev
->wext
.bssid
, bssid
, ETH_ALEN
);
470 wdev
->wext
.ibss
.bssid
= wdev
->wext
.bssid
;
472 wdev
->wext
.ibss
.bssid
= NULL
;
474 mutex_lock(&rdev
->devlist_mtx
);
476 err
= cfg80211_ibss_wext_join(rdev
, wdev
);
478 mutex_unlock(&rdev
->devlist_mtx
);
483 int cfg80211_ibss_wext_giwap(struct net_device
*dev
,
484 struct iw_request_info
*info
,
485 struct sockaddr
*ap_addr
, char *extra
)
487 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
489 /* call only for ibss! */
490 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
493 ap_addr
->sa_family
= ARPHRD_ETHER
;
496 if (wdev
->current_bss
)
497 memcpy(ap_addr
->sa_data
, wdev
->current_bss
->pub
.bssid
, ETH_ALEN
);
498 else if (wdev
->wext
.ibss
.bssid
)
499 memcpy(ap_addr
->sa_data
, wdev
->wext
.ibss
.bssid
, ETH_ALEN
);
501 memset(ap_addr
->sa_data
, 0, ETH_ALEN
);