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