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
;
84 struct ieee80211_channel
*chan
;
87 ASSERT_WDEV_LOCK(wdev
);
89 chan
= rdev_fixed_channel(rdev
, wdev
);
90 if (chan
&& chan
!= params
->channel
)
96 if (WARN_ON(wdev
->connect_keys
))
97 kfree(wdev
->connect_keys
);
98 wdev
->connect_keys
= connkeys
;
100 #ifdef CONFIG_CFG80211_WEXT
101 wdev
->wext
.ibss
.channel
= params
->channel
;
103 err
= rdev
->ops
->join_ibss(&rdev
->wiphy
, dev
, params
);
105 wdev
->connect_keys
= NULL
;
109 memcpy(wdev
->ssid
, params
->ssid
, params
->ssid_len
);
110 wdev
->ssid_len
= params
->ssid_len
;
115 int cfg80211_join_ibss(struct cfg80211_registered_device
*rdev
,
116 struct net_device
*dev
,
117 struct cfg80211_ibss_params
*params
,
118 struct cfg80211_cached_keys
*connkeys
)
120 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
123 mutex_lock(&rdev
->devlist_mtx
);
125 err
= __cfg80211_join_ibss(rdev
, dev
, params
, connkeys
);
127 mutex_unlock(&rdev
->devlist_mtx
);
132 static void __cfg80211_clear_ibss(struct net_device
*dev
, bool nowext
)
134 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
135 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
138 ASSERT_WDEV_LOCK(wdev
);
140 kfree(wdev
->connect_keys
);
141 wdev
->connect_keys
= NULL
;
144 * Delete all the keys ... pairwise keys can't really
145 * exist any more anyway, but default keys might.
147 if (rdev
->ops
->del_key
)
148 for (i
= 0; i
< 6; i
++)
149 rdev
->ops
->del_key(wdev
->wiphy
, dev
, i
, NULL
);
151 if (wdev
->current_bss
) {
152 cfg80211_unhold_bss(wdev
->current_bss
);
153 cfg80211_put_bss(&wdev
->current_bss
->pub
);
156 wdev
->current_bss
= NULL
;
158 #ifdef CONFIG_CFG80211_WEXT
160 wdev
->wext
.ibss
.ssid_len
= 0;
164 void cfg80211_clear_ibss(struct net_device
*dev
, bool nowext
)
166 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
169 __cfg80211_clear_ibss(dev
, nowext
);
173 int __cfg80211_leave_ibss(struct cfg80211_registered_device
*rdev
,
174 struct net_device
*dev
, bool nowext
)
176 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
179 ASSERT_WDEV_LOCK(wdev
);
184 err
= rdev
->ops
->leave_ibss(&rdev
->wiphy
, dev
);
189 __cfg80211_clear_ibss(dev
, nowext
);
194 int cfg80211_leave_ibss(struct cfg80211_registered_device
*rdev
,
195 struct net_device
*dev
, bool nowext
)
197 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
201 err
= __cfg80211_leave_ibss(rdev
, dev
, nowext
);
207 #ifdef CONFIG_CFG80211_WEXT
208 int cfg80211_ibss_wext_join(struct cfg80211_registered_device
*rdev
,
209 struct wireless_dev
*wdev
)
211 struct cfg80211_cached_keys
*ck
= NULL
;
212 enum ieee80211_band band
;
215 ASSERT_WDEV_LOCK(wdev
);
217 if (!wdev
->wext
.ibss
.beacon_interval
)
218 wdev
->wext
.ibss
.beacon_interval
= 100;
220 /* try to find an IBSS channel if none requested ... */
221 if (!wdev
->wext
.ibss
.channel
) {
222 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
223 struct ieee80211_supported_band
*sband
;
224 struct ieee80211_channel
*chan
;
226 sband
= rdev
->wiphy
.bands
[band
];
230 for (i
= 0; i
< sband
->n_channels
; i
++) {
231 chan
= &sband
->channels
[i
];
232 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
)
234 if (chan
->flags
& IEEE80211_CHAN_DISABLED
)
236 wdev
->wext
.ibss
.channel
= chan
;
240 if (wdev
->wext
.ibss
.channel
)
244 if (!wdev
->wext
.ibss
.channel
)
248 /* don't join -- SSID is not there */
249 if (!wdev
->wext
.ibss
.ssid_len
)
252 if (!netif_running(wdev
->netdev
))
256 wdev
->wext
.keys
->def
= wdev
->wext
.default_key
;
258 wdev
->wext
.ibss
.privacy
= wdev
->wext
.default_key
!= -1;
260 if (wdev
->wext
.keys
) {
261 ck
= kmemdup(wdev
->wext
.keys
, sizeof(*ck
), GFP_KERNEL
);
264 for (i
= 0; i
< 6; i
++)
265 ck
->params
[i
].key
= ck
->data
[i
];
267 err
= __cfg80211_join_ibss(rdev
, wdev
->netdev
,
268 &wdev
->wext
.ibss
, ck
);
275 int cfg80211_ibss_wext_siwfreq(struct net_device
*dev
,
276 struct iw_request_info
*info
,
277 struct iw_freq
*wextfreq
, char *extra
)
279 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
280 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
281 struct ieee80211_channel
*chan
= NULL
;
284 /* call only for ibss! */
285 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
288 if (!rdev
->ops
->join_ibss
)
291 freq
= cfg80211_wext_freq(wdev
->wiphy
, wextfreq
);
296 chan
= ieee80211_get_channel(wdev
->wiphy
, freq
);
299 if (chan
->flags
& IEEE80211_CHAN_NO_IBSS
||
300 chan
->flags
& IEEE80211_CHAN_DISABLED
)
304 if (wdev
->wext
.ibss
.channel
== chan
)
310 err
= __cfg80211_leave_ibss(rdev
, dev
, true);
317 wdev
->wext
.ibss
.channel
= chan
;
318 wdev
->wext
.ibss
.channel_fixed
= true;
320 /* cfg80211_ibss_wext_join will pick one if needed */
321 wdev
->wext
.ibss
.channel_fixed
= false;
324 mutex_lock(&rdev
->devlist_mtx
);
326 err
= cfg80211_ibss_wext_join(rdev
, wdev
);
328 mutex_unlock(&rdev
->devlist_mtx
);
333 int cfg80211_ibss_wext_giwfreq(struct net_device
*dev
,
334 struct iw_request_info
*info
,
335 struct iw_freq
*freq
, char *extra
)
337 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
338 struct ieee80211_channel
*chan
= NULL
;
340 /* call only for ibss! */
341 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
345 if (wdev
->current_bss
)
346 chan
= wdev
->current_bss
->pub
.channel
;
347 else if (wdev
->wext
.ibss
.channel
)
348 chan
= wdev
->wext
.ibss
.channel
;
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_dev(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
)
380 err
= __cfg80211_leave_ibss(rdev
, dev
, true);
386 /* iwconfig uses nul termination in SSID.. */
387 if (len
> 0 && ssid
[len
- 1] == '\0')
390 wdev
->wext
.ibss
.ssid
= wdev
->ssid
;
391 memcpy(wdev
->wext
.ibss
.ssid
, ssid
, len
);
392 wdev
->wext
.ibss
.ssid_len
= len
;
394 mutex_lock(&rdev
->devlist_mtx
);
396 err
= cfg80211_ibss_wext_join(rdev
, wdev
);
398 mutex_unlock(&rdev
->devlist_mtx
);
403 int cfg80211_ibss_wext_giwessid(struct net_device
*dev
,
404 struct iw_request_info
*info
,
405 struct iw_point
*data
, char *ssid
)
407 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
409 /* call only for ibss! */
410 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
416 if (wdev
->ssid_len
) {
418 data
->length
= wdev
->ssid_len
;
419 memcpy(ssid
, wdev
->ssid
, data
->length
);
420 } else if (wdev
->wext
.ibss
.ssid
&& wdev
->wext
.ibss
.ssid_len
) {
422 data
->length
= wdev
->wext
.ibss
.ssid_len
;
423 memcpy(ssid
, wdev
->wext
.ibss
.ssid
, data
->length
);
430 int cfg80211_ibss_wext_siwap(struct net_device
*dev
,
431 struct iw_request_info
*info
,
432 struct sockaddr
*ap_addr
, char *extra
)
434 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
435 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
436 u8
*bssid
= ap_addr
->sa_data
;
439 /* call only for ibss! */
440 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
443 if (!rdev
->ops
->join_ibss
)
446 if (ap_addr
->sa_family
!= ARPHRD_ETHER
)
450 if (is_zero_ether_addr(bssid
) || is_broadcast_ether_addr(bssid
))
454 if (!bssid
&& !wdev
->wext
.ibss
.bssid
)
457 /* fixed already - and no change */
458 if (wdev
->wext
.ibss
.bssid
&& bssid
&&
459 compare_ether_addr(bssid
, wdev
->wext
.ibss
.bssid
) == 0)
465 err
= __cfg80211_leave_ibss(rdev
, dev
, true);
472 memcpy(wdev
->wext
.bssid
, bssid
, ETH_ALEN
);
473 wdev
->wext
.ibss
.bssid
= wdev
->wext
.bssid
;
475 wdev
->wext
.ibss
.bssid
= NULL
;
477 mutex_lock(&rdev
->devlist_mtx
);
479 err
= cfg80211_ibss_wext_join(rdev
, wdev
);
481 mutex_unlock(&rdev
->devlist_mtx
);
486 int cfg80211_ibss_wext_giwap(struct net_device
*dev
,
487 struct iw_request_info
*info
,
488 struct sockaddr
*ap_addr
, char *extra
)
490 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
492 /* call only for ibss! */
493 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_ADHOC
))
496 ap_addr
->sa_family
= ARPHRD_ETHER
;
499 if (wdev
->current_bss
)
500 memcpy(ap_addr
->sa_data
, wdev
->current_bss
->pub
.bssid
, ETH_ALEN
);
501 else if (wdev
->wext
.ibss
.bssid
)
502 memcpy(ap_addr
->sa_data
, wdev
->wext
.ibss
.bssid
, ETH_ALEN
);
504 memset(ap_addr
->sa_data
, 0, ETH_ALEN
);