Merge tag 'fixes-3.10-4' of git://git.infradead.org/users/jcooper/linux into fixes
[linux-2.6.git] / net / wireless / sme.c
blob8b5eddfba1e5dfed07b1fb528204c125db2d7331
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/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>
17 #include "nl80211.h"
18 #include "reg.h"
19 #include "rdev-ops.h"
21 struct cfg80211_conn {
22 struct cfg80211_connect_params params;
23 /* these are sub-states of the _CONNECTING sme_state */
24 enum {
25 CFG80211_CONN_IDLE,
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,
33 } state;
34 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
35 u8 *ie;
36 size_t ie_len;
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) {
56 wdev_lock(wdev);
57 if (wdev->sme_state != CFG80211_SME_IDLE)
58 is_all_idle = false;
59 wdev_unlock(wdev);
61 cfg80211_unlock_rdev(rdev);
64 mutex_unlock(&cfg80211_mutex);
66 return is_all_idle;
69 static void disconnect_work(struct work_struct *work)
71 if (!cfg80211_is_all_idle())
72 return;
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;
83 int n_channels, err;
85 ASSERT_RTNL();
86 ASSERT_RDEV_LOCK(rdev);
87 ASSERT_WDEV_LOCK(wdev);
88 lockdep_assert_held(&rdev->sched_scan_mtx);
90 if (rdev->scan_req)
91 return -EBUSY;
93 if (wdev->conn->params.channel) {
94 n_channels = 1;
95 } else {
96 enum ieee80211_band band;
97 n_channels = 0;
99 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
100 if (!wdev->wiphy->bands[band])
101 continue;
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,
107 GFP_KERNEL);
108 if (!request)
109 return -ENOMEM;
111 if (wdev->conn->params.channel)
112 request->channels[0] = wdev->conn->params.channel;
113 else {
114 int i = 0, j;
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];
121 if (!bands)
122 continue;
123 for (j = 0; j < bands->n_channels; j++) {
124 channel = &bands->channels[j];
125 if (channel->flags & IEEE80211_CHAN_DISABLED)
126 continue;
127 request->channels[i++] = channel;
129 request->rates[band] = (1 << bands->n_bitrates) - 1;
131 n_channels = i;
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);
148 if (!err) {
149 wdev->conn->state = CFG80211_CONN_SCANNING;
150 nl80211_send_scan_start(rdev, wdev);
151 dev_hold(wdev->netdev);
152 } else {
153 rdev->scan_req = NULL;
154 kfree(request);
156 return err;
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 struct cfg80211_assoc_request req = {};
164 int err;
166 ASSERT_WDEV_LOCK(wdev);
168 if (!wdev->conn)
169 return 0;
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,
181 params->bssid,
182 params->ssid, params->ssid_len,
183 NULL, 0,
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 req.prev_bssid = wdev->conn->prev_bssid;
191 req.ie = params->ie;
192 req.ie_len = params->ie_len;
193 req.use_mfp = params->mfp != NL80211_MFP_NO;
194 req.crypto = params->crypto;
195 req.flags = params->flags;
196 req.ht_capa = params->ht_capa;
197 req.ht_capa_mask = params->ht_capa_mask;
198 req.vht_capa = params->vht_capa;
199 req.vht_capa_mask = params->vht_capa_mask;
201 err = __cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
202 params->bssid, params->ssid,
203 params->ssid_len, &req);
204 if (err)
205 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
206 NULL, 0,
207 WLAN_REASON_DEAUTH_LEAVING,
208 false);
209 return err;
210 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
211 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
212 NULL, 0,
213 WLAN_REASON_DEAUTH_LEAVING, false);
214 /* return an error so that we call __cfg80211_connect_result() */
215 return -EINVAL;
216 default:
217 return 0;
221 void cfg80211_conn_work(struct work_struct *work)
223 struct cfg80211_registered_device *rdev =
224 container_of(work, struct cfg80211_registered_device, conn_work);
225 struct wireless_dev *wdev;
226 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
228 rtnl_lock();
229 cfg80211_lock_rdev(rdev);
230 mutex_lock(&rdev->devlist_mtx);
231 mutex_lock(&rdev->sched_scan_mtx);
233 list_for_each_entry(wdev, &rdev->wdev_list, list) {
234 wdev_lock(wdev);
235 if (!netif_running(wdev->netdev)) {
236 wdev_unlock(wdev);
237 continue;
239 if (wdev->sme_state != CFG80211_SME_CONNECTING || !wdev->conn) {
240 wdev_unlock(wdev);
241 continue;
243 if (wdev->conn->params.bssid) {
244 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
245 bssid = bssid_buf;
247 if (cfg80211_conn_do_work(wdev))
248 __cfg80211_connect_result(
249 wdev->netdev, bssid,
250 NULL, 0, NULL, 0,
251 WLAN_STATUS_UNSPECIFIED_FAILURE,
252 false, NULL);
253 wdev_unlock(wdev);
256 mutex_unlock(&rdev->sched_scan_mtx);
257 mutex_unlock(&rdev->devlist_mtx);
258 cfg80211_unlock_rdev(rdev);
259 rtnl_unlock();
262 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
264 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
265 struct cfg80211_bss *bss;
266 u16 capa = WLAN_CAPABILITY_ESS;
268 ASSERT_WDEV_LOCK(wdev);
270 if (wdev->conn->params.privacy)
271 capa |= WLAN_CAPABILITY_PRIVACY;
273 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
274 wdev->conn->params.bssid,
275 wdev->conn->params.ssid,
276 wdev->conn->params.ssid_len,
277 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
278 capa);
279 if (!bss)
280 return NULL;
282 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
283 wdev->conn->params.bssid = wdev->conn->bssid;
284 wdev->conn->params.channel = bss->channel;
285 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
286 schedule_work(&rdev->conn_work);
288 return bss;
291 static void __cfg80211_sme_scan_done(struct net_device *dev)
293 struct wireless_dev *wdev = dev->ieee80211_ptr;
294 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
295 struct cfg80211_bss *bss;
297 ASSERT_WDEV_LOCK(wdev);
299 if (wdev->sme_state != CFG80211_SME_CONNECTING)
300 return;
302 if (!wdev->conn)
303 return;
305 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
306 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
307 return;
309 bss = cfg80211_get_conn_bss(wdev);
310 if (bss) {
311 cfg80211_put_bss(&rdev->wiphy, bss);
312 } else {
313 /* not found */
314 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
315 schedule_work(&rdev->conn_work);
316 else
317 __cfg80211_connect_result(
318 wdev->netdev,
319 wdev->conn->params.bssid,
320 NULL, 0, NULL, 0,
321 WLAN_STATUS_UNSPECIFIED_FAILURE,
322 false, NULL);
326 void cfg80211_sme_scan_done(struct net_device *dev)
328 struct wireless_dev *wdev = dev->ieee80211_ptr;
330 wdev_lock(wdev);
331 __cfg80211_sme_scan_done(dev);
332 wdev_unlock(wdev);
335 void cfg80211_sme_rx_auth(struct net_device *dev,
336 const u8 *buf, size_t len)
338 struct wireless_dev *wdev = dev->ieee80211_ptr;
339 struct wiphy *wiphy = wdev->wiphy;
340 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
341 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
342 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
344 ASSERT_WDEV_LOCK(wdev);
346 /* should only RX auth frames when connecting */
347 if (wdev->sme_state != CFG80211_SME_CONNECTING)
348 return;
350 if (WARN_ON(!wdev->conn))
351 return;
353 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
354 wdev->conn->auto_auth &&
355 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
356 /* select automatically between only open, shared, leap */
357 switch (wdev->conn->params.auth_type) {
358 case NL80211_AUTHTYPE_OPEN_SYSTEM:
359 if (wdev->connect_keys)
360 wdev->conn->params.auth_type =
361 NL80211_AUTHTYPE_SHARED_KEY;
362 else
363 wdev->conn->params.auth_type =
364 NL80211_AUTHTYPE_NETWORK_EAP;
365 break;
366 case NL80211_AUTHTYPE_SHARED_KEY:
367 wdev->conn->params.auth_type =
368 NL80211_AUTHTYPE_NETWORK_EAP;
369 break;
370 default:
371 /* huh? */
372 wdev->conn->params.auth_type =
373 NL80211_AUTHTYPE_OPEN_SYSTEM;
374 break;
376 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
377 schedule_work(&rdev->conn_work);
378 } else if (status_code != WLAN_STATUS_SUCCESS) {
379 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
380 status_code, false, NULL);
381 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
382 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
383 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
384 schedule_work(&rdev->conn_work);
388 bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
390 struct wiphy *wiphy = wdev->wiphy;
391 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
393 if (WARN_ON(!wdev->conn))
394 return false;
396 if (!wdev->conn->prev_bssid_valid)
397 return false;
400 * Some stupid APs don't accept reassoc, so we
401 * need to fall back to trying regular assoc.
403 wdev->conn->prev_bssid_valid = false;
404 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
405 schedule_work(&rdev->conn_work);
407 return true;
410 void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
412 struct wiphy *wiphy = wdev->wiphy;
413 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
415 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
416 schedule_work(&rdev->conn_work);
419 void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
420 const u8 *req_ie, size_t req_ie_len,
421 const u8 *resp_ie, size_t resp_ie_len,
422 u16 status, bool wextev,
423 struct cfg80211_bss *bss)
425 struct wireless_dev *wdev = dev->ieee80211_ptr;
426 const u8 *country_ie;
427 #ifdef CONFIG_CFG80211_WEXT
428 union iwreq_data wrqu;
429 #endif
431 ASSERT_WDEV_LOCK(wdev);
433 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
434 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
435 return;
437 if (wdev->sme_state != CFG80211_SME_CONNECTING)
438 return;
440 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
441 bssid, req_ie, req_ie_len,
442 resp_ie, resp_ie_len,
443 status, GFP_KERNEL);
445 #ifdef CONFIG_CFG80211_WEXT
446 if (wextev) {
447 if (req_ie && status == WLAN_STATUS_SUCCESS) {
448 memset(&wrqu, 0, sizeof(wrqu));
449 wrqu.data.length = req_ie_len;
450 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
453 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
454 memset(&wrqu, 0, sizeof(wrqu));
455 wrqu.data.length = resp_ie_len;
456 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
459 memset(&wrqu, 0, sizeof(wrqu));
460 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
461 if (bssid && status == WLAN_STATUS_SUCCESS) {
462 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
463 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
464 wdev->wext.prev_bssid_valid = true;
466 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
468 #endif
470 if (wdev->current_bss) {
471 cfg80211_unhold_bss(wdev->current_bss);
472 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
473 wdev->current_bss = NULL;
476 if (wdev->conn)
477 wdev->conn->state = CFG80211_CONN_IDLE;
479 if (status != WLAN_STATUS_SUCCESS) {
480 wdev->sme_state = CFG80211_SME_IDLE;
481 if (wdev->conn)
482 kfree(wdev->conn->ie);
483 kfree(wdev->conn);
484 wdev->conn = NULL;
485 kfree(wdev->connect_keys);
486 wdev->connect_keys = NULL;
487 wdev->ssid_len = 0;
488 cfg80211_put_bss(wdev->wiphy, bss);
489 return;
492 if (!bss)
493 bss = cfg80211_get_bss(wdev->wiphy,
494 wdev->conn ? wdev->conn->params.channel :
495 NULL,
496 bssid,
497 wdev->ssid, wdev->ssid_len,
498 WLAN_CAPABILITY_ESS,
499 WLAN_CAPABILITY_ESS);
501 if (WARN_ON(!bss))
502 return;
504 cfg80211_hold_bss(bss_from_pub(bss));
505 wdev->current_bss = bss_from_pub(bss);
507 wdev->sme_state = CFG80211_SME_CONNECTED;
508 cfg80211_upload_connect_keys(wdev);
510 rcu_read_lock();
511 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
512 if (!country_ie) {
513 rcu_read_unlock();
514 return;
517 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
518 rcu_read_unlock();
520 if (!country_ie)
521 return;
524 * ieee80211_bss_get_ie() ensures we can access:
525 * - country_ie + 2, the start of the country ie data, and
526 * - and country_ie[1] which is the IE length
528 regulatory_hint_11d(wdev->wiphy, bss->channel->band,
529 country_ie + 2, country_ie[1]);
530 kfree(country_ie);
533 void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
534 const u8 *req_ie, size_t req_ie_len,
535 const u8 *resp_ie, size_t resp_ie_len,
536 u16 status, gfp_t gfp)
538 struct wireless_dev *wdev = dev->ieee80211_ptr;
539 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
540 struct cfg80211_event *ev;
541 unsigned long flags;
543 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
545 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
546 if (!ev)
547 return;
549 ev->type = EVENT_CONNECT_RESULT;
550 if (bssid)
551 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
552 if (req_ie_len) {
553 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
554 ev->cr.req_ie_len = req_ie_len;
555 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
557 if (resp_ie_len) {
558 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
559 ev->cr.resp_ie_len = resp_ie_len;
560 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
562 ev->cr.status = status;
564 spin_lock_irqsave(&wdev->event_lock, flags);
565 list_add_tail(&ev->list, &wdev->event_list);
566 spin_unlock_irqrestore(&wdev->event_lock, flags);
567 queue_work(cfg80211_wq, &rdev->event_work);
569 EXPORT_SYMBOL(cfg80211_connect_result);
571 void __cfg80211_roamed(struct wireless_dev *wdev,
572 struct cfg80211_bss *bss,
573 const u8 *req_ie, size_t req_ie_len,
574 const u8 *resp_ie, size_t resp_ie_len)
576 #ifdef CONFIG_CFG80211_WEXT
577 union iwreq_data wrqu;
578 #endif
579 ASSERT_WDEV_LOCK(wdev);
581 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
582 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
583 goto out;
585 if (wdev->sme_state != CFG80211_SME_CONNECTED)
586 goto out;
588 /* internal error -- how did we get to CONNECTED w/o BSS? */
589 if (WARN_ON(!wdev->current_bss)) {
590 goto out;
593 cfg80211_unhold_bss(wdev->current_bss);
594 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
595 wdev->current_bss = NULL;
597 cfg80211_hold_bss(bss_from_pub(bss));
598 wdev->current_bss = bss_from_pub(bss);
600 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
601 req_ie, req_ie_len, resp_ie, resp_ie_len,
602 GFP_KERNEL);
604 #ifdef CONFIG_CFG80211_WEXT
605 if (req_ie) {
606 memset(&wrqu, 0, sizeof(wrqu));
607 wrqu.data.length = req_ie_len;
608 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
609 &wrqu, req_ie);
612 if (resp_ie) {
613 memset(&wrqu, 0, sizeof(wrqu));
614 wrqu.data.length = resp_ie_len;
615 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
616 &wrqu, resp_ie);
619 memset(&wrqu, 0, sizeof(wrqu));
620 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
621 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
622 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
623 wdev->wext.prev_bssid_valid = true;
624 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
625 #endif
627 return;
628 out:
629 cfg80211_put_bss(wdev->wiphy, bss);
632 void cfg80211_roamed(struct net_device *dev,
633 struct ieee80211_channel *channel,
634 const u8 *bssid,
635 const u8 *req_ie, size_t req_ie_len,
636 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
638 struct wireless_dev *wdev = dev->ieee80211_ptr;
639 struct cfg80211_bss *bss;
641 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
643 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
644 wdev->ssid_len, WLAN_CAPABILITY_ESS,
645 WLAN_CAPABILITY_ESS);
646 if (WARN_ON(!bss))
647 return;
649 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
650 resp_ie_len, gfp);
652 EXPORT_SYMBOL(cfg80211_roamed);
654 void cfg80211_roamed_bss(struct net_device *dev,
655 struct cfg80211_bss *bss, const u8 *req_ie,
656 size_t req_ie_len, const u8 *resp_ie,
657 size_t resp_ie_len, gfp_t gfp)
659 struct wireless_dev *wdev = dev->ieee80211_ptr;
660 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
661 struct cfg80211_event *ev;
662 unsigned long flags;
664 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
666 if (WARN_ON(!bss))
667 return;
669 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
670 if (!ev) {
671 cfg80211_put_bss(wdev->wiphy, bss);
672 return;
675 ev->type = EVENT_ROAMED;
676 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
677 ev->rm.req_ie_len = req_ie_len;
678 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
679 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
680 ev->rm.resp_ie_len = resp_ie_len;
681 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
682 ev->rm.bss = bss;
684 spin_lock_irqsave(&wdev->event_lock, flags);
685 list_add_tail(&ev->list, &wdev->event_list);
686 spin_unlock_irqrestore(&wdev->event_lock, flags);
687 queue_work(cfg80211_wq, &rdev->event_work);
689 EXPORT_SYMBOL(cfg80211_roamed_bss);
691 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
692 size_t ie_len, u16 reason, bool from_ap)
694 struct wireless_dev *wdev = dev->ieee80211_ptr;
695 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
696 int i;
697 #ifdef CONFIG_CFG80211_WEXT
698 union iwreq_data wrqu;
699 #endif
701 ASSERT_WDEV_LOCK(wdev);
703 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
704 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
705 return;
707 if (wdev->sme_state != CFG80211_SME_CONNECTED)
708 return;
710 if (wdev->current_bss) {
711 cfg80211_unhold_bss(wdev->current_bss);
712 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
715 wdev->current_bss = NULL;
716 wdev->sme_state = CFG80211_SME_IDLE;
717 wdev->ssid_len = 0;
719 if (wdev->conn) {
720 kfree(wdev->conn->ie);
721 wdev->conn->ie = NULL;
722 kfree(wdev->conn);
723 wdev->conn = NULL;
726 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
729 * Delete all the keys ... pairwise keys can't really
730 * exist any more anyway, but default keys might.
732 if (rdev->ops->del_key)
733 for (i = 0; i < 6; i++)
734 rdev_del_key(rdev, dev, i, false, NULL);
736 #ifdef CONFIG_CFG80211_WEXT
737 memset(&wrqu, 0, sizeof(wrqu));
738 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
739 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
740 wdev->wext.connect.ssid_len = 0;
741 #endif
743 schedule_work(&cfg80211_disconnect_work);
746 void cfg80211_disconnected(struct net_device *dev, u16 reason,
747 u8 *ie, size_t ie_len, gfp_t gfp)
749 struct wireless_dev *wdev = dev->ieee80211_ptr;
750 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
751 struct cfg80211_event *ev;
752 unsigned long flags;
754 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
756 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
757 if (!ev)
758 return;
760 ev->type = EVENT_DISCONNECTED;
761 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
762 ev->dc.ie_len = ie_len;
763 memcpy((void *)ev->dc.ie, ie, ie_len);
764 ev->dc.reason = reason;
766 spin_lock_irqsave(&wdev->event_lock, flags);
767 list_add_tail(&ev->list, &wdev->event_list);
768 spin_unlock_irqrestore(&wdev->event_lock, flags);
769 queue_work(cfg80211_wq, &rdev->event_work);
771 EXPORT_SYMBOL(cfg80211_disconnected);
773 int __cfg80211_connect(struct cfg80211_registered_device *rdev,
774 struct net_device *dev,
775 struct cfg80211_connect_params *connect,
776 struct cfg80211_cached_keys *connkeys,
777 const u8 *prev_bssid)
779 struct wireless_dev *wdev = dev->ieee80211_ptr;
780 struct cfg80211_bss *bss = NULL;
781 int err;
783 ASSERT_WDEV_LOCK(wdev);
785 if (wdev->sme_state != CFG80211_SME_IDLE)
786 return -EALREADY;
788 if (WARN_ON(wdev->connect_keys)) {
789 kfree(wdev->connect_keys);
790 wdev->connect_keys = NULL;
793 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
794 rdev->wiphy.ht_capa_mod_mask);
796 if (connkeys && connkeys->def >= 0) {
797 int idx;
798 u32 cipher;
800 idx = connkeys->def;
801 cipher = connkeys->params[idx].cipher;
802 /* If given a WEP key we may need it for shared key auth */
803 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
804 cipher == WLAN_CIPHER_SUITE_WEP104) {
805 connect->key_idx = idx;
806 connect->key = connkeys->params[idx].key;
807 connect->key_len = connkeys->params[idx].key_len;
810 * If ciphers are not set (e.g. when going through
811 * iwconfig), we have to set them appropriately here.
813 if (connect->crypto.cipher_group == 0)
814 connect->crypto.cipher_group = cipher;
816 if (connect->crypto.n_ciphers_pairwise == 0) {
817 connect->crypto.n_ciphers_pairwise = 1;
818 connect->crypto.ciphers_pairwise[0] = cipher;
823 if (!rdev->ops->connect) {
824 if (!rdev->ops->auth || !rdev->ops->assoc)
825 return -EOPNOTSUPP;
827 if (WARN_ON(wdev->conn))
828 return -EINPROGRESS;
830 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
831 if (!wdev->conn)
832 return -ENOMEM;
835 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
837 memcpy(&wdev->conn->params, connect, sizeof(*connect));
838 if (connect->bssid) {
839 wdev->conn->params.bssid = wdev->conn->bssid;
840 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
843 if (connect->ie) {
844 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
845 GFP_KERNEL);
846 wdev->conn->params.ie = wdev->conn->ie;
847 if (!wdev->conn->ie) {
848 kfree(wdev->conn);
849 wdev->conn = NULL;
850 return -ENOMEM;
854 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
855 wdev->conn->auto_auth = true;
856 /* start with open system ... should mostly work */
857 wdev->conn->params.auth_type =
858 NL80211_AUTHTYPE_OPEN_SYSTEM;
859 } else {
860 wdev->conn->auto_auth = false;
863 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
864 wdev->ssid_len = connect->ssid_len;
865 wdev->conn->params.ssid = wdev->ssid;
866 wdev->conn->params.ssid_len = connect->ssid_len;
868 /* see if we have the bss already */
869 bss = cfg80211_get_conn_bss(wdev);
871 wdev->sme_state = CFG80211_SME_CONNECTING;
872 wdev->connect_keys = connkeys;
874 if (prev_bssid) {
875 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
876 wdev->conn->prev_bssid_valid = true;
879 /* we're good if we have a matching bss struct */
880 if (bss) {
881 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
882 err = cfg80211_conn_do_work(wdev);
883 cfg80211_put_bss(wdev->wiphy, bss);
884 } else {
885 /* otherwise we'll need to scan for the AP first */
886 err = cfg80211_conn_scan(wdev);
888 * If we can't scan right now, then we need to scan again
889 * after the current scan finished, since the parameters
890 * changed (unless we find a good AP anyway).
892 if (err == -EBUSY) {
893 err = 0;
894 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
897 if (err) {
898 kfree(wdev->conn->ie);
899 kfree(wdev->conn);
900 wdev->conn = NULL;
901 wdev->sme_state = CFG80211_SME_IDLE;
902 wdev->connect_keys = NULL;
903 wdev->ssid_len = 0;
906 return err;
907 } else {
908 wdev->sme_state = CFG80211_SME_CONNECTING;
909 wdev->connect_keys = connkeys;
910 err = rdev_connect(rdev, dev, connect);
911 if (err) {
912 wdev->connect_keys = NULL;
913 wdev->sme_state = CFG80211_SME_IDLE;
914 return err;
917 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
918 wdev->ssid_len = connect->ssid_len;
920 return 0;
924 int cfg80211_connect(struct cfg80211_registered_device *rdev,
925 struct net_device *dev,
926 struct cfg80211_connect_params *connect,
927 struct cfg80211_cached_keys *connkeys)
929 int err;
931 mutex_lock(&rdev->devlist_mtx);
932 /* might request scan - scan_mtx -> wdev_mtx dependency */
933 mutex_lock(&rdev->sched_scan_mtx);
934 wdev_lock(dev->ieee80211_ptr);
935 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
936 wdev_unlock(dev->ieee80211_ptr);
937 mutex_unlock(&rdev->sched_scan_mtx);
938 mutex_unlock(&rdev->devlist_mtx);
940 return err;
943 int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
944 struct net_device *dev, u16 reason, bool wextev)
946 struct wireless_dev *wdev = dev->ieee80211_ptr;
947 int err;
949 ASSERT_WDEV_LOCK(wdev);
951 if (wdev->sme_state == CFG80211_SME_IDLE)
952 return -EINVAL;
954 kfree(wdev->connect_keys);
955 wdev->connect_keys = NULL;
957 if (!rdev->ops->disconnect) {
958 if (!rdev->ops->deauth)
959 return -EOPNOTSUPP;
961 /* was it connected by userspace SME? */
962 if (!wdev->conn) {
963 cfg80211_mlme_down(rdev, dev);
964 goto disconnect;
967 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
968 (wdev->conn->state == CFG80211_CONN_SCANNING ||
969 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
970 wdev->sme_state = CFG80211_SME_IDLE;
971 kfree(wdev->conn->ie);
972 kfree(wdev->conn);
973 wdev->conn = NULL;
974 wdev->ssid_len = 0;
975 return 0;
978 /* wdev->conn->params.bssid must be set if > SCANNING */
979 err = __cfg80211_mlme_deauth(rdev, dev,
980 wdev->conn->params.bssid,
981 NULL, 0, reason, false);
982 if (err)
983 return err;
984 } else {
985 err = rdev_disconnect(rdev, dev, reason);
986 if (err)
987 return err;
990 disconnect:
991 if (wdev->sme_state == CFG80211_SME_CONNECTED)
992 __cfg80211_disconnected(dev, NULL, 0, 0, false);
993 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
994 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
995 WLAN_STATUS_UNSPECIFIED_FAILURE,
996 wextev, NULL);
998 return 0;
1001 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1002 struct net_device *dev,
1003 u16 reason, bool wextev)
1005 int err;
1007 wdev_lock(dev->ieee80211_ptr);
1008 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
1009 wdev_unlock(dev->ieee80211_ptr);
1011 return err;
1014 void cfg80211_sme_disassoc(struct net_device *dev,
1015 struct cfg80211_internal_bss *bss)
1017 struct wireless_dev *wdev = dev->ieee80211_ptr;
1018 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1019 u8 bssid[ETH_ALEN];
1021 ASSERT_WDEV_LOCK(wdev);
1023 if (!wdev->conn)
1024 return;
1026 if (wdev->conn->state == CFG80211_CONN_IDLE)
1027 return;
1030 * Ok, so the association was made by this SME -- we don't
1031 * want it any more so deauthenticate too.
1034 memcpy(bssid, bss->pub.bssid, ETH_ALEN);
1036 __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
1037 WLAN_REASON_DEAUTH_LEAVING, false);