OMAP3: GPIO: disable GPIO debounce clocks on idle
[linux-ginger.git] / net / wireless / sme.c
blob93c3ed329204f034c18a1ec08b9d54bb75364abe
1 /*
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.
6 */
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>
15 #include "nl80211.h"
16 #include "reg.h"
18 struct cfg80211_conn {
19 struct cfg80211_connect_params params;
20 /* these are sub-states of the _CONNECTING sme_state */
21 enum {
22 CFG80211_CONN_IDLE,
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 } state;
30 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
31 u8 *ie;
32 size_t ie_len;
33 bool auto_auth, prev_bssid_valid;
37 static int cfg80211_conn_scan(struct wireless_dev *wdev)
39 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
40 struct cfg80211_scan_request *request;
41 int n_channels, err;
43 ASSERT_RTNL();
44 ASSERT_RDEV_LOCK(rdev);
45 ASSERT_WDEV_LOCK(wdev);
47 if (rdev->scan_req)
48 return -EBUSY;
50 if (wdev->conn->params.channel) {
51 n_channels = 1;
52 } else {
53 enum ieee80211_band band;
54 n_channels = 0;
56 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
57 if (!wdev->wiphy->bands[band])
58 continue;
59 n_channels += wdev->wiphy->bands[band]->n_channels;
62 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
63 sizeof(request->channels[0]) * n_channels,
64 GFP_KERNEL);
65 if (!request)
66 return -ENOMEM;
68 if (wdev->conn->params.channel)
69 request->channels[0] = wdev->conn->params.channel;
70 else {
71 int i = 0, j;
72 enum ieee80211_band band;
74 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
75 if (!wdev->wiphy->bands[band])
76 continue;
77 for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
78 i++, j++)
79 request->channels[i] =
80 &wdev->wiphy->bands[band]->channels[j];
83 request->n_channels = n_channels;
84 request->ssids = (void *)&request->channels[n_channels];
85 request->n_ssids = 1;
87 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
88 wdev->conn->params.ssid_len);
89 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
91 request->dev = wdev->netdev;
92 request->wiphy = &rdev->wiphy;
94 rdev->scan_req = request;
96 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
97 if (!err) {
98 wdev->conn->state = CFG80211_CONN_SCANNING;
99 nl80211_send_scan_start(rdev, wdev->netdev);
100 dev_hold(wdev->netdev);
101 } else {
102 rdev->scan_req = NULL;
103 kfree(request);
105 return err;
108 static int cfg80211_conn_do_work(struct wireless_dev *wdev)
110 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
111 struct cfg80211_connect_params *params;
112 const u8 *prev_bssid = NULL;
113 int err;
115 ASSERT_WDEV_LOCK(wdev);
117 if (!wdev->conn)
118 return 0;
120 params = &wdev->conn->params;
122 switch (wdev->conn->state) {
123 case CFG80211_CONN_SCAN_AGAIN:
124 return cfg80211_conn_scan(wdev);
125 case CFG80211_CONN_AUTHENTICATE_NEXT:
126 BUG_ON(!rdev->ops->auth);
127 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
128 return __cfg80211_mlme_auth(rdev, wdev->netdev,
129 params->channel, params->auth_type,
130 params->bssid,
131 params->ssid, params->ssid_len,
132 NULL, 0,
133 params->key, params->key_len,
134 params->key_idx);
135 case CFG80211_CONN_ASSOCIATE_NEXT:
136 BUG_ON(!rdev->ops->assoc);
137 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
138 if (wdev->conn->prev_bssid_valid)
139 prev_bssid = wdev->conn->prev_bssid;
140 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
141 params->channel, params->bssid,
142 prev_bssid,
143 params->ssid, params->ssid_len,
144 params->ie, params->ie_len,
145 false, &params->crypto);
146 if (err)
147 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
148 NULL, 0,
149 WLAN_REASON_DEAUTH_LEAVING);
150 return err;
151 default:
152 return 0;
156 void cfg80211_conn_work(struct work_struct *work)
158 struct cfg80211_registered_device *rdev =
159 container_of(work, struct cfg80211_registered_device, conn_work);
160 struct wireless_dev *wdev;
162 rtnl_lock();
163 cfg80211_lock_rdev(rdev);
164 mutex_lock(&rdev->devlist_mtx);
166 list_for_each_entry(wdev, &rdev->netdev_list, list) {
167 wdev_lock(wdev);
168 if (!netif_running(wdev->netdev)) {
169 wdev_unlock(wdev);
170 continue;
172 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
173 wdev_unlock(wdev);
174 continue;
176 if (cfg80211_conn_do_work(wdev))
177 __cfg80211_connect_result(
178 wdev->netdev,
179 wdev->conn->params.bssid,
180 NULL, 0, NULL, 0,
181 WLAN_STATUS_UNSPECIFIED_FAILURE,
182 false, NULL);
183 wdev_unlock(wdev);
186 mutex_unlock(&rdev->devlist_mtx);
187 cfg80211_unlock_rdev(rdev);
188 rtnl_unlock();
191 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
193 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
194 struct cfg80211_bss *bss;
195 u16 capa = WLAN_CAPABILITY_ESS;
197 ASSERT_WDEV_LOCK(wdev);
199 if (wdev->conn->params.privacy)
200 capa |= WLAN_CAPABILITY_PRIVACY;
202 bss = cfg80211_get_bss(wdev->wiphy, NULL, wdev->conn->params.bssid,
203 wdev->conn->params.ssid,
204 wdev->conn->params.ssid_len,
205 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
206 capa);
207 if (!bss)
208 return NULL;
210 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
211 wdev->conn->params.bssid = wdev->conn->bssid;
212 wdev->conn->params.channel = bss->channel;
213 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
214 schedule_work(&rdev->conn_work);
216 return bss;
219 static void __cfg80211_sme_scan_done(struct net_device *dev)
221 struct wireless_dev *wdev = dev->ieee80211_ptr;
222 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
223 struct cfg80211_bss *bss;
225 ASSERT_WDEV_LOCK(wdev);
227 if (wdev->sme_state != CFG80211_SME_CONNECTING)
228 return;
230 if (!wdev->conn)
231 return;
233 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
234 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
235 return;
237 bss = cfg80211_get_conn_bss(wdev);
238 if (bss) {
239 cfg80211_put_bss(bss);
240 } else {
241 /* not found */
242 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
243 schedule_work(&rdev->conn_work);
244 else
245 __cfg80211_connect_result(
246 wdev->netdev,
247 wdev->conn->params.bssid,
248 NULL, 0, NULL, 0,
249 WLAN_STATUS_UNSPECIFIED_FAILURE,
250 false, NULL);
254 void cfg80211_sme_scan_done(struct net_device *dev)
256 struct wireless_dev *wdev = dev->ieee80211_ptr;
258 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
259 wdev_lock(wdev);
260 __cfg80211_sme_scan_done(dev);
261 wdev_unlock(wdev);
262 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
265 void cfg80211_sme_rx_auth(struct net_device *dev,
266 const u8 *buf, size_t len)
268 struct wireless_dev *wdev = dev->ieee80211_ptr;
269 struct wiphy *wiphy = wdev->wiphy;
270 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
271 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
272 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
274 ASSERT_WDEV_LOCK(wdev);
276 /* should only RX auth frames when connecting */
277 if (wdev->sme_state != CFG80211_SME_CONNECTING)
278 return;
280 if (WARN_ON(!wdev->conn))
281 return;
283 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
284 wdev->conn->auto_auth &&
285 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
286 /* select automatically between only open, shared, leap */
287 switch (wdev->conn->params.auth_type) {
288 case NL80211_AUTHTYPE_OPEN_SYSTEM:
289 if (wdev->connect_keys)
290 wdev->conn->params.auth_type =
291 NL80211_AUTHTYPE_SHARED_KEY;
292 else
293 wdev->conn->params.auth_type =
294 NL80211_AUTHTYPE_NETWORK_EAP;
295 break;
296 case NL80211_AUTHTYPE_SHARED_KEY:
297 wdev->conn->params.auth_type =
298 NL80211_AUTHTYPE_NETWORK_EAP;
299 break;
300 default:
301 /* huh? */
302 wdev->conn->params.auth_type =
303 NL80211_AUTHTYPE_OPEN_SYSTEM;
304 break;
306 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
307 schedule_work(&rdev->conn_work);
308 } else if (status_code != WLAN_STATUS_SUCCESS) {
309 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
310 status_code, false, NULL);
311 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
312 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
313 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
314 schedule_work(&rdev->conn_work);
318 bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
320 struct wiphy *wiphy = wdev->wiphy;
321 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
323 if (WARN_ON(!wdev->conn))
324 return false;
326 if (!wdev->conn->prev_bssid_valid)
327 return false;
330 * Some stupid APs don't accept reassoc, so we
331 * need to fall back to trying regular assoc.
333 wdev->conn->prev_bssid_valid = false;
334 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
335 schedule_work(&rdev->conn_work);
337 return true;
340 void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
341 const u8 *req_ie, size_t req_ie_len,
342 const u8 *resp_ie, size_t resp_ie_len,
343 u16 status, bool wextev,
344 struct cfg80211_bss *bss)
346 struct wireless_dev *wdev = dev->ieee80211_ptr;
347 u8 *country_ie;
348 #ifdef CONFIG_WIRELESS_EXT
349 union iwreq_data wrqu;
350 #endif
352 ASSERT_WDEV_LOCK(wdev);
354 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
355 return;
357 if (wdev->sme_state != CFG80211_SME_CONNECTING)
358 return;
360 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
361 bssid, req_ie, req_ie_len,
362 resp_ie, resp_ie_len,
363 status, GFP_KERNEL);
365 #ifdef CONFIG_WIRELESS_EXT
366 if (wextev) {
367 if (req_ie && status == WLAN_STATUS_SUCCESS) {
368 memset(&wrqu, 0, sizeof(wrqu));
369 wrqu.data.length = req_ie_len;
370 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
373 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
374 memset(&wrqu, 0, sizeof(wrqu));
375 wrqu.data.length = resp_ie_len;
376 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
379 memset(&wrqu, 0, sizeof(wrqu));
380 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
381 if (bssid && status == WLAN_STATUS_SUCCESS) {
382 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
383 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
384 wdev->wext.prev_bssid_valid = true;
386 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
388 #endif
390 if (wdev->current_bss) {
391 cfg80211_unhold_bss(wdev->current_bss);
392 cfg80211_put_bss(&wdev->current_bss->pub);
393 wdev->current_bss = NULL;
396 if (wdev->conn)
397 wdev->conn->state = CFG80211_CONN_IDLE;
399 if (status != WLAN_STATUS_SUCCESS) {
400 wdev->sme_state = CFG80211_SME_IDLE;
401 if (wdev->conn)
402 kfree(wdev->conn->ie);
403 kfree(wdev->conn);
404 wdev->conn = NULL;
405 kfree(wdev->connect_keys);
406 wdev->connect_keys = NULL;
407 wdev->ssid_len = 0;
408 return;
411 if (!bss)
412 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
413 wdev->ssid, wdev->ssid_len,
414 WLAN_CAPABILITY_ESS,
415 WLAN_CAPABILITY_ESS);
417 if (WARN_ON(!bss))
418 return;
420 cfg80211_hold_bss(bss_from_pub(bss));
421 wdev->current_bss = bss_from_pub(bss);
423 wdev->sme_state = CFG80211_SME_CONNECTED;
424 cfg80211_upload_connect_keys(wdev);
426 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
428 if (!country_ie)
429 return;
432 * ieee80211_bss_get_ie() ensures we can access:
433 * - country_ie + 2, the start of the country ie data, and
434 * - and country_ie[1] which is the IE length
436 regulatory_hint_11d(wdev->wiphy,
437 country_ie + 2,
438 country_ie[1]);
441 void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
442 const u8 *req_ie, size_t req_ie_len,
443 const u8 *resp_ie, size_t resp_ie_len,
444 u16 status, gfp_t gfp)
446 struct wireless_dev *wdev = dev->ieee80211_ptr;
447 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
448 struct cfg80211_event *ev;
449 unsigned long flags;
451 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
453 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
454 if (!ev)
455 return;
457 ev->type = EVENT_CONNECT_RESULT;
458 if (bssid)
459 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
460 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
461 ev->cr.req_ie_len = req_ie_len;
462 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
463 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
464 ev->cr.resp_ie_len = resp_ie_len;
465 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
466 ev->cr.status = status;
468 spin_lock_irqsave(&wdev->event_lock, flags);
469 list_add_tail(&ev->list, &wdev->event_list);
470 spin_unlock_irqrestore(&wdev->event_lock, flags);
471 schedule_work(&rdev->event_work);
473 EXPORT_SYMBOL(cfg80211_connect_result);
475 void __cfg80211_roamed(struct wireless_dev *wdev, const u8 *bssid,
476 const u8 *req_ie, size_t req_ie_len,
477 const u8 *resp_ie, size_t resp_ie_len)
479 struct cfg80211_bss *bss;
480 #ifdef CONFIG_WIRELESS_EXT
481 union iwreq_data wrqu;
482 #endif
484 ASSERT_WDEV_LOCK(wdev);
486 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
487 return;
489 if (wdev->sme_state != CFG80211_SME_CONNECTED)
490 return;
492 /* internal error -- how did we get to CONNECTED w/o BSS? */
493 if (WARN_ON(!wdev->current_bss)) {
494 return;
497 cfg80211_unhold_bss(wdev->current_bss);
498 cfg80211_put_bss(&wdev->current_bss->pub);
499 wdev->current_bss = NULL;
501 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
502 wdev->ssid, wdev->ssid_len,
503 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
505 if (WARN_ON(!bss))
506 return;
508 cfg80211_hold_bss(bss_from_pub(bss));
509 wdev->current_bss = bss_from_pub(bss);
511 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
512 req_ie, req_ie_len, resp_ie, resp_ie_len,
513 GFP_KERNEL);
515 #ifdef CONFIG_WIRELESS_EXT
516 if (req_ie) {
517 memset(&wrqu, 0, sizeof(wrqu));
518 wrqu.data.length = req_ie_len;
519 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
520 &wrqu, req_ie);
523 if (resp_ie) {
524 memset(&wrqu, 0, sizeof(wrqu));
525 wrqu.data.length = resp_ie_len;
526 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
527 &wrqu, resp_ie);
530 memset(&wrqu, 0, sizeof(wrqu));
531 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
532 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
533 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
534 wdev->wext.prev_bssid_valid = true;
535 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
536 #endif
539 void cfg80211_roamed(struct net_device *dev, const u8 *bssid,
540 const u8 *req_ie, size_t req_ie_len,
541 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
543 struct wireless_dev *wdev = dev->ieee80211_ptr;
544 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
545 struct cfg80211_event *ev;
546 unsigned long flags;
548 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
550 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
551 if (!ev)
552 return;
554 ev->type = EVENT_ROAMED;
555 memcpy(ev->rm.bssid, bssid, ETH_ALEN);
556 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
557 ev->rm.req_ie_len = req_ie_len;
558 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
559 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
560 ev->rm.resp_ie_len = resp_ie_len;
561 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
563 spin_lock_irqsave(&wdev->event_lock, flags);
564 list_add_tail(&ev->list, &wdev->event_list);
565 spin_unlock_irqrestore(&wdev->event_lock, flags);
566 schedule_work(&rdev->event_work);
568 EXPORT_SYMBOL(cfg80211_roamed);
570 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
571 size_t ie_len, u16 reason, bool from_ap)
573 struct wireless_dev *wdev = dev->ieee80211_ptr;
574 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
575 int i;
576 #ifdef CONFIG_WIRELESS_EXT
577 union iwreq_data wrqu;
578 #endif
580 ASSERT_WDEV_LOCK(wdev);
582 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
583 return;
585 if (wdev->sme_state != CFG80211_SME_CONNECTED)
586 return;
588 if (wdev->current_bss) {
589 cfg80211_unhold_bss(wdev->current_bss);
590 cfg80211_put_bss(&wdev->current_bss->pub);
593 wdev->current_bss = NULL;
594 wdev->sme_state = CFG80211_SME_IDLE;
595 wdev->ssid_len = 0;
597 if (wdev->conn) {
598 const u8 *bssid;
599 int ret;
601 kfree(wdev->conn->ie);
602 wdev->conn->ie = NULL;
603 kfree(wdev->conn);
604 wdev->conn = NULL;
607 * If this disconnect was due to a disassoc, we
608 * we might still have an auth BSS around. For
609 * the userspace SME that's currently expected,
610 * but for the kernel SME (nl80211 CONNECT or
611 * wireless extensions) we want to clear up all
612 * state.
614 for (i = 0; i < MAX_AUTH_BSSES; i++) {
615 if (!wdev->auth_bsses[i])
616 continue;
617 bssid = wdev->auth_bsses[i]->pub.bssid;
618 ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
619 WLAN_REASON_DEAUTH_LEAVING);
620 WARN(ret, "deauth failed: %d\n", ret);
624 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
627 * Delete all the keys ... pairwise keys can't really
628 * exist any more anyway, but default keys might.
630 if (rdev->ops->del_key)
631 for (i = 0; i < 6; i++)
632 rdev->ops->del_key(wdev->wiphy, dev, i, NULL);
634 #ifdef CONFIG_WIRELESS_EXT
635 memset(&wrqu, 0, sizeof(wrqu));
636 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
637 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
638 #endif
641 void cfg80211_disconnected(struct net_device *dev, u16 reason,
642 u8 *ie, size_t ie_len, gfp_t gfp)
644 struct wireless_dev *wdev = dev->ieee80211_ptr;
645 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
646 struct cfg80211_event *ev;
647 unsigned long flags;
649 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
651 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
652 if (!ev)
653 return;
655 ev->type = EVENT_DISCONNECTED;
656 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
657 ev->dc.ie_len = ie_len;
658 memcpy((void *)ev->dc.ie, ie, ie_len);
659 ev->dc.reason = reason;
661 spin_lock_irqsave(&wdev->event_lock, flags);
662 list_add_tail(&ev->list, &wdev->event_list);
663 spin_unlock_irqrestore(&wdev->event_lock, flags);
664 schedule_work(&rdev->event_work);
666 EXPORT_SYMBOL(cfg80211_disconnected);
668 int __cfg80211_connect(struct cfg80211_registered_device *rdev,
669 struct net_device *dev,
670 struct cfg80211_connect_params *connect,
671 struct cfg80211_cached_keys *connkeys,
672 const u8 *prev_bssid)
674 struct wireless_dev *wdev = dev->ieee80211_ptr;
675 struct ieee80211_channel *chan;
676 struct cfg80211_bss *bss = NULL;
677 int err;
679 ASSERT_WDEV_LOCK(wdev);
681 if (wdev->sme_state != CFG80211_SME_IDLE)
682 return -EALREADY;
684 chan = rdev_fixed_channel(rdev, wdev);
685 if (chan && chan != connect->channel)
686 return -EBUSY;
688 if (WARN_ON(wdev->connect_keys)) {
689 kfree(wdev->connect_keys);
690 wdev->connect_keys = NULL;
693 if (connkeys && connkeys->def >= 0) {
694 int idx;
695 u32 cipher;
697 idx = connkeys->def;
698 cipher = connkeys->params[idx].cipher;
699 /* If given a WEP key we may need it for shared key auth */
700 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
701 cipher == WLAN_CIPHER_SUITE_WEP104) {
702 connect->key_idx = idx;
703 connect->key = connkeys->params[idx].key;
704 connect->key_len = connkeys->params[idx].key_len;
707 * If ciphers are not set (e.g. when going through
708 * iwconfig), we have to set them appropriately here.
710 if (connect->crypto.cipher_group == 0)
711 connect->crypto.cipher_group = cipher;
713 if (connect->crypto.n_ciphers_pairwise == 0) {
714 connect->crypto.n_ciphers_pairwise = 1;
715 connect->crypto.ciphers_pairwise[0] = cipher;
720 if (!rdev->ops->connect) {
721 if (!rdev->ops->auth || !rdev->ops->assoc)
722 return -EOPNOTSUPP;
724 if (WARN_ON(wdev->conn))
725 return -EINPROGRESS;
727 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
728 if (!wdev->conn)
729 return -ENOMEM;
732 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
734 memcpy(&wdev->conn->params, connect, sizeof(*connect));
735 if (connect->bssid) {
736 wdev->conn->params.bssid = wdev->conn->bssid;
737 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
740 if (connect->ie) {
741 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
742 GFP_KERNEL);
743 wdev->conn->params.ie = wdev->conn->ie;
744 if (!wdev->conn->ie) {
745 kfree(wdev->conn);
746 wdev->conn = NULL;
747 return -ENOMEM;
751 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
752 wdev->conn->auto_auth = true;
753 /* start with open system ... should mostly work */
754 wdev->conn->params.auth_type =
755 NL80211_AUTHTYPE_OPEN_SYSTEM;
756 } else {
757 wdev->conn->auto_auth = false;
760 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
761 wdev->ssid_len = connect->ssid_len;
762 wdev->conn->params.ssid = wdev->ssid;
763 wdev->conn->params.ssid_len = connect->ssid_len;
765 /* see if we have the bss already */
766 bss = cfg80211_get_conn_bss(wdev);
768 wdev->sme_state = CFG80211_SME_CONNECTING;
769 wdev->connect_keys = connkeys;
771 if (prev_bssid) {
772 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
773 wdev->conn->prev_bssid_valid = true;
776 /* we're good if we have a matching bss struct */
777 if (bss) {
778 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
779 err = cfg80211_conn_do_work(wdev);
780 cfg80211_put_bss(bss);
781 } else {
782 /* otherwise we'll need to scan for the AP first */
783 err = cfg80211_conn_scan(wdev);
785 * If we can't scan right now, then we need to scan again
786 * after the current scan finished, since the parameters
787 * changed (unless we find a good AP anyway).
789 if (err == -EBUSY) {
790 err = 0;
791 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
794 if (err) {
795 kfree(wdev->conn->ie);
796 kfree(wdev->conn);
797 wdev->conn = NULL;
798 wdev->sme_state = CFG80211_SME_IDLE;
799 wdev->connect_keys = NULL;
800 wdev->ssid_len = 0;
803 return err;
804 } else {
805 wdev->sme_state = CFG80211_SME_CONNECTING;
806 wdev->connect_keys = connkeys;
807 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
808 if (err) {
809 wdev->connect_keys = NULL;
810 wdev->sme_state = CFG80211_SME_IDLE;
811 return err;
814 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
815 wdev->ssid_len = connect->ssid_len;
817 return 0;
821 int cfg80211_connect(struct cfg80211_registered_device *rdev,
822 struct net_device *dev,
823 struct cfg80211_connect_params *connect,
824 struct cfg80211_cached_keys *connkeys)
826 int err;
828 mutex_lock(&rdev->devlist_mtx);
829 wdev_lock(dev->ieee80211_ptr);
830 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
831 wdev_unlock(dev->ieee80211_ptr);
832 mutex_unlock(&rdev->devlist_mtx);
834 return err;
837 int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
838 struct net_device *dev, u16 reason, bool wextev)
840 struct wireless_dev *wdev = dev->ieee80211_ptr;
841 int err;
843 ASSERT_WDEV_LOCK(wdev);
845 if (wdev->sme_state == CFG80211_SME_IDLE)
846 return -EINVAL;
848 kfree(wdev->connect_keys);
849 wdev->connect_keys = NULL;
851 if (!rdev->ops->disconnect) {
852 if (!rdev->ops->deauth)
853 return -EOPNOTSUPP;
855 /* was it connected by userspace SME? */
856 if (!wdev->conn) {
857 cfg80211_mlme_down(rdev, dev);
858 return 0;
861 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
862 (wdev->conn->state == CFG80211_CONN_SCANNING ||
863 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
864 wdev->sme_state = CFG80211_SME_IDLE;
865 kfree(wdev->conn->ie);
866 kfree(wdev->conn);
867 wdev->conn = NULL;
868 wdev->ssid_len = 0;
869 return 0;
872 /* wdev->conn->params.bssid must be set if > SCANNING */
873 err = __cfg80211_mlme_deauth(rdev, dev,
874 wdev->conn->params.bssid,
875 NULL, 0, reason);
876 if (err)
877 return err;
878 } else {
879 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
880 if (err)
881 return err;
884 if (wdev->sme_state == CFG80211_SME_CONNECTED)
885 __cfg80211_disconnected(dev, NULL, 0, 0, false);
886 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
887 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
888 WLAN_STATUS_UNSPECIFIED_FAILURE,
889 wextev, NULL);
891 return 0;
894 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
895 struct net_device *dev,
896 u16 reason, bool wextev)
898 int err;
900 wdev_lock(dev->ieee80211_ptr);
901 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
902 wdev_unlock(dev->ieee80211_ptr);
904 return err;
907 void cfg80211_sme_disassoc(struct net_device *dev, int idx)
909 struct wireless_dev *wdev = dev->ieee80211_ptr;
910 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
911 u8 bssid[ETH_ALEN];
913 ASSERT_WDEV_LOCK(wdev);
915 if (!wdev->conn)
916 return;
918 if (wdev->conn->state == CFG80211_CONN_IDLE)
919 return;
922 * Ok, so the association was made by this SME -- we don't
923 * want it any more so deauthenticate too.
926 if (!wdev->auth_bsses[idx])
927 return;
929 memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
930 if (__cfg80211_mlme_deauth(rdev, dev, bssid,
931 NULL, 0, WLAN_REASON_DEAUTH_LEAVING)) {
932 /* whatever -- assume gone anyway */
933 cfg80211_unhold_bss(wdev->auth_bsses[idx]);
934 cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
935 wdev->auth_bsses[idx] = NULL;