media: cpia2_usb: drop bogus interface-release call
[linux/fpc-iii.git] / net / wireless / sme.c
blob701cfd7acc1bc477aa03bdf8ccb30e72755d1e33
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SME code for cfg80211
4 * both driver SME event handling and the SME implementation
5 * (for nl80211's connect() and wext)
7 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
8 * Copyright (C) 2009 Intel Corporation. All rights reserved.
9 * Copyright 2017 Intel Deutschland GmbH
12 #include <linux/etherdevice.h>
13 #include <linux/if_arp.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/wireless.h>
17 #include <linux/export.h>
18 #include <net/iw_handler.h>
19 #include <net/cfg80211.h>
20 #include <net/rtnetlink.h>
21 #include "nl80211.h"
22 #include "reg.h"
23 #include "rdev-ops.h"
26 * Software SME in cfg80211, using auth/assoc/deauth calls to the
27 * driver. This is is for implementing nl80211's connect/disconnect
28 * and wireless extensions (if configured.)
31 struct cfg80211_conn {
32 struct cfg80211_connect_params params;
33 /* these are sub-states of the _CONNECTING sme_state */
34 enum {
35 CFG80211_CONN_SCANNING,
36 CFG80211_CONN_SCAN_AGAIN,
37 CFG80211_CONN_AUTHENTICATE_NEXT,
38 CFG80211_CONN_AUTHENTICATING,
39 CFG80211_CONN_AUTH_FAILED_TIMEOUT,
40 CFG80211_CONN_ASSOCIATE_NEXT,
41 CFG80211_CONN_ASSOCIATING,
42 CFG80211_CONN_ASSOC_FAILED,
43 CFG80211_CONN_ASSOC_FAILED_TIMEOUT,
44 CFG80211_CONN_DEAUTH,
45 CFG80211_CONN_ABANDON,
46 CFG80211_CONN_CONNECTED,
47 } state;
48 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
49 const u8 *ie;
50 size_t ie_len;
51 bool auto_auth, prev_bssid_valid;
54 static void cfg80211_sme_free(struct wireless_dev *wdev)
56 if (!wdev->conn)
57 return;
59 kfree(wdev->conn->ie);
60 kfree(wdev->conn);
61 wdev->conn = NULL;
64 static int cfg80211_conn_scan(struct wireless_dev *wdev)
66 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
67 struct cfg80211_scan_request *request;
68 int n_channels, err;
70 ASSERT_RTNL();
71 ASSERT_WDEV_LOCK(wdev);
73 if (rdev->scan_req || rdev->scan_msg)
74 return -EBUSY;
76 if (wdev->conn->params.channel)
77 n_channels = 1;
78 else
79 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
81 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
82 sizeof(request->channels[0]) * n_channels,
83 GFP_KERNEL);
84 if (!request)
85 return -ENOMEM;
87 if (wdev->conn->params.channel) {
88 enum nl80211_band band = wdev->conn->params.channel->band;
89 struct ieee80211_supported_band *sband =
90 wdev->wiphy->bands[band];
92 if (!sband) {
93 kfree(request);
94 return -EINVAL;
96 request->channels[0] = wdev->conn->params.channel;
97 request->rates[band] = (1 << sband->n_bitrates) - 1;
98 } else {
99 int i = 0, j;
100 enum nl80211_band band;
101 struct ieee80211_supported_band *bands;
102 struct ieee80211_channel *channel;
104 for (band = 0; band < NUM_NL80211_BANDS; band++) {
105 bands = wdev->wiphy->bands[band];
106 if (!bands)
107 continue;
108 for (j = 0; j < bands->n_channels; j++) {
109 channel = &bands->channels[j];
110 if (channel->flags & IEEE80211_CHAN_DISABLED)
111 continue;
112 request->channels[i++] = channel;
114 request->rates[band] = (1 << bands->n_bitrates) - 1;
116 n_channels = i;
118 request->n_channels = n_channels;
119 request->ssids = (void *)&request->channels[n_channels];
120 request->n_ssids = 1;
122 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
123 wdev->conn->params.ssid_len);
124 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
126 eth_broadcast_addr(request->bssid);
128 request->wdev = wdev;
129 request->wiphy = &rdev->wiphy;
130 request->scan_start = jiffies;
132 rdev->scan_req = request;
134 err = rdev_scan(rdev, request);
135 if (!err) {
136 wdev->conn->state = CFG80211_CONN_SCANNING;
137 nl80211_send_scan_start(rdev, wdev);
138 dev_hold(wdev->netdev);
139 } else {
140 rdev->scan_req = NULL;
141 kfree(request);
143 return err;
146 static int cfg80211_conn_do_work(struct wireless_dev *wdev,
147 enum nl80211_timeout_reason *treason)
149 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
150 struct cfg80211_connect_params *params;
151 struct cfg80211_assoc_request req = {};
152 int err;
154 ASSERT_WDEV_LOCK(wdev);
156 if (!wdev->conn)
157 return 0;
159 params = &wdev->conn->params;
161 switch (wdev->conn->state) {
162 case CFG80211_CONN_SCANNING:
163 /* didn't find it during scan ... */
164 return -ENOENT;
165 case CFG80211_CONN_SCAN_AGAIN:
166 return cfg80211_conn_scan(wdev);
167 case CFG80211_CONN_AUTHENTICATE_NEXT:
168 if (WARN_ON(!rdev->ops->auth))
169 return -EOPNOTSUPP;
170 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
171 return cfg80211_mlme_auth(rdev, wdev->netdev,
172 params->channel, params->auth_type,
173 params->bssid,
174 params->ssid, params->ssid_len,
175 NULL, 0,
176 params->key, params->key_len,
177 params->key_idx, NULL, 0);
178 case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
179 *treason = NL80211_TIMEOUT_AUTH;
180 return -ENOTCONN;
181 case CFG80211_CONN_ASSOCIATE_NEXT:
182 if (WARN_ON(!rdev->ops->assoc))
183 return -EOPNOTSUPP;
184 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
185 if (wdev->conn->prev_bssid_valid)
186 req.prev_bssid = wdev->conn->prev_bssid;
187 req.ie = params->ie;
188 req.ie_len = params->ie_len;
189 req.use_mfp = params->mfp != NL80211_MFP_NO;
190 req.crypto = params->crypto;
191 req.flags = params->flags;
192 req.ht_capa = params->ht_capa;
193 req.ht_capa_mask = params->ht_capa_mask;
194 req.vht_capa = params->vht_capa;
195 req.vht_capa_mask = params->vht_capa_mask;
197 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
198 params->bssid, params->ssid,
199 params->ssid_len, &req);
200 if (err)
201 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 NULL, 0,
203 WLAN_REASON_DEAUTH_LEAVING,
204 false);
205 return err;
206 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
207 *treason = NL80211_TIMEOUT_ASSOC;
208 /* fall through */
209 case CFG80211_CONN_ASSOC_FAILED:
210 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
211 NULL, 0,
212 WLAN_REASON_DEAUTH_LEAVING, false);
213 return -ENOTCONN;
214 case CFG80211_CONN_DEAUTH:
215 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
216 NULL, 0,
217 WLAN_REASON_DEAUTH_LEAVING, false);
218 /* fall through */
219 case CFG80211_CONN_ABANDON:
220 /* free directly, disconnected event already sent */
221 cfg80211_sme_free(wdev);
222 return 0;
223 default:
224 return 0;
228 void cfg80211_conn_work(struct work_struct *work)
230 struct cfg80211_registered_device *rdev =
231 container_of(work, struct cfg80211_registered_device, conn_work);
232 struct wireless_dev *wdev;
233 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
234 enum nl80211_timeout_reason treason;
236 rtnl_lock();
238 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
239 if (!wdev->netdev)
240 continue;
242 wdev_lock(wdev);
243 if (!netif_running(wdev->netdev)) {
244 wdev_unlock(wdev);
245 continue;
247 if (!wdev->conn ||
248 wdev->conn->state == CFG80211_CONN_CONNECTED) {
249 wdev_unlock(wdev);
250 continue;
252 if (wdev->conn->params.bssid) {
253 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
254 bssid = bssid_buf;
256 treason = NL80211_TIMEOUT_UNSPECIFIED;
257 if (cfg80211_conn_do_work(wdev, &treason)) {
258 struct cfg80211_connect_resp_params cr;
260 memset(&cr, 0, sizeof(cr));
261 cr.status = -1;
262 cr.bssid = bssid;
263 cr.timeout_reason = treason;
264 __cfg80211_connect_result(wdev->netdev, &cr, false);
266 wdev_unlock(wdev);
269 rtnl_unlock();
272 /* Returned bss is reference counted and must be cleaned up appropriately. */
273 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
275 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
276 struct cfg80211_bss *bss;
278 ASSERT_WDEV_LOCK(wdev);
280 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
281 wdev->conn->params.bssid,
282 wdev->conn->params.ssid,
283 wdev->conn->params.ssid_len,
284 wdev->conn_bss_type,
285 IEEE80211_PRIVACY(wdev->conn->params.privacy));
286 if (!bss)
287 return NULL;
289 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
290 wdev->conn->params.bssid = wdev->conn->bssid;
291 wdev->conn->params.channel = bss->channel;
292 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
293 schedule_work(&rdev->conn_work);
295 return bss;
298 static void __cfg80211_sme_scan_done(struct net_device *dev)
300 struct wireless_dev *wdev = dev->ieee80211_ptr;
301 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
302 struct cfg80211_bss *bss;
304 ASSERT_WDEV_LOCK(wdev);
306 if (!wdev->conn)
307 return;
309 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
310 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
311 return;
313 bss = cfg80211_get_conn_bss(wdev);
314 if (bss)
315 cfg80211_put_bss(&rdev->wiphy, bss);
316 else
317 schedule_work(&rdev->conn_work);
320 void cfg80211_sme_scan_done(struct net_device *dev)
322 struct wireless_dev *wdev = dev->ieee80211_ptr;
324 wdev_lock(wdev);
325 __cfg80211_sme_scan_done(dev);
326 wdev_unlock(wdev);
329 void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
331 struct wiphy *wiphy = wdev->wiphy;
332 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
333 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
334 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
336 ASSERT_WDEV_LOCK(wdev);
338 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
339 return;
341 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
342 wdev->conn->auto_auth &&
343 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
344 /* select automatically between only open, shared, leap */
345 switch (wdev->conn->params.auth_type) {
346 case NL80211_AUTHTYPE_OPEN_SYSTEM:
347 if (wdev->connect_keys)
348 wdev->conn->params.auth_type =
349 NL80211_AUTHTYPE_SHARED_KEY;
350 else
351 wdev->conn->params.auth_type =
352 NL80211_AUTHTYPE_NETWORK_EAP;
353 break;
354 case NL80211_AUTHTYPE_SHARED_KEY:
355 wdev->conn->params.auth_type =
356 NL80211_AUTHTYPE_NETWORK_EAP;
357 break;
358 default:
359 /* huh? */
360 wdev->conn->params.auth_type =
361 NL80211_AUTHTYPE_OPEN_SYSTEM;
362 break;
364 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
365 schedule_work(&rdev->conn_work);
366 } else if (status_code != WLAN_STATUS_SUCCESS) {
367 struct cfg80211_connect_resp_params cr;
369 memset(&cr, 0, sizeof(cr));
370 cr.status = status_code;
371 cr.bssid = mgmt->bssid;
372 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
373 __cfg80211_connect_result(wdev->netdev, &cr, false);
374 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
375 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
376 schedule_work(&rdev->conn_work);
380 bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
382 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
384 if (!wdev->conn)
385 return false;
387 if (status == WLAN_STATUS_SUCCESS) {
388 wdev->conn->state = CFG80211_CONN_CONNECTED;
389 return false;
392 if (wdev->conn->prev_bssid_valid) {
394 * Some stupid APs don't accept reassoc, so we
395 * need to fall back to trying regular assoc;
396 * return true so no event is sent to userspace.
398 wdev->conn->prev_bssid_valid = false;
399 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
400 schedule_work(&rdev->conn_work);
401 return true;
404 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
405 schedule_work(&rdev->conn_work);
406 return false;
409 void cfg80211_sme_deauth(struct wireless_dev *wdev)
411 cfg80211_sme_free(wdev);
414 void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
416 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
418 if (!wdev->conn)
419 return;
421 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
422 schedule_work(&rdev->conn_work);
425 void cfg80211_sme_disassoc(struct wireless_dev *wdev)
427 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
429 if (!wdev->conn)
430 return;
432 wdev->conn->state = CFG80211_CONN_DEAUTH;
433 schedule_work(&rdev->conn_work);
436 void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
438 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
440 if (!wdev->conn)
441 return;
443 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
444 schedule_work(&rdev->conn_work);
447 void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
449 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
451 if (!wdev->conn)
452 return;
454 wdev->conn->state = CFG80211_CONN_ABANDON;
455 schedule_work(&rdev->conn_work);
458 static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
459 const u8 *ies, size_t ies_len,
460 const u8 **out_ies, size_t *out_ies_len)
462 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
463 u8 *buf;
464 size_t offs;
466 if (!rdev->wiphy.extended_capabilities_len ||
467 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
468 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
469 if (!*out_ies)
470 return -ENOMEM;
471 *out_ies_len = ies_len;
472 return 0;
475 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
476 GFP_KERNEL);
477 if (!buf)
478 return -ENOMEM;
480 if (ies_len) {
481 static const u8 before_extcapa[] = {
482 /* not listing IEs expected to be created by driver */
483 WLAN_EID_RSN,
484 WLAN_EID_QOS_CAPA,
485 WLAN_EID_RRM_ENABLED_CAPABILITIES,
486 WLAN_EID_MOBILITY_DOMAIN,
487 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
488 WLAN_EID_BSS_COEX_2040,
491 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
492 ARRAY_SIZE(before_extcapa), 0);
493 memcpy(buf, ies, offs);
494 /* leave a whole for extended capabilities IE */
495 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
496 ies + offs, ies_len - offs);
497 } else {
498 offs = 0;
501 /* place extended capabilities IE (with only driver capabilities) */
502 buf[offs] = WLAN_EID_EXT_CAPABILITY;
503 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
504 memcpy(buf + offs + 2,
505 rdev->wiphy.extended_capabilities,
506 rdev->wiphy.extended_capabilities_len);
508 *out_ies = buf;
509 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
511 return 0;
514 static int cfg80211_sme_connect(struct wireless_dev *wdev,
515 struct cfg80211_connect_params *connect,
516 const u8 *prev_bssid)
518 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
519 struct cfg80211_bss *bss;
520 int err;
522 if (!rdev->ops->auth || !rdev->ops->assoc)
523 return -EOPNOTSUPP;
525 if (wdev->current_bss) {
526 cfg80211_unhold_bss(wdev->current_bss);
527 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
528 wdev->current_bss = NULL;
530 cfg80211_sme_free(wdev);
533 if (WARN_ON(wdev->conn))
534 return -EINPROGRESS;
536 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
537 if (!wdev->conn)
538 return -ENOMEM;
541 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
543 memcpy(&wdev->conn->params, connect, sizeof(*connect));
544 if (connect->bssid) {
545 wdev->conn->params.bssid = wdev->conn->bssid;
546 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
549 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
550 &wdev->conn->ie,
551 &wdev->conn->params.ie_len)) {
552 kfree(wdev->conn);
553 wdev->conn = NULL;
554 return -ENOMEM;
556 wdev->conn->params.ie = wdev->conn->ie;
558 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
559 wdev->conn->auto_auth = true;
560 /* start with open system ... should mostly work */
561 wdev->conn->params.auth_type =
562 NL80211_AUTHTYPE_OPEN_SYSTEM;
563 } else {
564 wdev->conn->auto_auth = false;
567 wdev->conn->params.ssid = wdev->ssid;
568 wdev->conn->params.ssid_len = wdev->ssid_len;
570 /* see if we have the bss already */
571 bss = cfg80211_get_conn_bss(wdev);
573 if (prev_bssid) {
574 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
575 wdev->conn->prev_bssid_valid = true;
578 /* we're good if we have a matching bss struct */
579 if (bss) {
580 enum nl80211_timeout_reason treason;
582 err = cfg80211_conn_do_work(wdev, &treason);
583 cfg80211_put_bss(wdev->wiphy, bss);
584 } else {
585 /* otherwise we'll need to scan for the AP first */
586 err = cfg80211_conn_scan(wdev);
589 * If we can't scan right now, then we need to scan again
590 * after the current scan finished, since the parameters
591 * changed (unless we find a good AP anyway).
593 if (err == -EBUSY) {
594 err = 0;
595 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
599 if (err)
600 cfg80211_sme_free(wdev);
602 return err;
605 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
607 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
608 int err;
610 if (!wdev->conn)
611 return 0;
613 if (!rdev->ops->deauth)
614 return -EOPNOTSUPP;
616 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
617 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
618 err = 0;
619 goto out;
622 /* wdev->conn->params.bssid must be set if > SCANNING */
623 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
624 wdev->conn->params.bssid,
625 NULL, 0, reason, false);
626 out:
627 cfg80211_sme_free(wdev);
628 return err;
632 * code shared for in-device and software SME
635 static bool cfg80211_is_all_idle(void)
637 struct cfg80211_registered_device *rdev;
638 struct wireless_dev *wdev;
639 bool is_all_idle = true;
642 * All devices must be idle as otherwise if you are actively
643 * scanning some new beacon hints could be learned and would
644 * count as new regulatory hints.
646 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
647 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
648 wdev_lock(wdev);
649 if (wdev->conn || wdev->current_bss)
650 is_all_idle = false;
651 wdev_unlock(wdev);
655 return is_all_idle;
658 static void disconnect_work(struct work_struct *work)
660 rtnl_lock();
661 if (cfg80211_is_all_idle())
662 regulatory_hint_disconnect();
663 rtnl_unlock();
666 static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
670 * API calls for drivers implementing connect/disconnect and
671 * SME event handling
674 /* This method must consume bss one way or another */
675 void __cfg80211_connect_result(struct net_device *dev,
676 struct cfg80211_connect_resp_params *cr,
677 bool wextev)
679 struct wireless_dev *wdev = dev->ieee80211_ptr;
680 const u8 *country_ie;
681 #ifdef CONFIG_CFG80211_WEXT
682 union iwreq_data wrqu;
683 #endif
685 ASSERT_WDEV_LOCK(wdev);
687 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
688 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
689 cfg80211_put_bss(wdev->wiphy, cr->bss);
690 return;
693 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
694 GFP_KERNEL);
696 #ifdef CONFIG_CFG80211_WEXT
697 if (wextev) {
698 if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
699 memset(&wrqu, 0, sizeof(wrqu));
700 wrqu.data.length = cr->req_ie_len;
701 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
702 cr->req_ie);
705 if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
706 memset(&wrqu, 0, sizeof(wrqu));
707 wrqu.data.length = cr->resp_ie_len;
708 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
709 cr->resp_ie);
712 memset(&wrqu, 0, sizeof(wrqu));
713 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
714 if (cr->bssid && cr->status == WLAN_STATUS_SUCCESS) {
715 memcpy(wrqu.ap_addr.sa_data, cr->bssid, ETH_ALEN);
716 memcpy(wdev->wext.prev_bssid, cr->bssid, ETH_ALEN);
717 wdev->wext.prev_bssid_valid = true;
719 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
721 #endif
723 if (!cr->bss && (cr->status == WLAN_STATUS_SUCCESS)) {
724 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
725 cr->bss = cfg80211_get_bss(wdev->wiphy, NULL, cr->bssid,
726 wdev->ssid, wdev->ssid_len,
727 wdev->conn_bss_type,
728 IEEE80211_PRIVACY_ANY);
729 if (cr->bss)
730 cfg80211_hold_bss(bss_from_pub(cr->bss));
733 if (wdev->current_bss) {
734 cfg80211_unhold_bss(wdev->current_bss);
735 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
736 wdev->current_bss = NULL;
739 if (cr->status != WLAN_STATUS_SUCCESS) {
740 kzfree(wdev->connect_keys);
741 wdev->connect_keys = NULL;
742 wdev->ssid_len = 0;
743 wdev->conn_owner_nlportid = 0;
744 if (cr->bss) {
745 cfg80211_unhold_bss(bss_from_pub(cr->bss));
746 cfg80211_put_bss(wdev->wiphy, cr->bss);
748 cfg80211_sme_free(wdev);
749 return;
752 if (WARN_ON(!cr->bss))
753 return;
755 wdev->current_bss = bss_from_pub(cr->bss);
757 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
758 cfg80211_upload_connect_keys(wdev);
760 rcu_read_lock();
761 country_ie = ieee80211_bss_get_ie(cr->bss, WLAN_EID_COUNTRY);
762 if (!country_ie) {
763 rcu_read_unlock();
764 return;
767 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
768 rcu_read_unlock();
770 if (!country_ie)
771 return;
774 * ieee80211_bss_get_ie() ensures we can access:
775 * - country_ie + 2, the start of the country ie data, and
776 * - and country_ie[1] which is the IE length
778 regulatory_hint_country_ie(wdev->wiphy, cr->bss->channel->band,
779 country_ie + 2, country_ie[1]);
780 kfree(country_ie);
783 /* Consumes bss object one way or another */
784 void cfg80211_connect_done(struct net_device *dev,
785 struct cfg80211_connect_resp_params *params,
786 gfp_t gfp)
788 struct wireless_dev *wdev = dev->ieee80211_ptr;
789 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
790 struct cfg80211_event *ev;
791 unsigned long flags;
792 u8 *next;
794 if (params->bss) {
795 /* Make sure the bss entry provided by the driver is valid. */
796 struct cfg80211_internal_bss *ibss = bss_from_pub(params->bss);
798 if (WARN_ON(list_empty(&ibss->list))) {
799 cfg80211_put_bss(wdev->wiphy, params->bss);
800 return;
804 ev = kzalloc(sizeof(*ev) + (params->bssid ? ETH_ALEN : 0) +
805 params->req_ie_len + params->resp_ie_len +
806 params->fils_kek_len + params->pmk_len +
807 (params->pmkid ? WLAN_PMKID_LEN : 0), gfp);
808 if (!ev) {
809 cfg80211_put_bss(wdev->wiphy, params->bss);
810 return;
813 ev->type = EVENT_CONNECT_RESULT;
814 next = ((u8 *)ev) + sizeof(*ev);
815 if (params->bssid) {
816 ev->cr.bssid = next;
817 memcpy((void *)ev->cr.bssid, params->bssid, ETH_ALEN);
818 next += ETH_ALEN;
820 if (params->req_ie_len) {
821 ev->cr.req_ie = next;
822 ev->cr.req_ie_len = params->req_ie_len;
823 memcpy((void *)ev->cr.req_ie, params->req_ie,
824 params->req_ie_len);
825 next += params->req_ie_len;
827 if (params->resp_ie_len) {
828 ev->cr.resp_ie = next;
829 ev->cr.resp_ie_len = params->resp_ie_len;
830 memcpy((void *)ev->cr.resp_ie, params->resp_ie,
831 params->resp_ie_len);
832 next += params->resp_ie_len;
834 if (params->fils_kek_len) {
835 ev->cr.fils_kek = next;
836 ev->cr.fils_kek_len = params->fils_kek_len;
837 memcpy((void *)ev->cr.fils_kek, params->fils_kek,
838 params->fils_kek_len);
839 next += params->fils_kek_len;
841 if (params->pmk_len) {
842 ev->cr.pmk = next;
843 ev->cr.pmk_len = params->pmk_len;
844 memcpy((void *)ev->cr.pmk, params->pmk, params->pmk_len);
845 next += params->pmk_len;
847 if (params->pmkid) {
848 ev->cr.pmkid = next;
849 memcpy((void *)ev->cr.pmkid, params->pmkid, WLAN_PMKID_LEN);
850 next += WLAN_PMKID_LEN;
852 ev->cr.update_erp_next_seq_num = params->update_erp_next_seq_num;
853 if (params->update_erp_next_seq_num)
854 ev->cr.fils_erp_next_seq_num = params->fils_erp_next_seq_num;
855 if (params->bss)
856 cfg80211_hold_bss(bss_from_pub(params->bss));
857 ev->cr.bss = params->bss;
858 ev->cr.status = params->status;
859 ev->cr.timeout_reason = params->timeout_reason;
861 spin_lock_irqsave(&wdev->event_lock, flags);
862 list_add_tail(&ev->list, &wdev->event_list);
863 spin_unlock_irqrestore(&wdev->event_lock, flags);
864 queue_work(cfg80211_wq, &rdev->event_work);
866 EXPORT_SYMBOL(cfg80211_connect_done);
868 /* Consumes bss object one way or another */
869 void __cfg80211_roamed(struct wireless_dev *wdev,
870 struct cfg80211_roam_info *info)
872 #ifdef CONFIG_CFG80211_WEXT
873 union iwreq_data wrqu;
874 #endif
875 ASSERT_WDEV_LOCK(wdev);
877 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
878 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
879 goto out;
881 if (WARN_ON(!wdev->current_bss))
882 goto out;
884 cfg80211_unhold_bss(wdev->current_bss);
885 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
886 wdev->current_bss = NULL;
888 if (WARN_ON(!info->bss))
889 return;
891 cfg80211_hold_bss(bss_from_pub(info->bss));
892 wdev->current_bss = bss_from_pub(info->bss);
894 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
895 wdev->netdev, info, GFP_KERNEL);
897 #ifdef CONFIG_CFG80211_WEXT
898 if (info->req_ie) {
899 memset(&wrqu, 0, sizeof(wrqu));
900 wrqu.data.length = info->req_ie_len;
901 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
902 &wrqu, info->req_ie);
905 if (info->resp_ie) {
906 memset(&wrqu, 0, sizeof(wrqu));
907 wrqu.data.length = info->resp_ie_len;
908 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
909 &wrqu, info->resp_ie);
912 memset(&wrqu, 0, sizeof(wrqu));
913 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
914 memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN);
915 memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN);
916 wdev->wext.prev_bssid_valid = true;
917 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
918 #endif
920 return;
921 out:
922 cfg80211_put_bss(wdev->wiphy, info->bss);
925 /* Consumes info->bss object one way or another */
926 void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
927 gfp_t gfp)
929 struct wireless_dev *wdev = dev->ieee80211_ptr;
930 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
931 struct cfg80211_event *ev;
932 unsigned long flags;
934 if (!info->bss) {
935 info->bss = cfg80211_get_bss(wdev->wiphy, info->channel,
936 info->bssid, wdev->ssid,
937 wdev->ssid_len,
938 wdev->conn_bss_type,
939 IEEE80211_PRIVACY_ANY);
942 if (WARN_ON(!info->bss))
943 return;
945 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len, gfp);
946 if (!ev) {
947 cfg80211_put_bss(wdev->wiphy, info->bss);
948 return;
951 ev->type = EVENT_ROAMED;
952 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
953 ev->rm.req_ie_len = info->req_ie_len;
954 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
955 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + info->req_ie_len;
956 ev->rm.resp_ie_len = info->resp_ie_len;
957 memcpy((void *)ev->rm.resp_ie, info->resp_ie, info->resp_ie_len);
958 ev->rm.bss = info->bss;
960 spin_lock_irqsave(&wdev->event_lock, flags);
961 list_add_tail(&ev->list, &wdev->event_list);
962 spin_unlock_irqrestore(&wdev->event_lock, flags);
963 queue_work(cfg80211_wq, &rdev->event_work);
965 EXPORT_SYMBOL(cfg80211_roamed);
967 void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid)
969 ASSERT_WDEV_LOCK(wdev);
971 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
972 return;
974 if (WARN_ON(!wdev->current_bss) ||
975 WARN_ON(!ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
976 return;
978 nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
979 bssid);
982 void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
983 gfp_t gfp)
985 struct wireless_dev *wdev = dev->ieee80211_ptr;
986 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
987 struct cfg80211_event *ev;
988 unsigned long flags;
990 if (WARN_ON(!bssid))
991 return;
993 ev = kzalloc(sizeof(*ev), gfp);
994 if (!ev)
995 return;
997 ev->type = EVENT_PORT_AUTHORIZED;
998 memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1001 * Use the wdev event list so that if there are pending
1002 * connected/roamed events, they will be reported first.
1004 spin_lock_irqsave(&wdev->event_lock, flags);
1005 list_add_tail(&ev->list, &wdev->event_list);
1006 spin_unlock_irqrestore(&wdev->event_lock, flags);
1007 queue_work(cfg80211_wq, &rdev->event_work);
1009 EXPORT_SYMBOL(cfg80211_port_authorized);
1011 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
1012 size_t ie_len, u16 reason, bool from_ap)
1014 struct wireless_dev *wdev = dev->ieee80211_ptr;
1015 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1016 int i;
1017 #ifdef CONFIG_CFG80211_WEXT
1018 union iwreq_data wrqu;
1019 #endif
1021 ASSERT_WDEV_LOCK(wdev);
1023 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1024 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1025 return;
1027 if (wdev->current_bss) {
1028 cfg80211_unhold_bss(wdev->current_bss);
1029 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
1032 wdev->current_bss = NULL;
1033 wdev->ssid_len = 0;
1034 wdev->conn_owner_nlportid = 0;
1035 kzfree(wdev->connect_keys);
1036 wdev->connect_keys = NULL;
1038 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1040 /* stop critical protocol if supported */
1041 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1042 rdev->crit_proto_nlportid = 0;
1043 rdev_crit_proto_stop(rdev, wdev);
1047 * Delete all the keys ... pairwise keys can't really
1048 * exist any more anyway, but default keys might.
1050 if (rdev->ops->del_key)
1051 for (i = 0; i < 6; i++)
1052 rdev_del_key(rdev, dev, i, false, NULL);
1054 rdev_set_qos_map(rdev, dev, NULL);
1056 #ifdef CONFIG_CFG80211_WEXT
1057 memset(&wrqu, 0, sizeof(wrqu));
1058 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1059 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1060 wdev->wext.connect.ssid_len = 0;
1061 #endif
1063 schedule_work(&cfg80211_disconnect_work);
1066 void cfg80211_disconnected(struct net_device *dev, u16 reason,
1067 const u8 *ie, size_t ie_len,
1068 bool locally_generated, gfp_t gfp)
1070 struct wireless_dev *wdev = dev->ieee80211_ptr;
1071 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1072 struct cfg80211_event *ev;
1073 unsigned long flags;
1075 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1076 if (!ev)
1077 return;
1079 ev->type = EVENT_DISCONNECTED;
1080 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1081 ev->dc.ie_len = ie_len;
1082 memcpy((void *)ev->dc.ie, ie, ie_len);
1083 ev->dc.reason = reason;
1084 ev->dc.locally_generated = locally_generated;
1086 spin_lock_irqsave(&wdev->event_lock, flags);
1087 list_add_tail(&ev->list, &wdev->event_list);
1088 spin_unlock_irqrestore(&wdev->event_lock, flags);
1089 queue_work(cfg80211_wq, &rdev->event_work);
1091 EXPORT_SYMBOL(cfg80211_disconnected);
1094 * API calls for nl80211/wext compatibility code
1096 int cfg80211_connect(struct cfg80211_registered_device *rdev,
1097 struct net_device *dev,
1098 struct cfg80211_connect_params *connect,
1099 struct cfg80211_cached_keys *connkeys,
1100 const u8 *prev_bssid)
1102 struct wireless_dev *wdev = dev->ieee80211_ptr;
1103 int err;
1105 ASSERT_WDEV_LOCK(wdev);
1108 * If we have an ssid_len, we're trying to connect or are
1109 * already connected, so reject a new SSID unless it's the
1110 * same (which is the case for re-association.)
1112 if (wdev->ssid_len &&
1113 (wdev->ssid_len != connect->ssid_len ||
1114 memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
1115 return -EALREADY;
1118 * If connected, reject (re-)association unless prev_bssid
1119 * matches the current BSSID.
1121 if (wdev->current_bss) {
1122 if (!prev_bssid)
1123 return -EALREADY;
1124 if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
1125 return -ENOTCONN;
1129 * Reject if we're in the process of connecting with WEP,
1130 * this case isn't very interesting and trying to handle
1131 * it would make the code much more complex.
1133 if (wdev->connect_keys)
1134 return -EINPROGRESS;
1136 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1137 rdev->wiphy.ht_capa_mod_mask);
1139 if (connkeys && connkeys->def >= 0) {
1140 int idx;
1141 u32 cipher;
1143 idx = connkeys->def;
1144 cipher = connkeys->params[idx].cipher;
1145 /* If given a WEP key we may need it for shared key auth */
1146 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1147 cipher == WLAN_CIPHER_SUITE_WEP104) {
1148 connect->key_idx = idx;
1149 connect->key = connkeys->params[idx].key;
1150 connect->key_len = connkeys->params[idx].key_len;
1153 * If ciphers are not set (e.g. when going through
1154 * iwconfig), we have to set them appropriately here.
1156 if (connect->crypto.cipher_group == 0)
1157 connect->crypto.cipher_group = cipher;
1159 if (connect->crypto.n_ciphers_pairwise == 0) {
1160 connect->crypto.n_ciphers_pairwise = 1;
1161 connect->crypto.ciphers_pairwise[0] = cipher;
1165 connect->crypto.wep_keys = connkeys->params;
1166 connect->crypto.wep_tx_key = connkeys->def;
1167 } else {
1168 if (WARN_ON(connkeys))
1169 return -EINVAL;
1172 wdev->connect_keys = connkeys;
1173 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1174 wdev->ssid_len = connect->ssid_len;
1176 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1177 IEEE80211_BSS_TYPE_ESS;
1179 if (!rdev->ops->connect)
1180 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1181 else
1182 err = rdev_connect(rdev, dev, connect);
1184 if (err) {
1185 wdev->connect_keys = NULL;
1187 * This could be reassoc getting refused, don't clear
1188 * ssid_len in that case.
1190 if (!wdev->current_bss)
1191 wdev->ssid_len = 0;
1192 return err;
1195 return 0;
1198 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1199 struct net_device *dev, u16 reason, bool wextev)
1201 struct wireless_dev *wdev = dev->ieee80211_ptr;
1202 int err = 0;
1204 ASSERT_WDEV_LOCK(wdev);
1206 kzfree(wdev->connect_keys);
1207 wdev->connect_keys = NULL;
1209 wdev->conn_owner_nlportid = 0;
1211 if (wdev->conn)
1212 err = cfg80211_sme_disconnect(wdev, reason);
1213 else if (!rdev->ops->disconnect)
1214 cfg80211_mlme_down(rdev, dev);
1215 else if (wdev->ssid_len)
1216 err = rdev_disconnect(rdev, dev, reason);
1219 * Clear ssid_len unless we actually were fully connected,
1220 * in which case cfg80211_disconnected() will take care of
1221 * this later.
1223 if (!wdev->current_bss)
1224 wdev->ssid_len = 0;
1226 return err;
1230 * Used to clean up after the connection / connection attempt owner socket
1231 * disconnects
1233 void cfg80211_autodisconnect_wk(struct work_struct *work)
1235 struct wireless_dev *wdev =
1236 container_of(work, struct wireless_dev, disconnect_wk);
1237 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1239 wdev_lock(wdev);
1241 if (wdev->conn_owner_nlportid) {
1243 * Use disconnect_bssid if still connecting and ops->disconnect
1244 * not implemented. Otherwise we can use cfg80211_disconnect.
1246 if (rdev->ops->disconnect || wdev->current_bss)
1247 cfg80211_disconnect(rdev, wdev->netdev,
1248 WLAN_REASON_DEAUTH_LEAVING, true);
1249 else
1250 cfg80211_mlme_deauth(rdev, wdev->netdev,
1251 wdev->disconnect_bssid, NULL, 0,
1252 WLAN_REASON_DEAUTH_LEAVING, false);
1255 wdev_unlock(wdev);