xref: /openbmc/linux/net/wireless/sme.c (revision 0545810f)
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)
6  *
7  * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
8  * Copyright (C) 2009, 2020, 2022 Intel Corporation. All rights reserved.
9  * Copyright 2017	Intel Deutschland GmbH
10  */
11 
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"
24 
25 /*
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.)
29  */
30 
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;
52 };
53 
54 static void cfg80211_sme_free(struct wireless_dev *wdev)
55 {
56 	if (!wdev->conn)
57 		return;
58 
59 	kfree(wdev->conn->ie);
60 	kfree(wdev->conn);
61 	wdev->conn = NULL;
62 }
63 
64 static int cfg80211_conn_scan(struct wireless_dev *wdev)
65 {
66 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
67 	struct cfg80211_scan_request *request;
68 	int n_channels, err;
69 
70 	ASSERT_WDEV_LOCK(wdev);
71 
72 	if (rdev->scan_req || rdev->scan_msg)
73 		return -EBUSY;
74 
75 	if (wdev->conn->params.channel)
76 		n_channels = 1;
77 	else
78 		n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
79 
80 	request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
81 			  sizeof(request->channels[0]) * n_channels,
82 			  GFP_KERNEL);
83 	if (!request)
84 		return -ENOMEM;
85 
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];
90 
91 		if (!sband) {
92 			kfree(request);
93 			return -EINVAL;
94 		}
95 		request->channels[0] = wdev->conn->params.channel;
96 		request->rates[band] = (1 << sband->n_bitrates) - 1;
97 	} else {
98 		int i = 0, j;
99 		enum nl80211_band band;
100 		struct ieee80211_supported_band *bands;
101 		struct ieee80211_channel *channel;
102 
103 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
104 			bands = wdev->wiphy->bands[band];
105 			if (!bands)
106 				continue;
107 			for (j = 0; j < bands->n_channels; j++) {
108 				channel = &bands->channels[j];
109 				if (channel->flags & IEEE80211_CHAN_DISABLED)
110 					continue;
111 				request->channels[i++] = channel;
112 			}
113 			request->rates[band] = (1 << bands->n_bitrates) - 1;
114 		}
115 		n_channels = i;
116 	}
117 	request->n_channels = n_channels;
118 	request->ssids = (void *)&request->channels[n_channels];
119 	request->n_ssids = 1;
120 
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;
124 
125 	eth_broadcast_addr(request->bssid);
126 
127 	request->wdev = wdev;
128 	request->wiphy = &rdev->wiphy;
129 	request->scan_start = jiffies;
130 
131 	rdev->scan_req = request;
132 
133 	err = rdev_scan(rdev, request);
134 	if (!err) {
135 		wdev->conn->state = CFG80211_CONN_SCANNING;
136 		nl80211_send_scan_start(rdev, wdev);
137 		dev_hold(wdev->netdev);
138 	} else {
139 		rdev->scan_req = NULL;
140 		kfree(request);
141 	}
142 	return err;
143 }
144 
145 static int cfg80211_conn_do_work(struct wireless_dev *wdev,
146 				 enum nl80211_timeout_reason *treason)
147 {
148 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
149 	struct cfg80211_connect_params *params;
150 	struct cfg80211_auth_request auth_req = {};
151 	struct cfg80211_assoc_request req = {};
152 	int err;
153 
154 	ASSERT_WDEV_LOCK(wdev);
155 
156 	if (!wdev->conn)
157 		return 0;
158 
159 	params = &wdev->conn->params;
160 
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 		auth_req.key = params->key;
172 		auth_req.key_len = params->key_len;
173 		auth_req.key_idx = params->key_idx;
174 		auth_req.auth_type = params->auth_type;
175 		auth_req.bss = cfg80211_get_bss(&rdev->wiphy, params->channel,
176 						params->bssid,
177 						params->ssid, params->ssid_len,
178 						IEEE80211_BSS_TYPE_ESS,
179 						IEEE80211_PRIVACY_ANY);
180 		auth_req.link_id = -1;
181 		err = cfg80211_mlme_auth(rdev, wdev->netdev, &auth_req);
182 		cfg80211_put_bss(&rdev->wiphy, auth_req.bss);
183 		return err;
184 	case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
185 		*treason = NL80211_TIMEOUT_AUTH;
186 		return -ENOTCONN;
187 	case CFG80211_CONN_ASSOCIATE_NEXT:
188 		if (WARN_ON(!rdev->ops->assoc))
189 			return -EOPNOTSUPP;
190 		wdev->conn->state = CFG80211_CONN_ASSOCIATING;
191 		if (wdev->conn->prev_bssid_valid)
192 			req.prev_bssid = wdev->conn->prev_bssid;
193 		req.ie = params->ie;
194 		req.ie_len = params->ie_len;
195 		req.use_mfp = params->mfp != NL80211_MFP_NO;
196 		req.crypto = params->crypto;
197 		req.flags = params->flags;
198 		req.ht_capa = params->ht_capa;
199 		req.ht_capa_mask = params->ht_capa_mask;
200 		req.vht_capa = params->vht_capa;
201 		req.vht_capa_mask = params->vht_capa_mask;
202 		req.link_id = -1;
203 
204 		req.bss = cfg80211_get_bss(&rdev->wiphy, params->channel,
205 					   params->bssid,
206 					   params->ssid, params->ssid_len,
207 					   IEEE80211_BSS_TYPE_ESS,
208 					   IEEE80211_PRIVACY_ANY);
209 		if (!req.bss) {
210 			err = -ENOENT;
211 		} else {
212 			err = cfg80211_mlme_assoc(rdev, wdev->netdev, &req);
213 			cfg80211_put_bss(&rdev->wiphy, req.bss);
214 		}
215 
216 		if (err)
217 			cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
218 					     NULL, 0,
219 					     WLAN_REASON_DEAUTH_LEAVING,
220 					     false);
221 		return err;
222 	case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
223 		*treason = NL80211_TIMEOUT_ASSOC;
224 		fallthrough;
225 	case CFG80211_CONN_ASSOC_FAILED:
226 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
227 				     NULL, 0,
228 				     WLAN_REASON_DEAUTH_LEAVING, false);
229 		return -ENOTCONN;
230 	case CFG80211_CONN_DEAUTH:
231 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
232 				     NULL, 0,
233 				     WLAN_REASON_DEAUTH_LEAVING, false);
234 		fallthrough;
235 	case CFG80211_CONN_ABANDON:
236 		/* free directly, disconnected event already sent */
237 		cfg80211_sme_free(wdev);
238 		return 0;
239 	default:
240 		return 0;
241 	}
242 }
243 
244 void cfg80211_conn_work(struct work_struct *work)
245 {
246 	struct cfg80211_registered_device *rdev =
247 		container_of(work, struct cfg80211_registered_device, conn_work);
248 	struct wireless_dev *wdev;
249 	u8 bssid_buf[ETH_ALEN], *bssid = NULL;
250 	enum nl80211_timeout_reason treason;
251 
252 	wiphy_lock(&rdev->wiphy);
253 
254 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
255 		if (!wdev->netdev)
256 			continue;
257 
258 		wdev_lock(wdev);
259 		if (!netif_running(wdev->netdev)) {
260 			wdev_unlock(wdev);
261 			continue;
262 		}
263 		if (!wdev->conn ||
264 		    wdev->conn->state == CFG80211_CONN_CONNECTED) {
265 			wdev_unlock(wdev);
266 			continue;
267 		}
268 		if (wdev->conn->params.bssid) {
269 			memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
270 			bssid = bssid_buf;
271 		}
272 		treason = NL80211_TIMEOUT_UNSPECIFIED;
273 		if (cfg80211_conn_do_work(wdev, &treason)) {
274 			struct cfg80211_connect_resp_params cr;
275 
276 			memset(&cr, 0, sizeof(cr));
277 			cr.status = -1;
278 			cr.links[0].bssid = bssid;
279 			cr.timeout_reason = treason;
280 			__cfg80211_connect_result(wdev->netdev, &cr, false);
281 		}
282 		wdev_unlock(wdev);
283 	}
284 
285 	wiphy_unlock(&rdev->wiphy);
286 }
287 
288 static void cfg80211_step_auth_next(struct cfg80211_conn *conn,
289 				    struct cfg80211_bss *bss)
290 {
291 	memcpy(conn->bssid, bss->bssid, ETH_ALEN);
292 	conn->params.bssid = conn->bssid;
293 	conn->params.channel = bss->channel;
294 	conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
295 }
296 
297 /* Returned bss is reference counted and must be cleaned up appropriately. */
298 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
299 {
300 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
301 	struct cfg80211_bss *bss;
302 
303 	ASSERT_WDEV_LOCK(wdev);
304 
305 	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
306 			       wdev->conn->params.bssid,
307 			       wdev->conn->params.ssid,
308 			       wdev->conn->params.ssid_len,
309 			       wdev->conn_bss_type,
310 			       IEEE80211_PRIVACY(wdev->conn->params.privacy));
311 	if (!bss)
312 		return NULL;
313 
314 	cfg80211_step_auth_next(wdev->conn, bss);
315 	schedule_work(&rdev->conn_work);
316 
317 	return bss;
318 }
319 
320 static void __cfg80211_sme_scan_done(struct net_device *dev)
321 {
322 	struct wireless_dev *wdev = dev->ieee80211_ptr;
323 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
324 	struct cfg80211_bss *bss;
325 
326 	ASSERT_WDEV_LOCK(wdev);
327 
328 	if (!wdev->conn)
329 		return;
330 
331 	if (wdev->conn->state != CFG80211_CONN_SCANNING &&
332 	    wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
333 		return;
334 
335 	bss = cfg80211_get_conn_bss(wdev);
336 	if (bss)
337 		cfg80211_put_bss(&rdev->wiphy, bss);
338 	else
339 		schedule_work(&rdev->conn_work);
340 }
341 
342 void cfg80211_sme_scan_done(struct net_device *dev)
343 {
344 	struct wireless_dev *wdev = dev->ieee80211_ptr;
345 
346 	wdev_lock(wdev);
347 	__cfg80211_sme_scan_done(dev);
348 	wdev_unlock(wdev);
349 }
350 
351 void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
352 {
353 	struct wiphy *wiphy = wdev->wiphy;
354 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
355 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
356 	u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
357 
358 	ASSERT_WDEV_LOCK(wdev);
359 
360 	if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
361 		return;
362 
363 	if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
364 	    wdev->conn->auto_auth &&
365 	    wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
366 		/* select automatically between only open, shared, leap */
367 		switch (wdev->conn->params.auth_type) {
368 		case NL80211_AUTHTYPE_OPEN_SYSTEM:
369 			if (wdev->connect_keys)
370 				wdev->conn->params.auth_type =
371 					NL80211_AUTHTYPE_SHARED_KEY;
372 			else
373 				wdev->conn->params.auth_type =
374 					NL80211_AUTHTYPE_NETWORK_EAP;
375 			break;
376 		case NL80211_AUTHTYPE_SHARED_KEY:
377 			wdev->conn->params.auth_type =
378 				NL80211_AUTHTYPE_NETWORK_EAP;
379 			break;
380 		default:
381 			/* huh? */
382 			wdev->conn->params.auth_type =
383 				NL80211_AUTHTYPE_OPEN_SYSTEM;
384 			break;
385 		}
386 		wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
387 		schedule_work(&rdev->conn_work);
388 	} else if (status_code != WLAN_STATUS_SUCCESS) {
389 		struct cfg80211_connect_resp_params cr;
390 
391 		memset(&cr, 0, sizeof(cr));
392 		cr.status = status_code;
393 		cr.links[0].bssid = mgmt->bssid;
394 		cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
395 		__cfg80211_connect_result(wdev->netdev, &cr, false);
396 	} else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
397 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
398 		schedule_work(&rdev->conn_work);
399 	}
400 }
401 
402 bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
403 {
404 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
405 
406 	if (!wdev->conn)
407 		return false;
408 
409 	if (status == WLAN_STATUS_SUCCESS) {
410 		wdev->conn->state = CFG80211_CONN_CONNECTED;
411 		return false;
412 	}
413 
414 	if (wdev->conn->prev_bssid_valid) {
415 		/*
416 		 * Some stupid APs don't accept reassoc, so we
417 		 * need to fall back to trying regular assoc;
418 		 * return true so no event is sent to userspace.
419 		 */
420 		wdev->conn->prev_bssid_valid = false;
421 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
422 		schedule_work(&rdev->conn_work);
423 		return true;
424 	}
425 
426 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
427 	schedule_work(&rdev->conn_work);
428 	return false;
429 }
430 
431 void cfg80211_sme_deauth(struct wireless_dev *wdev)
432 {
433 	cfg80211_sme_free(wdev);
434 }
435 
436 void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
437 {
438 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
439 
440 	if (!wdev->conn)
441 		return;
442 
443 	wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
444 	schedule_work(&rdev->conn_work);
445 }
446 
447 void cfg80211_sme_disassoc(struct wireless_dev *wdev)
448 {
449 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
450 
451 	if (!wdev->conn)
452 		return;
453 
454 	wdev->conn->state = CFG80211_CONN_DEAUTH;
455 	schedule_work(&rdev->conn_work);
456 }
457 
458 void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
459 {
460 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
461 
462 	if (!wdev->conn)
463 		return;
464 
465 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
466 	schedule_work(&rdev->conn_work);
467 }
468 
469 void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
470 {
471 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
472 
473 	if (!wdev->conn)
474 		return;
475 
476 	wdev->conn->state = CFG80211_CONN_ABANDON;
477 	schedule_work(&rdev->conn_work);
478 }
479 
480 static void cfg80211_wdev_release_bsses(struct wireless_dev *wdev)
481 {
482 	unsigned int link;
483 
484 	for_each_valid_link(wdev, link) {
485 		if (!wdev->links[link].client.current_bss)
486 			continue;
487 		cfg80211_unhold_bss(wdev->links[link].client.current_bss);
488 		cfg80211_put_bss(wdev->wiphy,
489 				 &wdev->links[link].client.current_bss->pub);
490 		wdev->links[link].client.current_bss = NULL;
491 	}
492 }
493 
494 static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
495 				     const u8 *ies, size_t ies_len,
496 				     const u8 **out_ies, size_t *out_ies_len)
497 {
498 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
499 	u8 *buf;
500 	size_t offs;
501 
502 	if (!rdev->wiphy.extended_capabilities_len ||
503 	    (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
504 		*out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
505 		if (!*out_ies)
506 			return -ENOMEM;
507 		*out_ies_len = ies_len;
508 		return 0;
509 	}
510 
511 	buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
512 		      GFP_KERNEL);
513 	if (!buf)
514 		return -ENOMEM;
515 
516 	if (ies_len) {
517 		static const u8 before_extcapa[] = {
518 			/* not listing IEs expected to be created by driver */
519 			WLAN_EID_RSN,
520 			WLAN_EID_QOS_CAPA,
521 			WLAN_EID_RRM_ENABLED_CAPABILITIES,
522 			WLAN_EID_MOBILITY_DOMAIN,
523 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
524 			WLAN_EID_BSS_COEX_2040,
525 		};
526 
527 		offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
528 					  ARRAY_SIZE(before_extcapa), 0);
529 		memcpy(buf, ies, offs);
530 		/* leave a whole for extended capabilities IE */
531 		memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
532 		       ies + offs, ies_len - offs);
533 	} else {
534 		offs = 0;
535 	}
536 
537 	/* place extended capabilities IE (with only driver capabilities) */
538 	buf[offs] = WLAN_EID_EXT_CAPABILITY;
539 	buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
540 	memcpy(buf + offs + 2,
541 	       rdev->wiphy.extended_capabilities,
542 	       rdev->wiphy.extended_capabilities_len);
543 
544 	*out_ies = buf;
545 	*out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
546 
547 	return 0;
548 }
549 
550 static int cfg80211_sme_connect(struct wireless_dev *wdev,
551 				struct cfg80211_connect_params *connect,
552 				const u8 *prev_bssid)
553 {
554 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
555 	struct cfg80211_bss *bss;
556 	int err;
557 
558 	if (!rdev->ops->auth || !rdev->ops->assoc)
559 		return -EOPNOTSUPP;
560 
561 	cfg80211_wdev_release_bsses(wdev);
562 
563 	if (wdev->connected) {
564 		cfg80211_sme_free(wdev);
565 		wdev->connected = false;
566 	}
567 
568 	if (wdev->conn)
569 		return -EINPROGRESS;
570 
571 	wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
572 	if (!wdev->conn)
573 		return -ENOMEM;
574 
575 	/*
576 	 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
577 	 */
578 	memcpy(&wdev->conn->params, connect, sizeof(*connect));
579 	if (connect->bssid) {
580 		wdev->conn->params.bssid = wdev->conn->bssid;
581 		memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
582 	}
583 
584 	if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
585 				      &wdev->conn->ie,
586 				      &wdev->conn->params.ie_len)) {
587 		kfree(wdev->conn);
588 		wdev->conn = NULL;
589 		return -ENOMEM;
590 	}
591 	wdev->conn->params.ie = wdev->conn->ie;
592 
593 	if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
594 		wdev->conn->auto_auth = true;
595 		/* start with open system ... should mostly work */
596 		wdev->conn->params.auth_type =
597 			NL80211_AUTHTYPE_OPEN_SYSTEM;
598 	} else {
599 		wdev->conn->auto_auth = false;
600 	}
601 
602 	wdev->conn->params.ssid = wdev->u.client.ssid;
603 	wdev->conn->params.ssid_len = wdev->u.client.ssid_len;
604 
605 	/* see if we have the bss already */
606 	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
607 			       wdev->conn->params.bssid,
608 			       wdev->conn->params.ssid,
609 			       wdev->conn->params.ssid_len,
610 			       wdev->conn_bss_type,
611 			       IEEE80211_PRIVACY(wdev->conn->params.privacy));
612 
613 	if (prev_bssid) {
614 		memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
615 		wdev->conn->prev_bssid_valid = true;
616 	}
617 
618 	/* we're good if we have a matching bss struct */
619 	if (bss) {
620 		enum nl80211_timeout_reason treason;
621 
622 		cfg80211_step_auth_next(wdev->conn, bss);
623 		err = cfg80211_conn_do_work(wdev, &treason);
624 		cfg80211_put_bss(wdev->wiphy, bss);
625 	} else {
626 		/* otherwise we'll need to scan for the AP first */
627 		err = cfg80211_conn_scan(wdev);
628 
629 		/*
630 		 * If we can't scan right now, then we need to scan again
631 		 * after the current scan finished, since the parameters
632 		 * changed (unless we find a good AP anyway).
633 		 */
634 		if (err == -EBUSY) {
635 			err = 0;
636 			wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
637 		}
638 	}
639 
640 	if (err)
641 		cfg80211_sme_free(wdev);
642 
643 	return err;
644 }
645 
646 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
647 {
648 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
649 	int err;
650 
651 	if (!wdev->conn)
652 		return 0;
653 
654 	if (!rdev->ops->deauth)
655 		return -EOPNOTSUPP;
656 
657 	if (wdev->conn->state == CFG80211_CONN_SCANNING ||
658 	    wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
659 		err = 0;
660 		goto out;
661 	}
662 
663 	/* wdev->conn->params.bssid must be set if > SCANNING */
664 	err = cfg80211_mlme_deauth(rdev, wdev->netdev,
665 				   wdev->conn->params.bssid,
666 				   NULL, 0, reason, false);
667  out:
668 	cfg80211_sme_free(wdev);
669 	return err;
670 }
671 
672 /*
673  * code shared for in-device and software SME
674  */
675 
676 static bool cfg80211_is_all_idle(void)
677 {
678 	struct cfg80211_registered_device *rdev;
679 	struct wireless_dev *wdev;
680 	bool is_all_idle = true;
681 
682 	/*
683 	 * All devices must be idle as otherwise if you are actively
684 	 * scanning some new beacon hints could be learned and would
685 	 * count as new regulatory hints.
686 	 * Also if there is any other active beaconing interface we
687 	 * need not issue a disconnect hint and reset any info such
688 	 * as chan dfs state, etc.
689 	 */
690 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
691 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
692 			wdev_lock(wdev);
693 			if (wdev->conn || wdev->connected ||
694 			    cfg80211_beaconing_iface_active(wdev))
695 				is_all_idle = false;
696 			wdev_unlock(wdev);
697 		}
698 	}
699 
700 	return is_all_idle;
701 }
702 
703 static void disconnect_work(struct work_struct *work)
704 {
705 	rtnl_lock();
706 	if (cfg80211_is_all_idle())
707 		regulatory_hint_disconnect();
708 	rtnl_unlock();
709 }
710 
711 DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
712 
713 static void
714 cfg80211_connect_result_release_bsses(struct wireless_dev *wdev,
715 				      struct cfg80211_connect_resp_params *cr)
716 {
717 	unsigned int link;
718 
719 	for_each_valid_link(cr, link) {
720 		if (!cr->links[link].bss)
721 			continue;
722 		cfg80211_unhold_bss(bss_from_pub(cr->links[link].bss));
723 		cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
724 	}
725 }
726 
727 /*
728  * API calls for drivers implementing connect/disconnect and
729  * SME event handling
730  */
731 
732 /* This method must consume bss one way or another */
733 void __cfg80211_connect_result(struct net_device *dev,
734 			       struct cfg80211_connect_resp_params *cr,
735 			       bool wextev)
736 {
737 	struct wireless_dev *wdev = dev->ieee80211_ptr;
738 	const struct element *country_elem = NULL;
739 	const struct element *ssid;
740 	const u8 *country_data;
741 	u8 country_datalen;
742 #ifdef CONFIG_CFG80211_WEXT
743 	union iwreq_data wrqu;
744 #endif
745 	unsigned int link;
746 	const u8 *connected_addr;
747 	bool bss_not_found = false;
748 
749 	ASSERT_WDEV_LOCK(wdev);
750 
751 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
752 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
753 		goto out;
754 
755 	if (cr->valid_links) {
756 		if (WARN_ON(!cr->ap_mld_addr))
757 			goto out;
758 
759 		for_each_valid_link(cr, link) {
760 			if (WARN_ON(!cr->links[link].addr))
761 				goto out;
762 		}
763 
764 		if (WARN_ON(wdev->connect_keys))
765 			goto out;
766 	}
767 
768 	wdev->unprot_beacon_reported = 0;
769 	nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
770 				    GFP_KERNEL);
771 	connected_addr = cr->valid_links ? cr->ap_mld_addr : cr->links[0].bssid;
772 
773 #ifdef CONFIG_CFG80211_WEXT
774 	if (wextev && !cr->valid_links) {
775 		if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
776 			memset(&wrqu, 0, sizeof(wrqu));
777 			wrqu.data.length = cr->req_ie_len;
778 			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
779 					    cr->req_ie);
780 		}
781 
782 		if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
783 			memset(&wrqu, 0, sizeof(wrqu));
784 			wrqu.data.length = cr->resp_ie_len;
785 			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
786 					    cr->resp_ie);
787 		}
788 
789 		memset(&wrqu, 0, sizeof(wrqu));
790 		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
791 		if (connected_addr && cr->status == WLAN_STATUS_SUCCESS) {
792 			memcpy(wrqu.ap_addr.sa_data, connected_addr, ETH_ALEN);
793 			memcpy(wdev->wext.prev_bssid, connected_addr, ETH_ALEN);
794 			wdev->wext.prev_bssid_valid = true;
795 		}
796 		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
797 	}
798 #endif
799 
800 	if (cr->status == WLAN_STATUS_SUCCESS) {
801 		if (!wiphy_to_rdev(wdev->wiphy)->ops->connect) {
802 			for_each_valid_link(cr, link) {
803 				if (WARN_ON_ONCE(!cr->links[link].bss))
804 					break;
805 			}
806 		}
807 
808 		for_each_valid_link(cr, link) {
809 			/* don't do extra lookups for failures */
810 			if (cr->links[link].status != WLAN_STATUS_SUCCESS)
811 				continue;
812 
813 			if (cr->links[link].bss)
814 				continue;
815 
816 			cr->links[link].bss =
817 				cfg80211_get_bss(wdev->wiphy, NULL,
818 						 cr->links[link].bssid,
819 						 wdev->u.client.ssid,
820 						 wdev->u.client.ssid_len,
821 						 wdev->conn_bss_type,
822 						 IEEE80211_PRIVACY_ANY);
823 			if (!cr->links[link].bss) {
824 				bss_not_found = true;
825 				break;
826 			}
827 			cfg80211_hold_bss(bss_from_pub(cr->links[link].bss));
828 		}
829 	}
830 
831 	cfg80211_wdev_release_bsses(wdev);
832 
833 	if (cr->status != WLAN_STATUS_SUCCESS) {
834 		kfree_sensitive(wdev->connect_keys);
835 		wdev->connect_keys = NULL;
836 		wdev->u.client.ssid_len = 0;
837 		wdev->conn_owner_nlportid = 0;
838 		cfg80211_connect_result_release_bsses(wdev, cr);
839 		cfg80211_sme_free(wdev);
840 		return;
841 	}
842 
843 	if (WARN_ON(bss_not_found)) {
844 		cfg80211_connect_result_release_bsses(wdev, cr);
845 		return;
846 	}
847 
848 	memset(wdev->links, 0, sizeof(wdev->links));
849 	for_each_valid_link(cr, link) {
850 		if (cr->links[link].status == WLAN_STATUS_SUCCESS)
851 			continue;
852 		cr->valid_links &= ~BIT(link);
853 		/* don't require bss pointer for failed links */
854 		if (!cr->links[link].bss)
855 			continue;
856 		cfg80211_unhold_bss(bss_from_pub(cr->links[link].bss));
857 		cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
858 	}
859 	wdev->valid_links = cr->valid_links;
860 	for_each_valid_link(cr, link)
861 		wdev->links[link].client.current_bss =
862 			bss_from_pub(cr->links[link].bss);
863 	wdev->connected = true;
864 	ether_addr_copy(wdev->u.client.connected_addr, connected_addr);
865 	if (cr->valid_links) {
866 		for_each_valid_link(cr, link)
867 			memcpy(wdev->links[link].addr, cr->links[link].addr,
868 			       ETH_ALEN);
869 	}
870 
871 	cfg80211_upload_connect_keys(wdev);
872 
873 	rcu_read_lock();
874 	for_each_valid_link(cr, link) {
875 		country_elem =
876 			ieee80211_bss_get_elem(cr->links[link].bss,
877 					       WLAN_EID_COUNTRY);
878 		if (country_elem)
879 			break;
880 	}
881 	if (!country_elem) {
882 		rcu_read_unlock();
883 		return;
884 	}
885 
886 	country_datalen = country_elem->datalen;
887 	country_data = kmemdup(country_elem->data, country_datalen, GFP_ATOMIC);
888 	rcu_read_unlock();
889 
890 	if (!country_data)
891 		return;
892 
893 	regulatory_hint_country_ie(wdev->wiphy,
894 				   cr->links[link].bss->channel->band,
895 				   country_data, country_datalen);
896 	kfree(country_data);
897 
898 	if (!wdev->u.client.ssid_len) {
899 		rcu_read_lock();
900 		for_each_valid_link(cr, link) {
901 			ssid = ieee80211_bss_get_elem(cr->links[link].bss,
902 						      WLAN_EID_SSID);
903 
904 			if (!ssid || !ssid->datalen)
905 				continue;
906 
907 			memcpy(wdev->u.client.ssid, ssid->data, ssid->datalen);
908 			wdev->u.client.ssid_len = ssid->datalen;
909 			break;
910 		}
911 		rcu_read_unlock();
912 	}
913 
914 	return;
915 out:
916 	for_each_valid_link(cr, link)
917 		cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
918 }
919 
920 static void cfg80211_update_link_bss(struct wireless_dev *wdev,
921 				     struct cfg80211_bss **bss)
922 {
923 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
924 	struct cfg80211_internal_bss *ibss;
925 
926 	if (!*bss)
927 		return;
928 
929 	ibss = bss_from_pub(*bss);
930 	if (list_empty(&ibss->list)) {
931 		struct cfg80211_bss *found = NULL, *tmp = *bss;
932 
933 		found = cfg80211_get_bss(wdev->wiphy, NULL,
934 					 (*bss)->bssid,
935 					 wdev->u.client.ssid,
936 					 wdev->u.client.ssid_len,
937 					 wdev->conn_bss_type,
938 					 IEEE80211_PRIVACY_ANY);
939 		if (found) {
940 			/* The same BSS is already updated so use it
941 			 * instead, as it has latest info.
942 			 */
943 			*bss = found;
944 		} else {
945 			/* Update with BSS provided by driver, it will
946 			 * be freshly added and ref cnted, we can free
947 			 * the old one.
948 			 *
949 			 * signal_valid can be false, as we are not
950 			 * expecting the BSS to be found.
951 			 *
952 			 * keep the old timestamp to avoid confusion
953 			 */
954 			cfg80211_bss_update(rdev, ibss, false,
955 					    ibss->ts);
956 		}
957 
958 		cfg80211_put_bss(wdev->wiphy, tmp);
959 	}
960 }
961 
962 /* Consumes bss object(s) one way or another */
963 void cfg80211_connect_done(struct net_device *dev,
964 			   struct cfg80211_connect_resp_params *params,
965 			   gfp_t gfp)
966 {
967 	struct wireless_dev *wdev = dev->ieee80211_ptr;
968 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
969 	struct cfg80211_event *ev;
970 	unsigned long flags;
971 	u8 *next;
972 	size_t link_info_size = 0;
973 	unsigned int link;
974 
975 	for_each_valid_link(params, link) {
976 		cfg80211_update_link_bss(wdev, &params->links[link].bss);
977 		link_info_size += params->links[link].bssid ? ETH_ALEN : 0;
978 		link_info_size += params->links[link].addr ? ETH_ALEN : 0;
979 	}
980 
981 	ev = kzalloc(sizeof(*ev) + (params->ap_mld_addr ? ETH_ALEN : 0) +
982 		     params->req_ie_len + params->resp_ie_len +
983 		     params->fils.kek_len + params->fils.pmk_len +
984 		     (params->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
985 		     gfp);
986 
987 	if (!ev) {
988 		for_each_valid_link(params, link)
989 			cfg80211_put_bss(wdev->wiphy,
990 					 params->links[link].bss);
991 		return;
992 	}
993 
994 	ev->type = EVENT_CONNECT_RESULT;
995 	next = ((u8 *)ev) + sizeof(*ev);
996 	if (params->ap_mld_addr) {
997 		ev->cr.ap_mld_addr = next;
998 		memcpy((void *)ev->cr.ap_mld_addr, params->ap_mld_addr,
999 		       ETH_ALEN);
1000 		next += ETH_ALEN;
1001 	}
1002 	if (params->req_ie_len) {
1003 		ev->cr.req_ie = next;
1004 		ev->cr.req_ie_len = params->req_ie_len;
1005 		memcpy((void *)ev->cr.req_ie, params->req_ie,
1006 		       params->req_ie_len);
1007 		next += params->req_ie_len;
1008 	}
1009 	if (params->resp_ie_len) {
1010 		ev->cr.resp_ie = next;
1011 		ev->cr.resp_ie_len = params->resp_ie_len;
1012 		memcpy((void *)ev->cr.resp_ie, params->resp_ie,
1013 		       params->resp_ie_len);
1014 		next += params->resp_ie_len;
1015 	}
1016 	if (params->fils.kek_len) {
1017 		ev->cr.fils.kek = next;
1018 		ev->cr.fils.kek_len = params->fils.kek_len;
1019 		memcpy((void *)ev->cr.fils.kek, params->fils.kek,
1020 		       params->fils.kek_len);
1021 		next += params->fils.kek_len;
1022 	}
1023 	if (params->fils.pmk_len) {
1024 		ev->cr.fils.pmk = next;
1025 		ev->cr.fils.pmk_len = params->fils.pmk_len;
1026 		memcpy((void *)ev->cr.fils.pmk, params->fils.pmk,
1027 		       params->fils.pmk_len);
1028 		next += params->fils.pmk_len;
1029 	}
1030 	if (params->fils.pmkid) {
1031 		ev->cr.fils.pmkid = next;
1032 		memcpy((void *)ev->cr.fils.pmkid, params->fils.pmkid,
1033 		       WLAN_PMKID_LEN);
1034 		next += WLAN_PMKID_LEN;
1035 	}
1036 	ev->cr.fils.update_erp_next_seq_num = params->fils.update_erp_next_seq_num;
1037 	if (params->fils.update_erp_next_seq_num)
1038 		ev->cr.fils.erp_next_seq_num = params->fils.erp_next_seq_num;
1039 	ev->cr.valid_links = params->valid_links;
1040 	for_each_valid_link(params, link) {
1041 		if (params->links[link].bss)
1042 			cfg80211_hold_bss(
1043 				bss_from_pub(params->links[link].bss));
1044 		ev->cr.links[link].bss = params->links[link].bss;
1045 
1046 		if (params->links[link].addr) {
1047 			ev->cr.links[link].addr = next;
1048 			memcpy((void *)ev->cr.links[link].addr,
1049 			       params->links[link].addr,
1050 			       ETH_ALEN);
1051 			next += ETH_ALEN;
1052 		}
1053 		if (params->links[link].bssid) {
1054 			ev->cr.links[link].bssid = next;
1055 			memcpy((void *)ev->cr.links[link].bssid,
1056 			       params->links[link].bssid,
1057 			       ETH_ALEN);
1058 			next += ETH_ALEN;
1059 		}
1060 	}
1061 	ev->cr.status = params->status;
1062 	ev->cr.timeout_reason = params->timeout_reason;
1063 
1064 	spin_lock_irqsave(&wdev->event_lock, flags);
1065 	list_add_tail(&ev->list, &wdev->event_list);
1066 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1067 	queue_work(cfg80211_wq, &rdev->event_work);
1068 }
1069 EXPORT_SYMBOL(cfg80211_connect_done);
1070 
1071 /* Consumes bss object one way or another */
1072 void __cfg80211_roamed(struct wireless_dev *wdev,
1073 		       struct cfg80211_roam_info *info)
1074 {
1075 #ifdef CONFIG_CFG80211_WEXT
1076 	union iwreq_data wrqu;
1077 #endif
1078 	unsigned int link;
1079 	const u8 *connected_addr;
1080 
1081 	ASSERT_WDEV_LOCK(wdev);
1082 
1083 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1084 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1085 		goto out;
1086 
1087 	if (WARN_ON(!wdev->connected))
1088 		goto out;
1089 
1090 	if (info->valid_links) {
1091 		if (WARN_ON(!info->ap_mld_addr))
1092 			goto out;
1093 
1094 		for_each_valid_link(info, link) {
1095 			if (WARN_ON(!info->links[link].addr))
1096 				goto out;
1097 		}
1098 	}
1099 
1100 	cfg80211_wdev_release_bsses(wdev);
1101 
1102 	for_each_valid_link(info, link) {
1103 		if (WARN_ON(!info->links[link].bss))
1104 			goto out;
1105 	}
1106 
1107 	memset(wdev->links, 0, sizeof(wdev->links));
1108 	wdev->valid_links = info->valid_links;
1109 	for_each_valid_link(info, link) {
1110 		cfg80211_hold_bss(bss_from_pub(info->links[link].bss));
1111 		wdev->links[link].client.current_bss =
1112 			bss_from_pub(info->links[link].bss);
1113 	}
1114 
1115 	connected_addr = info->valid_links ?
1116 			 info->ap_mld_addr :
1117 			 info->links[0].bss->bssid;
1118 	ether_addr_copy(wdev->u.client.connected_addr, connected_addr);
1119 	if (info->valid_links) {
1120 		for_each_valid_link(info, link)
1121 			memcpy(wdev->links[link].addr, info->links[link].addr,
1122 			       ETH_ALEN);
1123 	}
1124 	wdev->unprot_beacon_reported = 0;
1125 	nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
1126 			    wdev->netdev, info, GFP_KERNEL);
1127 
1128 #ifdef CONFIG_CFG80211_WEXT
1129 	if (!info->valid_links) {
1130 		if (info->req_ie) {
1131 			memset(&wrqu, 0, sizeof(wrqu));
1132 			wrqu.data.length = info->req_ie_len;
1133 			wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
1134 					    &wrqu, info->req_ie);
1135 		}
1136 
1137 		if (info->resp_ie) {
1138 			memset(&wrqu, 0, sizeof(wrqu));
1139 			wrqu.data.length = info->resp_ie_len;
1140 			wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
1141 					    &wrqu, info->resp_ie);
1142 		}
1143 
1144 		memset(&wrqu, 0, sizeof(wrqu));
1145 		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1146 		memcpy(wrqu.ap_addr.sa_data, connected_addr, ETH_ALEN);
1147 		memcpy(wdev->wext.prev_bssid, connected_addr, ETH_ALEN);
1148 		wdev->wext.prev_bssid_valid = true;
1149 		wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
1150 	}
1151 #endif
1152 
1153 	return;
1154 out:
1155 	for_each_valid_link(info, link)
1156 		cfg80211_put_bss(wdev->wiphy, info->links[link].bss);
1157 }
1158 
1159 /* Consumes info->links.bss object(s) one way or another */
1160 void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
1161 		     gfp_t gfp)
1162 {
1163 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1164 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1165 	struct cfg80211_event *ev;
1166 	unsigned long flags;
1167 	u8 *next;
1168 	unsigned int link;
1169 	size_t link_info_size = 0;
1170 	bool bss_not_found = false;
1171 
1172 	for_each_valid_link(info, link) {
1173 		link_info_size += info->links[link].addr ? ETH_ALEN : 0;
1174 		link_info_size += info->links[link].bssid ? ETH_ALEN : 0;
1175 
1176 		if (info->links[link].bss)
1177 			continue;
1178 
1179 		info->links[link].bss =
1180 			cfg80211_get_bss(wdev->wiphy,
1181 					 info->links[link].channel,
1182 					 info->links[link].bssid,
1183 					 wdev->u.client.ssid,
1184 					 wdev->u.client.ssid_len,
1185 					 wdev->conn_bss_type,
1186 					 IEEE80211_PRIVACY_ANY);
1187 
1188 		if (!info->links[link].bss) {
1189 			bss_not_found = true;
1190 			break;
1191 		}
1192 	}
1193 
1194 	if (WARN_ON(bss_not_found))
1195 		goto out;
1196 
1197 	ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
1198 		     info->fils.kek_len + info->fils.pmk_len +
1199 		     (info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
1200 		     (info->ap_mld_addr ? ETH_ALEN : 0) + link_info_size, gfp);
1201 	if (!ev)
1202 		goto out;
1203 
1204 	ev->type = EVENT_ROAMED;
1205 	next = ((u8 *)ev) + sizeof(*ev);
1206 	if (info->req_ie_len) {
1207 		ev->rm.req_ie = next;
1208 		ev->rm.req_ie_len = info->req_ie_len;
1209 		memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
1210 		next += info->req_ie_len;
1211 	}
1212 	if (info->resp_ie_len) {
1213 		ev->rm.resp_ie = next;
1214 		ev->rm.resp_ie_len = info->resp_ie_len;
1215 		memcpy((void *)ev->rm.resp_ie, info->resp_ie,
1216 		       info->resp_ie_len);
1217 		next += info->resp_ie_len;
1218 	}
1219 	if (info->fils.kek_len) {
1220 		ev->rm.fils.kek = next;
1221 		ev->rm.fils.kek_len = info->fils.kek_len;
1222 		memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1223 		       info->fils.kek_len);
1224 		next += info->fils.kek_len;
1225 	}
1226 	if (info->fils.pmk_len) {
1227 		ev->rm.fils.pmk = next;
1228 		ev->rm.fils.pmk_len = info->fils.pmk_len;
1229 		memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1230 		       info->fils.pmk_len);
1231 		next += info->fils.pmk_len;
1232 	}
1233 	if (info->fils.pmkid) {
1234 		ev->rm.fils.pmkid = next;
1235 		memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1236 		       WLAN_PMKID_LEN);
1237 		next += WLAN_PMKID_LEN;
1238 	}
1239 	ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1240 	if (info->fils.update_erp_next_seq_num)
1241 		ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
1242 	if (info->ap_mld_addr) {
1243 		ev->rm.ap_mld_addr = next;
1244 		memcpy((void *)ev->rm.ap_mld_addr, info->ap_mld_addr,
1245 		       ETH_ALEN);
1246 		next += ETH_ALEN;
1247 	}
1248 	ev->rm.valid_links = info->valid_links;
1249 	for_each_valid_link(info, link) {
1250 		ev->rm.links[link].bss = info->links[link].bss;
1251 
1252 		if (info->links[link].addr) {
1253 			ev->rm.links[link].addr = next;
1254 			memcpy((void *)ev->rm.links[link].addr,
1255 			       info->links[link].addr,
1256 			       ETH_ALEN);
1257 			next += ETH_ALEN;
1258 		}
1259 
1260 		if (info->links[link].bssid) {
1261 			ev->rm.links[link].bssid = next;
1262 			memcpy((void *)ev->rm.links[link].bssid,
1263 			       info->links[link].bssid,
1264 			       ETH_ALEN);
1265 			next += ETH_ALEN;
1266 		}
1267 	}
1268 
1269 	spin_lock_irqsave(&wdev->event_lock, flags);
1270 	list_add_tail(&ev->list, &wdev->event_list);
1271 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1272 	queue_work(cfg80211_wq, &rdev->event_work);
1273 
1274 	return;
1275 out:
1276 	for_each_valid_link(info, link)
1277 		cfg80211_put_bss(wdev->wiphy, info->links[link].bss);
1278 
1279 }
1280 EXPORT_SYMBOL(cfg80211_roamed);
1281 
1282 void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid,
1283 					const u8 *td_bitmap, u8 td_bitmap_len)
1284 {
1285 	ASSERT_WDEV_LOCK(wdev);
1286 
1287 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1288 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1289 		return;
1290 
1291 	if (WARN_ON(!wdev->connected) ||
1292 	    WARN_ON(!ether_addr_equal(wdev->u.client.connected_addr, bssid)))
1293 		return;
1294 
1295 	nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
1296 				     bssid, td_bitmap, td_bitmap_len);
1297 }
1298 
1299 void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
1300 			      const u8 *td_bitmap, u8 td_bitmap_len, gfp_t gfp)
1301 {
1302 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1303 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1304 	struct cfg80211_event *ev;
1305 	unsigned long flags;
1306 
1307 	if (WARN_ON(!bssid))
1308 		return;
1309 
1310 	ev = kzalloc(sizeof(*ev) + td_bitmap_len, gfp);
1311 	if (!ev)
1312 		return;
1313 
1314 	ev->type = EVENT_PORT_AUTHORIZED;
1315 	memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1316 	ev->pa.td_bitmap = ((u8 *)ev) + sizeof(*ev);
1317 	ev->pa.td_bitmap_len = td_bitmap_len;
1318 	memcpy((void *)ev->pa.td_bitmap, td_bitmap, td_bitmap_len);
1319 
1320 	/*
1321 	 * Use the wdev event list so that if there are pending
1322 	 * connected/roamed events, they will be reported first.
1323 	 */
1324 	spin_lock_irqsave(&wdev->event_lock, flags);
1325 	list_add_tail(&ev->list, &wdev->event_list);
1326 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1327 	queue_work(cfg80211_wq, &rdev->event_work);
1328 }
1329 EXPORT_SYMBOL(cfg80211_port_authorized);
1330 
1331 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
1332 			     size_t ie_len, u16 reason, bool from_ap)
1333 {
1334 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1335 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1336 	int i;
1337 #ifdef CONFIG_CFG80211_WEXT
1338 	union iwreq_data wrqu;
1339 #endif
1340 
1341 	ASSERT_WDEV_LOCK(wdev);
1342 
1343 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1344 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1345 		return;
1346 
1347 	cfg80211_wdev_release_bsses(wdev);
1348 	wdev->connected = false;
1349 	wdev->u.client.ssid_len = 0;
1350 	wdev->conn_owner_nlportid = 0;
1351 	kfree_sensitive(wdev->connect_keys);
1352 	wdev->connect_keys = NULL;
1353 
1354 	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1355 
1356 	/* stop critical protocol if supported */
1357 	if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1358 		rdev->crit_proto_nlportid = 0;
1359 		rdev_crit_proto_stop(rdev, wdev);
1360 	}
1361 
1362 	/*
1363 	 * Delete all the keys ... pairwise keys can't really
1364 	 * exist any more anyway, but default keys might.
1365 	 */
1366 	if (rdev->ops->del_key) {
1367 		int max_key_idx = 5;
1368 
1369 		if (wiphy_ext_feature_isset(
1370 			    wdev->wiphy,
1371 			    NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
1372 		    wiphy_ext_feature_isset(
1373 			    wdev->wiphy,
1374 			    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
1375 			max_key_idx = 7;
1376 		for (i = 0; i <= max_key_idx; i++)
1377 			rdev_del_key(rdev, dev, -1, i, false, NULL);
1378 	}
1379 
1380 	rdev_set_qos_map(rdev, dev, NULL);
1381 
1382 #ifdef CONFIG_CFG80211_WEXT
1383 	memset(&wrqu, 0, sizeof(wrqu));
1384 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1385 	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1386 	wdev->wext.connect.ssid_len = 0;
1387 #endif
1388 
1389 	schedule_work(&cfg80211_disconnect_work);
1390 }
1391 
1392 void cfg80211_disconnected(struct net_device *dev, u16 reason,
1393 			   const u8 *ie, size_t ie_len,
1394 			   bool locally_generated, gfp_t gfp)
1395 {
1396 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1397 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1398 	struct cfg80211_event *ev;
1399 	unsigned long flags;
1400 
1401 	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1402 	if (!ev)
1403 		return;
1404 
1405 	ev->type = EVENT_DISCONNECTED;
1406 	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1407 	ev->dc.ie_len = ie_len;
1408 	memcpy((void *)ev->dc.ie, ie, ie_len);
1409 	ev->dc.reason = reason;
1410 	ev->dc.locally_generated = locally_generated;
1411 
1412 	spin_lock_irqsave(&wdev->event_lock, flags);
1413 	list_add_tail(&ev->list, &wdev->event_list);
1414 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1415 	queue_work(cfg80211_wq, &rdev->event_work);
1416 }
1417 EXPORT_SYMBOL(cfg80211_disconnected);
1418 
1419 /*
1420  * API calls for nl80211/wext compatibility code
1421  */
1422 int cfg80211_connect(struct cfg80211_registered_device *rdev,
1423 		     struct net_device *dev,
1424 		     struct cfg80211_connect_params *connect,
1425 		     struct cfg80211_cached_keys *connkeys,
1426 		     const u8 *prev_bssid)
1427 {
1428 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1429 	int err;
1430 
1431 	ASSERT_WDEV_LOCK(wdev);
1432 
1433 	/*
1434 	 * If we have an ssid_len, we're trying to connect or are
1435 	 * already connected, so reject a new SSID unless it's the
1436 	 * same (which is the case for re-association.)
1437 	 */
1438 	if (wdev->u.client.ssid_len &&
1439 	    (wdev->u.client.ssid_len != connect->ssid_len ||
1440 	     memcmp(wdev->u.client.ssid, connect->ssid, wdev->u.client.ssid_len)))
1441 		return -EALREADY;
1442 
1443 	/*
1444 	 * If connected, reject (re-)association unless prev_bssid
1445 	 * matches the current BSSID.
1446 	 */
1447 	if (wdev->connected) {
1448 		if (!prev_bssid)
1449 			return -EALREADY;
1450 		if (!ether_addr_equal(prev_bssid,
1451 				      wdev->u.client.connected_addr))
1452 			return -ENOTCONN;
1453 	}
1454 
1455 	/*
1456 	 * Reject if we're in the process of connecting with WEP,
1457 	 * this case isn't very interesting and trying to handle
1458 	 * it would make the code much more complex.
1459 	 */
1460 	if (wdev->connect_keys)
1461 		return -EINPROGRESS;
1462 
1463 	cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1464 				  rdev->wiphy.ht_capa_mod_mask);
1465 	cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
1466 				   rdev->wiphy.vht_capa_mod_mask);
1467 
1468 	if (connkeys && connkeys->def >= 0) {
1469 		int idx;
1470 		u32 cipher;
1471 
1472 		idx = connkeys->def;
1473 		cipher = connkeys->params[idx].cipher;
1474 		/* If given a WEP key we may need it for shared key auth */
1475 		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1476 		    cipher == WLAN_CIPHER_SUITE_WEP104) {
1477 			connect->key_idx = idx;
1478 			connect->key = connkeys->params[idx].key;
1479 			connect->key_len = connkeys->params[idx].key_len;
1480 
1481 			/*
1482 			 * If ciphers are not set (e.g. when going through
1483 			 * iwconfig), we have to set them appropriately here.
1484 			 */
1485 			if (connect->crypto.cipher_group == 0)
1486 				connect->crypto.cipher_group = cipher;
1487 
1488 			if (connect->crypto.n_ciphers_pairwise == 0) {
1489 				connect->crypto.n_ciphers_pairwise = 1;
1490 				connect->crypto.ciphers_pairwise[0] = cipher;
1491 			}
1492 		}
1493 	} else {
1494 		if (WARN_ON(connkeys))
1495 			return -EINVAL;
1496 
1497 		/* connect can point to wdev->wext.connect which
1498 		 * can hold key data from a previous connection
1499 		 */
1500 		connect->key = NULL;
1501 		connect->key_len = 0;
1502 		connect->key_idx = 0;
1503 	}
1504 
1505 	wdev->connect_keys = connkeys;
1506 	memcpy(wdev->u.client.ssid, connect->ssid, connect->ssid_len);
1507 	wdev->u.client.ssid_len = connect->ssid_len;
1508 
1509 	wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1510 					      IEEE80211_BSS_TYPE_ESS;
1511 
1512 	if (!rdev->ops->connect)
1513 		err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1514 	else
1515 		err = rdev_connect(rdev, dev, connect);
1516 
1517 	if (err) {
1518 		wdev->connect_keys = NULL;
1519 		/*
1520 		 * This could be reassoc getting refused, don't clear
1521 		 * ssid_len in that case.
1522 		 */
1523 		if (!wdev->connected)
1524 			wdev->u.client.ssid_len = 0;
1525 		return err;
1526 	}
1527 
1528 	return 0;
1529 }
1530 
1531 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1532 			struct net_device *dev, u16 reason, bool wextev)
1533 {
1534 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1535 	int err = 0;
1536 
1537 	ASSERT_WDEV_LOCK(wdev);
1538 
1539 	kfree_sensitive(wdev->connect_keys);
1540 	wdev->connect_keys = NULL;
1541 
1542 	wdev->conn_owner_nlportid = 0;
1543 
1544 	if (wdev->conn)
1545 		err = cfg80211_sme_disconnect(wdev, reason);
1546 	else if (!rdev->ops->disconnect)
1547 		cfg80211_mlme_down(rdev, dev);
1548 	else if (wdev->u.client.ssid_len)
1549 		err = rdev_disconnect(rdev, dev, reason);
1550 
1551 	/*
1552 	 * Clear ssid_len unless we actually were fully connected,
1553 	 * in which case cfg80211_disconnected() will take care of
1554 	 * this later.
1555 	 */
1556 	if (!wdev->connected)
1557 		wdev->u.client.ssid_len = 0;
1558 
1559 	return err;
1560 }
1561 
1562 /*
1563  * Used to clean up after the connection / connection attempt owner socket
1564  * disconnects
1565  */
1566 void cfg80211_autodisconnect_wk(struct work_struct *work)
1567 {
1568 	struct wireless_dev *wdev =
1569 		container_of(work, struct wireless_dev, disconnect_wk);
1570 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1571 
1572 	wdev_lock(wdev);
1573 
1574 	if (wdev->conn_owner_nlportid) {
1575 		switch (wdev->iftype) {
1576 		case NL80211_IFTYPE_ADHOC:
1577 			__cfg80211_leave_ibss(rdev, wdev->netdev, false);
1578 			break;
1579 		case NL80211_IFTYPE_AP:
1580 		case NL80211_IFTYPE_P2P_GO:
1581 			__cfg80211_stop_ap(rdev, wdev->netdev, -1, false);
1582 			break;
1583 		case NL80211_IFTYPE_MESH_POINT:
1584 			__cfg80211_leave_mesh(rdev, wdev->netdev);
1585 			break;
1586 		case NL80211_IFTYPE_STATION:
1587 		case NL80211_IFTYPE_P2P_CLIENT:
1588 			/*
1589 			 * Use disconnect_bssid if still connecting and
1590 			 * ops->disconnect not implemented.  Otherwise we can
1591 			 * use cfg80211_disconnect.
1592 			 */
1593 			if (rdev->ops->disconnect || wdev->connected)
1594 				cfg80211_disconnect(rdev, wdev->netdev,
1595 						    WLAN_REASON_DEAUTH_LEAVING,
1596 						    true);
1597 			else
1598 				cfg80211_mlme_deauth(rdev, wdev->netdev,
1599 						     wdev->disconnect_bssid,
1600 						     NULL, 0,
1601 						     WLAN_REASON_DEAUTH_LEAVING,
1602 						     false);
1603 			break;
1604 		default:
1605 			break;
1606 		}
1607 	}
1608 
1609 	wdev_unlock(wdev);
1610 }
1611