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
);
88 lockdep_assert_held(&rdev
->sched_scan_mtx
);
93 if (wdev
->conn
->params
.channel
) {
96 enum ieee80211_band band
;
99 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
100 if (!wdev
->wiphy
->bands
[band
])
102 n_channels
+= wdev
->wiphy
->bands
[band
]->n_channels
;
105 request
= kzalloc(sizeof(*request
) + sizeof(request
->ssids
[0]) +
106 sizeof(request
->channels
[0]) * n_channels
,
111 if (wdev
->conn
->params
.channel
)
112 request
->channels
[0] = wdev
->conn
->params
.channel
;
115 enum ieee80211_band band
;
116 struct ieee80211_supported_band
*bands
;
117 struct ieee80211_channel
*channel
;
119 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
120 bands
= wdev
->wiphy
->bands
[band
];
123 for (j
= 0; j
< bands
->n_channels
; j
++) {
124 channel
= &bands
->channels
[j
];
125 if (channel
->flags
& IEEE80211_CHAN_DISABLED
)
127 request
->channels
[i
++] = channel
;
129 request
->rates
[band
] = (1 << bands
->n_bitrates
) - 1;
133 request
->n_channels
= n_channels
;
134 request
->ssids
= (void *)&request
->channels
[n_channels
];
135 request
->n_ssids
= 1;
137 memcpy(request
->ssids
[0].ssid
, wdev
->conn
->params
.ssid
,
138 wdev
->conn
->params
.ssid_len
);
139 request
->ssids
[0].ssid_len
= wdev
->conn
->params
.ssid_len
;
141 request
->wdev
= wdev
;
142 request
->wiphy
= &rdev
->wiphy
;
143 request
->scan_start
= jiffies
;
145 rdev
->scan_req
= request
;
147 err
= rdev_scan(rdev
, request
);
149 wdev
->conn
->state
= CFG80211_CONN_SCANNING
;
150 nl80211_send_scan_start(rdev
, wdev
);
151 dev_hold(wdev
->netdev
);
153 rdev
->scan_req
= NULL
;
159 static int cfg80211_conn_do_work(struct wireless_dev
*wdev
)
161 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
162 struct cfg80211_connect_params
*params
;
163 const u8
*prev_bssid
= NULL
;
166 ASSERT_WDEV_LOCK(wdev
);
171 params
= &wdev
->conn
->params
;
173 switch (wdev
->conn
->state
) {
174 case CFG80211_CONN_SCAN_AGAIN
:
175 return cfg80211_conn_scan(wdev
);
176 case CFG80211_CONN_AUTHENTICATE_NEXT
:
177 BUG_ON(!rdev
->ops
->auth
);
178 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATING
;
179 return __cfg80211_mlme_auth(rdev
, wdev
->netdev
,
180 params
->channel
, params
->auth_type
,
182 params
->ssid
, params
->ssid_len
,
184 params
->key
, params
->key_len
,
185 params
->key_idx
, NULL
, 0);
186 case CFG80211_CONN_ASSOCIATE_NEXT
:
187 BUG_ON(!rdev
->ops
->assoc
);
188 wdev
->conn
->state
= CFG80211_CONN_ASSOCIATING
;
189 if (wdev
->conn
->prev_bssid_valid
)
190 prev_bssid
= wdev
->conn
->prev_bssid
;
191 err
= __cfg80211_mlme_assoc(rdev
, wdev
->netdev
,
192 params
->channel
, params
->bssid
,
194 params
->ssid
, params
->ssid_len
,
195 params
->ie
, params
->ie_len
,
196 params
->mfp
!= NL80211_MFP_NO
,
198 params
->flags
, ¶ms
->ht_capa
,
199 ¶ms
->ht_capa_mask
);
201 __cfg80211_mlme_deauth(rdev
, wdev
->netdev
, params
->bssid
,
203 WLAN_REASON_DEAUTH_LEAVING
,
206 case CFG80211_CONN_DEAUTH_ASSOC_FAIL
:
207 __cfg80211_mlme_deauth(rdev
, wdev
->netdev
, params
->bssid
,
209 WLAN_REASON_DEAUTH_LEAVING
, false);
210 /* return an error so that we call __cfg80211_connect_result() */
217 void cfg80211_conn_work(struct work_struct
*work
)
219 struct cfg80211_registered_device
*rdev
=
220 container_of(work
, struct cfg80211_registered_device
, conn_work
);
221 struct wireless_dev
*wdev
;
222 u8 bssid_buf
[ETH_ALEN
], *bssid
= NULL
;
225 cfg80211_lock_rdev(rdev
);
226 mutex_lock(&rdev
->devlist_mtx
);
228 list_for_each_entry(wdev
, &rdev
->wdev_list
, list
) {
230 if (!netif_running(wdev
->netdev
)) {
234 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
) {
238 if (wdev
->conn
->params
.bssid
) {
239 memcpy(bssid_buf
, wdev
->conn
->params
.bssid
, ETH_ALEN
);
242 if (cfg80211_conn_do_work(wdev
))
243 __cfg80211_connect_result(
246 WLAN_STATUS_UNSPECIFIED_FAILURE
,
251 mutex_unlock(&rdev
->devlist_mtx
);
252 cfg80211_unlock_rdev(rdev
);
256 static struct cfg80211_bss
*cfg80211_get_conn_bss(struct wireless_dev
*wdev
)
258 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
259 struct cfg80211_bss
*bss
;
260 u16 capa
= WLAN_CAPABILITY_ESS
;
262 ASSERT_WDEV_LOCK(wdev
);
264 if (wdev
->conn
->params
.privacy
)
265 capa
|= WLAN_CAPABILITY_PRIVACY
;
267 bss
= cfg80211_get_bss(wdev
->wiphy
, wdev
->conn
->params
.channel
,
268 wdev
->conn
->params
.bssid
,
269 wdev
->conn
->params
.ssid
,
270 wdev
->conn
->params
.ssid_len
,
271 WLAN_CAPABILITY_ESS
| WLAN_CAPABILITY_PRIVACY
,
276 memcpy(wdev
->conn
->bssid
, bss
->bssid
, ETH_ALEN
);
277 wdev
->conn
->params
.bssid
= wdev
->conn
->bssid
;
278 wdev
->conn
->params
.channel
= bss
->channel
;
279 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATE_NEXT
;
280 schedule_work(&rdev
->conn_work
);
285 static void __cfg80211_sme_scan_done(struct net_device
*dev
)
287 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
288 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
289 struct cfg80211_bss
*bss
;
291 ASSERT_WDEV_LOCK(wdev
);
293 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
)
299 if (wdev
->conn
->state
!= CFG80211_CONN_SCANNING
&&
300 wdev
->conn
->state
!= CFG80211_CONN_SCAN_AGAIN
)
303 bss
= cfg80211_get_conn_bss(wdev
);
305 cfg80211_put_bss(&rdev
->wiphy
, bss
);
308 if (wdev
->conn
->state
== CFG80211_CONN_SCAN_AGAIN
)
309 schedule_work(&rdev
->conn_work
);
311 __cfg80211_connect_result(
313 wdev
->conn
->params
.bssid
,
315 WLAN_STATUS_UNSPECIFIED_FAILURE
,
320 void cfg80211_sme_scan_done(struct net_device
*dev
)
322 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
325 __cfg80211_sme_scan_done(dev
);
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
->wiphy
, &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(wdev
->wiphy
, 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
, bss
->channel
->band
,
523 country_ie
+ 2, country_ie
[1]);
527 void cfg80211_connect_result(struct net_device
*dev
, const u8
*bssid
,
528 const u8
*req_ie
, size_t req_ie_len
,
529 const u8
*resp_ie
, size_t resp_ie_len
,
530 u16 status
, gfp_t gfp
)
532 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
533 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
534 struct cfg80211_event
*ev
;
537 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTING
);
539 ev
= kzalloc(sizeof(*ev
) + req_ie_len
+ resp_ie_len
, gfp
);
543 ev
->type
= EVENT_CONNECT_RESULT
;
545 memcpy(ev
->cr
.bssid
, bssid
, ETH_ALEN
);
547 ev
->cr
.req_ie
= ((u8
*)ev
) + sizeof(*ev
);
548 ev
->cr
.req_ie_len
= req_ie_len
;
549 memcpy((void *)ev
->cr
.req_ie
, req_ie
, req_ie_len
);
552 ev
->cr
.resp_ie
= ((u8
*)ev
) + sizeof(*ev
) + req_ie_len
;
553 ev
->cr
.resp_ie_len
= resp_ie_len
;
554 memcpy((void *)ev
->cr
.resp_ie
, resp_ie
, resp_ie_len
);
556 ev
->cr
.status
= status
;
558 spin_lock_irqsave(&wdev
->event_lock
, flags
);
559 list_add_tail(&ev
->list
, &wdev
->event_list
);
560 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
561 queue_work(cfg80211_wq
, &rdev
->event_work
);
563 EXPORT_SYMBOL(cfg80211_connect_result
);
565 void __cfg80211_roamed(struct wireless_dev
*wdev
,
566 struct cfg80211_bss
*bss
,
567 const u8
*req_ie
, size_t req_ie_len
,
568 const u8
*resp_ie
, size_t resp_ie_len
)
570 #ifdef CONFIG_CFG80211_WEXT
571 union iwreq_data wrqu
;
573 ASSERT_WDEV_LOCK(wdev
);
575 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
&&
576 wdev
->iftype
!= NL80211_IFTYPE_P2P_CLIENT
))
579 if (wdev
->sme_state
!= CFG80211_SME_CONNECTED
)
582 /* internal error -- how did we get to CONNECTED w/o BSS? */
583 if (WARN_ON(!wdev
->current_bss
)) {
587 cfg80211_unhold_bss(wdev
->current_bss
);
588 cfg80211_put_bss(wdev
->wiphy
, &wdev
->current_bss
->pub
);
589 wdev
->current_bss
= NULL
;
591 cfg80211_hold_bss(bss_from_pub(bss
));
592 wdev
->current_bss
= bss_from_pub(bss
);
594 nl80211_send_roamed(wiphy_to_dev(wdev
->wiphy
), wdev
->netdev
, bss
->bssid
,
595 req_ie
, req_ie_len
, resp_ie
, resp_ie_len
,
598 #ifdef CONFIG_CFG80211_WEXT
600 memset(&wrqu
, 0, sizeof(wrqu
));
601 wrqu
.data
.length
= req_ie_len
;
602 wireless_send_event(wdev
->netdev
, IWEVASSOCREQIE
,
607 memset(&wrqu
, 0, sizeof(wrqu
));
608 wrqu
.data
.length
= resp_ie_len
;
609 wireless_send_event(wdev
->netdev
, IWEVASSOCRESPIE
,
613 memset(&wrqu
, 0, sizeof(wrqu
));
614 wrqu
.ap_addr
.sa_family
= ARPHRD_ETHER
;
615 memcpy(wrqu
.ap_addr
.sa_data
, bss
->bssid
, ETH_ALEN
);
616 memcpy(wdev
->wext
.prev_bssid
, bss
->bssid
, ETH_ALEN
);
617 wdev
->wext
.prev_bssid_valid
= true;
618 wireless_send_event(wdev
->netdev
, SIOCGIWAP
, &wrqu
, NULL
);
623 cfg80211_put_bss(wdev
->wiphy
, bss
);
626 void cfg80211_roamed(struct net_device
*dev
,
627 struct ieee80211_channel
*channel
,
629 const u8
*req_ie
, size_t req_ie_len
,
630 const u8
*resp_ie
, size_t resp_ie_len
, gfp_t gfp
)
632 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
633 struct cfg80211_bss
*bss
;
635 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTED
);
637 bss
= cfg80211_get_bss(wdev
->wiphy
, channel
, bssid
, wdev
->ssid
,
638 wdev
->ssid_len
, WLAN_CAPABILITY_ESS
,
639 WLAN_CAPABILITY_ESS
);
643 cfg80211_roamed_bss(dev
, bss
, req_ie
, req_ie_len
, resp_ie
,
646 EXPORT_SYMBOL(cfg80211_roamed
);
648 void cfg80211_roamed_bss(struct net_device
*dev
,
649 struct cfg80211_bss
*bss
, const u8
*req_ie
,
650 size_t req_ie_len
, const u8
*resp_ie
,
651 size_t resp_ie_len
, gfp_t gfp
)
653 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
654 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
655 struct cfg80211_event
*ev
;
658 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTED
);
663 ev
= kzalloc(sizeof(*ev
) + req_ie_len
+ resp_ie_len
, gfp
);
665 cfg80211_put_bss(wdev
->wiphy
, bss
);
669 ev
->type
= EVENT_ROAMED
;
670 ev
->rm
.req_ie
= ((u8
*)ev
) + sizeof(*ev
);
671 ev
->rm
.req_ie_len
= req_ie_len
;
672 memcpy((void *)ev
->rm
.req_ie
, req_ie
, req_ie_len
);
673 ev
->rm
.resp_ie
= ((u8
*)ev
) + sizeof(*ev
) + req_ie_len
;
674 ev
->rm
.resp_ie_len
= resp_ie_len
;
675 memcpy((void *)ev
->rm
.resp_ie
, resp_ie
, resp_ie_len
);
678 spin_lock_irqsave(&wdev
->event_lock
, flags
);
679 list_add_tail(&ev
->list
, &wdev
->event_list
);
680 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
681 queue_work(cfg80211_wq
, &rdev
->event_work
);
683 EXPORT_SYMBOL(cfg80211_roamed_bss
);
685 void __cfg80211_disconnected(struct net_device
*dev
, const u8
*ie
,
686 size_t ie_len
, u16 reason
, bool from_ap
)
688 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
689 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
691 #ifdef CONFIG_CFG80211_WEXT
692 union iwreq_data wrqu
;
695 ASSERT_WDEV_LOCK(wdev
);
697 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
&&
698 wdev
->iftype
!= NL80211_IFTYPE_P2P_CLIENT
))
701 if (wdev
->sme_state
!= CFG80211_SME_CONNECTED
)
704 if (wdev
->current_bss
) {
705 cfg80211_unhold_bss(wdev
->current_bss
);
706 cfg80211_put_bss(wdev
->wiphy
, &wdev
->current_bss
->pub
);
709 wdev
->current_bss
= NULL
;
710 wdev
->sme_state
= CFG80211_SME_IDLE
;
714 kfree(wdev
->conn
->ie
);
715 wdev
->conn
->ie
= NULL
;
720 nl80211_send_disconnected(rdev
, dev
, reason
, ie
, ie_len
, from_ap
);
723 * Delete all the keys ... pairwise keys can't really
724 * exist any more anyway, but default keys might.
726 if (rdev
->ops
->del_key
)
727 for (i
= 0; i
< 6; i
++)
728 rdev_del_key(rdev
, dev
, i
, false, NULL
);
730 #ifdef CONFIG_CFG80211_WEXT
731 memset(&wrqu
, 0, sizeof(wrqu
));
732 wrqu
.ap_addr
.sa_family
= ARPHRD_ETHER
;
733 wireless_send_event(dev
, SIOCGIWAP
, &wrqu
, NULL
);
734 wdev
->wext
.connect
.ssid_len
= 0;
737 schedule_work(&cfg80211_disconnect_work
);
740 void cfg80211_disconnected(struct net_device
*dev
, u16 reason
,
741 u8
*ie
, size_t ie_len
, gfp_t gfp
)
743 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
744 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
745 struct cfg80211_event
*ev
;
748 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTED
);
750 ev
= kzalloc(sizeof(*ev
) + ie_len
, gfp
);
754 ev
->type
= EVENT_DISCONNECTED
;
755 ev
->dc
.ie
= ((u8
*)ev
) + sizeof(*ev
);
756 ev
->dc
.ie_len
= ie_len
;
757 memcpy((void *)ev
->dc
.ie
, ie
, ie_len
);
758 ev
->dc
.reason
= reason
;
760 spin_lock_irqsave(&wdev
->event_lock
, flags
);
761 list_add_tail(&ev
->list
, &wdev
->event_list
);
762 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
763 queue_work(cfg80211_wq
, &rdev
->event_work
);
765 EXPORT_SYMBOL(cfg80211_disconnected
);
767 int __cfg80211_connect(struct cfg80211_registered_device
*rdev
,
768 struct net_device
*dev
,
769 struct cfg80211_connect_params
*connect
,
770 struct cfg80211_cached_keys
*connkeys
,
771 const u8
*prev_bssid
)
773 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
774 struct cfg80211_bss
*bss
= NULL
;
777 ASSERT_WDEV_LOCK(wdev
);
779 if (wdev
->sme_state
!= CFG80211_SME_IDLE
)
782 if (WARN_ON(wdev
->connect_keys
)) {
783 kfree(wdev
->connect_keys
);
784 wdev
->connect_keys
= NULL
;
787 cfg80211_oper_and_ht_capa(&connect
->ht_capa_mask
,
788 rdev
->wiphy
.ht_capa_mod_mask
);
790 if (connkeys
&& connkeys
->def
>= 0) {
795 cipher
= connkeys
->params
[idx
].cipher
;
796 /* If given a WEP key we may need it for shared key auth */
797 if (cipher
== WLAN_CIPHER_SUITE_WEP40
||
798 cipher
== WLAN_CIPHER_SUITE_WEP104
) {
799 connect
->key_idx
= idx
;
800 connect
->key
= connkeys
->params
[idx
].key
;
801 connect
->key_len
= connkeys
->params
[idx
].key_len
;
804 * If ciphers are not set (e.g. when going through
805 * iwconfig), we have to set them appropriately here.
807 if (connect
->crypto
.cipher_group
== 0)
808 connect
->crypto
.cipher_group
= cipher
;
810 if (connect
->crypto
.n_ciphers_pairwise
== 0) {
811 connect
->crypto
.n_ciphers_pairwise
= 1;
812 connect
->crypto
.ciphers_pairwise
[0] = cipher
;
817 if (!rdev
->ops
->connect
) {
818 if (!rdev
->ops
->auth
|| !rdev
->ops
->assoc
)
821 if (WARN_ON(wdev
->conn
))
824 wdev
->conn
= kzalloc(sizeof(*wdev
->conn
), GFP_KERNEL
);
829 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
831 memcpy(&wdev
->conn
->params
, connect
, sizeof(*connect
));
832 if (connect
->bssid
) {
833 wdev
->conn
->params
.bssid
= wdev
->conn
->bssid
;
834 memcpy(wdev
->conn
->bssid
, connect
->bssid
, ETH_ALEN
);
838 wdev
->conn
->ie
= kmemdup(connect
->ie
, connect
->ie_len
,
840 wdev
->conn
->params
.ie
= wdev
->conn
->ie
;
841 if (!wdev
->conn
->ie
) {
848 if (connect
->auth_type
== NL80211_AUTHTYPE_AUTOMATIC
) {
849 wdev
->conn
->auto_auth
= true;
850 /* start with open system ... should mostly work */
851 wdev
->conn
->params
.auth_type
=
852 NL80211_AUTHTYPE_OPEN_SYSTEM
;
854 wdev
->conn
->auto_auth
= false;
857 memcpy(wdev
->ssid
, connect
->ssid
, connect
->ssid_len
);
858 wdev
->ssid_len
= connect
->ssid_len
;
859 wdev
->conn
->params
.ssid
= wdev
->ssid
;
860 wdev
->conn
->params
.ssid_len
= connect
->ssid_len
;
862 /* see if we have the bss already */
863 bss
= cfg80211_get_conn_bss(wdev
);
865 wdev
->sme_state
= CFG80211_SME_CONNECTING
;
866 wdev
->connect_keys
= connkeys
;
869 memcpy(wdev
->conn
->prev_bssid
, prev_bssid
, ETH_ALEN
);
870 wdev
->conn
->prev_bssid_valid
= true;
873 /* we're good if we have a matching bss struct */
875 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATE_NEXT
;
876 err
= cfg80211_conn_do_work(wdev
);
877 cfg80211_put_bss(wdev
->wiphy
, bss
);
879 /* otherwise we'll need to scan for the AP first */
880 err
= cfg80211_conn_scan(wdev
);
882 * If we can't scan right now, then we need to scan again
883 * after the current scan finished, since the parameters
884 * changed (unless we find a good AP anyway).
888 wdev
->conn
->state
= CFG80211_CONN_SCAN_AGAIN
;
892 kfree(wdev
->conn
->ie
);
895 wdev
->sme_state
= CFG80211_SME_IDLE
;
896 wdev
->connect_keys
= NULL
;
902 wdev
->sme_state
= CFG80211_SME_CONNECTING
;
903 wdev
->connect_keys
= connkeys
;
904 err
= rdev_connect(rdev
, dev
, connect
);
906 wdev
->connect_keys
= NULL
;
907 wdev
->sme_state
= CFG80211_SME_IDLE
;
911 memcpy(wdev
->ssid
, connect
->ssid
, connect
->ssid_len
);
912 wdev
->ssid_len
= connect
->ssid_len
;
918 int cfg80211_connect(struct cfg80211_registered_device
*rdev
,
919 struct net_device
*dev
,
920 struct cfg80211_connect_params
*connect
,
921 struct cfg80211_cached_keys
*connkeys
)
925 mutex_lock(&rdev
->devlist_mtx
);
926 /* might request scan - scan_mtx -> wdev_mtx dependency */
927 mutex_lock(&rdev
->sched_scan_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
->sched_scan_mtx
);
932 mutex_unlock(&rdev
->devlist_mtx
);
937 int __cfg80211_disconnect(struct cfg80211_registered_device
*rdev
,
938 struct net_device
*dev
, u16 reason
, bool wextev
)
940 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
943 ASSERT_WDEV_LOCK(wdev
);
945 if (wdev
->sme_state
== CFG80211_SME_IDLE
)
948 kfree(wdev
->connect_keys
);
949 wdev
->connect_keys
= NULL
;
951 if (!rdev
->ops
->disconnect
) {
952 if (!rdev
->ops
->deauth
)
955 /* was it connected by userspace SME? */
957 cfg80211_mlme_down(rdev
, dev
);
961 if (wdev
->sme_state
== CFG80211_SME_CONNECTING
&&
962 (wdev
->conn
->state
== CFG80211_CONN_SCANNING
||
963 wdev
->conn
->state
== CFG80211_CONN_SCAN_AGAIN
)) {
964 wdev
->sme_state
= CFG80211_SME_IDLE
;
965 kfree(wdev
->conn
->ie
);
972 /* wdev->conn->params.bssid must be set if > SCANNING */
973 err
= __cfg80211_mlme_deauth(rdev
, dev
,
974 wdev
->conn
->params
.bssid
,
975 NULL
, 0, reason
, false);
979 err
= rdev_disconnect(rdev
, dev
, reason
);
984 if (wdev
->sme_state
== CFG80211_SME_CONNECTED
)
985 __cfg80211_disconnected(dev
, NULL
, 0, 0, false);
986 else if (wdev
->sme_state
== CFG80211_SME_CONNECTING
)
987 __cfg80211_connect_result(dev
, NULL
, NULL
, 0, NULL
, 0,
988 WLAN_STATUS_UNSPECIFIED_FAILURE
,
994 int cfg80211_disconnect(struct cfg80211_registered_device
*rdev
,
995 struct net_device
*dev
,
996 u16 reason
, bool wextev
)
1000 wdev_lock(dev
->ieee80211_ptr
);
1001 err
= __cfg80211_disconnect(rdev
, dev
, reason
, wextev
);
1002 wdev_unlock(dev
->ieee80211_ptr
);
1007 void cfg80211_sme_disassoc(struct net_device
*dev
,
1008 struct cfg80211_internal_bss
*bss
)
1010 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
1011 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
1014 ASSERT_WDEV_LOCK(wdev
);
1019 if (wdev
->conn
->state
== CFG80211_CONN_IDLE
)
1023 * Ok, so the association was made by this SME -- we don't
1024 * want it any more so deauthenticate too.
1027 memcpy(bssid
, bss
->pub
.bssid
, ETH_ALEN
);
1029 __cfg80211_mlme_deauth(rdev
, dev
, bssid
, NULL
, 0,
1030 WLAN_REASON_DEAUTH_LEAVING
, false);