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