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/workqueue.h>
11 #include <linux/wireless.h>
12 #include <net/iw_handler.h>
13 #include <net/cfg80211.h>
14 #include <net/rtnetlink.h>
18 struct cfg80211_conn
{
19 struct cfg80211_connect_params params
;
20 /* these are sub-states of the _CONNECTING sme_state */
23 CFG80211_CONN_SCANNING
,
24 CFG80211_CONN_SCAN_AGAIN
,
25 CFG80211_CONN_AUTHENTICATE_NEXT
,
26 CFG80211_CONN_AUTHENTICATING
,
27 CFG80211_CONN_ASSOCIATE_NEXT
,
28 CFG80211_CONN_ASSOCIATING
,
29 CFG80211_CONN_DEAUTH_ASSOC_FAIL
,
31 u8 bssid
[ETH_ALEN
], prev_bssid
[ETH_ALEN
];
34 bool auto_auth
, prev_bssid_valid
;
38 static int cfg80211_conn_scan(struct wireless_dev
*wdev
)
40 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
41 struct cfg80211_scan_request
*request
;
45 ASSERT_RDEV_LOCK(rdev
);
46 ASSERT_WDEV_LOCK(wdev
);
51 if (wdev
->conn
->params
.channel
) {
54 enum ieee80211_band band
;
57 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
58 if (!wdev
->wiphy
->bands
[band
])
60 n_channels
+= wdev
->wiphy
->bands
[band
]->n_channels
;
63 request
= kzalloc(sizeof(*request
) + sizeof(request
->ssids
[0]) +
64 sizeof(request
->channels
[0]) * n_channels
,
69 if (wdev
->conn
->params
.channel
)
70 request
->channels
[0] = wdev
->conn
->params
.channel
;
73 enum ieee80211_band band
;
75 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
76 if (!wdev
->wiphy
->bands
[band
])
78 for (j
= 0; j
< wdev
->wiphy
->bands
[band
]->n_channels
;
80 request
->channels
[i
] =
81 &wdev
->wiphy
->bands
[band
]->channels
[j
];
84 request
->n_channels
= n_channels
;
85 request
->ssids
= (void *)&request
->channels
[n_channels
];
88 memcpy(request
->ssids
[0].ssid
, wdev
->conn
->params
.ssid
,
89 wdev
->conn
->params
.ssid_len
);
90 request
->ssids
[0].ssid_len
= wdev
->conn
->params
.ssid_len
;
92 request
->dev
= wdev
->netdev
;
93 request
->wiphy
= &rdev
->wiphy
;
95 rdev
->scan_req
= request
;
97 err
= rdev
->ops
->scan(wdev
->wiphy
, wdev
->netdev
, request
);
99 wdev
->conn
->state
= CFG80211_CONN_SCANNING
;
100 nl80211_send_scan_start(rdev
, wdev
->netdev
);
101 dev_hold(wdev
->netdev
);
103 rdev
->scan_req
= NULL
;
109 static int cfg80211_conn_do_work(struct wireless_dev
*wdev
)
111 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
112 struct cfg80211_connect_params
*params
;
113 const u8
*prev_bssid
= NULL
;
116 ASSERT_WDEV_LOCK(wdev
);
121 params
= &wdev
->conn
->params
;
123 switch (wdev
->conn
->state
) {
124 case CFG80211_CONN_SCAN_AGAIN
:
125 return cfg80211_conn_scan(wdev
);
126 case CFG80211_CONN_AUTHENTICATE_NEXT
:
127 BUG_ON(!rdev
->ops
->auth
);
128 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATING
;
129 return __cfg80211_mlme_auth(rdev
, wdev
->netdev
,
130 params
->channel
, params
->auth_type
,
132 params
->ssid
, params
->ssid_len
,
134 params
->key
, params
->key_len
,
136 case CFG80211_CONN_ASSOCIATE_NEXT
:
137 BUG_ON(!rdev
->ops
->assoc
);
138 wdev
->conn
->state
= CFG80211_CONN_ASSOCIATING
;
139 if (wdev
->conn
->prev_bssid_valid
)
140 prev_bssid
= wdev
->conn
->prev_bssid
;
141 err
= __cfg80211_mlme_assoc(rdev
, wdev
->netdev
,
142 params
->channel
, params
->bssid
,
144 params
->ssid
, params
->ssid_len
,
145 params
->ie
, params
->ie_len
,
146 false, ¶ms
->crypto
);
148 __cfg80211_mlme_deauth(rdev
, wdev
->netdev
, params
->bssid
,
150 WLAN_REASON_DEAUTH_LEAVING
);
152 case CFG80211_CONN_DEAUTH_ASSOC_FAIL
:
153 __cfg80211_mlme_deauth(rdev
, wdev
->netdev
, params
->bssid
,
155 WLAN_REASON_DEAUTH_LEAVING
);
156 /* return an error so that we call __cfg80211_connect_result() */
163 void cfg80211_conn_work(struct work_struct
*work
)
165 struct cfg80211_registered_device
*rdev
=
166 container_of(work
, struct cfg80211_registered_device
, conn_work
);
167 struct wireless_dev
*wdev
;
168 u8 bssid_buf
[ETH_ALEN
], *bssid
= NULL
;
171 cfg80211_lock_rdev(rdev
);
172 mutex_lock(&rdev
->devlist_mtx
);
174 list_for_each_entry(wdev
, &rdev
->netdev_list
, list
) {
176 if (!netif_running(wdev
->netdev
)) {
180 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
) {
184 if (wdev
->conn
->params
.bssid
) {
185 memcpy(bssid_buf
, wdev
->conn
->params
.bssid
, ETH_ALEN
);
188 if (cfg80211_conn_do_work(wdev
))
189 __cfg80211_connect_result(
192 WLAN_STATUS_UNSPECIFIED_FAILURE
,
197 mutex_unlock(&rdev
->devlist_mtx
);
198 cfg80211_unlock_rdev(rdev
);
202 static struct cfg80211_bss
*cfg80211_get_conn_bss(struct wireless_dev
*wdev
)
204 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
205 struct cfg80211_bss
*bss
;
206 u16 capa
= WLAN_CAPABILITY_ESS
;
208 ASSERT_WDEV_LOCK(wdev
);
210 if (wdev
->conn
->params
.privacy
)
211 capa
|= WLAN_CAPABILITY_PRIVACY
;
213 bss
= cfg80211_get_bss(wdev
->wiphy
, NULL
, wdev
->conn
->params
.bssid
,
214 wdev
->conn
->params
.ssid
,
215 wdev
->conn
->params
.ssid_len
,
216 WLAN_CAPABILITY_ESS
| WLAN_CAPABILITY_PRIVACY
,
221 memcpy(wdev
->conn
->bssid
, bss
->bssid
, ETH_ALEN
);
222 wdev
->conn
->params
.bssid
= wdev
->conn
->bssid
;
223 wdev
->conn
->params
.channel
= bss
->channel
;
224 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATE_NEXT
;
225 schedule_work(&rdev
->conn_work
);
230 static void __cfg80211_sme_scan_done(struct net_device
*dev
)
232 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
233 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
234 struct cfg80211_bss
*bss
;
236 ASSERT_WDEV_LOCK(wdev
);
238 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
)
244 if (wdev
->conn
->state
!= CFG80211_CONN_SCANNING
&&
245 wdev
->conn
->state
!= CFG80211_CONN_SCAN_AGAIN
)
248 bss
= cfg80211_get_conn_bss(wdev
);
250 cfg80211_put_bss(bss
);
253 if (wdev
->conn
->state
== CFG80211_CONN_SCAN_AGAIN
)
254 schedule_work(&rdev
->conn_work
);
256 __cfg80211_connect_result(
258 wdev
->conn
->params
.bssid
,
260 WLAN_STATUS_UNSPECIFIED_FAILURE
,
265 void cfg80211_sme_scan_done(struct net_device
*dev
)
267 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
269 mutex_lock(&wiphy_to_dev(wdev
->wiphy
)->devlist_mtx
);
271 __cfg80211_sme_scan_done(dev
);
273 mutex_unlock(&wiphy_to_dev(wdev
->wiphy
)->devlist_mtx
);
276 void cfg80211_sme_rx_auth(struct net_device
*dev
,
277 const u8
*buf
, size_t len
)
279 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
280 struct wiphy
*wiphy
= wdev
->wiphy
;
281 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wiphy
);
282 struct ieee80211_mgmt
*mgmt
= (struct ieee80211_mgmt
*)buf
;
283 u16 status_code
= le16_to_cpu(mgmt
->u
.auth
.status_code
);
285 ASSERT_WDEV_LOCK(wdev
);
287 /* should only RX auth frames when connecting */
288 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
)
291 if (WARN_ON(!wdev
->conn
))
294 if (status_code
== WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG
&&
295 wdev
->conn
->auto_auth
&&
296 wdev
->conn
->params
.auth_type
!= NL80211_AUTHTYPE_NETWORK_EAP
) {
297 /* select automatically between only open, shared, leap */
298 switch (wdev
->conn
->params
.auth_type
) {
299 case NL80211_AUTHTYPE_OPEN_SYSTEM
:
300 if (wdev
->connect_keys
)
301 wdev
->conn
->params
.auth_type
=
302 NL80211_AUTHTYPE_SHARED_KEY
;
304 wdev
->conn
->params
.auth_type
=
305 NL80211_AUTHTYPE_NETWORK_EAP
;
307 case NL80211_AUTHTYPE_SHARED_KEY
:
308 wdev
->conn
->params
.auth_type
=
309 NL80211_AUTHTYPE_NETWORK_EAP
;
313 wdev
->conn
->params
.auth_type
=
314 NL80211_AUTHTYPE_OPEN_SYSTEM
;
317 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATE_NEXT
;
318 schedule_work(&rdev
->conn_work
);
319 } else if (status_code
!= WLAN_STATUS_SUCCESS
) {
320 __cfg80211_connect_result(dev
, mgmt
->bssid
, NULL
, 0, NULL
, 0,
321 status_code
, false, NULL
);
322 } else if (wdev
->sme_state
== CFG80211_SME_CONNECTING
&&
323 wdev
->conn
->state
== CFG80211_CONN_AUTHENTICATING
) {
324 wdev
->conn
->state
= CFG80211_CONN_ASSOCIATE_NEXT
;
325 schedule_work(&rdev
->conn_work
);
329 bool cfg80211_sme_failed_reassoc(struct wireless_dev
*wdev
)
331 struct wiphy
*wiphy
= wdev
->wiphy
;
332 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wiphy
);
334 if (WARN_ON(!wdev
->conn
))
337 if (!wdev
->conn
->prev_bssid_valid
)
341 * Some stupid APs don't accept reassoc, so we
342 * need to fall back to trying regular assoc.
344 wdev
->conn
->prev_bssid_valid
= false;
345 wdev
->conn
->state
= CFG80211_CONN_ASSOCIATE_NEXT
;
346 schedule_work(&rdev
->conn_work
);
351 void cfg80211_sme_failed_assoc(struct wireless_dev
*wdev
)
353 struct wiphy
*wiphy
= wdev
->wiphy
;
354 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wiphy
);
356 wdev
->conn
->state
= CFG80211_CONN_DEAUTH_ASSOC_FAIL
;
357 schedule_work(&rdev
->conn_work
);
360 void __cfg80211_connect_result(struct net_device
*dev
, const u8
*bssid
,
361 const u8
*req_ie
, size_t req_ie_len
,
362 const u8
*resp_ie
, size_t resp_ie_len
,
363 u16 status
, bool wextev
,
364 struct cfg80211_bss
*bss
)
366 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
368 #ifdef CONFIG_CFG80211_WEXT
369 union iwreq_data wrqu
;
372 ASSERT_WDEV_LOCK(wdev
);
374 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
))
377 if (wdev
->sme_state
!= CFG80211_SME_CONNECTING
)
380 nl80211_send_connect_result(wiphy_to_dev(wdev
->wiphy
), dev
,
381 bssid
, req_ie
, req_ie_len
,
382 resp_ie
, resp_ie_len
,
385 #ifdef CONFIG_CFG80211_WEXT
387 if (req_ie
&& status
== WLAN_STATUS_SUCCESS
) {
388 memset(&wrqu
, 0, sizeof(wrqu
));
389 wrqu
.data
.length
= req_ie_len
;
390 wireless_send_event(dev
, IWEVASSOCREQIE
, &wrqu
, req_ie
);
393 if (resp_ie
&& status
== WLAN_STATUS_SUCCESS
) {
394 memset(&wrqu
, 0, sizeof(wrqu
));
395 wrqu
.data
.length
= resp_ie_len
;
396 wireless_send_event(dev
, IWEVASSOCRESPIE
, &wrqu
, resp_ie
);
399 memset(&wrqu
, 0, sizeof(wrqu
));
400 wrqu
.ap_addr
.sa_family
= ARPHRD_ETHER
;
401 if (bssid
&& status
== WLAN_STATUS_SUCCESS
) {
402 memcpy(wrqu
.ap_addr
.sa_data
, bssid
, ETH_ALEN
);
403 memcpy(wdev
->wext
.prev_bssid
, bssid
, ETH_ALEN
);
404 wdev
->wext
.prev_bssid_valid
= true;
406 wireless_send_event(dev
, SIOCGIWAP
, &wrqu
, NULL
);
410 if (wdev
->current_bss
) {
411 cfg80211_unhold_bss(wdev
->current_bss
);
412 cfg80211_put_bss(&wdev
->current_bss
->pub
);
413 wdev
->current_bss
= NULL
;
417 wdev
->conn
->state
= CFG80211_CONN_IDLE
;
419 if (status
!= WLAN_STATUS_SUCCESS
) {
420 wdev
->sme_state
= CFG80211_SME_IDLE
;
422 kfree(wdev
->conn
->ie
);
425 kfree(wdev
->connect_keys
);
426 wdev
->connect_keys
= NULL
;
432 bss
= cfg80211_get_bss(wdev
->wiphy
, NULL
, bssid
,
433 wdev
->ssid
, wdev
->ssid_len
,
435 WLAN_CAPABILITY_ESS
);
440 cfg80211_hold_bss(bss_from_pub(bss
));
441 wdev
->current_bss
= bss_from_pub(bss
);
443 wdev
->sme_state
= CFG80211_SME_CONNECTED
;
444 cfg80211_upload_connect_keys(wdev
);
446 country_ie
= (u8
*) ieee80211_bss_get_ie(bss
, WLAN_EID_COUNTRY
);
452 * ieee80211_bss_get_ie() ensures we can access:
453 * - country_ie + 2, the start of the country ie data, and
454 * - and country_ie[1] which is the IE length
456 regulatory_hint_11d(wdev
->wiphy
,
461 void cfg80211_connect_result(struct net_device
*dev
, const u8
*bssid
,
462 const u8
*req_ie
, size_t req_ie_len
,
463 const u8
*resp_ie
, size_t resp_ie_len
,
464 u16 status
, gfp_t gfp
)
466 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
467 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
468 struct cfg80211_event
*ev
;
471 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTING
);
473 ev
= kzalloc(sizeof(*ev
) + req_ie_len
+ resp_ie_len
, gfp
);
477 ev
->type
= EVENT_CONNECT_RESULT
;
479 memcpy(ev
->cr
.bssid
, bssid
, ETH_ALEN
);
480 ev
->cr
.req_ie
= ((u8
*)ev
) + sizeof(*ev
);
481 ev
->cr
.req_ie_len
= req_ie_len
;
482 memcpy((void *)ev
->cr
.req_ie
, req_ie
, req_ie_len
);
483 ev
->cr
.resp_ie
= ((u8
*)ev
) + sizeof(*ev
) + req_ie_len
;
484 ev
->cr
.resp_ie_len
= resp_ie_len
;
485 memcpy((void *)ev
->cr
.resp_ie
, resp_ie
, resp_ie_len
);
486 ev
->cr
.status
= status
;
488 spin_lock_irqsave(&wdev
->event_lock
, flags
);
489 list_add_tail(&ev
->list
, &wdev
->event_list
);
490 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
491 schedule_work(&rdev
->event_work
);
493 EXPORT_SYMBOL(cfg80211_connect_result
);
495 void __cfg80211_roamed(struct wireless_dev
*wdev
, const u8
*bssid
,
496 const u8
*req_ie
, size_t req_ie_len
,
497 const u8
*resp_ie
, size_t resp_ie_len
)
499 struct cfg80211_bss
*bss
;
500 #ifdef CONFIG_CFG80211_WEXT
501 union iwreq_data wrqu
;
504 ASSERT_WDEV_LOCK(wdev
);
506 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
))
509 if (wdev
->sme_state
!= CFG80211_SME_CONNECTED
)
512 /* internal error -- how did we get to CONNECTED w/o BSS? */
513 if (WARN_ON(!wdev
->current_bss
)) {
517 cfg80211_unhold_bss(wdev
->current_bss
);
518 cfg80211_put_bss(&wdev
->current_bss
->pub
);
519 wdev
->current_bss
= NULL
;
521 bss
= cfg80211_get_bss(wdev
->wiphy
, NULL
, bssid
,
522 wdev
->ssid
, wdev
->ssid_len
,
523 WLAN_CAPABILITY_ESS
, WLAN_CAPABILITY_ESS
);
528 cfg80211_hold_bss(bss_from_pub(bss
));
529 wdev
->current_bss
= bss_from_pub(bss
);
531 nl80211_send_roamed(wiphy_to_dev(wdev
->wiphy
), wdev
->netdev
, bssid
,
532 req_ie
, req_ie_len
, resp_ie
, resp_ie_len
,
535 #ifdef CONFIG_CFG80211_WEXT
537 memset(&wrqu
, 0, sizeof(wrqu
));
538 wrqu
.data
.length
= req_ie_len
;
539 wireless_send_event(wdev
->netdev
, IWEVASSOCREQIE
,
544 memset(&wrqu
, 0, sizeof(wrqu
));
545 wrqu
.data
.length
= resp_ie_len
;
546 wireless_send_event(wdev
->netdev
, IWEVASSOCRESPIE
,
550 memset(&wrqu
, 0, sizeof(wrqu
));
551 wrqu
.ap_addr
.sa_family
= ARPHRD_ETHER
;
552 memcpy(wrqu
.ap_addr
.sa_data
, bssid
, ETH_ALEN
);
553 memcpy(wdev
->wext
.prev_bssid
, bssid
, ETH_ALEN
);
554 wdev
->wext
.prev_bssid_valid
= true;
555 wireless_send_event(wdev
->netdev
, SIOCGIWAP
, &wrqu
, NULL
);
559 void cfg80211_roamed(struct net_device
*dev
, const u8
*bssid
,
560 const u8
*req_ie
, size_t req_ie_len
,
561 const u8
*resp_ie
, size_t resp_ie_len
, gfp_t gfp
)
563 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
564 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
565 struct cfg80211_event
*ev
;
568 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTED
);
570 ev
= kzalloc(sizeof(*ev
) + req_ie_len
+ resp_ie_len
, gfp
);
574 ev
->type
= EVENT_ROAMED
;
575 memcpy(ev
->rm
.bssid
, bssid
, ETH_ALEN
);
576 ev
->rm
.req_ie
= ((u8
*)ev
) + sizeof(*ev
);
577 ev
->rm
.req_ie_len
= req_ie_len
;
578 memcpy((void *)ev
->rm
.req_ie
, req_ie
, req_ie_len
);
579 ev
->rm
.resp_ie
= ((u8
*)ev
) + sizeof(*ev
) + req_ie_len
;
580 ev
->rm
.resp_ie_len
= resp_ie_len
;
581 memcpy((void *)ev
->rm
.resp_ie
, resp_ie
, resp_ie_len
);
583 spin_lock_irqsave(&wdev
->event_lock
, flags
);
584 list_add_tail(&ev
->list
, &wdev
->event_list
);
585 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
586 schedule_work(&rdev
->event_work
);
588 EXPORT_SYMBOL(cfg80211_roamed
);
590 void __cfg80211_disconnected(struct net_device
*dev
, const u8
*ie
,
591 size_t ie_len
, u16 reason
, bool from_ap
)
593 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
594 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
596 #ifdef CONFIG_CFG80211_WEXT
597 union iwreq_data wrqu
;
600 ASSERT_WDEV_LOCK(wdev
);
602 if (WARN_ON(wdev
->iftype
!= NL80211_IFTYPE_STATION
))
605 if (wdev
->sme_state
!= CFG80211_SME_CONNECTED
)
608 if (wdev
->current_bss
) {
609 cfg80211_unhold_bss(wdev
->current_bss
);
610 cfg80211_put_bss(&wdev
->current_bss
->pub
);
613 wdev
->current_bss
= NULL
;
614 wdev
->sme_state
= CFG80211_SME_IDLE
;
621 kfree(wdev
->conn
->ie
);
622 wdev
->conn
->ie
= NULL
;
627 * If this disconnect was due to a disassoc, we
628 * we might still have an auth BSS around. For
629 * the userspace SME that's currently expected,
630 * but for the kernel SME (nl80211 CONNECT or
631 * wireless extensions) we want to clear up all
634 for (i
= 0; i
< MAX_AUTH_BSSES
; i
++) {
635 if (!wdev
->auth_bsses
[i
])
637 bssid
= wdev
->auth_bsses
[i
]->pub
.bssid
;
638 ret
= __cfg80211_mlme_deauth(rdev
, dev
, bssid
, NULL
, 0,
639 WLAN_REASON_DEAUTH_LEAVING
);
640 WARN(ret
, "deauth failed: %d\n", ret
);
644 nl80211_send_disconnected(rdev
, dev
, reason
, ie
, ie_len
, from_ap
);
647 * Delete all the keys ... pairwise keys can't really
648 * exist any more anyway, but default keys might.
650 if (rdev
->ops
->del_key
)
651 for (i
= 0; i
< 6; i
++)
652 rdev
->ops
->del_key(wdev
->wiphy
, dev
, i
, NULL
);
654 #ifdef CONFIG_CFG80211_WEXT
655 memset(&wrqu
, 0, sizeof(wrqu
));
656 wrqu
.ap_addr
.sa_family
= ARPHRD_ETHER
;
657 wireless_send_event(dev
, SIOCGIWAP
, &wrqu
, NULL
);
661 void cfg80211_disconnected(struct net_device
*dev
, u16 reason
,
662 u8
*ie
, size_t ie_len
, gfp_t gfp
)
664 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
665 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
666 struct cfg80211_event
*ev
;
669 CFG80211_DEV_WARN_ON(wdev
->sme_state
!= CFG80211_SME_CONNECTED
);
671 ev
= kzalloc(sizeof(*ev
) + ie_len
, gfp
);
675 ev
->type
= EVENT_DISCONNECTED
;
676 ev
->dc
.ie
= ((u8
*)ev
) + sizeof(*ev
);
677 ev
->dc
.ie_len
= ie_len
;
678 memcpy((void *)ev
->dc
.ie
, ie
, ie_len
);
679 ev
->dc
.reason
= reason
;
681 spin_lock_irqsave(&wdev
->event_lock
, flags
);
682 list_add_tail(&ev
->list
, &wdev
->event_list
);
683 spin_unlock_irqrestore(&wdev
->event_lock
, flags
);
684 schedule_work(&rdev
->event_work
);
686 EXPORT_SYMBOL(cfg80211_disconnected
);
688 int __cfg80211_connect(struct cfg80211_registered_device
*rdev
,
689 struct net_device
*dev
,
690 struct cfg80211_connect_params
*connect
,
691 struct cfg80211_cached_keys
*connkeys
,
692 const u8
*prev_bssid
)
694 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
695 struct ieee80211_channel
*chan
;
696 struct cfg80211_bss
*bss
= NULL
;
699 ASSERT_WDEV_LOCK(wdev
);
701 if (wdev
->sme_state
!= CFG80211_SME_IDLE
)
704 chan
= rdev_fixed_channel(rdev
, wdev
);
705 if (chan
&& chan
!= connect
->channel
)
708 if (WARN_ON(wdev
->connect_keys
)) {
709 kfree(wdev
->connect_keys
);
710 wdev
->connect_keys
= NULL
;
713 if (connkeys
&& connkeys
->def
>= 0) {
718 cipher
= connkeys
->params
[idx
].cipher
;
719 /* If given a WEP key we may need it for shared key auth */
720 if (cipher
== WLAN_CIPHER_SUITE_WEP40
||
721 cipher
== WLAN_CIPHER_SUITE_WEP104
) {
722 connect
->key_idx
= idx
;
723 connect
->key
= connkeys
->params
[idx
].key
;
724 connect
->key_len
= connkeys
->params
[idx
].key_len
;
727 * If ciphers are not set (e.g. when going through
728 * iwconfig), we have to set them appropriately here.
730 if (connect
->crypto
.cipher_group
== 0)
731 connect
->crypto
.cipher_group
= cipher
;
733 if (connect
->crypto
.n_ciphers_pairwise
== 0) {
734 connect
->crypto
.n_ciphers_pairwise
= 1;
735 connect
->crypto
.ciphers_pairwise
[0] = cipher
;
740 if (!rdev
->ops
->connect
) {
741 if (!rdev
->ops
->auth
|| !rdev
->ops
->assoc
)
744 if (WARN_ON(wdev
->conn
))
747 wdev
->conn
= kzalloc(sizeof(*wdev
->conn
), GFP_KERNEL
);
752 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
754 memcpy(&wdev
->conn
->params
, connect
, sizeof(*connect
));
755 if (connect
->bssid
) {
756 wdev
->conn
->params
.bssid
= wdev
->conn
->bssid
;
757 memcpy(wdev
->conn
->bssid
, connect
->bssid
, ETH_ALEN
);
761 wdev
->conn
->ie
= kmemdup(connect
->ie
, connect
->ie_len
,
763 wdev
->conn
->params
.ie
= wdev
->conn
->ie
;
764 if (!wdev
->conn
->ie
) {
771 if (connect
->auth_type
== NL80211_AUTHTYPE_AUTOMATIC
) {
772 wdev
->conn
->auto_auth
= true;
773 /* start with open system ... should mostly work */
774 wdev
->conn
->params
.auth_type
=
775 NL80211_AUTHTYPE_OPEN_SYSTEM
;
777 wdev
->conn
->auto_auth
= false;
780 memcpy(wdev
->ssid
, connect
->ssid
, connect
->ssid_len
);
781 wdev
->ssid_len
= connect
->ssid_len
;
782 wdev
->conn
->params
.ssid
= wdev
->ssid
;
783 wdev
->conn
->params
.ssid_len
= connect
->ssid_len
;
785 /* see if we have the bss already */
786 bss
= cfg80211_get_conn_bss(wdev
);
788 wdev
->sme_state
= CFG80211_SME_CONNECTING
;
789 wdev
->connect_keys
= connkeys
;
792 memcpy(wdev
->conn
->prev_bssid
, prev_bssid
, ETH_ALEN
);
793 wdev
->conn
->prev_bssid_valid
= true;
796 /* we're good if we have a matching bss struct */
798 wdev
->conn
->state
= CFG80211_CONN_AUTHENTICATE_NEXT
;
799 err
= cfg80211_conn_do_work(wdev
);
800 cfg80211_put_bss(bss
);
802 /* otherwise we'll need to scan for the AP first */
803 err
= cfg80211_conn_scan(wdev
);
805 * If we can't scan right now, then we need to scan again
806 * after the current scan finished, since the parameters
807 * changed (unless we find a good AP anyway).
811 wdev
->conn
->state
= CFG80211_CONN_SCAN_AGAIN
;
815 kfree(wdev
->conn
->ie
);
818 wdev
->sme_state
= CFG80211_SME_IDLE
;
819 wdev
->connect_keys
= NULL
;
825 wdev
->sme_state
= CFG80211_SME_CONNECTING
;
826 wdev
->connect_keys
= connkeys
;
827 err
= rdev
->ops
->connect(&rdev
->wiphy
, dev
, connect
);
829 wdev
->connect_keys
= NULL
;
830 wdev
->sme_state
= CFG80211_SME_IDLE
;
834 memcpy(wdev
->ssid
, connect
->ssid
, connect
->ssid_len
);
835 wdev
->ssid_len
= connect
->ssid_len
;
841 int cfg80211_connect(struct cfg80211_registered_device
*rdev
,
842 struct net_device
*dev
,
843 struct cfg80211_connect_params
*connect
,
844 struct cfg80211_cached_keys
*connkeys
)
848 mutex_lock(&rdev
->devlist_mtx
);
849 wdev_lock(dev
->ieee80211_ptr
);
850 err
= __cfg80211_connect(rdev
, dev
, connect
, connkeys
, NULL
);
851 wdev_unlock(dev
->ieee80211_ptr
);
852 mutex_unlock(&rdev
->devlist_mtx
);
857 int __cfg80211_disconnect(struct cfg80211_registered_device
*rdev
,
858 struct net_device
*dev
, u16 reason
, bool wextev
)
860 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
863 ASSERT_WDEV_LOCK(wdev
);
865 if (wdev
->sme_state
== CFG80211_SME_IDLE
)
868 kfree(wdev
->connect_keys
);
869 wdev
->connect_keys
= NULL
;
871 if (!rdev
->ops
->disconnect
) {
872 if (!rdev
->ops
->deauth
)
875 /* was it connected by userspace SME? */
877 cfg80211_mlme_down(rdev
, dev
);
881 if (wdev
->sme_state
== CFG80211_SME_CONNECTING
&&
882 (wdev
->conn
->state
== CFG80211_CONN_SCANNING
||
883 wdev
->conn
->state
== CFG80211_CONN_SCAN_AGAIN
)) {
884 wdev
->sme_state
= CFG80211_SME_IDLE
;
885 kfree(wdev
->conn
->ie
);
892 /* wdev->conn->params.bssid must be set if > SCANNING */
893 err
= __cfg80211_mlme_deauth(rdev
, dev
,
894 wdev
->conn
->params
.bssid
,
899 err
= rdev
->ops
->disconnect(&rdev
->wiphy
, dev
, reason
);
904 if (wdev
->sme_state
== CFG80211_SME_CONNECTED
)
905 __cfg80211_disconnected(dev
, NULL
, 0, 0, false);
906 else if (wdev
->sme_state
== CFG80211_SME_CONNECTING
)
907 __cfg80211_connect_result(dev
, NULL
, NULL
, 0, NULL
, 0,
908 WLAN_STATUS_UNSPECIFIED_FAILURE
,
914 int cfg80211_disconnect(struct cfg80211_registered_device
*rdev
,
915 struct net_device
*dev
,
916 u16 reason
, bool wextev
)
920 wdev_lock(dev
->ieee80211_ptr
);
921 err
= __cfg80211_disconnect(rdev
, dev
, reason
, wextev
);
922 wdev_unlock(dev
->ieee80211_ptr
);
927 void cfg80211_sme_disassoc(struct net_device
*dev
, int idx
)
929 struct wireless_dev
*wdev
= dev
->ieee80211_ptr
;
930 struct cfg80211_registered_device
*rdev
= wiphy_to_dev(wdev
->wiphy
);
933 ASSERT_WDEV_LOCK(wdev
);
938 if (wdev
->conn
->state
== CFG80211_CONN_IDLE
)
942 * Ok, so the association was made by this SME -- we don't
943 * want it any more so deauthenticate too.
946 if (!wdev
->auth_bsses
[idx
])
949 memcpy(bssid
, wdev
->auth_bsses
[idx
]->pub
.bssid
, ETH_ALEN
);
950 if (__cfg80211_mlme_deauth(rdev
, dev
, bssid
,
951 NULL
, 0, WLAN_REASON_DEAUTH_LEAVING
)) {
952 /* whatever -- assume gone anyway */
953 cfg80211_unhold_bss(wdev
->auth_bsses
[idx
]);
954 cfg80211_put_bss(&wdev
->auth_bsses
[idx
]->pub
);
955 wdev
->auth_bsses
[idx
] = NULL
;