2 * SME code for cfg80211's connect emulation.
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright (C) 2009 Intel Corporation. All rights reserved.
8 #include <linux/etherdevice.h>
9 #include <linux/if_arp.h>
10 #include <linux/slab.h>
11 #include <linux/workqueue.h>
12 #include <linux/wireless.h>
13 #include <linux/export.h>
14 #include <net/iw_handler.h>
15 #include <net/cfg80211.h>
16 #include <net/rtnetlink.h>
21 struct cfg80211_conn
{
22 struct cfg80211_connect_params params
;
23 /* these are sub-states of the _CONNECTING sme_state */
26 CFG80211_CONN_SCANNING
,
27 CFG80211_CONN_SCAN_AGAIN
,
28 CFG80211_CONN_AUTHENTICATE_NEXT
,
29 CFG80211_CONN_AUTHENTICATING
,
30 CFG80211_CONN_ASSOCIATE_NEXT
,
31 CFG80211_CONN_ASSOCIATING
,
32 CFG80211_CONN_DEAUTH_ASSOC_FAIL
,
34 u8 bssid
[ETH_ALEN
], prev_bssid
[ETH_ALEN
];
37 bool auto_auth
, prev_bssid_valid
;
40 static bool cfg80211_is_all_idle(void)
42 struct cfg80211_registered_device
*rdev
;
43 struct wireless_dev
*wdev
;
44 bool is_all_idle
= true;
46 mutex_lock(&cfg80211_mutex
);
49 * All devices must be idle as otherwise if you are actively
50 * scanning some new beacon hints could be learned and would
51 * count as new regulatory hints.
53 list_for_each_entry(rdev
, &cfg80211_rdev_list
, list
) {
54 cfg80211_lock_rdev(rdev
);
55 list_for_each_entry(wdev
, &rdev
->wdev_list
, list
) {
57 if (wdev
->sme_state
!= CFG80211_SME_IDLE
)
61 cfg80211_unlock_rdev(rdev
);
64 mutex_unlock(&cfg80211_mutex
);
69 static void disconnect_work(struct work_struct
*work
)
71 if (!cfg80211_is_all_idle())
74 regulatory_hint_disconnect();
77 static DECLARE_WORK(cfg80211_disconnect_work
, disconnect_work
);
79 static int cfg80211_conn_scan(struct wireless_dev
*wdev
)
81 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
82 struct cfg80211_scan_request
*request
;
86 ASSERT_RDEV_LOCK(rdev
);
87 ASSERT_WDEV_LOCK(wdev
);
92 if (wdev
->conn
->params
.channel
) {
95 enum ieee80211_band band
;
98 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
99 if (!wdev
->wiphy
->bands
[band
])
101 n_channels
+= wdev
->wiphy
->bands
[band
]->n_channels
;
104 request
= kzalloc(sizeof(*request
) + sizeof(request
->ssids
[0]) +
105 sizeof(request
->channels
[0]) * n_channels
,
110 if (wdev
->conn
->params
.channel
)
111 request
->channels
[0] = wdev
->conn
->params
.channel
;
114 enum ieee80211_band band
;
115 struct ieee80211_supported_band
*bands
;
116 struct ieee80211_channel
*channel
;
118 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
119 bands
= wdev
->wiphy
->bands
[band
];
122 for (j
= 0; j
< bands
->n_channels
; j
++) {
123 channel
= &bands
->channels
[j
];
124 if (channel
->flags
& IEEE80211_CHAN_DISABLED
)
126 request
->channels
[i
++] = channel
;
128 request
->rates
[band
] = (1 << bands
->n_bitrates
) - 1;
132 request
->n_channels
= n_channels
;
133 request
->ssids
= (void *)&request
->channels
[n_channels
];
134 request
->n_ssids
= 1;
136 memcpy(request
->ssids
[0].ssid
, wdev
->conn
->params
.ssid
,
137 wdev
->conn
->params
.ssid_len
);
138 request
->ssids
[0].ssid_len
= wdev
->conn
->params
.ssid_len
;
140 request
->wdev
= wdev
;
141 request
->wiphy
= &rdev
->wiphy
;
142 request
->scan_start
= jiffies
;
144 rdev
->scan_req
= request
;
146 err
= rdev_scan(rdev
, request
);
148 wdev
->conn
->state
= CFG80211_CONN_SCANNING
;
149 nl80211_send_scan_start(rdev
, wdev
);
150 dev_hold(wdev
->netdev
);
152 rdev
->scan_req
= NULL
;
158 static int cfg80211_conn_do_work(struct wireless_dev
*wdev
)
160 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
161 struct cfg80211_connect_params
*params
;
162 const u8
*prev_bssid
= NULL
;
165 ASSERT_WDEV_LOCK(wdev
);
170 params
= &wdev
->conn
->params
;
172 switch (wdev
->conn
->state
) {
173 case CFG80211_CONN_SCAN_AGAIN
:
174 return cfg80211_conn_scan(wdev
);
175 case CFG80211_CONN_AUTHENTICATE_NEXT
:
176 BUG_ON(!rdev
->ops
->auth
);
177 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATING
;
178 return __cfg80211_mlme_auth(rdev
, wdev
->netdev
,
179 params
->channel
, params
->auth_type
,
181 params
->ssid
, params
->ssid_len
,
183 params
->key
, params
->key_len
,
184 params
->key_idx
, NULL
, 0);
185 case CFG80211_CONN_ASSOCIATE_NEXT
:
186 BUG_ON(!rdev
->ops
->assoc
);
187 wdev
->conn
->state
= CFG80211_CONN_ASSOCIATING
;
188 if (wdev
->conn
->prev_bssid_valid
)
189 prev_bssid
= wdev
->conn
->prev_bssid
;
190 err
= __cfg80211_mlme_assoc(rdev
, wdev
->netdev
,
191 params
->channel
, params
->bssid
,
193 params
->ssid
, params
->ssid_len
,
194 params
->ie
, params
->ie_len
,
195 false, ¶ms
->crypto
,
196 params
->flags
, ¶ms
->ht_capa
,
197 ¶ms
->ht_capa_mask
);
199 __cfg80211_mlme_deauth(rdev
, wdev
->netdev
, params
->bssid
,
201 WLAN_REASON_DEAUTH_LEAVING
,
204 case CFG80211_CONN_DEAUTH_ASSOC_FAIL
:
205 __cfg80211_mlme_deauth(rdev
, wdev
->netdev
, params
->bssid
,
207 WLAN_REASON_DEAUTH_LEAVING
, false);
208 /* return an error so that we call __cfg80211_connect_result() */
215 void cfg80211_conn_work(struct work_struct
*work
)
217 struct cfg80211_registered_device
*rdev
=
218 container_of(work
, struct cfg80211_registered_device
, conn_work
);
219 struct wireless_dev
*wdev
;
220 u8 bssid_buf
[ETH_ALEN
], *bssid
= NULL
;
223 cfg80211_lock_rdev(rdev
);
224 mutex_lock(&rdev
->devlist_mtx
);
226 list_for_each_entry(wdev
, &rdev
->wdev_list
, list
) {
228 if (!netif_running(wdev
->netdev
)) {
232 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
) {
236 if (wdev
->conn
->params
.bssid
) {
237 memcpy(bssid_buf
, wdev
->conn
->params
.bssid
, ETH_ALEN
);
240 if (cfg80211_conn_do_work(wdev
))
241 __cfg80211_connect_result(
244 WLAN_STATUS_UNSPECIFIED_FAILURE
,
249 mutex_unlock(&rdev
->devlist_mtx
);
250 cfg80211_unlock_rdev(rdev
);
254 static struct cfg80211_bss
*cfg80211_get_conn_bss(struct wireless_dev
*wdev
)
256 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
257 struct cfg80211_bss
*bss
;
258 u16 capa
= WLAN_CAPABILITY_ESS
;
260 ASSERT_WDEV_LOCK(wdev
);
262 if (wdev
->conn
->params
.privacy
)
263 capa
|= WLAN_CAPABILITY_PRIVACY
;
265 bss
= cfg80211_get_bss(wdev
->wiphy
, wdev
->conn
->params
.channel
,
266 wdev
->conn
->params
.bssid
,
267 wdev
->conn
->params
.ssid
,
268 wdev
->conn
->params
.ssid_len
,
269 WLAN_CAPABILITY_ESS
| WLAN_CAPABILITY_PRIVACY
,
274 memcpy(wdev
->conn
->bssid
, bss
->bssid
, ETH_ALEN
);
275 wdev
->conn
->params
.bssid
= wdev
->conn
->bssid
;
276 wdev
->conn
->params
.channel
= bss
->channel
;
277 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATE_NEXT
;
278 schedule_work(&rdev
->conn_work
);
283 static void __cfg80211_sme_scan_done(struct net_device
*dev
)
285 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
286 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
287 struct cfg80211_bss
*bss
;
289 ASSERT_WDEV_LOCK(wdev
);
291 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
)
297 if (wdev
->conn
->state
!= CFG80211_CONN_SCANNING
&&
298 wdev
->conn
->state
!= CFG80211_CONN_SCAN_AGAIN
)
301 bss
= cfg80211_get_conn_bss(wdev
);
303 cfg80211_put_bss(bss
);
306 if (wdev
->conn
->state
== CFG80211_CONN_SCAN_AGAIN
)
307 schedule_work(&rdev
->conn_work
);
309 __cfg80211_connect_result(
311 wdev
->conn
->params
.bssid
,
313 WLAN_STATUS_UNSPECIFIED_FAILURE
,
318 void cfg80211_sme_scan_done(struct net_device
*dev
)
320 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
322 mutex_lock(&wiphy_to_dev(wdev
->wiphy
)->devlist_mtx
);
324 __cfg80211_sme_scan_done(dev
);
326 mutex_unlock(&wiphy_to_dev(wdev
->wiphy
)->devlist_mtx
);
329 void cfg80211_sme_rx_auth(struct net_device
*dev
,
330 const u8
*buf
, size_t len
)
332 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
333 struct wiphy
*wiphy
= wdev
->wiphy
;
334 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wiphy
);
335 struct ieee80211_mgmt
*mgmt
= (struct ieee80211_mgmt
*)buf
;
336 u16 status_code
= le16_to_cpu(mgmt
->u
.auth
.status_code
);
338 ASSERT_WDEV_LOCK(wdev
);
340 /* should only RX auth frames when connecting */
341 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
)
344 if (WARN_ON(!wdev
->conn
))
347 if (status_code
== WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG
&&
348 wdev
->conn
->auto_auth
&&
349 wdev
->conn
->params
.auth_type
!= NL80211_AUTHTYPE_NETWORK_EAP
) {
350 /* select automatically between only open, shared, leap */
351 switch (wdev
->conn
->params
.auth_type
) {
352 case NL80211_AUTHTYPE_OPEN_SYSTEM
:
353 if (wdev
->connect_keys
)
354 wdev
->conn
->params
.auth_type
=
355 NL80211_AUTHTYPE_SHARED_KEY
;
357 wdev
->conn
->params
.auth_type
=
358 NL80211_AUTHTYPE_NETWORK_EAP
;
360 case NL80211_AUTHTYPE_SHARED_KEY
:
361 wdev
->conn
->params
.auth_type
=
362 NL80211_AUTHTYPE_NETWORK_EAP
;
366 wdev
->conn
->params
.auth_type
=
367 NL80211_AUTHTYPE_OPEN_SYSTEM
;
370 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATE_NEXT
;
371 schedule_work(&rdev
->conn_work
);
372 } else if (status_code
!= WLAN_STATUS_SUCCESS
) {
373 __cfg80211_connect_result(dev
, mgmt
->bssid
, NULL
, 0, NULL
, 0,
374 status_code
, false, NULL
);
375 } else if (wdev
->sme_state
== CFG80211_SME_CONNECTING
&&
376 wdev
->conn
->state
== CFG80211_CONN_AUTHENTICATING
) {
377 wdev
->conn
->state
= CFG80211_CONN_ASSOCIATE_NEXT
;
378 schedule_work(&rdev
->conn_work
);
382 bool cfg80211_sme_failed_reassoc(struct wireless_dev
*wdev
)
384 struct wiphy
*wiphy
= wdev
->wiphy
;
385 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wiphy
);
387 if (WARN_ON(!wdev
->conn
))
390 if (!wdev
->conn
->prev_bssid_valid
)
394 * Some stupid APs don't accept reassoc, so we
395 * need to fall back to trying regular assoc.
397 wdev
->conn
->prev_bssid_valid
= false;
398 wdev
->conn
->state
= CFG80211_CONN_ASSOCIATE_NEXT
;
399 schedule_work(&rdev
->conn_work
);
404 void cfg80211_sme_failed_assoc(struct wireless_dev
*wdev
)
406 struct wiphy
*wiphy
= wdev
->wiphy
;
407 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wiphy
);
409 wdev
->conn
->state
= CFG80211_CONN_DEAUTH_ASSOC_FAIL
;
410 schedule_work(&rdev
->conn_work
);
413 void __cfg80211_connect_result(struct net_device
*dev
, const u8
*bssid
,
414 const u8
*req_ie
, size_t req_ie_len
,
415 const u8
*resp_ie
, size_t resp_ie_len
,
416 u16 status
, bool wextev
,
417 struct cfg80211_bss
*bss
)
419 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
420 const u8
*country_ie
;
421 #ifdef CONFIG_CFG80211_WEXT
422 union iwreq_data wrqu
;
425 ASSERT_WDEV_LOCK(wdev
);
427 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
&&
428 wdev
->iftype
!= NL80211_IFTYPE_P2P_CLIENT
))
431 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
)
434 nl80211_send_connect_result(wiphy_to_dev(wdev
->wiphy
), dev
,
435 bssid
, req_ie
, req_ie_len
,
436 resp_ie
, resp_ie_len
,
439 #ifdef CONFIG_CFG80211_WEXT
441 if (req_ie
&& status
== WLAN_STATUS_SUCCESS
) {
442 memset(&wrqu
, 0, sizeof(wrqu
));
443 wrqu
.data
.length
= req_ie_len
;
444 wireless_send_event(dev
, IWEVASSOCREQIE
, &wrqu
, req_ie
);
447 if (resp_ie
&& status
== WLAN_STATUS_SUCCESS
) {
448 memset(&wrqu
, 0, sizeof(wrqu
));
449 wrqu
.data
.length
= resp_ie_len
;
450 wireless_send_event(dev
, IWEVASSOCRESPIE
, &wrqu
, resp_ie
);
453 memset(&wrqu
, 0, sizeof(wrqu
));
454 wrqu
.ap_addr
.sa_family
= ARPHRD_ETHER
;
455 if (bssid
&& status
== WLAN_STATUS_SUCCESS
) {
456 memcpy(wrqu
.ap_addr
.sa_data
, bssid
, ETH_ALEN
);
457 memcpy(wdev
->wext
.prev_bssid
, bssid
, ETH_ALEN
);
458 wdev
->wext
.prev_bssid_valid
= true;
460 wireless_send_event(dev
, SIOCGIWAP
, &wrqu
, NULL
);
464 if (wdev
->current_bss
) {
465 cfg80211_unhold_bss(wdev
->current_bss
);
466 cfg80211_put_bss(&wdev
->current_bss
->pub
);
467 wdev
->current_bss
= NULL
;
471 wdev
->conn
->state
= CFG80211_CONN_IDLE
;
473 if (status
!= WLAN_STATUS_SUCCESS
) {
474 wdev
->sme_state
= CFG80211_SME_IDLE
;
476 kfree(wdev
->conn
->ie
);
479 kfree(wdev
->connect_keys
);
480 wdev
->connect_keys
= NULL
;
482 cfg80211_put_bss(bss
);
487 bss
= cfg80211_get_bss(wdev
->wiphy
,
488 wdev
->conn
? wdev
->conn
->params
.channel
:
491 wdev
->ssid
, wdev
->ssid_len
,
493 WLAN_CAPABILITY_ESS
);
498 cfg80211_hold_bss(bss_from_pub(bss
));
499 wdev
->current_bss
= bss_from_pub(bss
);
501 wdev
->sme_state
= CFG80211_SME_CONNECTED
;
502 cfg80211_upload_connect_keys(wdev
);
505 country_ie
= ieee80211_bss_get_ie(bss
, WLAN_EID_COUNTRY
);
511 country_ie
= kmemdup(country_ie
, 2 + country_ie
[1], GFP_ATOMIC
);
518 * ieee80211_bss_get_ie() ensures we can access:
519 * - country_ie + 2, the start of the country ie data, and
520 * - and country_ie[1] which is the IE length
522 regulatory_hint_11d(wdev
->wiphy
,
529 void cfg80211_connect_result(struct net_device
*dev
, const u8
*bssid
,
530 const u8
*req_ie
, size_t req_ie_len
,
531 const u8
*resp_ie
, size_t resp_ie_len
,
532 u16 status
, gfp_t gfp
)
534 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
535 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
536 struct cfg80211_event
*ev
;
539 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTING
);
541 ev
= kzalloc(sizeof(*ev
) + req_ie_len
+ resp_ie_len
, gfp
);
545 ev
->type
= EVENT_CONNECT_RESULT
;
547 memcpy(ev
->cr
.bssid
, bssid
, ETH_ALEN
);
549 ev
->cr
.req_ie
= ((u8
*)ev
) + sizeof(*ev
);
550 ev
->cr
.req_ie_len
= req_ie_len
;
551 memcpy((void *)ev
->cr
.req_ie
, req_ie
, req_ie_len
);
554 ev
->cr
.resp_ie
= ((u8
*)ev
) + sizeof(*ev
) + req_ie_len
;
555 ev
->cr
.resp_ie_len
= resp_ie_len
;
556 memcpy((void *)ev
->cr
.resp_ie
, resp_ie
, resp_ie_len
);
558 ev
->cr
.status
= status
;
560 spin_lock_irqsave(&wdev
->event_lock
, flags
);
561 list_add_tail(&ev
->list
, &wdev
->event_list
);
562 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
563 queue_work(cfg80211_wq
, &rdev
->event_work
);
565 EXPORT_SYMBOL(cfg80211_connect_result
);
567 void __cfg80211_roamed(struct wireless_dev
*wdev
,
568 struct cfg80211_bss
*bss
,
569 const u8
*req_ie
, size_t req_ie_len
,
570 const u8
*resp_ie
, size_t resp_ie_len
)
572 #ifdef CONFIG_CFG80211_WEXT
573 union iwreq_data wrqu
;
575 ASSERT_WDEV_LOCK(wdev
);
577 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
&&
578 wdev
->iftype
!= NL80211_IFTYPE_P2P_CLIENT
))
581 if (wdev
->sme_state
!= CFG80211_SME_CONNECTED
)
584 /* internal error -- how did we get to CONNECTED w/o BSS? */
585 if (WARN_ON(!wdev
->current_bss
)) {
589 cfg80211_unhold_bss(wdev
->current_bss
);
590 cfg80211_put_bss(&wdev
->current_bss
->pub
);
591 wdev
->current_bss
= NULL
;
593 cfg80211_hold_bss(bss_from_pub(bss
));
594 wdev
->current_bss
= bss_from_pub(bss
);
596 nl80211_send_roamed(wiphy_to_dev(wdev
->wiphy
), wdev
->netdev
, bss
->bssid
,
597 req_ie
, req_ie_len
, resp_ie
, resp_ie_len
,
600 #ifdef CONFIG_CFG80211_WEXT
602 memset(&wrqu
, 0, sizeof(wrqu
));
603 wrqu
.data
.length
= req_ie_len
;
604 wireless_send_event(wdev
->netdev
, IWEVASSOCREQIE
,
609 memset(&wrqu
, 0, sizeof(wrqu
));
610 wrqu
.data
.length
= resp_ie_len
;
611 wireless_send_event(wdev
->netdev
, IWEVASSOCRESPIE
,
615 memset(&wrqu
, 0, sizeof(wrqu
));
616 wrqu
.ap_addr
.sa_family
= ARPHRD_ETHER
;
617 memcpy(wrqu
.ap_addr
.sa_data
, bss
->bssid
, ETH_ALEN
);
618 memcpy(wdev
->wext
.prev_bssid
, bss
->bssid
, ETH_ALEN
);
619 wdev
->wext
.prev_bssid_valid
= true;
620 wireless_send_event(wdev
->netdev
, SIOCGIWAP
, &wrqu
, NULL
);
625 cfg80211_put_bss(bss
);
628 void cfg80211_roamed(struct net_device
*dev
,
629 struct ieee80211_channel
*channel
,
631 const u8
*req_ie
, size_t req_ie_len
,
632 const u8
*resp_ie
, size_t resp_ie_len
, gfp_t gfp
)
634 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
635 struct cfg80211_bss
*bss
;
637 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTED
);
639 bss
= cfg80211_get_bss(wdev
->wiphy
, channel
, bssid
, wdev
->ssid
,
640 wdev
->ssid_len
, WLAN_CAPABILITY_ESS
,
641 WLAN_CAPABILITY_ESS
);
645 cfg80211_roamed_bss(dev
, bss
, req_ie
, req_ie_len
, resp_ie
,
648 EXPORT_SYMBOL(cfg80211_roamed
);
650 void cfg80211_roamed_bss(struct net_device
*dev
,
651 struct cfg80211_bss
*bss
, const u8
*req_ie
,
652 size_t req_ie_len
, const u8
*resp_ie
,
653 size_t resp_ie_len
, gfp_t gfp
)
655 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
656 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
657 struct cfg80211_event
*ev
;
660 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTED
);
665 ev
= kzalloc(sizeof(*ev
) + req_ie_len
+ resp_ie_len
, gfp
);
667 cfg80211_put_bss(bss
);
671 ev
->type
= EVENT_ROAMED
;
672 ev
->rm
.req_ie
= ((u8
*)ev
) + sizeof(*ev
);
673 ev
->rm
.req_ie_len
= req_ie_len
;
674 memcpy((void *)ev
->rm
.req_ie
, req_ie
, req_ie_len
);
675 ev
->rm
.resp_ie
= ((u8
*)ev
) + sizeof(*ev
) + req_ie_len
;
676 ev
->rm
.resp_ie_len
= resp_ie_len
;
677 memcpy((void *)ev
->rm
.resp_ie
, resp_ie
, resp_ie_len
);
680 spin_lock_irqsave(&wdev
->event_lock
, flags
);
681 list_add_tail(&ev
->list
, &wdev
->event_list
);
682 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
683 queue_work(cfg80211_wq
, &rdev
->event_work
);
685 EXPORT_SYMBOL(cfg80211_roamed_bss
);
687 void __cfg80211_disconnected(struct net_device
*dev
, const u8
*ie
,
688 size_t ie_len
, u16 reason
, bool from_ap
)
690 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
691 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
693 #ifdef CONFIG_CFG80211_WEXT
694 union iwreq_data wrqu
;
697 ASSERT_WDEV_LOCK(wdev
);
699 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
&&
700 wdev
->iftype
!= NL80211_IFTYPE_P2P_CLIENT
))
703 if (wdev
->sme_state
!= CFG80211_SME_CONNECTED
)
706 if (wdev
->current_bss
) {
707 cfg80211_unhold_bss(wdev
->current_bss
);
708 cfg80211_put_bss(&wdev
->current_bss
->pub
);
711 wdev
->current_bss
= NULL
;
712 wdev
->sme_state
= CFG80211_SME_IDLE
;
716 kfree(wdev
->conn
->ie
);
717 wdev
->conn
->ie
= NULL
;
722 nl80211_send_disconnected(rdev
, dev
, reason
, ie
, ie_len
, from_ap
);
725 * Delete all the keys ... pairwise keys can't really
726 * exist any more anyway, but default keys might.
728 if (rdev
->ops
->del_key
)
729 for (i
= 0; i
< 6; i
++)
730 rdev_del_key(rdev
, dev
, i
, false, NULL
);
732 #ifdef CONFIG_CFG80211_WEXT
733 memset(&wrqu
, 0, sizeof(wrqu
));
734 wrqu
.ap_addr
.sa_family
= ARPHRD_ETHER
;
735 wireless_send_event(dev
, SIOCGIWAP
, &wrqu
, NULL
);
736 wdev
->wext
.connect
.ssid_len
= 0;
739 schedule_work(&cfg80211_disconnect_work
);
742 void cfg80211_disconnected(struct net_device
*dev
, u16 reason
,
743 u8
*ie
, size_t ie_len
, gfp_t gfp
)
745 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
746 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
747 struct cfg80211_event
*ev
;
750 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTED
);
752 ev
= kzalloc(sizeof(*ev
) + ie_len
, gfp
);
756 ev
->type
= EVENT_DISCONNECTED
;
757 ev
->dc
.ie
= ((u8
*)ev
) + sizeof(*ev
);
758 ev
->dc
.ie_len
= ie_len
;
759 memcpy((void *)ev
->dc
.ie
, ie
, ie_len
);
760 ev
->dc
.reason
= reason
;
762 spin_lock_irqsave(&wdev
->event_lock
, flags
);
763 list_add_tail(&ev
->list
, &wdev
->event_list
);
764 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
765 queue_work(cfg80211_wq
, &rdev
->event_work
);
767 EXPORT_SYMBOL(cfg80211_disconnected
);
769 int __cfg80211_connect(struct cfg80211_registered_device
*rdev
,
770 struct net_device
*dev
,
771 struct cfg80211_connect_params
*connect
,
772 struct cfg80211_cached_keys
*connkeys
,
773 const u8
*prev_bssid
)
775 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
776 struct cfg80211_bss
*bss
= NULL
;
779 ASSERT_WDEV_LOCK(wdev
);
781 if (wdev
->sme_state
!= CFG80211_SME_IDLE
)
784 if (WARN_ON(wdev
->connect_keys
)) {
785 kfree(wdev
->connect_keys
);
786 wdev
->connect_keys
= NULL
;
789 cfg80211_oper_and_ht_capa(&connect
->ht_capa_mask
,
790 rdev
->wiphy
.ht_capa_mod_mask
);
792 if (connkeys
&& connkeys
->def
>= 0) {
797 cipher
= connkeys
->params
[idx
].cipher
;
798 /* If given a WEP key we may need it for shared key auth */
799 if (cipher
== WLAN_CIPHER_SUITE_WEP40
||
800 cipher
== WLAN_CIPHER_SUITE_WEP104
) {
801 connect
->key_idx
= idx
;
802 connect
->key
= connkeys
->params
[idx
].key
;
803 connect
->key_len
= connkeys
->params
[idx
].key_len
;
806 * If ciphers are not set (e.g. when going through
807 * iwconfig), we have to set them appropriately here.
809 if (connect
->crypto
.cipher_group
== 0)
810 connect
->crypto
.cipher_group
= cipher
;
812 if (connect
->crypto
.n_ciphers_pairwise
== 0) {
813 connect
->crypto
.n_ciphers_pairwise
= 1;
814 connect
->crypto
.ciphers_pairwise
[0] = cipher
;
819 if (!rdev
->ops
->connect
) {
820 if (!rdev
->ops
->auth
|| !rdev
->ops
->assoc
)
823 if (WARN_ON(wdev
->conn
))
826 wdev
->conn
= kzalloc(sizeof(*wdev
->conn
), GFP_KERNEL
);
831 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
833 memcpy(&wdev
->conn
->params
, connect
, sizeof(*connect
));
834 if (connect
->bssid
) {
835 wdev
->conn
->params
.bssid
= wdev
->conn
->bssid
;
836 memcpy(wdev
->conn
->bssid
, connect
->bssid
, ETH_ALEN
);
840 wdev
->conn
->ie
= kmemdup(connect
->ie
, connect
->ie_len
,
842 wdev
->conn
->params
.ie
= wdev
->conn
->ie
;
843 if (!wdev
->conn
->ie
) {
850 if (connect
->auth_type
== NL80211_AUTHTYPE_AUTOMATIC
) {
851 wdev
->conn
->auto_auth
= true;
852 /* start with open system ... should mostly work */
853 wdev
->conn
->params
.auth_type
=
854 NL80211_AUTHTYPE_OPEN_SYSTEM
;
856 wdev
->conn
->auto_auth
= false;
859 memcpy(wdev
->ssid
, connect
->ssid
, connect
->ssid_len
);
860 wdev
->ssid_len
= connect
->ssid_len
;
861 wdev
->conn
->params
.ssid
= wdev
->ssid
;
862 wdev
->conn
->params
.ssid_len
= connect
->ssid_len
;
864 /* see if we have the bss already */
865 bss
= cfg80211_get_conn_bss(wdev
);
867 wdev
->sme_state
= CFG80211_SME_CONNECTING
;
868 wdev
->connect_keys
= connkeys
;
871 memcpy(wdev
->conn
->prev_bssid
, prev_bssid
, ETH_ALEN
);
872 wdev
->conn
->prev_bssid_valid
= true;
875 /* we're good if we have a matching bss struct */
877 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATE_NEXT
;
878 err
= cfg80211_conn_do_work(wdev
);
879 cfg80211_put_bss(bss
);
881 /* otherwise we'll need to scan for the AP first */
882 err
= cfg80211_conn_scan(wdev
);
884 * If we can't scan right now, then we need to scan again
885 * after the current scan finished, since the parameters
886 * changed (unless we find a good AP anyway).
890 wdev
->conn
->state
= CFG80211_CONN_SCAN_AGAIN
;
894 kfree(wdev
->conn
->ie
);
897 wdev
->sme_state
= CFG80211_SME_IDLE
;
898 wdev
->connect_keys
= NULL
;
904 wdev
->sme_state
= CFG80211_SME_CONNECTING
;
905 wdev
->connect_keys
= connkeys
;
906 err
= rdev_connect(rdev
, dev
, connect
);
908 wdev
->connect_keys
= NULL
;
909 wdev
->sme_state
= CFG80211_SME_IDLE
;
913 memcpy(wdev
->ssid
, connect
->ssid
, connect
->ssid_len
);
914 wdev
->ssid_len
= connect
->ssid_len
;
920 int cfg80211_connect(struct cfg80211_registered_device
*rdev
,
921 struct net_device
*dev
,
922 struct cfg80211_connect_params
*connect
,
923 struct cfg80211_cached_keys
*connkeys
)
927 mutex_lock(&rdev
->devlist_mtx
);
928 wdev_lock(dev
->ieee80211_ptr
);
929 err
= __cfg80211_connect(rdev
, dev
, connect
, connkeys
, NULL
);
930 wdev_unlock(dev
->ieee80211_ptr
);
931 mutex_unlock(&rdev
->devlist_mtx
);
936 int __cfg80211_disconnect(struct cfg80211_registered_device
*rdev
,
937 struct net_device
*dev
, u16 reason
, bool wextev
)
939 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
942 ASSERT_WDEV_LOCK(wdev
);
944 if (wdev
->sme_state
== CFG80211_SME_IDLE
)
947 kfree(wdev
->connect_keys
);
948 wdev
->connect_keys
= NULL
;
950 if (!rdev
->ops
->disconnect
) {
951 if (!rdev
->ops
->deauth
)
954 /* was it connected by userspace SME? */
956 cfg80211_mlme_down(rdev
, dev
);
960 if (wdev
->sme_state
== CFG80211_SME_CONNECTING
&&
961 (wdev
->conn
->state
== CFG80211_CONN_SCANNING
||
962 wdev
->conn
->state
== CFG80211_CONN_SCAN_AGAIN
)) {
963 wdev
->sme_state
= CFG80211_SME_IDLE
;
964 kfree(wdev
->conn
->ie
);
971 /* wdev->conn->params.bssid must be set if > SCANNING */
972 err
= __cfg80211_mlme_deauth(rdev
, dev
,
973 wdev
->conn
->params
.bssid
,
974 NULL
, 0, reason
, false);
978 err
= rdev_disconnect(rdev
, dev
, reason
);
983 if (wdev
->sme_state
== CFG80211_SME_CONNECTED
)
984 __cfg80211_disconnected(dev
, NULL
, 0, 0, false);
985 else if (wdev
->sme_state
== CFG80211_SME_CONNECTING
)
986 __cfg80211_connect_result(dev
, NULL
, NULL
, 0, NULL
, 0,
987 WLAN_STATUS_UNSPECIFIED_FAILURE
,
993 int cfg80211_disconnect(struct cfg80211_registered_device
*rdev
,
994 struct net_device
*dev
,
995 u16 reason
, bool wextev
)
999 wdev_lock(dev
->ieee80211_ptr
);
1000 err
= __cfg80211_disconnect(rdev
, dev
, reason
, wextev
);
1001 wdev_unlock(dev
->ieee80211_ptr
);
1006 void cfg80211_sme_disassoc(struct net_device
*dev
,
1007 struct cfg80211_internal_bss
*bss
)
1009 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
1010 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
1013 ASSERT_WDEV_LOCK(wdev
);
1018 if (wdev
->conn
->state
== CFG80211_CONN_IDLE
)
1022 * Ok, so the association was made by this SME -- we don't
1023 * want it any more so deauthenticate too.
1026 memcpy(bssid
, bss
->pub
.bssid
, ETH_ALEN
);
1028 __cfg80211_mlme_deauth(rdev
, dev
, bssid
, NULL
, 0,
1029 WLAN_REASON_DEAUTH_LEAVING
, false);