Linux 4.8-rc8
[linux/fpc-iii.git] / net / wireless / sme.c
blobadd6824c44fd70af60feee7d4380d1bb935fee7f
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 const 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_rdev(wdev->wiphy);
63 struct cfg80211_scan_request *request;
64 int n_channels, err;
66 ASSERT_RTNL();
67 ASSERT_WDEV_LOCK(wdev);
69 if (rdev->scan_req || rdev->scan_msg)
70 return -EBUSY;
72 if (wdev->conn->params.channel)
73 n_channels = 1;
74 else
75 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
77 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
78 sizeof(request->channels[0]) * n_channels,
79 GFP_KERNEL);
80 if (!request)
81 return -ENOMEM;
83 if (wdev->conn->params.channel) {
84 enum nl80211_band band = wdev->conn->params.channel->band;
85 struct ieee80211_supported_band *sband =
86 wdev->wiphy->bands[band];
88 if (!sband) {
89 kfree(request);
90 return -EINVAL;
92 request->channels[0] = wdev->conn->params.channel;
93 request->rates[band] = (1 << sband->n_bitrates) - 1;
94 } else {
95 int i = 0, j;
96 enum nl80211_band band;
97 struct ieee80211_supported_band *bands;
98 struct ieee80211_channel *channel;
100 for (band = 0; band < NUM_NL80211_BANDS; band++) {
101 bands = wdev->wiphy->bands[band];
102 if (!bands)
103 continue;
104 for (j = 0; j < bands->n_channels; j++) {
105 channel = &bands->channels[j];
106 if (channel->flags & IEEE80211_CHAN_DISABLED)
107 continue;
108 request->channels[i++] = channel;
110 request->rates[band] = (1 << bands->n_bitrates) - 1;
112 n_channels = i;
114 request->n_channels = n_channels;
115 request->ssids = (void *)&request->channels[n_channels];
116 request->n_ssids = 1;
118 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
119 wdev->conn->params.ssid_len);
120 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
122 eth_broadcast_addr(request->bssid);
124 request->wdev = wdev;
125 request->wiphy = &rdev->wiphy;
126 request->scan_start = jiffies;
128 rdev->scan_req = request;
130 err = rdev_scan(rdev, request);
131 if (!err) {
132 wdev->conn->state = CFG80211_CONN_SCANNING;
133 nl80211_send_scan_start(rdev, wdev);
134 dev_hold(wdev->netdev);
135 } else {
136 rdev->scan_req = NULL;
137 kfree(request);
139 return err;
142 static int cfg80211_conn_do_work(struct wireless_dev *wdev)
144 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
145 struct cfg80211_connect_params *params;
146 struct cfg80211_assoc_request req = {};
147 int err;
149 ASSERT_WDEV_LOCK(wdev);
151 if (!wdev->conn)
152 return 0;
154 params = &wdev->conn->params;
156 switch (wdev->conn->state) {
157 case CFG80211_CONN_SCANNING:
158 /* didn't find it during scan ... */
159 return -ENOENT;
160 case CFG80211_CONN_SCAN_AGAIN:
161 return cfg80211_conn_scan(wdev);
162 case CFG80211_CONN_AUTHENTICATE_NEXT:
163 if (WARN_ON(!rdev->ops->auth))
164 return -EOPNOTSUPP;
165 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
166 return cfg80211_mlme_auth(rdev, wdev->netdev,
167 params->channel, params->auth_type,
168 params->bssid,
169 params->ssid, params->ssid_len,
170 NULL, 0,
171 params->key, params->key_len,
172 params->key_idx, NULL, 0);
173 case CFG80211_CONN_AUTH_FAILED:
174 return -ENOTCONN;
175 case CFG80211_CONN_ASSOCIATE_NEXT:
176 if (WARN_ON(!rdev->ops->assoc))
177 return -EOPNOTSUPP;
178 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
179 if (wdev->conn->prev_bssid_valid)
180 req.prev_bssid = wdev->conn->prev_bssid;
181 req.ie = params->ie;
182 req.ie_len = params->ie_len;
183 req.use_mfp = params->mfp != NL80211_MFP_NO;
184 req.crypto = params->crypto;
185 req.flags = params->flags;
186 req.ht_capa = params->ht_capa;
187 req.ht_capa_mask = params->ht_capa_mask;
188 req.vht_capa = params->vht_capa;
189 req.vht_capa_mask = params->vht_capa_mask;
191 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
192 params->bssid, params->ssid,
193 params->ssid_len, &req);
194 if (err)
195 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
196 NULL, 0,
197 WLAN_REASON_DEAUTH_LEAVING,
198 false);
199 return err;
200 case CFG80211_CONN_ASSOC_FAILED:
201 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 NULL, 0,
203 WLAN_REASON_DEAUTH_LEAVING, false);
204 return -ENOTCONN;
205 case CFG80211_CONN_DEAUTH:
206 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
207 NULL, 0,
208 WLAN_REASON_DEAUTH_LEAVING, false);
209 /* free directly, disconnected event already sent */
210 cfg80211_sme_free(wdev);
211 return 0;
212 default:
213 return 0;
217 void cfg80211_conn_work(struct work_struct *work)
219 struct cfg80211_registered_device *rdev =
220 container_of(work, struct cfg80211_registered_device, conn_work);
221 struct wireless_dev *wdev;
222 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
224 rtnl_lock();
226 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
227 if (!wdev->netdev)
228 continue;
230 wdev_lock(wdev);
231 if (!netif_running(wdev->netdev)) {
232 wdev_unlock(wdev);
233 continue;
235 if (!wdev->conn ||
236 wdev->conn->state == CFG80211_CONN_CONNECTED) {
237 wdev_unlock(wdev);
238 continue;
240 if (wdev->conn->params.bssid) {
241 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
242 bssid = bssid_buf;
244 if (cfg80211_conn_do_work(wdev)) {
245 __cfg80211_connect_result(
246 wdev->netdev, bssid,
247 NULL, 0, NULL, 0, -1, false, NULL);
249 wdev_unlock(wdev);
252 rtnl_unlock();
255 /* Returned bss is reference counted and must be cleaned up appropriately. */
256 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
258 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
259 struct cfg80211_bss *bss;
261 ASSERT_WDEV_LOCK(wdev);
263 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
264 wdev->conn->params.bssid,
265 wdev->conn->params.ssid,
266 wdev->conn->params.ssid_len,
267 wdev->conn_bss_type,
268 IEEE80211_PRIVACY(wdev->conn->params.privacy));
269 if (!bss)
270 return NULL;
272 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
273 wdev->conn->params.bssid = wdev->conn->bssid;
274 wdev->conn->params.channel = bss->channel;
275 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
276 schedule_work(&rdev->conn_work);
278 return bss;
281 static void __cfg80211_sme_scan_done(struct net_device *dev)
283 struct wireless_dev *wdev = dev->ieee80211_ptr;
284 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
285 struct cfg80211_bss *bss;
287 ASSERT_WDEV_LOCK(wdev);
289 if (!wdev->conn)
290 return;
292 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
293 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
294 return;
296 bss = cfg80211_get_conn_bss(wdev);
297 if (bss)
298 cfg80211_put_bss(&rdev->wiphy, bss);
299 else
300 schedule_work(&rdev->conn_work);
303 void cfg80211_sme_scan_done(struct net_device *dev)
305 struct wireless_dev *wdev = dev->ieee80211_ptr;
307 wdev_lock(wdev);
308 __cfg80211_sme_scan_done(dev);
309 wdev_unlock(wdev);
312 void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
314 struct wiphy *wiphy = wdev->wiphy;
315 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
316 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
317 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
319 ASSERT_WDEV_LOCK(wdev);
321 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
322 return;
324 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
325 wdev->conn->auto_auth &&
326 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
327 /* select automatically between only open, shared, leap */
328 switch (wdev->conn->params.auth_type) {
329 case NL80211_AUTHTYPE_OPEN_SYSTEM:
330 if (wdev->connect_keys)
331 wdev->conn->params.auth_type =
332 NL80211_AUTHTYPE_SHARED_KEY;
333 else
334 wdev->conn->params.auth_type =
335 NL80211_AUTHTYPE_NETWORK_EAP;
336 break;
337 case NL80211_AUTHTYPE_SHARED_KEY:
338 wdev->conn->params.auth_type =
339 NL80211_AUTHTYPE_NETWORK_EAP;
340 break;
341 default:
342 /* huh? */
343 wdev->conn->params.auth_type =
344 NL80211_AUTHTYPE_OPEN_SYSTEM;
345 break;
347 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
348 schedule_work(&rdev->conn_work);
349 } else if (status_code != WLAN_STATUS_SUCCESS) {
350 __cfg80211_connect_result(wdev->netdev, mgmt->bssid,
351 NULL, 0, NULL, 0,
352 status_code, false, NULL);
353 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
354 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
355 schedule_work(&rdev->conn_work);
359 bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
361 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
363 if (!wdev->conn)
364 return false;
366 if (status == WLAN_STATUS_SUCCESS) {
367 wdev->conn->state = CFG80211_CONN_CONNECTED;
368 return false;
371 if (wdev->conn->prev_bssid_valid) {
373 * Some stupid APs don't accept reassoc, so we
374 * need to fall back to trying regular assoc;
375 * return true so no event is sent to userspace.
377 wdev->conn->prev_bssid_valid = false;
378 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
379 schedule_work(&rdev->conn_work);
380 return true;
383 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
384 schedule_work(&rdev->conn_work);
385 return false;
388 void cfg80211_sme_deauth(struct wireless_dev *wdev)
390 cfg80211_sme_free(wdev);
393 void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
395 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
397 if (!wdev->conn)
398 return;
400 wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
401 schedule_work(&rdev->conn_work);
404 void cfg80211_sme_disassoc(struct wireless_dev *wdev)
406 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
408 if (!wdev->conn)
409 return;
411 wdev->conn->state = CFG80211_CONN_DEAUTH;
412 schedule_work(&rdev->conn_work);
415 void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
417 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
419 if (!wdev->conn)
420 return;
422 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
423 schedule_work(&rdev->conn_work);
426 static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
427 const u8 *ies, size_t ies_len,
428 const u8 **out_ies, size_t *out_ies_len)
430 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
431 u8 *buf;
432 size_t offs;
434 if (!rdev->wiphy.extended_capabilities_len ||
435 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
436 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
437 if (!*out_ies)
438 return -ENOMEM;
439 *out_ies_len = ies_len;
440 return 0;
443 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
444 GFP_KERNEL);
445 if (!buf)
446 return -ENOMEM;
448 if (ies_len) {
449 static const u8 before_extcapa[] = {
450 /* not listing IEs expected to be created by driver */
451 WLAN_EID_RSN,
452 WLAN_EID_QOS_CAPA,
453 WLAN_EID_RRM_ENABLED_CAPABILITIES,
454 WLAN_EID_MOBILITY_DOMAIN,
455 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
456 WLAN_EID_BSS_COEX_2040,
459 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
460 ARRAY_SIZE(before_extcapa), 0);
461 memcpy(buf, ies, offs);
462 /* leave a whole for extended capabilities IE */
463 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
464 ies + offs, ies_len - offs);
465 } else {
466 offs = 0;
469 /* place extended capabilities IE (with only driver capabilities) */
470 buf[offs] = WLAN_EID_EXT_CAPABILITY;
471 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
472 memcpy(buf + offs + 2,
473 rdev->wiphy.extended_capabilities,
474 rdev->wiphy.extended_capabilities_len);
476 *out_ies = buf;
477 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
479 return 0;
482 static int cfg80211_sme_connect(struct wireless_dev *wdev,
483 struct cfg80211_connect_params *connect,
484 const u8 *prev_bssid)
486 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
487 struct cfg80211_bss *bss;
488 int err;
490 if (!rdev->ops->auth || !rdev->ops->assoc)
491 return -EOPNOTSUPP;
493 if (wdev->current_bss) {
494 if (!prev_bssid)
495 return -EALREADY;
496 if (prev_bssid &&
497 !ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
498 return -ENOTCONN;
499 cfg80211_unhold_bss(wdev->current_bss);
500 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
501 wdev->current_bss = NULL;
503 cfg80211_sme_free(wdev);
506 if (WARN_ON(wdev->conn))
507 return -EINPROGRESS;
509 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
510 if (!wdev->conn)
511 return -ENOMEM;
514 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
516 memcpy(&wdev->conn->params, connect, sizeof(*connect));
517 if (connect->bssid) {
518 wdev->conn->params.bssid = wdev->conn->bssid;
519 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
522 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
523 &wdev->conn->ie,
524 &wdev->conn->params.ie_len)) {
525 kfree(wdev->conn);
526 wdev->conn = NULL;
527 return -ENOMEM;
529 wdev->conn->params.ie = wdev->conn->ie;
531 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
532 wdev->conn->auto_auth = true;
533 /* start with open system ... should mostly work */
534 wdev->conn->params.auth_type =
535 NL80211_AUTHTYPE_OPEN_SYSTEM;
536 } else {
537 wdev->conn->auto_auth = false;
540 wdev->conn->params.ssid = wdev->ssid;
541 wdev->conn->params.ssid_len = wdev->ssid_len;
543 /* see if we have the bss already */
544 bss = cfg80211_get_conn_bss(wdev);
546 if (prev_bssid) {
547 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
548 wdev->conn->prev_bssid_valid = true;
551 /* we're good if we have a matching bss struct */
552 if (bss) {
553 err = cfg80211_conn_do_work(wdev);
554 cfg80211_put_bss(wdev->wiphy, bss);
555 } else {
556 /* otherwise we'll need to scan for the AP first */
557 err = cfg80211_conn_scan(wdev);
560 * If we can't scan right now, then we need to scan again
561 * after the current scan finished, since the parameters
562 * changed (unless we find a good AP anyway).
564 if (err == -EBUSY) {
565 err = 0;
566 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
570 if (err)
571 cfg80211_sme_free(wdev);
573 return err;
576 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
578 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
579 int err;
581 if (!wdev->conn)
582 return 0;
584 if (!rdev->ops->deauth)
585 return -EOPNOTSUPP;
587 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
588 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
589 err = 0;
590 goto out;
593 /* wdev->conn->params.bssid must be set if > SCANNING */
594 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
595 wdev->conn->params.bssid,
596 NULL, 0, reason, false);
597 out:
598 cfg80211_sme_free(wdev);
599 return err;
603 * code shared for in-device and software SME
606 static bool cfg80211_is_all_idle(void)
608 struct cfg80211_registered_device *rdev;
609 struct wireless_dev *wdev;
610 bool is_all_idle = true;
613 * All devices must be idle as otherwise if you are actively
614 * scanning some new beacon hints could be learned and would
615 * count as new regulatory hints.
617 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
618 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
619 wdev_lock(wdev);
620 if (wdev->conn || wdev->current_bss)
621 is_all_idle = false;
622 wdev_unlock(wdev);
626 return is_all_idle;
629 static void disconnect_work(struct work_struct *work)
631 rtnl_lock();
632 if (cfg80211_is_all_idle())
633 regulatory_hint_disconnect();
634 rtnl_unlock();
637 static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
641 * API calls for drivers implementing connect/disconnect and
642 * SME event handling
645 /* This method must consume bss one way or another */
646 void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
647 const u8 *req_ie, size_t req_ie_len,
648 const u8 *resp_ie, size_t resp_ie_len,
649 int status, bool wextev,
650 struct cfg80211_bss *bss)
652 struct wireless_dev *wdev = dev->ieee80211_ptr;
653 const u8 *country_ie;
654 #ifdef CONFIG_CFG80211_WEXT
655 union iwreq_data wrqu;
656 #endif
658 ASSERT_WDEV_LOCK(wdev);
660 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
661 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
662 cfg80211_put_bss(wdev->wiphy, bss);
663 return;
666 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
667 bssid, req_ie, req_ie_len,
668 resp_ie, resp_ie_len,
669 status, GFP_KERNEL);
671 #ifdef CONFIG_CFG80211_WEXT
672 if (wextev) {
673 if (req_ie && status == WLAN_STATUS_SUCCESS) {
674 memset(&wrqu, 0, sizeof(wrqu));
675 wrqu.data.length = req_ie_len;
676 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
679 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
680 memset(&wrqu, 0, sizeof(wrqu));
681 wrqu.data.length = resp_ie_len;
682 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
685 memset(&wrqu, 0, sizeof(wrqu));
686 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
687 if (bssid && status == WLAN_STATUS_SUCCESS) {
688 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
689 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
690 wdev->wext.prev_bssid_valid = true;
692 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
694 #endif
696 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
697 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
698 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
699 wdev->ssid, wdev->ssid_len,
700 wdev->conn_bss_type,
701 IEEE80211_PRIVACY_ANY);
702 if (bss)
703 cfg80211_hold_bss(bss_from_pub(bss));
706 if (wdev->current_bss) {
707 cfg80211_unhold_bss(wdev->current_bss);
708 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
709 wdev->current_bss = NULL;
712 if (status != WLAN_STATUS_SUCCESS) {
713 kzfree(wdev->connect_keys);
714 wdev->connect_keys = NULL;
715 wdev->ssid_len = 0;
716 if (bss) {
717 cfg80211_unhold_bss(bss_from_pub(bss));
718 cfg80211_put_bss(wdev->wiphy, bss);
720 cfg80211_sme_free(wdev);
721 return;
724 if (WARN_ON(!bss))
725 return;
727 wdev->current_bss = bss_from_pub(bss);
729 cfg80211_upload_connect_keys(wdev);
731 rcu_read_lock();
732 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
733 if (!country_ie) {
734 rcu_read_unlock();
735 return;
738 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
739 rcu_read_unlock();
741 if (!country_ie)
742 return;
745 * ieee80211_bss_get_ie() ensures we can access:
746 * - country_ie + 2, the start of the country ie data, and
747 * - and country_ie[1] which is the IE length
749 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
750 country_ie + 2, country_ie[1]);
751 kfree(country_ie);
754 /* Consumes bss object one way or another */
755 void cfg80211_connect_bss(struct net_device *dev, const u8 *bssid,
756 struct cfg80211_bss *bss, const u8 *req_ie,
757 size_t req_ie_len, const u8 *resp_ie,
758 size_t resp_ie_len, int status, gfp_t gfp)
760 struct wireless_dev *wdev = dev->ieee80211_ptr;
761 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
762 struct cfg80211_event *ev;
763 unsigned long flags;
765 if (bss) {
766 /* Make sure the bss entry provided by the driver is valid. */
767 struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
769 if (WARN_ON(list_empty(&ibss->list))) {
770 cfg80211_put_bss(wdev->wiphy, bss);
771 return;
775 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
776 if (!ev) {
777 cfg80211_put_bss(wdev->wiphy, bss);
778 return;
781 ev->type = EVENT_CONNECT_RESULT;
782 if (bssid)
783 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
784 if (req_ie_len) {
785 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
786 ev->cr.req_ie_len = req_ie_len;
787 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
789 if (resp_ie_len) {
790 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
791 ev->cr.resp_ie_len = resp_ie_len;
792 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
794 if (bss)
795 cfg80211_hold_bss(bss_from_pub(bss));
796 ev->cr.bss = bss;
797 ev->cr.status = status;
799 spin_lock_irqsave(&wdev->event_lock, flags);
800 list_add_tail(&ev->list, &wdev->event_list);
801 spin_unlock_irqrestore(&wdev->event_lock, flags);
802 queue_work(cfg80211_wq, &rdev->event_work);
804 EXPORT_SYMBOL(cfg80211_connect_bss);
806 /* Consumes bss object one way or another */
807 void __cfg80211_roamed(struct wireless_dev *wdev,
808 struct cfg80211_bss *bss,
809 const u8 *req_ie, size_t req_ie_len,
810 const u8 *resp_ie, size_t resp_ie_len)
812 #ifdef CONFIG_CFG80211_WEXT
813 union iwreq_data wrqu;
814 #endif
815 ASSERT_WDEV_LOCK(wdev);
817 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
818 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
819 goto out;
821 if (WARN_ON(!wdev->current_bss))
822 goto out;
824 cfg80211_unhold_bss(wdev->current_bss);
825 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
826 wdev->current_bss = NULL;
828 cfg80211_hold_bss(bss_from_pub(bss));
829 wdev->current_bss = bss_from_pub(bss);
831 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
832 wdev->netdev, bss->bssid,
833 req_ie, req_ie_len, resp_ie, resp_ie_len,
834 GFP_KERNEL);
836 #ifdef CONFIG_CFG80211_WEXT
837 if (req_ie) {
838 memset(&wrqu, 0, sizeof(wrqu));
839 wrqu.data.length = req_ie_len;
840 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
841 &wrqu, req_ie);
844 if (resp_ie) {
845 memset(&wrqu, 0, sizeof(wrqu));
846 wrqu.data.length = resp_ie_len;
847 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
848 &wrqu, resp_ie);
851 memset(&wrqu, 0, sizeof(wrqu));
852 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
853 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
854 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
855 wdev->wext.prev_bssid_valid = true;
856 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
857 #endif
859 return;
860 out:
861 cfg80211_put_bss(wdev->wiphy, bss);
864 void cfg80211_roamed(struct net_device *dev,
865 struct ieee80211_channel *channel,
866 const u8 *bssid,
867 const u8 *req_ie, size_t req_ie_len,
868 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
870 struct wireless_dev *wdev = dev->ieee80211_ptr;
871 struct cfg80211_bss *bss;
873 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
874 wdev->ssid_len,
875 wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
876 if (WARN_ON(!bss))
877 return;
879 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
880 resp_ie_len, gfp);
882 EXPORT_SYMBOL(cfg80211_roamed);
884 /* Consumes bss object one way or another */
885 void cfg80211_roamed_bss(struct net_device *dev,
886 struct cfg80211_bss *bss, const u8 *req_ie,
887 size_t req_ie_len, const u8 *resp_ie,
888 size_t resp_ie_len, gfp_t gfp)
890 struct wireless_dev *wdev = dev->ieee80211_ptr;
891 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
892 struct cfg80211_event *ev;
893 unsigned long flags;
895 if (WARN_ON(!bss))
896 return;
898 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
899 if (!ev) {
900 cfg80211_put_bss(wdev->wiphy, bss);
901 return;
904 ev->type = EVENT_ROAMED;
905 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
906 ev->rm.req_ie_len = req_ie_len;
907 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
908 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
909 ev->rm.resp_ie_len = resp_ie_len;
910 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
911 ev->rm.bss = bss;
913 spin_lock_irqsave(&wdev->event_lock, flags);
914 list_add_tail(&ev->list, &wdev->event_list);
915 spin_unlock_irqrestore(&wdev->event_lock, flags);
916 queue_work(cfg80211_wq, &rdev->event_work);
918 EXPORT_SYMBOL(cfg80211_roamed_bss);
920 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
921 size_t ie_len, u16 reason, bool from_ap)
923 struct wireless_dev *wdev = dev->ieee80211_ptr;
924 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
925 int i;
926 #ifdef CONFIG_CFG80211_WEXT
927 union iwreq_data wrqu;
928 #endif
930 ASSERT_WDEV_LOCK(wdev);
932 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
933 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
934 return;
936 if (wdev->current_bss) {
937 cfg80211_unhold_bss(wdev->current_bss);
938 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
941 wdev->current_bss = NULL;
942 wdev->ssid_len = 0;
944 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
946 /* stop critical protocol if supported */
947 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
948 rdev->crit_proto_nlportid = 0;
949 rdev_crit_proto_stop(rdev, wdev);
953 * Delete all the keys ... pairwise keys can't really
954 * exist any more anyway, but default keys might.
956 if (rdev->ops->del_key)
957 for (i = 0; i < 6; i++)
958 rdev_del_key(rdev, dev, i, false, NULL);
960 rdev_set_qos_map(rdev, dev, NULL);
962 #ifdef CONFIG_CFG80211_WEXT
963 memset(&wrqu, 0, sizeof(wrqu));
964 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
965 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
966 wdev->wext.connect.ssid_len = 0;
967 #endif
969 schedule_work(&cfg80211_disconnect_work);
972 void cfg80211_disconnected(struct net_device *dev, u16 reason,
973 const u8 *ie, size_t ie_len,
974 bool locally_generated, gfp_t gfp)
976 struct wireless_dev *wdev = dev->ieee80211_ptr;
977 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
978 struct cfg80211_event *ev;
979 unsigned long flags;
981 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
982 if (!ev)
983 return;
985 ev->type = EVENT_DISCONNECTED;
986 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
987 ev->dc.ie_len = ie_len;
988 memcpy((void *)ev->dc.ie, ie, ie_len);
989 ev->dc.reason = reason;
990 ev->dc.locally_generated = locally_generated;
992 spin_lock_irqsave(&wdev->event_lock, flags);
993 list_add_tail(&ev->list, &wdev->event_list);
994 spin_unlock_irqrestore(&wdev->event_lock, flags);
995 queue_work(cfg80211_wq, &rdev->event_work);
997 EXPORT_SYMBOL(cfg80211_disconnected);
1000 * API calls for nl80211/wext compatibility code
1002 int cfg80211_connect(struct cfg80211_registered_device *rdev,
1003 struct net_device *dev,
1004 struct cfg80211_connect_params *connect,
1005 struct cfg80211_cached_keys *connkeys,
1006 const u8 *prev_bssid)
1008 struct wireless_dev *wdev = dev->ieee80211_ptr;
1009 int err;
1011 ASSERT_WDEV_LOCK(wdev);
1013 if (WARN_ON(wdev->connect_keys)) {
1014 kzfree(wdev->connect_keys);
1015 wdev->connect_keys = NULL;
1018 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1019 rdev->wiphy.ht_capa_mod_mask);
1021 if (connkeys && connkeys->def >= 0) {
1022 int idx;
1023 u32 cipher;
1025 idx = connkeys->def;
1026 cipher = connkeys->params[idx].cipher;
1027 /* If given a WEP key we may need it for shared key auth */
1028 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1029 cipher == WLAN_CIPHER_SUITE_WEP104) {
1030 connect->key_idx = idx;
1031 connect->key = connkeys->params[idx].key;
1032 connect->key_len = connkeys->params[idx].key_len;
1035 * If ciphers are not set (e.g. when going through
1036 * iwconfig), we have to set them appropriately here.
1038 if (connect->crypto.cipher_group == 0)
1039 connect->crypto.cipher_group = cipher;
1041 if (connect->crypto.n_ciphers_pairwise == 0) {
1042 connect->crypto.n_ciphers_pairwise = 1;
1043 connect->crypto.ciphers_pairwise[0] = cipher;
1048 wdev->connect_keys = connkeys;
1049 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1050 wdev->ssid_len = connect->ssid_len;
1052 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1053 IEEE80211_BSS_TYPE_ESS;
1055 if (!rdev->ops->connect)
1056 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1057 else
1058 err = rdev_connect(rdev, dev, connect);
1060 if (err) {
1061 wdev->connect_keys = NULL;
1062 wdev->ssid_len = 0;
1063 return err;
1066 return 0;
1069 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1070 struct net_device *dev, u16 reason, bool wextev)
1072 struct wireless_dev *wdev = dev->ieee80211_ptr;
1073 int err = 0;
1075 ASSERT_WDEV_LOCK(wdev);
1077 kzfree(wdev->connect_keys);
1078 wdev->connect_keys = NULL;
1080 if (wdev->conn)
1081 err = cfg80211_sme_disconnect(wdev, reason);
1082 else if (!rdev->ops->disconnect)
1083 cfg80211_mlme_down(rdev, dev);
1084 else if (wdev->current_bss)
1085 err = rdev_disconnect(rdev, dev, reason);
1087 return err;