Linux 3.12.28
[linux/fpc-iii.git] / net / wireless / sme.c
blob2f844eec9c6d1416323d888100925bea9b5d2d04
1 /*
2 * SME code for cfg80211
3 * both driver SME event handling and the SME implementation
4 * (for nl80211's connect() and wext)
6 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (C) 2009 Intel Corporation. All rights reserved.
8 */
10 #include <linux/etherdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/slab.h>
13 #include <linux/workqueue.h>
14 #include <linux/wireless.h>
15 #include <linux/export.h>
16 #include <net/iw_handler.h>
17 #include <net/cfg80211.h>
18 #include <net/rtnetlink.h>
19 #include "nl80211.h"
20 #include "reg.h"
21 #include "rdev-ops.h"
24 * Software SME in cfg80211, using auth/assoc/deauth calls to the
25 * driver. This is is for implementing nl80211's connect/disconnect
26 * and wireless extensions (if configured.)
29 struct cfg80211_conn {
30 struct cfg80211_connect_params params;
31 /* these are sub-states of the _CONNECTING sme_state */
32 enum {
33 CFG80211_CONN_SCANNING,
34 CFG80211_CONN_SCAN_AGAIN,
35 CFG80211_CONN_AUTHENTICATE_NEXT,
36 CFG80211_CONN_AUTHENTICATING,
37 CFG80211_CONN_AUTH_FAILED,
38 CFG80211_CONN_ASSOCIATE_NEXT,
39 CFG80211_CONN_ASSOCIATING,
40 CFG80211_CONN_ASSOC_FAILED,
41 CFG80211_CONN_DEAUTH,
42 CFG80211_CONN_CONNECTED,
43 } state;
44 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
45 u8 *ie;
46 size_t ie_len;
47 bool auto_auth, prev_bssid_valid;
50 static void cfg80211_sme_free(struct wireless_dev *wdev)
52 if (!wdev->conn)
53 return;
55 kfree(wdev->conn->ie);
56 kfree(wdev->conn);
57 wdev->conn = NULL;
60 static int cfg80211_conn_scan(struct wireless_dev *wdev)
62 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
63 struct cfg80211_scan_request *request;
64 int n_channels, err;
66 ASSERT_RTNL();
67 ASSERT_RDEV_LOCK(rdev);
68 ASSERT_WDEV_LOCK(wdev);
70 if (rdev->scan_req)
71 return -EBUSY;
73 if (wdev->conn->params.channel) {
74 n_channels = 1;
75 } else {
76 enum ieee80211_band band;
77 n_channels = 0;
79 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
80 if (!wdev->wiphy->bands[band])
81 continue;
82 n_channels += wdev->wiphy->bands[band]->n_channels;
85 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
86 sizeof(request->channels[0]) * n_channels,
87 GFP_KERNEL);
88 if (!request)
89 return -ENOMEM;
91 if (wdev->conn->params.channel)
92 request->channels[0] = wdev->conn->params.channel;
93 else {
94 int i = 0, j;
95 enum ieee80211_band band;
96 struct ieee80211_supported_band *bands;
97 struct ieee80211_channel *channel;
99 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
100 bands = wdev->wiphy->bands[band];
101 if (!bands)
102 continue;
103 for (j = 0; j < bands->n_channels; j++) {
104 channel = &bands->channels[j];
105 if (channel->flags & IEEE80211_CHAN_DISABLED)
106 continue;
107 request->channels[i++] = channel;
109 request->rates[band] = (1 << bands->n_bitrates) - 1;
111 n_channels = i;
113 request->n_channels = n_channels;
114 request->ssids = (void *)&request->channels[n_channels];
115 request->n_ssids = 1;
117 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
118 wdev->conn->params.ssid_len);
119 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
121 request->wdev = wdev;
122 request->wiphy = &rdev->wiphy;
123 request->scan_start = jiffies;
125 rdev->scan_req = request;
127 err = rdev_scan(rdev, request);
128 if (!err) {
129 wdev->conn->state = CFG80211_CONN_SCANNING;
130 nl80211_send_scan_start(rdev, wdev);
131 dev_hold(wdev->netdev);
132 } else {
133 rdev->scan_req = NULL;
134 kfree(request);
136 return err;
139 static int cfg80211_conn_do_work(struct wireless_dev *wdev)
141 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
142 struct cfg80211_connect_params *params;
143 struct cfg80211_assoc_request req = {};
144 int err;
146 ASSERT_WDEV_LOCK(wdev);
148 if (!wdev->conn)
149 return 0;
151 params = &wdev->conn->params;
153 switch (wdev->conn->state) {
154 case CFG80211_CONN_SCANNING:
155 /* didn't find it during scan ... */
156 return -ENOENT;
157 case CFG80211_CONN_SCAN_AGAIN:
158 return cfg80211_conn_scan(wdev);
159 case CFG80211_CONN_AUTHENTICATE_NEXT:
160 BUG_ON(!rdev->ops->auth);
161 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
162 return cfg80211_mlme_auth(rdev, wdev->netdev,
163 params->channel, params->auth_type,
164 params->bssid,
165 params->ssid, params->ssid_len,
166 NULL, 0,
167 params->key, params->key_len,
168 params->key_idx, NULL, 0);
169 case CFG80211_CONN_AUTH_FAILED:
170 return -ENOTCONN;
171 case CFG80211_CONN_ASSOCIATE_NEXT:
172 BUG_ON(!rdev->ops->assoc);
173 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
174 if (wdev->conn->prev_bssid_valid)
175 req.prev_bssid = wdev->conn->prev_bssid;
176 req.ie = params->ie;
177 req.ie_len = params->ie_len;
178 req.use_mfp = params->mfp != NL80211_MFP_NO;
179 req.crypto = params->crypto;
180 req.flags = params->flags;
181 req.ht_capa = params->ht_capa;
182 req.ht_capa_mask = params->ht_capa_mask;
183 req.vht_capa = params->vht_capa;
184 req.vht_capa_mask = params->vht_capa_mask;
186 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
187 params->bssid, params->ssid,
188 params->ssid_len, &req);
189 if (err)
190 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
191 NULL, 0,
192 WLAN_REASON_DEAUTH_LEAVING,
193 false);
194 return err;
195 case CFG80211_CONN_ASSOC_FAILED:
196 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
197 NULL, 0,
198 WLAN_REASON_DEAUTH_LEAVING, false);
199 return -ENOTCONN;
200 case CFG80211_CONN_DEAUTH:
201 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 NULL, 0,
203 WLAN_REASON_DEAUTH_LEAVING, false);
204 /* free directly, disconnected event already sent */
205 cfg80211_sme_free(wdev);
206 return 0;
207 default:
208 return 0;
212 void cfg80211_conn_work(struct work_struct *work)
214 struct cfg80211_registered_device *rdev =
215 container_of(work, struct cfg80211_registered_device, conn_work);
216 struct wireless_dev *wdev;
217 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
219 rtnl_lock();
221 list_for_each_entry(wdev, &rdev->wdev_list, list) {
222 if (!wdev->netdev)
223 continue;
225 wdev_lock(wdev);
226 if (!netif_running(wdev->netdev)) {
227 wdev_unlock(wdev);
228 continue;
230 if (!wdev->conn ||
231 wdev->conn->state == CFG80211_CONN_CONNECTED) {
232 wdev_unlock(wdev);
233 continue;
235 if (wdev->conn->params.bssid) {
236 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
237 bssid = bssid_buf;
239 if (cfg80211_conn_do_work(wdev)) {
240 __cfg80211_connect_result(
241 wdev->netdev, bssid,
242 NULL, 0, NULL, 0,
243 WLAN_STATUS_UNSPECIFIED_FAILURE,
244 false, NULL);
246 wdev_unlock(wdev);
249 rtnl_unlock();
252 /* Returned bss is reference counted and must be cleaned up appropriately. */
253 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
255 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
256 struct cfg80211_bss *bss;
257 u16 capa = WLAN_CAPABILITY_ESS;
259 ASSERT_WDEV_LOCK(wdev);
261 if (wdev->conn->params.privacy)
262 capa |= WLAN_CAPABILITY_PRIVACY;
264 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
265 wdev->conn->params.bssid,
266 wdev->conn->params.ssid,
267 wdev->conn->params.ssid_len,
268 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
269 capa);
270 if (!bss)
271 return NULL;
273 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
274 wdev->conn->params.bssid = wdev->conn->bssid;
275 wdev->conn->params.channel = bss->channel;
276 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
277 schedule_work(&rdev->conn_work);
279 return bss;
282 static void __cfg80211_sme_scan_done(struct net_device *dev)
284 struct wireless_dev *wdev = dev->ieee80211_ptr;
285 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
286 struct cfg80211_bss *bss;
288 ASSERT_WDEV_LOCK(wdev);
290 if (!wdev->conn)
291 return;
293 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
294 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
295 return;
297 bss = cfg80211_get_conn_bss(wdev);
298 if (bss)
299 cfg80211_put_bss(&rdev->wiphy, bss);
300 else
301 schedule_work(&rdev->conn_work);
304 void cfg80211_sme_scan_done(struct net_device *dev)
306 struct wireless_dev *wdev = dev->ieee80211_ptr;
308 wdev_lock(wdev);
309 __cfg80211_sme_scan_done(dev);
310 wdev_unlock(wdev);
313 void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
315 struct wiphy *wiphy = wdev->wiphy;
316 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
317 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
318 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
320 ASSERT_WDEV_LOCK(wdev);
322 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
323 return;
325 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
326 wdev->conn->auto_auth &&
327 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
328 /* select automatically between only open, shared, leap */
329 switch (wdev->conn->params.auth_type) {
330 case NL80211_AUTHTYPE_OPEN_SYSTEM:
331 if (wdev->connect_keys)
332 wdev->conn->params.auth_type =
333 NL80211_AUTHTYPE_SHARED_KEY;
334 else
335 wdev->conn->params.auth_type =
336 NL80211_AUTHTYPE_NETWORK_EAP;
337 break;
338 case NL80211_AUTHTYPE_SHARED_KEY:
339 wdev->conn->params.auth_type =
340 NL80211_AUTHTYPE_NETWORK_EAP;
341 break;
342 default:
343 /* huh? */
344 wdev->conn->params.auth_type =
345 NL80211_AUTHTYPE_OPEN_SYSTEM;
346 break;
348 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
349 schedule_work(&rdev->conn_work);
350 } else if (status_code != WLAN_STATUS_SUCCESS) {
351 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
352 NULL, 0, NULL, 0,
353 status_code, false, NULL);
354 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
355 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
356 schedule_work(&rdev->conn_work);
360 bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
362 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
364 if (!wdev->conn)
365 return false;
367 if (status == WLAN_STATUS_SUCCESS) {
368 wdev->conn->state = CFG80211_CONN_CONNECTED;
369 return false;
372 if (wdev->conn->prev_bssid_valid) {
374 * Some stupid APs don't accept reassoc, so we
375 * need to fall back to trying regular assoc;
376 * return true so no event is sent to userspace.
378 wdev->conn->prev_bssid_valid = false;
379 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
380 schedule_work(&rdev->conn_work);
381 return true;
384 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
385 schedule_work(&rdev->conn_work);
386 return false;
389 void cfg80211_sme_deauth(struct wireless_dev *wdev)
391 cfg80211_sme_free(wdev);
394 void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
396 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
398 if (!wdev->conn)
399 return;
401 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
402 schedule_work(&rdev->conn_work);
405 void cfg80211_sme_disassoc(struct wireless_dev *wdev)
407 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
409 if (!wdev->conn)
410 return;
412 wdev->conn->state = CFG80211_CONN_DEAUTH;
413 schedule_work(&rdev->conn_work);
416 void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
418 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
420 if (!wdev->conn)
421 return;
423 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
424 schedule_work(&rdev->conn_work);
427 static int cfg80211_sme_connect(struct wireless_dev *wdev,
428 struct cfg80211_connect_params *connect,
429 const u8 *prev_bssid)
431 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
432 struct cfg80211_bss *bss;
433 int err;
435 if (!rdev->ops->auth || !rdev->ops->assoc)
436 return -EOPNOTSUPP;
438 if (wdev->current_bss)
439 return -EALREADY;
441 if (WARN_ON(wdev->conn))
442 return -EINPROGRESS;
444 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
445 if (!wdev->conn)
446 return -ENOMEM;
449 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
451 memcpy(&wdev->conn->params, connect, sizeof(*connect));
452 if (connect->bssid) {
453 wdev->conn->params.bssid = wdev->conn->bssid;
454 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
457 if (connect->ie) {
458 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
459 GFP_KERNEL);
460 wdev->conn->params.ie = wdev->conn->ie;
461 if (!wdev->conn->ie) {
462 kfree(wdev->conn);
463 wdev->conn = NULL;
464 return -ENOMEM;
468 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
469 wdev->conn->auto_auth = true;
470 /* start with open system ... should mostly work */
471 wdev->conn->params.auth_type =
472 NL80211_AUTHTYPE_OPEN_SYSTEM;
473 } else {
474 wdev->conn->auto_auth = false;
477 wdev->conn->params.ssid = wdev->ssid;
478 wdev->conn->params.ssid_len = connect->ssid_len;
480 /* see if we have the bss already */
481 bss = cfg80211_get_conn_bss(wdev);
483 if (prev_bssid) {
484 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
485 wdev->conn->prev_bssid_valid = true;
488 /* we're good if we have a matching bss struct */
489 if (bss) {
490 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
491 err = cfg80211_conn_do_work(wdev);
492 cfg80211_put_bss(wdev->wiphy, bss);
493 } else {
494 /* otherwise we'll need to scan for the AP first */
495 err = cfg80211_conn_scan(wdev);
498 * If we can't scan right now, then we need to scan again
499 * after the current scan finished, since the parameters
500 * changed (unless we find a good AP anyway).
502 if (err == -EBUSY) {
503 err = 0;
504 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
508 if (err)
509 cfg80211_sme_free(wdev);
511 return err;
514 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
516 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
517 int err;
519 if (!wdev->conn)
520 return 0;
522 if (!rdev->ops->deauth)
523 return -EOPNOTSUPP;
525 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
526 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
527 err = 0;
528 goto out;
531 /* wdev->conn->params.bssid must be set if > SCANNING */
532 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
533 wdev->conn->params.bssid,
534 NULL, 0, reason, false);
535 out:
536 cfg80211_sme_free(wdev);
537 return err;
541 * code shared for in-device and software SME
544 static bool cfg80211_is_all_idle(void)
546 struct cfg80211_registered_device *rdev;
547 struct wireless_dev *wdev;
548 bool is_all_idle = true;
551 * All devices must be idle as otherwise if you are actively
552 * scanning some new beacon hints could be learned and would
553 * count as new regulatory hints.
555 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
556 list_for_each_entry(wdev, &rdev->wdev_list, list) {
557 wdev_lock(wdev);
558 if (wdev->conn || wdev->current_bss)
559 is_all_idle = false;
560 wdev_unlock(wdev);
564 return is_all_idle;
567 static void disconnect_work(struct work_struct *work)
569 rtnl_lock();
570 if (cfg80211_is_all_idle())
571 regulatory_hint_disconnect();
572 rtnl_unlock();
575 static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
579 * API calls for drivers implementing connect/disconnect and
580 * SME event handling
583 /* This method must consume bss one way or another */
584 void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
585 const u8 *req_ie, size_t req_ie_len,
586 const u8 *resp_ie, size_t resp_ie_len,
587 u16 status, bool wextev,
588 struct cfg80211_bss *bss)
590 struct wireless_dev *wdev = dev->ieee80211_ptr;
591 const u8 *country_ie;
592 #ifdef CONFIG_CFG80211_WEXT
593 union iwreq_data wrqu;
594 #endif
596 ASSERT_WDEV_LOCK(wdev);
598 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
599 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
600 cfg80211_put_bss(wdev->wiphy, bss);
601 return;
604 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
605 bssid, req_ie, req_ie_len,
606 resp_ie, resp_ie_len,
607 status, GFP_KERNEL);
609 #ifdef CONFIG_CFG80211_WEXT
610 if (wextev) {
611 if (req_ie && status == WLAN_STATUS_SUCCESS) {
612 memset(&wrqu, 0, sizeof(wrqu));
613 wrqu.data.length = req_ie_len;
614 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
617 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
618 memset(&wrqu, 0, sizeof(wrqu));
619 wrqu.data.length = resp_ie_len;
620 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
623 memset(&wrqu, 0, sizeof(wrqu));
624 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
625 if (bssid && status == WLAN_STATUS_SUCCESS) {
626 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
627 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
628 wdev->wext.prev_bssid_valid = true;
630 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
632 #endif
634 if (wdev->current_bss) {
635 cfg80211_unhold_bss(wdev->current_bss);
636 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
637 wdev->current_bss = NULL;
640 if (status != WLAN_STATUS_SUCCESS) {
641 kfree(wdev->connect_keys);
642 wdev->connect_keys = NULL;
643 wdev->ssid_len = 0;
644 if (bss) {
645 cfg80211_unhold_bss(bss_from_pub(bss));
646 cfg80211_put_bss(wdev->wiphy, bss);
648 cfg80211_sme_free(wdev);
649 return;
652 if (!bss) {
653 WARN_ON_ONCE(!wiphy_to_dev(wdev->wiphy)->ops->connect);
654 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
655 wdev->ssid, wdev->ssid_len,
656 WLAN_CAPABILITY_ESS,
657 WLAN_CAPABILITY_ESS);
658 if (WARN_ON(!bss))
659 return;
660 cfg80211_hold_bss(bss_from_pub(bss));
663 wdev->current_bss = bss_from_pub(bss);
665 cfg80211_upload_connect_keys(wdev);
667 rcu_read_lock();
668 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
669 if (!country_ie) {
670 rcu_read_unlock();
671 return;
674 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
675 rcu_read_unlock();
677 if (!country_ie)
678 return;
681 * ieee80211_bss_get_ie() ensures we can access:
682 * - country_ie + 2, the start of the country ie data, and
683 * - and country_ie[1] which is the IE length
685 regulatory_hint_11d(wdev->wiphy, bss->channel->band,
686 country_ie + 2, country_ie[1]);
687 kfree(country_ie);
690 void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
691 const u8 *req_ie, size_t req_ie_len,
692 const u8 *resp_ie, size_t resp_ie_len,
693 u16 status, gfp_t gfp)
695 struct wireless_dev *wdev = dev->ieee80211_ptr;
696 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
697 struct cfg80211_event *ev;
698 unsigned long flags;
700 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
701 if (!ev)
702 return;
704 ev->type = EVENT_CONNECT_RESULT;
705 if (bssid)
706 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
707 if (req_ie_len) {
708 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
709 ev->cr.req_ie_len = req_ie_len;
710 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
712 if (resp_ie_len) {
713 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
714 ev->cr.resp_ie_len = resp_ie_len;
715 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
717 ev->cr.status = status;
719 spin_lock_irqsave(&wdev->event_lock, flags);
720 list_add_tail(&ev->list, &wdev->event_list);
721 spin_unlock_irqrestore(&wdev->event_lock, flags);
722 queue_work(cfg80211_wq, &rdev->event_work);
724 EXPORT_SYMBOL(cfg80211_connect_result);
726 /* Consumes bss object one way or another */
727 void __cfg80211_roamed(struct wireless_dev *wdev,
728 struct cfg80211_bss *bss,
729 const u8 *req_ie, size_t req_ie_len,
730 const u8 *resp_ie, size_t resp_ie_len)
732 #ifdef CONFIG_CFG80211_WEXT
733 union iwreq_data wrqu;
734 #endif
735 ASSERT_WDEV_LOCK(wdev);
737 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
738 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
739 goto out;
741 if (WARN_ON(!wdev->current_bss))
742 goto out;
744 cfg80211_unhold_bss(wdev->current_bss);
745 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
746 wdev->current_bss = NULL;
748 cfg80211_hold_bss(bss_from_pub(bss));
749 wdev->current_bss = bss_from_pub(bss);
751 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
752 req_ie, req_ie_len, resp_ie, resp_ie_len,
753 GFP_KERNEL);
755 #ifdef CONFIG_CFG80211_WEXT
756 if (req_ie) {
757 memset(&wrqu, 0, sizeof(wrqu));
758 wrqu.data.length = req_ie_len;
759 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
760 &wrqu, req_ie);
763 if (resp_ie) {
764 memset(&wrqu, 0, sizeof(wrqu));
765 wrqu.data.length = resp_ie_len;
766 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
767 &wrqu, resp_ie);
770 memset(&wrqu, 0, sizeof(wrqu));
771 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
772 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
773 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
774 wdev->wext.prev_bssid_valid = true;
775 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
776 #endif
778 return;
779 out:
780 cfg80211_put_bss(wdev->wiphy, bss);
783 void cfg80211_roamed(struct net_device *dev,
784 struct ieee80211_channel *channel,
785 const u8 *bssid,
786 const u8 *req_ie, size_t req_ie_len,
787 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
789 struct wireless_dev *wdev = dev->ieee80211_ptr;
790 struct cfg80211_bss *bss;
792 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
793 wdev->ssid_len, WLAN_CAPABILITY_ESS,
794 WLAN_CAPABILITY_ESS);
795 if (WARN_ON(!bss))
796 return;
798 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
799 resp_ie_len, gfp);
801 EXPORT_SYMBOL(cfg80211_roamed);
803 /* Consumes bss object one way or another */
804 void cfg80211_roamed_bss(struct net_device *dev,
805 struct cfg80211_bss *bss, const u8 *req_ie,
806 size_t req_ie_len, const u8 *resp_ie,
807 size_t resp_ie_len, gfp_t gfp)
809 struct wireless_dev *wdev = dev->ieee80211_ptr;
810 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
811 struct cfg80211_event *ev;
812 unsigned long flags;
814 if (WARN_ON(!bss))
815 return;
817 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
818 if (!ev) {
819 cfg80211_put_bss(wdev->wiphy, bss);
820 return;
823 ev->type = EVENT_ROAMED;
824 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
825 ev->rm.req_ie_len = req_ie_len;
826 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
827 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
828 ev->rm.resp_ie_len = resp_ie_len;
829 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
830 ev->rm.bss = bss;
832 spin_lock_irqsave(&wdev->event_lock, flags);
833 list_add_tail(&ev->list, &wdev->event_list);
834 spin_unlock_irqrestore(&wdev->event_lock, flags);
835 queue_work(cfg80211_wq, &rdev->event_work);
837 EXPORT_SYMBOL(cfg80211_roamed_bss);
839 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
840 size_t ie_len, u16 reason, bool from_ap)
842 struct wireless_dev *wdev = dev->ieee80211_ptr;
843 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
844 int i;
845 #ifdef CONFIG_CFG80211_WEXT
846 union iwreq_data wrqu;
847 #endif
849 ASSERT_WDEV_LOCK(wdev);
851 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
852 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
853 return;
855 if (wdev->current_bss) {
856 cfg80211_unhold_bss(wdev->current_bss);
857 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
860 wdev->current_bss = NULL;
861 wdev->ssid_len = 0;
863 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
866 * Delete all the keys ... pairwise keys can't really
867 * exist any more anyway, but default keys might.
869 if (rdev->ops->del_key)
870 for (i = 0; i < 6; i++)
871 rdev_del_key(rdev, dev, i, false, NULL);
873 #ifdef CONFIG_CFG80211_WEXT
874 memset(&wrqu, 0, sizeof(wrqu));
875 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
876 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
877 wdev->wext.connect.ssid_len = 0;
878 #endif
880 schedule_work(&cfg80211_disconnect_work);
883 void cfg80211_disconnected(struct net_device *dev, u16 reason,
884 u8 *ie, size_t ie_len, gfp_t gfp)
886 struct wireless_dev *wdev = dev->ieee80211_ptr;
887 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
888 struct cfg80211_event *ev;
889 unsigned long flags;
891 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
892 if (!ev)
893 return;
895 ev->type = EVENT_DISCONNECTED;
896 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
897 ev->dc.ie_len = ie_len;
898 memcpy((void *)ev->dc.ie, ie, ie_len);
899 ev->dc.reason = reason;
901 spin_lock_irqsave(&wdev->event_lock, flags);
902 list_add_tail(&ev->list, &wdev->event_list);
903 spin_unlock_irqrestore(&wdev->event_lock, flags);
904 queue_work(cfg80211_wq, &rdev->event_work);
906 EXPORT_SYMBOL(cfg80211_disconnected);
909 * API calls for nl80211/wext compatibility code
911 int cfg80211_connect(struct cfg80211_registered_device *rdev,
912 struct net_device *dev,
913 struct cfg80211_connect_params *connect,
914 struct cfg80211_cached_keys *connkeys,
915 const u8 *prev_bssid)
917 struct wireless_dev *wdev = dev->ieee80211_ptr;
918 int err;
920 ASSERT_WDEV_LOCK(wdev);
922 if (WARN_ON(wdev->connect_keys)) {
923 kfree(wdev->connect_keys);
924 wdev->connect_keys = NULL;
927 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
928 rdev->wiphy.ht_capa_mod_mask);
930 if (connkeys && connkeys->def >= 0) {
931 int idx;
932 u32 cipher;
934 idx = connkeys->def;
935 cipher = connkeys->params[idx].cipher;
936 /* If given a WEP key we may need it for shared key auth */
937 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
938 cipher == WLAN_CIPHER_SUITE_WEP104) {
939 connect->key_idx = idx;
940 connect->key = connkeys->params[idx].key;
941 connect->key_len = connkeys->params[idx].key_len;
944 * If ciphers are not set (e.g. when going through
945 * iwconfig), we have to set them appropriately here.
947 if (connect->crypto.cipher_group == 0)
948 connect->crypto.cipher_group = cipher;
950 if (connect->crypto.n_ciphers_pairwise == 0) {
951 connect->crypto.n_ciphers_pairwise = 1;
952 connect->crypto.ciphers_pairwise[0] = cipher;
957 wdev->connect_keys = connkeys;
958 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
959 wdev->ssid_len = connect->ssid_len;
961 if (!rdev->ops->connect)
962 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
963 else
964 err = rdev_connect(rdev, dev, connect);
966 if (err) {
967 wdev->connect_keys = NULL;
968 wdev->ssid_len = 0;
969 return err;
972 return 0;
975 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
976 struct net_device *dev, u16 reason, bool wextev)
978 struct wireless_dev *wdev = dev->ieee80211_ptr;
979 int err = 0;
981 ASSERT_WDEV_LOCK(wdev);
983 kfree(wdev->connect_keys);
984 wdev->connect_keys = NULL;
986 if (wdev->conn)
987 err = cfg80211_sme_disconnect(wdev, reason);
988 else if (!rdev->ops->disconnect)
989 cfg80211_mlme_down(rdev, dev);
990 else if (wdev->current_bss)
991 err = rdev_disconnect(rdev, dev, reason);
993 return err;