1 // SPDX-License-Identifier: GPL-2.0
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, 2020 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>
26 * Software SME in cfg80211, using auth/assoc/deauth calls to the
27 * driver. This 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 */
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,
45 CFG80211_CONN_ABANDON,
46 CFG80211_CONN_CONNECTED,
48 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
51 bool auto_auth, prev_bssid_valid;
54 static void cfg80211_sme_free(struct wireless_dev *wdev)
59 kfree(wdev->conn->ie);
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;
70 ASSERT_WDEV_LOCK(wdev);
72 if (rdev->scan_req || rdev->scan_msg)
75 if (wdev->conn->params.channel)
78 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
80 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
81 sizeof(request->channels[0]) * n_channels,
86 if (wdev->conn->params.channel) {
87 enum nl80211_band band = wdev->conn->params.channel->band;
88 struct ieee80211_supported_band *sband =
89 wdev->wiphy->bands[band];
95 request->channels[0] = wdev->conn->params.channel;
96 request->rates[band] = (1 << sband->n_bitrates) - 1;
99 enum nl80211_band band;
100 struct ieee80211_supported_band *bands;
101 struct ieee80211_channel *channel;
103 for (band = 0; band < NUM_NL80211_BANDS; band++) {
104 bands = wdev->wiphy->bands[band];
107 for (j = 0; j < bands->n_channels; j++) {
108 channel = &bands->channels[j];
109 if (channel->flags & IEEE80211_CHAN_DISABLED)
111 request->channels[i++] = channel;
113 request->rates[band] = (1 << bands->n_bitrates) - 1;
117 request->n_channels = n_channels;
118 request->ssids = (void *)&request->channels[n_channels];
119 request->n_ssids = 1;
121 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
122 wdev->conn->params.ssid_len);
123 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
125 eth_broadcast_addr(request->bssid);
127 request->wdev = wdev;
128 request->wiphy = &rdev->wiphy;
129 request->scan_start = jiffies;
131 rdev->scan_req = request;
133 err = rdev_scan(rdev, request);
135 wdev->conn->state = CFG80211_CONN_SCANNING;
136 nl80211_send_scan_start(rdev, wdev);
137 dev_hold(wdev->netdev);
139 rdev->scan_req = NULL;
145 static int cfg80211_conn_do_work(struct wireless_dev *wdev,
146 enum nl80211_timeout_reason *treason)
148 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
149 struct cfg80211_connect_params *params;
150 struct cfg80211_assoc_request req = {};
153 ASSERT_WDEV_LOCK(wdev);
158 params = &wdev->conn->params;
160 switch (wdev->conn->state) {
161 case CFG80211_CONN_SCANNING:
162 /* didn't find it during scan ... */
164 case CFG80211_CONN_SCAN_AGAIN:
165 return cfg80211_conn_scan(wdev);
166 case CFG80211_CONN_AUTHENTICATE_NEXT:
167 if (WARN_ON(!rdev->ops->auth))
169 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
170 return cfg80211_mlme_auth(rdev, wdev->netdev,
171 params->channel, params->auth_type,
173 params->ssid, params->ssid_len,
175 params->key, params->key_len,
176 params->key_idx, NULL, 0);
177 case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
178 *treason = NL80211_TIMEOUT_AUTH;
180 case CFG80211_CONN_ASSOCIATE_NEXT:
181 if (WARN_ON(!rdev->ops->assoc))
183 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
184 if (wdev->conn->prev_bssid_valid)
185 req.prev_bssid = wdev->conn->prev_bssid;
187 req.ie_len = params->ie_len;
188 req.use_mfp = params->mfp != NL80211_MFP_NO;
189 req.crypto = params->crypto;
190 req.flags = params->flags;
191 req.ht_capa = params->ht_capa;
192 req.ht_capa_mask = params->ht_capa_mask;
193 req.vht_capa = params->vht_capa;
194 req.vht_capa_mask = params->vht_capa_mask;
196 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
197 params->bssid, params->ssid,
198 params->ssid_len, &req);
200 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 WLAN_REASON_DEAUTH_LEAVING,
205 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
206 *treason = NL80211_TIMEOUT_ASSOC;
208 case CFG80211_CONN_ASSOC_FAILED:
209 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
211 WLAN_REASON_DEAUTH_LEAVING, false);
213 case CFG80211_CONN_DEAUTH:
214 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
216 WLAN_REASON_DEAUTH_LEAVING, false);
218 case CFG80211_CONN_ABANDON:
219 /* free directly, disconnected event already sent */
220 cfg80211_sme_free(wdev);
227 void cfg80211_conn_work(struct work_struct *work)
229 struct cfg80211_registered_device *rdev =
230 container_of(work, struct cfg80211_registered_device, conn_work);
231 struct wireless_dev *wdev;
232 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
233 enum nl80211_timeout_reason treason;
235 wiphy_lock(&rdev->wiphy);
237 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
242 if (!netif_running(wdev->netdev)) {
247 wdev->conn->state == CFG80211_CONN_CONNECTED) {
251 if (wdev->conn->params.bssid) {
252 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
255 treason = NL80211_TIMEOUT_UNSPECIFIED;
256 if (cfg80211_conn_do_work(wdev, &treason)) {
257 struct cfg80211_connect_resp_params cr;
259 memset(&cr, 0, sizeof(cr));
262 cr.timeout_reason = treason;
263 __cfg80211_connect_result(wdev->netdev, &cr, false);
268 wiphy_unlock(&rdev->wiphy);
271 /* Returned bss is reference counted and must be cleaned up appropriately. */
272 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
274 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
275 struct cfg80211_bss *bss;
277 ASSERT_WDEV_LOCK(wdev);
279 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
280 wdev->conn->params.bssid,
281 wdev->conn->params.ssid,
282 wdev->conn->params.ssid_len,
284 IEEE80211_PRIVACY(wdev->conn->params.privacy));
288 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
289 wdev->conn->params.bssid = wdev->conn->bssid;
290 wdev->conn->params.channel = bss->channel;
291 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
292 schedule_work(&rdev->conn_work);
297 static void __cfg80211_sme_scan_done(struct net_device *dev)
299 struct wireless_dev *wdev = dev->ieee80211_ptr;
300 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
301 struct cfg80211_bss *bss;
303 ASSERT_WDEV_LOCK(wdev);
308 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
309 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
312 bss = cfg80211_get_conn_bss(wdev);
314 cfg80211_put_bss(&rdev->wiphy, bss);
316 schedule_work(&rdev->conn_work);
319 void cfg80211_sme_scan_done(struct net_device *dev)
321 struct wireless_dev *wdev = dev->ieee80211_ptr;
324 __cfg80211_sme_scan_done(dev);
328 void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
330 struct wiphy *wiphy = wdev->wiphy;
331 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
332 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
333 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
335 ASSERT_WDEV_LOCK(wdev);
337 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
340 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
341 wdev->conn->auto_auth &&
342 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
343 /* select automatically between only open, shared, leap */
344 switch (wdev->conn->params.auth_type) {
345 case NL80211_AUTHTYPE_OPEN_SYSTEM:
346 if (wdev->connect_keys)
347 wdev->conn->params.auth_type =
348 NL80211_AUTHTYPE_SHARED_KEY;
350 wdev->conn->params.auth_type =
351 NL80211_AUTHTYPE_NETWORK_EAP;
353 case NL80211_AUTHTYPE_SHARED_KEY:
354 wdev->conn->params.auth_type =
355 NL80211_AUTHTYPE_NETWORK_EAP;
359 wdev->conn->params.auth_type =
360 NL80211_AUTHTYPE_OPEN_SYSTEM;
363 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
364 schedule_work(&rdev->conn_work);
365 } else if (status_code != WLAN_STATUS_SUCCESS) {
366 struct cfg80211_connect_resp_params cr;
368 memset(&cr, 0, sizeof(cr));
369 cr.status = status_code;
370 cr.bssid = mgmt->bssid;
371 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
372 __cfg80211_connect_result(wdev->netdev, &cr, false);
373 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
374 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
375 schedule_work(&rdev->conn_work);
379 bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
381 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
386 if (status == WLAN_STATUS_SUCCESS) {
387 wdev->conn->state = CFG80211_CONN_CONNECTED;
391 if (wdev->conn->prev_bssid_valid) {
393 * Some stupid APs don't accept reassoc, so we
394 * need to fall back to trying regular assoc;
395 * return true so no event is sent to userspace.
397 wdev->conn->prev_bssid_valid = false;
398 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
399 schedule_work(&rdev->conn_work);
403 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
404 schedule_work(&rdev->conn_work);
408 void cfg80211_sme_deauth(struct wireless_dev *wdev)
410 cfg80211_sme_free(wdev);
413 void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
415 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
420 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
421 schedule_work(&rdev->conn_work);
424 void cfg80211_sme_disassoc(struct wireless_dev *wdev)
426 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
431 wdev->conn->state = CFG80211_CONN_DEAUTH;
432 schedule_work(&rdev->conn_work);
435 void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
437 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
442 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
443 schedule_work(&rdev->conn_work);
446 void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
448 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
453 wdev->conn->state = CFG80211_CONN_ABANDON;
454 schedule_work(&rdev->conn_work);
457 static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
458 const u8 *ies, size_t ies_len,
459 const u8 **out_ies, size_t *out_ies_len)
461 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
465 if (!rdev->wiphy.extended_capabilities_len ||
466 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
467 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
470 *out_ies_len = ies_len;
474 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
480 static const u8 before_extcapa[] = {
481 /* not listing IEs expected to be created by driver */
484 WLAN_EID_RRM_ENABLED_CAPABILITIES,
485 WLAN_EID_MOBILITY_DOMAIN,
486 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
487 WLAN_EID_BSS_COEX_2040,
490 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
491 ARRAY_SIZE(before_extcapa), 0);
492 memcpy(buf, ies, offs);
493 /* leave a whole for extended capabilities IE */
494 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
495 ies + offs, ies_len - offs);
500 /* place extended capabilities IE (with only driver capabilities) */
501 buf[offs] = WLAN_EID_EXT_CAPABILITY;
502 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
503 memcpy(buf + offs + 2,
504 rdev->wiphy.extended_capabilities,
505 rdev->wiphy.extended_capabilities_len);
508 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
513 static int cfg80211_sme_connect(struct wireless_dev *wdev,
514 struct cfg80211_connect_params *connect,
515 const u8 *prev_bssid)
517 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
518 struct cfg80211_bss *bss;
521 if (!rdev->ops->auth || !rdev->ops->assoc)
524 if (wdev->current_bss) {
525 cfg80211_unhold_bss(wdev->current_bss);
526 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
527 wdev->current_bss = NULL;
529 cfg80211_sme_free(wdev);
535 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
540 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
542 memcpy(&wdev->conn->params, connect, sizeof(*connect));
543 if (connect->bssid) {
544 wdev->conn->params.bssid = wdev->conn->bssid;
545 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
548 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
550 &wdev->conn->params.ie_len)) {
555 wdev->conn->params.ie = wdev->conn->ie;
557 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
558 wdev->conn->auto_auth = true;
559 /* start with open system ... should mostly work */
560 wdev->conn->params.auth_type =
561 NL80211_AUTHTYPE_OPEN_SYSTEM;
563 wdev->conn->auto_auth = false;
566 wdev->conn->params.ssid = wdev->ssid;
567 wdev->conn->params.ssid_len = wdev->ssid_len;
569 /* see if we have the bss already */
570 bss = cfg80211_get_conn_bss(wdev);
573 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
574 wdev->conn->prev_bssid_valid = true;
577 /* we're good if we have a matching bss struct */
579 enum nl80211_timeout_reason treason;
581 err = cfg80211_conn_do_work(wdev, &treason);
582 cfg80211_put_bss(wdev->wiphy, bss);
584 /* otherwise we'll need to scan for the AP first */
585 err = cfg80211_conn_scan(wdev);
588 * If we can't scan right now, then we need to scan again
589 * after the current scan finished, since the parameters
590 * changed (unless we find a good AP anyway).
594 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
599 cfg80211_sme_free(wdev);
604 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
606 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
612 if (!rdev->ops->deauth)
615 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
616 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
621 /* wdev->conn->params.bssid must be set if > SCANNING */
622 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
623 wdev->conn->params.bssid,
624 NULL, 0, reason, false);
626 cfg80211_sme_free(wdev);
631 * code shared for in-device and software SME
634 static bool cfg80211_is_all_idle(void)
636 struct cfg80211_registered_device *rdev;
637 struct wireless_dev *wdev;
638 bool is_all_idle = true;
641 * All devices must be idle as otherwise if you are actively
642 * scanning some new beacon hints could be learned and would
643 * count as new regulatory hints.
644 * Also if there is any other active beaconing interface we
645 * need not issue a disconnect hint and reset any info such
646 * as chan dfs state, etc.
648 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
649 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
651 if (wdev->conn || wdev->current_bss ||
652 cfg80211_beaconing_iface_active(wdev))
661 static void disconnect_work(struct work_struct *work)
664 if (cfg80211_is_all_idle())
665 regulatory_hint_disconnect();
669 DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
673 * API calls for drivers implementing connect/disconnect and
677 /* This method must consume bss one way or another */
678 void __cfg80211_connect_result(struct net_device *dev,
679 struct cfg80211_connect_resp_params *cr,
682 struct wireless_dev *wdev = dev->ieee80211_ptr;
683 const u8 *country_ie;
684 #ifdef CONFIG_CFG80211_WEXT
685 union iwreq_data wrqu;
688 ASSERT_WDEV_LOCK(wdev);
690 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
691 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
692 cfg80211_put_bss(wdev->wiphy, cr->bss);
696 wdev->unprot_beacon_reported = 0;
697 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
700 #ifdef CONFIG_CFG80211_WEXT
702 if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
703 memset(&wrqu, 0, sizeof(wrqu));
704 wrqu.data.length = cr->req_ie_len;
705 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
709 if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
710 memset(&wrqu, 0, sizeof(wrqu));
711 wrqu.data.length = cr->resp_ie_len;
712 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
716 memset(&wrqu, 0, sizeof(wrqu));
717 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
718 if (cr->bssid && cr->status == WLAN_STATUS_SUCCESS) {
719 memcpy(wrqu.ap_addr.sa_data, cr->bssid, ETH_ALEN);
720 memcpy(wdev->wext.prev_bssid, cr->bssid, ETH_ALEN);
721 wdev->wext.prev_bssid_valid = true;
723 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
727 if (!cr->bss && (cr->status == WLAN_STATUS_SUCCESS)) {
728 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
729 cr->bss = cfg80211_get_bss(wdev->wiphy, NULL, cr->bssid,
730 wdev->ssid, wdev->ssid_len,
732 IEEE80211_PRIVACY_ANY);
734 cfg80211_hold_bss(bss_from_pub(cr->bss));
737 if (wdev->current_bss) {
738 cfg80211_unhold_bss(wdev->current_bss);
739 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
740 wdev->current_bss = NULL;
743 if (cr->status != WLAN_STATUS_SUCCESS) {
744 kfree_sensitive(wdev->connect_keys);
745 wdev->connect_keys = NULL;
747 wdev->conn_owner_nlportid = 0;
749 cfg80211_unhold_bss(bss_from_pub(cr->bss));
750 cfg80211_put_bss(wdev->wiphy, cr->bss);
752 cfg80211_sme_free(wdev);
756 if (WARN_ON(!cr->bss))
759 wdev->current_bss = bss_from_pub(cr->bss);
761 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
762 cfg80211_upload_connect_keys(wdev);
765 country_ie = ieee80211_bss_get_ie(cr->bss, WLAN_EID_COUNTRY);
771 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
778 * ieee80211_bss_get_ie() ensures we can access:
779 * - country_ie + 2, the start of the country ie data, and
780 * - and country_ie[1] which is the IE length
782 regulatory_hint_country_ie(wdev->wiphy, cr->bss->channel->band,
783 country_ie + 2, country_ie[1]);
787 /* Consumes bss object one way or another */
788 void cfg80211_connect_done(struct net_device *dev,
789 struct cfg80211_connect_resp_params *params,
792 struct wireless_dev *wdev = dev->ieee80211_ptr;
793 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
794 struct cfg80211_event *ev;
799 struct cfg80211_internal_bss *ibss = bss_from_pub(params->bss);
801 if (list_empty(&ibss->list)) {
802 struct cfg80211_bss *found = NULL, *tmp = params->bss;
804 found = cfg80211_get_bss(wdev->wiphy, NULL,
806 wdev->ssid, wdev->ssid_len,
808 IEEE80211_PRIVACY_ANY);
810 /* The same BSS is already updated so use it
811 * instead, as it has latest info.
815 /* Update with BSS provided by driver, it will
816 * be freshly added and ref cnted, we can free
819 * signal_valid can be false, as we are not
820 * expecting the BSS to be found.
822 * keep the old timestamp to avoid confusion
824 cfg80211_bss_update(rdev, ibss, false,
828 cfg80211_put_bss(wdev->wiphy, tmp);
832 ev = kzalloc(sizeof(*ev) + (params->bssid ? ETH_ALEN : 0) +
833 params->req_ie_len + params->resp_ie_len +
834 params->fils.kek_len + params->fils.pmk_len +
835 (params->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
837 cfg80211_put_bss(wdev->wiphy, params->bss);
841 ev->type = EVENT_CONNECT_RESULT;
842 next = ((u8 *)ev) + sizeof(*ev);
845 memcpy((void *)ev->cr.bssid, params->bssid, ETH_ALEN);
848 if (params->req_ie_len) {
849 ev->cr.req_ie = next;
850 ev->cr.req_ie_len = params->req_ie_len;
851 memcpy((void *)ev->cr.req_ie, params->req_ie,
853 next += params->req_ie_len;
855 if (params->resp_ie_len) {
856 ev->cr.resp_ie = next;
857 ev->cr.resp_ie_len = params->resp_ie_len;
858 memcpy((void *)ev->cr.resp_ie, params->resp_ie,
859 params->resp_ie_len);
860 next += params->resp_ie_len;
862 if (params->fils.kek_len) {
863 ev->cr.fils.kek = next;
864 ev->cr.fils.kek_len = params->fils.kek_len;
865 memcpy((void *)ev->cr.fils.kek, params->fils.kek,
866 params->fils.kek_len);
867 next += params->fils.kek_len;
869 if (params->fils.pmk_len) {
870 ev->cr.fils.pmk = next;
871 ev->cr.fils.pmk_len = params->fils.pmk_len;
872 memcpy((void *)ev->cr.fils.pmk, params->fils.pmk,
873 params->fils.pmk_len);
874 next += params->fils.pmk_len;
876 if (params->fils.pmkid) {
877 ev->cr.fils.pmkid = next;
878 memcpy((void *)ev->cr.fils.pmkid, params->fils.pmkid,
880 next += WLAN_PMKID_LEN;
882 ev->cr.fils.update_erp_next_seq_num = params->fils.update_erp_next_seq_num;
883 if (params->fils.update_erp_next_seq_num)
884 ev->cr.fils.erp_next_seq_num = params->fils.erp_next_seq_num;
886 cfg80211_hold_bss(bss_from_pub(params->bss));
887 ev->cr.bss = params->bss;
888 ev->cr.status = params->status;
889 ev->cr.timeout_reason = params->timeout_reason;
891 spin_lock_irqsave(&wdev->event_lock, flags);
892 list_add_tail(&ev->list, &wdev->event_list);
893 spin_unlock_irqrestore(&wdev->event_lock, flags);
894 queue_work(cfg80211_wq, &rdev->event_work);
896 EXPORT_SYMBOL(cfg80211_connect_done);
898 /* Consumes bss object one way or another */
899 void __cfg80211_roamed(struct wireless_dev *wdev,
900 struct cfg80211_roam_info *info)
902 #ifdef CONFIG_CFG80211_WEXT
903 union iwreq_data wrqu;
905 ASSERT_WDEV_LOCK(wdev);
907 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
908 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
911 if (WARN_ON(!wdev->current_bss))
914 cfg80211_unhold_bss(wdev->current_bss);
915 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
916 wdev->current_bss = NULL;
918 if (WARN_ON(!info->bss))
921 cfg80211_hold_bss(bss_from_pub(info->bss));
922 wdev->current_bss = bss_from_pub(info->bss);
924 wdev->unprot_beacon_reported = 0;
925 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
926 wdev->netdev, info, GFP_KERNEL);
928 #ifdef CONFIG_CFG80211_WEXT
930 memset(&wrqu, 0, sizeof(wrqu));
931 wrqu.data.length = info->req_ie_len;
932 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
933 &wrqu, info->req_ie);
937 memset(&wrqu, 0, sizeof(wrqu));
938 wrqu.data.length = info->resp_ie_len;
939 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
940 &wrqu, info->resp_ie);
943 memset(&wrqu, 0, sizeof(wrqu));
944 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
945 memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN);
946 memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN);
947 wdev->wext.prev_bssid_valid = true;
948 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
953 cfg80211_put_bss(wdev->wiphy, info->bss);
956 /* Consumes info->bss object one way or another */
957 void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
960 struct wireless_dev *wdev = dev->ieee80211_ptr;
961 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
962 struct cfg80211_event *ev;
967 info->bss = cfg80211_get_bss(wdev->wiphy, info->channel,
968 info->bssid, wdev->ssid,
971 IEEE80211_PRIVACY_ANY);
974 if (WARN_ON(!info->bss))
977 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
978 info->fils.kek_len + info->fils.pmk_len +
979 (info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
981 cfg80211_put_bss(wdev->wiphy, info->bss);
985 ev->type = EVENT_ROAMED;
986 next = ((u8 *)ev) + sizeof(*ev);
987 if (info->req_ie_len) {
988 ev->rm.req_ie = next;
989 ev->rm.req_ie_len = info->req_ie_len;
990 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
991 next += info->req_ie_len;
993 if (info->resp_ie_len) {
994 ev->rm.resp_ie = next;
995 ev->rm.resp_ie_len = info->resp_ie_len;
996 memcpy((void *)ev->rm.resp_ie, info->resp_ie,
998 next += info->resp_ie_len;
1000 if (info->fils.kek_len) {
1001 ev->rm.fils.kek = next;
1002 ev->rm.fils.kek_len = info->fils.kek_len;
1003 memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1004 info->fils.kek_len);
1005 next += info->fils.kek_len;
1007 if (info->fils.pmk_len) {
1008 ev->rm.fils.pmk = next;
1009 ev->rm.fils.pmk_len = info->fils.pmk_len;
1010 memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1011 info->fils.pmk_len);
1012 next += info->fils.pmk_len;
1014 if (info->fils.pmkid) {
1015 ev->rm.fils.pmkid = next;
1016 memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1018 next += WLAN_PMKID_LEN;
1020 ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1021 if (info->fils.update_erp_next_seq_num)
1022 ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
1023 ev->rm.bss = info->bss;
1025 spin_lock_irqsave(&wdev->event_lock, flags);
1026 list_add_tail(&ev->list, &wdev->event_list);
1027 spin_unlock_irqrestore(&wdev->event_lock, flags);
1028 queue_work(cfg80211_wq, &rdev->event_work);
1030 EXPORT_SYMBOL(cfg80211_roamed);
1032 void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid)
1034 ASSERT_WDEV_LOCK(wdev);
1036 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
1039 if (WARN_ON(!wdev->current_bss) ||
1040 WARN_ON(!ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
1043 nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
1047 void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
1050 struct wireless_dev *wdev = dev->ieee80211_ptr;
1051 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1052 struct cfg80211_event *ev;
1053 unsigned long flags;
1055 if (WARN_ON(!bssid))
1058 ev = kzalloc(sizeof(*ev), gfp);
1062 ev->type = EVENT_PORT_AUTHORIZED;
1063 memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1066 * Use the wdev event list so that if there are pending
1067 * connected/roamed events, they will be reported first.
1069 spin_lock_irqsave(&wdev->event_lock, flags);
1070 list_add_tail(&ev->list, &wdev->event_list);
1071 spin_unlock_irqrestore(&wdev->event_lock, flags);
1072 queue_work(cfg80211_wq, &rdev->event_work);
1074 EXPORT_SYMBOL(cfg80211_port_authorized);
1076 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
1077 size_t ie_len, u16 reason, bool from_ap)
1079 struct wireless_dev *wdev = dev->ieee80211_ptr;
1080 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1082 #ifdef CONFIG_CFG80211_WEXT
1083 union iwreq_data wrqu;
1086 ASSERT_WDEV_LOCK(wdev);
1088 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1089 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1092 if (wdev->current_bss) {
1093 cfg80211_unhold_bss(wdev->current_bss);
1094 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
1097 wdev->current_bss = NULL;
1099 wdev->conn_owner_nlportid = 0;
1100 kfree_sensitive(wdev->connect_keys);
1101 wdev->connect_keys = NULL;
1103 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1105 /* stop critical protocol if supported */
1106 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1107 rdev->crit_proto_nlportid = 0;
1108 rdev_crit_proto_stop(rdev, wdev);
1112 * Delete all the keys ... pairwise keys can't really
1113 * exist any more anyway, but default keys might.
1115 if (rdev->ops->del_key) {
1116 int max_key_idx = 5;
1118 if (wiphy_ext_feature_isset(
1120 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
1121 wiphy_ext_feature_isset(
1123 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
1125 for (i = 0; i <= max_key_idx; i++)
1126 rdev_del_key(rdev, dev, i, false, NULL);
1129 rdev_set_qos_map(rdev, dev, NULL);
1131 #ifdef CONFIG_CFG80211_WEXT
1132 memset(&wrqu, 0, sizeof(wrqu));
1133 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1134 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1135 wdev->wext.connect.ssid_len = 0;
1138 schedule_work(&cfg80211_disconnect_work);
1141 void cfg80211_disconnected(struct net_device *dev, u16 reason,
1142 const u8 *ie, size_t ie_len,
1143 bool locally_generated, gfp_t gfp)
1145 struct wireless_dev *wdev = dev->ieee80211_ptr;
1146 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1147 struct cfg80211_event *ev;
1148 unsigned long flags;
1150 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1154 ev->type = EVENT_DISCONNECTED;
1155 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1156 ev->dc.ie_len = ie_len;
1157 memcpy((void *)ev->dc.ie, ie, ie_len);
1158 ev->dc.reason = reason;
1159 ev->dc.locally_generated = locally_generated;
1161 spin_lock_irqsave(&wdev->event_lock, flags);
1162 list_add_tail(&ev->list, &wdev->event_list);
1163 spin_unlock_irqrestore(&wdev->event_lock, flags);
1164 queue_work(cfg80211_wq, &rdev->event_work);
1166 EXPORT_SYMBOL(cfg80211_disconnected);
1169 * API calls for nl80211/wext compatibility code
1171 int cfg80211_connect(struct cfg80211_registered_device *rdev,
1172 struct net_device *dev,
1173 struct cfg80211_connect_params *connect,
1174 struct cfg80211_cached_keys *connkeys,
1175 const u8 *prev_bssid)
1177 struct wireless_dev *wdev = dev->ieee80211_ptr;
1180 ASSERT_WDEV_LOCK(wdev);
1183 * If we have an ssid_len, we're trying to connect or are
1184 * already connected, so reject a new SSID unless it's the
1185 * same (which is the case for re-association.)
1187 if (wdev->ssid_len &&
1188 (wdev->ssid_len != connect->ssid_len ||
1189 memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
1193 * If connected, reject (re-)association unless prev_bssid
1194 * matches the current BSSID.
1196 if (wdev->current_bss) {
1199 if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
1204 * Reject if we're in the process of connecting with WEP,
1205 * this case isn't very interesting and trying to handle
1206 * it would make the code much more complex.
1208 if (wdev->connect_keys)
1209 return -EINPROGRESS;
1211 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1212 rdev->wiphy.ht_capa_mod_mask);
1213 cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
1214 rdev->wiphy.vht_capa_mod_mask);
1216 if (connkeys && connkeys->def >= 0) {
1220 idx = connkeys->def;
1221 cipher = connkeys->params[idx].cipher;
1222 /* If given a WEP key we may need it for shared key auth */
1223 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1224 cipher == WLAN_CIPHER_SUITE_WEP104) {
1225 connect->key_idx = idx;
1226 connect->key = connkeys->params[idx].key;
1227 connect->key_len = connkeys->params[idx].key_len;
1230 * If ciphers are not set (e.g. when going through
1231 * iwconfig), we have to set them appropriately here.
1233 if (connect->crypto.cipher_group == 0)
1234 connect->crypto.cipher_group = cipher;
1236 if (connect->crypto.n_ciphers_pairwise == 0) {
1237 connect->crypto.n_ciphers_pairwise = 1;
1238 connect->crypto.ciphers_pairwise[0] = cipher;
1242 connect->crypto.wep_keys = connkeys->params;
1243 connect->crypto.wep_tx_key = connkeys->def;
1245 if (WARN_ON(connkeys))
1249 wdev->connect_keys = connkeys;
1250 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1251 wdev->ssid_len = connect->ssid_len;
1253 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1254 IEEE80211_BSS_TYPE_ESS;
1256 if (!rdev->ops->connect)
1257 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1259 err = rdev_connect(rdev, dev, connect);
1262 wdev->connect_keys = NULL;
1264 * This could be reassoc getting refused, don't clear
1265 * ssid_len in that case.
1267 if (!wdev->current_bss)
1275 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1276 struct net_device *dev, u16 reason, bool wextev)
1278 struct wireless_dev *wdev = dev->ieee80211_ptr;
1281 ASSERT_WDEV_LOCK(wdev);
1283 kfree_sensitive(wdev->connect_keys);
1284 wdev->connect_keys = NULL;
1286 wdev->conn_owner_nlportid = 0;
1289 err = cfg80211_sme_disconnect(wdev, reason);
1290 else if (!rdev->ops->disconnect)
1291 cfg80211_mlme_down(rdev, dev);
1292 else if (wdev->ssid_len)
1293 err = rdev_disconnect(rdev, dev, reason);
1296 * Clear ssid_len unless we actually were fully connected,
1297 * in which case cfg80211_disconnected() will take care of
1300 if (!wdev->current_bss)
1307 * Used to clean up after the connection / connection attempt owner socket
1310 void cfg80211_autodisconnect_wk(struct work_struct *work)
1312 struct wireless_dev *wdev =
1313 container_of(work, struct wireless_dev, disconnect_wk);
1314 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1318 if (wdev->conn_owner_nlportid) {
1319 switch (wdev->iftype) {
1320 case NL80211_IFTYPE_ADHOC:
1321 __cfg80211_leave_ibss(rdev, wdev->netdev, false);
1323 case NL80211_IFTYPE_AP:
1324 case NL80211_IFTYPE_P2P_GO:
1325 __cfg80211_stop_ap(rdev, wdev->netdev, false);
1327 case NL80211_IFTYPE_MESH_POINT:
1328 __cfg80211_leave_mesh(rdev, wdev->netdev);
1330 case NL80211_IFTYPE_STATION:
1331 case NL80211_IFTYPE_P2P_CLIENT:
1333 * Use disconnect_bssid if still connecting and
1334 * ops->disconnect not implemented. Otherwise we can
1335 * use cfg80211_disconnect.
1337 if (rdev->ops->disconnect || wdev->current_bss)
1338 cfg80211_disconnect(rdev, wdev->netdev,
1339 WLAN_REASON_DEAUTH_LEAVING,
1342 cfg80211_mlme_deauth(rdev, wdev->netdev,
1343 wdev->disconnect_bssid,
1345 WLAN_REASON_DEAUTH_LEAVING,