xref: /openbmc/linux/net/wireless/sme.c (revision 7587eb18)
1 /*
2  * SME code for cfg80211
3  * both driver SME event handling and the SME implementation
4  * (for nl80211's connect() and wext)
5  *
6  * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
7  * Copyright (C) 2009   Intel Corporation. All rights reserved.
8  */
9 
10 #include <linux/etherdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/slab.h>
13 #include <linux/workqueue.h>
14 #include <linux/wireless.h>
15 #include <linux/export.h>
16 #include <net/iw_handler.h>
17 #include <net/cfg80211.h>
18 #include <net/rtnetlink.h>
19 #include "nl80211.h"
20 #include "reg.h"
21 #include "rdev-ops.h"
22 
23 /*
24  * Software SME in cfg80211, using auth/assoc/deauth calls to the
25  * driver. This is is for implementing nl80211's connect/disconnect
26  * and wireless extensions (if configured.)
27  */
28 
29 struct cfg80211_conn {
30 	struct cfg80211_connect_params params;
31 	/* these are sub-states of the _CONNECTING sme_state */
32 	enum {
33 		CFG80211_CONN_SCANNING,
34 		CFG80211_CONN_SCAN_AGAIN,
35 		CFG80211_CONN_AUTHENTICATE_NEXT,
36 		CFG80211_CONN_AUTHENTICATING,
37 		CFG80211_CONN_AUTH_FAILED,
38 		CFG80211_CONN_ASSOCIATE_NEXT,
39 		CFG80211_CONN_ASSOCIATING,
40 		CFG80211_CONN_ASSOC_FAILED,
41 		CFG80211_CONN_DEAUTH,
42 		CFG80211_CONN_CONNECTED,
43 	} state;
44 	u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
45 	const u8 *ie;
46 	size_t ie_len;
47 	bool auto_auth, prev_bssid_valid;
48 };
49 
50 static void cfg80211_sme_free(struct wireless_dev *wdev)
51 {
52 	if (!wdev->conn)
53 		return;
54 
55 	kfree(wdev->conn->ie);
56 	kfree(wdev->conn);
57 	wdev->conn = NULL;
58 }
59 
60 static int cfg80211_conn_scan(struct wireless_dev *wdev)
61 {
62 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
63 	struct cfg80211_scan_request *request;
64 	int n_channels, err;
65 
66 	ASSERT_RTNL();
67 	ASSERT_WDEV_LOCK(wdev);
68 
69 	if (rdev->scan_req || rdev->scan_msg)
70 		return -EBUSY;
71 
72 	if (wdev->conn->params.channel)
73 		n_channels = 1;
74 	else
75 		n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
76 
77 	request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
78 			  sizeof(request->channels[0]) * n_channels,
79 			  GFP_KERNEL);
80 	if (!request)
81 		return -ENOMEM;
82 
83 	if (wdev->conn->params.channel) {
84 		enum nl80211_band band = wdev->conn->params.channel->band;
85 		struct ieee80211_supported_band *sband =
86 			wdev->wiphy->bands[band];
87 
88 		if (!sband) {
89 			kfree(request);
90 			return -EINVAL;
91 		}
92 		request->channels[0] = wdev->conn->params.channel;
93 		request->rates[band] = (1 << sband->n_bitrates) - 1;
94 	} else {
95 		int i = 0, j;
96 		enum nl80211_band band;
97 		struct ieee80211_supported_band *bands;
98 		struct ieee80211_channel *channel;
99 
100 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
101 			bands = wdev->wiphy->bands[band];
102 			if (!bands)
103 				continue;
104 			for (j = 0; j < bands->n_channels; j++) {
105 				channel = &bands->channels[j];
106 				if (channel->flags & IEEE80211_CHAN_DISABLED)
107 					continue;
108 				request->channels[i++] = channel;
109 			}
110 			request->rates[band] = (1 << bands->n_bitrates) - 1;
111 		}
112 		n_channels = i;
113 	}
114 	request->n_channels = n_channels;
115 	request->ssids = (void *)&request->channels[n_channels];
116 	request->n_ssids = 1;
117 
118 	memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
119 		wdev->conn->params.ssid_len);
120 	request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
121 
122 	eth_broadcast_addr(request->bssid);
123 
124 	request->wdev = wdev;
125 	request->wiphy = &rdev->wiphy;
126 	request->scan_start = jiffies;
127 
128 	rdev->scan_req = request;
129 
130 	err = rdev_scan(rdev, request);
131 	if (!err) {
132 		wdev->conn->state = CFG80211_CONN_SCANNING;
133 		nl80211_send_scan_start(rdev, wdev);
134 		dev_hold(wdev->netdev);
135 	} else {
136 		rdev->scan_req = NULL;
137 		kfree(request);
138 	}
139 	return err;
140 }
141 
142 static int cfg80211_conn_do_work(struct wireless_dev *wdev)
143 {
144 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
145 	struct cfg80211_connect_params *params;
146 	struct cfg80211_assoc_request req = {};
147 	int err;
148 
149 	ASSERT_WDEV_LOCK(wdev);
150 
151 	if (!wdev->conn)
152 		return 0;
153 
154 	params = &wdev->conn->params;
155 
156 	switch (wdev->conn->state) {
157 	case CFG80211_CONN_SCANNING:
158 		/* didn't find it during scan ... */
159 		return -ENOENT;
160 	case CFG80211_CONN_SCAN_AGAIN:
161 		return cfg80211_conn_scan(wdev);
162 	case CFG80211_CONN_AUTHENTICATE_NEXT:
163 		if (WARN_ON(!rdev->ops->auth))
164 			return -EOPNOTSUPP;
165 		wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
166 		return cfg80211_mlme_auth(rdev, wdev->netdev,
167 					  params->channel, params->auth_type,
168 					  params->bssid,
169 					  params->ssid, params->ssid_len,
170 					  NULL, 0,
171 					  params->key, params->key_len,
172 					  params->key_idx, NULL, 0);
173 	case CFG80211_CONN_AUTH_FAILED:
174 		return -ENOTCONN;
175 	case CFG80211_CONN_ASSOCIATE_NEXT:
176 		if (WARN_ON(!rdev->ops->assoc))
177 			return -EOPNOTSUPP;
178 		wdev->conn->state = CFG80211_CONN_ASSOCIATING;
179 		if (wdev->conn->prev_bssid_valid)
180 			req.prev_bssid = wdev->conn->prev_bssid;
181 		req.ie = params->ie;
182 		req.ie_len = params->ie_len;
183 		req.use_mfp = params->mfp != NL80211_MFP_NO;
184 		req.crypto = params->crypto;
185 		req.flags = params->flags;
186 		req.ht_capa = params->ht_capa;
187 		req.ht_capa_mask = params->ht_capa_mask;
188 		req.vht_capa = params->vht_capa;
189 		req.vht_capa_mask = params->vht_capa_mask;
190 
191 		err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
192 					  params->bssid, params->ssid,
193 					  params->ssid_len, &req);
194 		if (err)
195 			cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
196 					     NULL, 0,
197 					     WLAN_REASON_DEAUTH_LEAVING,
198 					     false);
199 		return err;
200 	case CFG80211_CONN_ASSOC_FAILED:
201 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 				     NULL, 0,
203 				     WLAN_REASON_DEAUTH_LEAVING, false);
204 		return -ENOTCONN;
205 	case CFG80211_CONN_DEAUTH:
206 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
207 				     NULL, 0,
208 				     WLAN_REASON_DEAUTH_LEAVING, false);
209 		/* free directly, disconnected event already sent */
210 		cfg80211_sme_free(wdev);
211 		return 0;
212 	default:
213 		return 0;
214 	}
215 }
216 
217 void cfg80211_conn_work(struct work_struct *work)
218 {
219 	struct cfg80211_registered_device *rdev =
220 		container_of(work, struct cfg80211_registered_device, conn_work);
221 	struct wireless_dev *wdev;
222 	u8 bssid_buf[ETH_ALEN], *bssid = NULL;
223 
224 	rtnl_lock();
225 
226 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
227 		if (!wdev->netdev)
228 			continue;
229 
230 		wdev_lock(wdev);
231 		if (!netif_running(wdev->netdev)) {
232 			wdev_unlock(wdev);
233 			continue;
234 		}
235 		if (!wdev->conn ||
236 		    wdev->conn->state == CFG80211_CONN_CONNECTED) {
237 			wdev_unlock(wdev);
238 			continue;
239 		}
240 		if (wdev->conn->params.bssid) {
241 			memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
242 			bssid = bssid_buf;
243 		}
244 		if (cfg80211_conn_do_work(wdev)) {
245 			__cfg80211_connect_result(
246 					wdev->netdev, bssid,
247 					NULL, 0, NULL, 0,
248 					WLAN_STATUS_UNSPECIFIED_FAILURE,
249 					false, NULL);
250 		}
251 		wdev_unlock(wdev);
252 	}
253 
254 	rtnl_unlock();
255 }
256 
257 /* Returned bss is reference counted and must be cleaned up appropriately. */
258 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
259 {
260 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
261 	struct cfg80211_bss *bss;
262 
263 	ASSERT_WDEV_LOCK(wdev);
264 
265 	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
266 			       wdev->conn->params.bssid,
267 			       wdev->conn->params.ssid,
268 			       wdev->conn->params.ssid_len,
269 			       wdev->conn_bss_type,
270 			       IEEE80211_PRIVACY(wdev->conn->params.privacy));
271 	if (!bss)
272 		return NULL;
273 
274 	memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
275 	wdev->conn->params.bssid = wdev->conn->bssid;
276 	wdev->conn->params.channel = bss->channel;
277 	wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
278 	schedule_work(&rdev->conn_work);
279 
280 	return bss;
281 }
282 
283 static void __cfg80211_sme_scan_done(struct net_device *dev)
284 {
285 	struct wireless_dev *wdev = dev->ieee80211_ptr;
286 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
287 	struct cfg80211_bss *bss;
288 
289 	ASSERT_WDEV_LOCK(wdev);
290 
291 	if (!wdev->conn)
292 		return;
293 
294 	if (wdev->conn->state != CFG80211_CONN_SCANNING &&
295 	    wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
296 		return;
297 
298 	bss = cfg80211_get_conn_bss(wdev);
299 	if (bss)
300 		cfg80211_put_bss(&rdev->wiphy, bss);
301 	else
302 		schedule_work(&rdev->conn_work);
303 }
304 
305 void cfg80211_sme_scan_done(struct net_device *dev)
306 {
307 	struct wireless_dev *wdev = dev->ieee80211_ptr;
308 
309 	wdev_lock(wdev);
310 	__cfg80211_sme_scan_done(dev);
311 	wdev_unlock(wdev);
312 }
313 
314 void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
315 {
316 	struct wiphy *wiphy = wdev->wiphy;
317 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
318 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
319 	u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
320 
321 	ASSERT_WDEV_LOCK(wdev);
322 
323 	if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
324 		return;
325 
326 	if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
327 	    wdev->conn->auto_auth &&
328 	    wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
329 		/* select automatically between only open, shared, leap */
330 		switch (wdev->conn->params.auth_type) {
331 		case NL80211_AUTHTYPE_OPEN_SYSTEM:
332 			if (wdev->connect_keys)
333 				wdev->conn->params.auth_type =
334 					NL80211_AUTHTYPE_SHARED_KEY;
335 			else
336 				wdev->conn->params.auth_type =
337 					NL80211_AUTHTYPE_NETWORK_EAP;
338 			break;
339 		case NL80211_AUTHTYPE_SHARED_KEY:
340 			wdev->conn->params.auth_type =
341 				NL80211_AUTHTYPE_NETWORK_EAP;
342 			break;
343 		default:
344 			/* huh? */
345 			wdev->conn->params.auth_type =
346 				NL80211_AUTHTYPE_OPEN_SYSTEM;
347 			break;
348 		}
349 		wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
350 		schedule_work(&rdev->conn_work);
351 	} else if (status_code != WLAN_STATUS_SUCCESS) {
352 		__cfg80211_connect_result(wdev->netdev, mgmt->bssid,
353 					  NULL, 0, NULL, 0,
354 					  status_code, false, NULL);
355 	} else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
356 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
357 		schedule_work(&rdev->conn_work);
358 	}
359 }
360 
361 bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
362 {
363 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
364 
365 	if (!wdev->conn)
366 		return false;
367 
368 	if (status == WLAN_STATUS_SUCCESS) {
369 		wdev->conn->state = CFG80211_CONN_CONNECTED;
370 		return false;
371 	}
372 
373 	if (wdev->conn->prev_bssid_valid) {
374 		/*
375 		 * Some stupid APs don't accept reassoc, so we
376 		 * need to fall back to trying regular assoc;
377 		 * return true so no event is sent to userspace.
378 		 */
379 		wdev->conn->prev_bssid_valid = false;
380 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
381 		schedule_work(&rdev->conn_work);
382 		return true;
383 	}
384 
385 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
386 	schedule_work(&rdev->conn_work);
387 	return false;
388 }
389 
390 void cfg80211_sme_deauth(struct wireless_dev *wdev)
391 {
392 	cfg80211_sme_free(wdev);
393 }
394 
395 void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
396 {
397 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
398 
399 	if (!wdev->conn)
400 		return;
401 
402 	wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
403 	schedule_work(&rdev->conn_work);
404 }
405 
406 void cfg80211_sme_disassoc(struct wireless_dev *wdev)
407 {
408 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
409 
410 	if (!wdev->conn)
411 		return;
412 
413 	wdev->conn->state = CFG80211_CONN_DEAUTH;
414 	schedule_work(&rdev->conn_work);
415 }
416 
417 void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
418 {
419 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
420 
421 	if (!wdev->conn)
422 		return;
423 
424 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
425 	schedule_work(&rdev->conn_work);
426 }
427 
428 static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
429 				     const u8 *ies, size_t ies_len,
430 				     const u8 **out_ies, size_t *out_ies_len)
431 {
432 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
433 	u8 *buf;
434 	size_t offs;
435 
436 	if (!rdev->wiphy.extended_capabilities_len ||
437 	    (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
438 		*out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
439 		if (!*out_ies)
440 			return -ENOMEM;
441 		*out_ies_len = ies_len;
442 		return 0;
443 	}
444 
445 	buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
446 		      GFP_KERNEL);
447 	if (!buf)
448 		return -ENOMEM;
449 
450 	if (ies_len) {
451 		static const u8 before_extcapa[] = {
452 			/* not listing IEs expected to be created by driver */
453 			WLAN_EID_RSN,
454 			WLAN_EID_QOS_CAPA,
455 			WLAN_EID_RRM_ENABLED_CAPABILITIES,
456 			WLAN_EID_MOBILITY_DOMAIN,
457 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
458 			WLAN_EID_BSS_COEX_2040,
459 		};
460 
461 		offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
462 					  ARRAY_SIZE(before_extcapa), 0);
463 		memcpy(buf, ies, offs);
464 		/* leave a whole for extended capabilities IE */
465 		memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
466 		       ies + offs, ies_len - offs);
467 	} else {
468 		offs = 0;
469 	}
470 
471 	/* place extended capabilities IE (with only driver capabilities) */
472 	buf[offs] = WLAN_EID_EXT_CAPABILITY;
473 	buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
474 	memcpy(buf + offs + 2,
475 	       rdev->wiphy.extended_capabilities,
476 	       rdev->wiphy.extended_capabilities_len);
477 
478 	*out_ies = buf;
479 	*out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
480 
481 	return 0;
482 }
483 
484 static int cfg80211_sme_connect(struct wireless_dev *wdev,
485 				struct cfg80211_connect_params *connect,
486 				const u8 *prev_bssid)
487 {
488 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
489 	struct cfg80211_bss *bss;
490 	int err;
491 
492 	if (!rdev->ops->auth || !rdev->ops->assoc)
493 		return -EOPNOTSUPP;
494 
495 	if (wdev->current_bss) {
496 		if (!prev_bssid)
497 			return -EALREADY;
498 		if (prev_bssid &&
499 		    !ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
500 			return -ENOTCONN;
501 		cfg80211_unhold_bss(wdev->current_bss);
502 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
503 		wdev->current_bss = NULL;
504 
505 		cfg80211_sme_free(wdev);
506 	}
507 
508 	if (WARN_ON(wdev->conn))
509 		return -EINPROGRESS;
510 
511 	wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
512 	if (!wdev->conn)
513 		return -ENOMEM;
514 
515 	/*
516 	 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
517 	 */
518 	memcpy(&wdev->conn->params, connect, sizeof(*connect));
519 	if (connect->bssid) {
520 		wdev->conn->params.bssid = wdev->conn->bssid;
521 		memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
522 	}
523 
524 	if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
525 				      &wdev->conn->ie,
526 				      &wdev->conn->params.ie_len)) {
527 		kfree(wdev->conn);
528 		wdev->conn = NULL;
529 		return -ENOMEM;
530 	}
531 	wdev->conn->params.ie = wdev->conn->ie;
532 
533 	if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
534 		wdev->conn->auto_auth = true;
535 		/* start with open system ... should mostly work */
536 		wdev->conn->params.auth_type =
537 			NL80211_AUTHTYPE_OPEN_SYSTEM;
538 	} else {
539 		wdev->conn->auto_auth = false;
540 	}
541 
542 	wdev->conn->params.ssid = wdev->ssid;
543 	wdev->conn->params.ssid_len = wdev->ssid_len;
544 
545 	/* see if we have the bss already */
546 	bss = cfg80211_get_conn_bss(wdev);
547 
548 	if (prev_bssid) {
549 		memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
550 		wdev->conn->prev_bssid_valid = true;
551 	}
552 
553 	/* we're good if we have a matching bss struct */
554 	if (bss) {
555 		err = cfg80211_conn_do_work(wdev);
556 		cfg80211_put_bss(wdev->wiphy, bss);
557 	} else {
558 		/* otherwise we'll need to scan for the AP first */
559 		err = cfg80211_conn_scan(wdev);
560 
561 		/*
562 		 * If we can't scan right now, then we need to scan again
563 		 * after the current scan finished, since the parameters
564 		 * changed (unless we find a good AP anyway).
565 		 */
566 		if (err == -EBUSY) {
567 			err = 0;
568 			wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
569 		}
570 	}
571 
572 	if (err)
573 		cfg80211_sme_free(wdev);
574 
575 	return err;
576 }
577 
578 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
579 {
580 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
581 	int err;
582 
583 	if (!wdev->conn)
584 		return 0;
585 
586 	if (!rdev->ops->deauth)
587 		return -EOPNOTSUPP;
588 
589 	if (wdev->conn->state == CFG80211_CONN_SCANNING ||
590 	    wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
591 		err = 0;
592 		goto out;
593 	}
594 
595 	/* wdev->conn->params.bssid must be set if > SCANNING */
596 	err = cfg80211_mlme_deauth(rdev, wdev->netdev,
597 				   wdev->conn->params.bssid,
598 				   NULL, 0, reason, false);
599  out:
600 	cfg80211_sme_free(wdev);
601 	return err;
602 }
603 
604 /*
605  * code shared for in-device and software SME
606  */
607 
608 static bool cfg80211_is_all_idle(void)
609 {
610 	struct cfg80211_registered_device *rdev;
611 	struct wireless_dev *wdev;
612 	bool is_all_idle = true;
613 
614 	/*
615 	 * All devices must be idle as otherwise if you are actively
616 	 * scanning some new beacon hints could be learned and would
617 	 * count as new regulatory hints.
618 	 */
619 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
620 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
621 			wdev_lock(wdev);
622 			if (wdev->conn || wdev->current_bss)
623 				is_all_idle = false;
624 			wdev_unlock(wdev);
625 		}
626 	}
627 
628 	return is_all_idle;
629 }
630 
631 static void disconnect_work(struct work_struct *work)
632 {
633 	rtnl_lock();
634 	if (cfg80211_is_all_idle())
635 		regulatory_hint_disconnect();
636 	rtnl_unlock();
637 }
638 
639 static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
640 
641 
642 /*
643  * API calls for drivers implementing connect/disconnect and
644  * SME event handling
645  */
646 
647 /* This method must consume bss one way or another */
648 void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
649 			       const u8 *req_ie, size_t req_ie_len,
650 			       const u8 *resp_ie, size_t resp_ie_len,
651 			       u16 status, bool wextev,
652 			       struct cfg80211_bss *bss)
653 {
654 	struct wireless_dev *wdev = dev->ieee80211_ptr;
655 	const u8 *country_ie;
656 #ifdef CONFIG_CFG80211_WEXT
657 	union iwreq_data wrqu;
658 #endif
659 
660 	ASSERT_WDEV_LOCK(wdev);
661 
662 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
663 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
664 		cfg80211_put_bss(wdev->wiphy, bss);
665 		return;
666 	}
667 
668 	nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
669 				    bssid, req_ie, req_ie_len,
670 				    resp_ie, resp_ie_len,
671 				    status, GFP_KERNEL);
672 
673 #ifdef CONFIG_CFG80211_WEXT
674 	if (wextev) {
675 		if (req_ie && status == WLAN_STATUS_SUCCESS) {
676 			memset(&wrqu, 0, sizeof(wrqu));
677 			wrqu.data.length = req_ie_len;
678 			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
679 		}
680 
681 		if (resp_ie && status == WLAN_STATUS_SUCCESS) {
682 			memset(&wrqu, 0, sizeof(wrqu));
683 			wrqu.data.length = resp_ie_len;
684 			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
685 		}
686 
687 		memset(&wrqu, 0, sizeof(wrqu));
688 		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
689 		if (bssid && status == WLAN_STATUS_SUCCESS) {
690 			memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
691 			memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
692 			wdev->wext.prev_bssid_valid = true;
693 		}
694 		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
695 	}
696 #endif
697 
698 	if (!bss && (status == WLAN_STATUS_SUCCESS)) {
699 		WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
700 		bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
701 				       wdev->ssid, wdev->ssid_len,
702 				       wdev->conn_bss_type,
703 				       IEEE80211_PRIVACY_ANY);
704 		if (bss)
705 			cfg80211_hold_bss(bss_from_pub(bss));
706 	}
707 
708 	if (wdev->current_bss) {
709 		cfg80211_unhold_bss(wdev->current_bss);
710 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
711 		wdev->current_bss = NULL;
712 	}
713 
714 	if (status != WLAN_STATUS_SUCCESS) {
715 		kzfree(wdev->connect_keys);
716 		wdev->connect_keys = NULL;
717 		wdev->ssid_len = 0;
718 		if (bss) {
719 			cfg80211_unhold_bss(bss_from_pub(bss));
720 			cfg80211_put_bss(wdev->wiphy, bss);
721 		}
722 		cfg80211_sme_free(wdev);
723 		return;
724 	}
725 
726 	if (WARN_ON(!bss))
727 		return;
728 
729 	wdev->current_bss = bss_from_pub(bss);
730 
731 	cfg80211_upload_connect_keys(wdev);
732 
733 	rcu_read_lock();
734 	country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
735 	if (!country_ie) {
736 		rcu_read_unlock();
737 		return;
738 	}
739 
740 	country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
741 	rcu_read_unlock();
742 
743 	if (!country_ie)
744 		return;
745 
746 	/*
747 	 * ieee80211_bss_get_ie() ensures we can access:
748 	 * - country_ie + 2, the start of the country ie data, and
749 	 * - and country_ie[1] which is the IE length
750 	 */
751 	regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
752 				   country_ie + 2, country_ie[1]);
753 	kfree(country_ie);
754 }
755 
756 /* Consumes bss object one way or another */
757 void cfg80211_connect_bss(struct net_device *dev, const u8 *bssid,
758 			  struct cfg80211_bss *bss, const u8 *req_ie,
759 			  size_t req_ie_len, const u8 *resp_ie,
760 			  size_t resp_ie_len, u16 status, gfp_t gfp)
761 {
762 	struct wireless_dev *wdev = dev->ieee80211_ptr;
763 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
764 	struct cfg80211_event *ev;
765 	unsigned long flags;
766 
767 	if (bss) {
768 		/* Make sure the bss entry provided by the driver is valid. */
769 		struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
770 
771 		if (WARN_ON(list_empty(&ibss->list))) {
772 			cfg80211_put_bss(wdev->wiphy, bss);
773 			return;
774 		}
775 	}
776 
777 	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
778 	if (!ev) {
779 		cfg80211_put_bss(wdev->wiphy, bss);
780 		return;
781 	}
782 
783 	ev->type = EVENT_CONNECT_RESULT;
784 	if (bssid)
785 		memcpy(ev->cr.bssid, bssid, ETH_ALEN);
786 	if (req_ie_len) {
787 		ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
788 		ev->cr.req_ie_len = req_ie_len;
789 		memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
790 	}
791 	if (resp_ie_len) {
792 		ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
793 		ev->cr.resp_ie_len = resp_ie_len;
794 		memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
795 	}
796 	if (bss)
797 		cfg80211_hold_bss(bss_from_pub(bss));
798 	ev->cr.bss = bss;
799 	ev->cr.status = status;
800 
801 	spin_lock_irqsave(&wdev->event_lock, flags);
802 	list_add_tail(&ev->list, &wdev->event_list);
803 	spin_unlock_irqrestore(&wdev->event_lock, flags);
804 	queue_work(cfg80211_wq, &rdev->event_work);
805 }
806 EXPORT_SYMBOL(cfg80211_connect_bss);
807 
808 /* Consumes bss object one way or another */
809 void __cfg80211_roamed(struct wireless_dev *wdev,
810 		       struct cfg80211_bss *bss,
811 		       const u8 *req_ie, size_t req_ie_len,
812 		       const u8 *resp_ie, size_t resp_ie_len)
813 {
814 #ifdef CONFIG_CFG80211_WEXT
815 	union iwreq_data wrqu;
816 #endif
817 	ASSERT_WDEV_LOCK(wdev);
818 
819 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
820 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
821 		goto out;
822 
823 	if (WARN_ON(!wdev->current_bss))
824 		goto out;
825 
826 	cfg80211_unhold_bss(wdev->current_bss);
827 	cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
828 	wdev->current_bss = NULL;
829 
830 	cfg80211_hold_bss(bss_from_pub(bss));
831 	wdev->current_bss = bss_from_pub(bss);
832 
833 	nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
834 			    wdev->netdev, bss->bssid,
835 			    req_ie, req_ie_len, resp_ie, resp_ie_len,
836 			    GFP_KERNEL);
837 
838 #ifdef CONFIG_CFG80211_WEXT
839 	if (req_ie) {
840 		memset(&wrqu, 0, sizeof(wrqu));
841 		wrqu.data.length = req_ie_len;
842 		wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
843 				    &wrqu, req_ie);
844 	}
845 
846 	if (resp_ie) {
847 		memset(&wrqu, 0, sizeof(wrqu));
848 		wrqu.data.length = resp_ie_len;
849 		wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
850 				    &wrqu, resp_ie);
851 	}
852 
853 	memset(&wrqu, 0, sizeof(wrqu));
854 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
855 	memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
856 	memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
857 	wdev->wext.prev_bssid_valid = true;
858 	wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
859 #endif
860 
861 	return;
862 out:
863 	cfg80211_put_bss(wdev->wiphy, bss);
864 }
865 
866 void cfg80211_roamed(struct net_device *dev,
867 		     struct ieee80211_channel *channel,
868 		     const u8 *bssid,
869 		     const u8 *req_ie, size_t req_ie_len,
870 		     const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
871 {
872 	struct wireless_dev *wdev = dev->ieee80211_ptr;
873 	struct cfg80211_bss *bss;
874 
875 	bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
876 			       wdev->ssid_len,
877 			       wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
878 	if (WARN_ON(!bss))
879 		return;
880 
881 	cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
882 			    resp_ie_len, gfp);
883 }
884 EXPORT_SYMBOL(cfg80211_roamed);
885 
886 /* Consumes bss object one way or another */
887 void cfg80211_roamed_bss(struct net_device *dev,
888 			 struct cfg80211_bss *bss, const u8 *req_ie,
889 			 size_t req_ie_len, const u8 *resp_ie,
890 			 size_t resp_ie_len, gfp_t gfp)
891 {
892 	struct wireless_dev *wdev = dev->ieee80211_ptr;
893 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
894 	struct cfg80211_event *ev;
895 	unsigned long flags;
896 
897 	if (WARN_ON(!bss))
898 		return;
899 
900 	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
901 	if (!ev) {
902 		cfg80211_put_bss(wdev->wiphy, bss);
903 		return;
904 	}
905 
906 	ev->type = EVENT_ROAMED;
907 	ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
908 	ev->rm.req_ie_len = req_ie_len;
909 	memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
910 	ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
911 	ev->rm.resp_ie_len = resp_ie_len;
912 	memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
913 	ev->rm.bss = bss;
914 
915 	spin_lock_irqsave(&wdev->event_lock, flags);
916 	list_add_tail(&ev->list, &wdev->event_list);
917 	spin_unlock_irqrestore(&wdev->event_lock, flags);
918 	queue_work(cfg80211_wq, &rdev->event_work);
919 }
920 EXPORT_SYMBOL(cfg80211_roamed_bss);
921 
922 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
923 			     size_t ie_len, u16 reason, bool from_ap)
924 {
925 	struct wireless_dev *wdev = dev->ieee80211_ptr;
926 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
927 	int i;
928 #ifdef CONFIG_CFG80211_WEXT
929 	union iwreq_data wrqu;
930 #endif
931 
932 	ASSERT_WDEV_LOCK(wdev);
933 
934 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
935 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
936 		return;
937 
938 	if (wdev->current_bss) {
939 		cfg80211_unhold_bss(wdev->current_bss);
940 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
941 	}
942 
943 	wdev->current_bss = NULL;
944 	wdev->ssid_len = 0;
945 
946 	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
947 
948 	/* stop critical protocol if supported */
949 	if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
950 		rdev->crit_proto_nlportid = 0;
951 		rdev_crit_proto_stop(rdev, wdev);
952 	}
953 
954 	/*
955 	 * Delete all the keys ... pairwise keys can't really
956 	 * exist any more anyway, but default keys might.
957 	 */
958 	if (rdev->ops->del_key)
959 		for (i = 0; i < 6; i++)
960 			rdev_del_key(rdev, dev, i, false, NULL);
961 
962 	rdev_set_qos_map(rdev, dev, NULL);
963 
964 #ifdef CONFIG_CFG80211_WEXT
965 	memset(&wrqu, 0, sizeof(wrqu));
966 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
967 	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
968 	wdev->wext.connect.ssid_len = 0;
969 #endif
970 
971 	schedule_work(&cfg80211_disconnect_work);
972 }
973 
974 void cfg80211_disconnected(struct net_device *dev, u16 reason,
975 			   const u8 *ie, size_t ie_len,
976 			   bool locally_generated, gfp_t gfp)
977 {
978 	struct wireless_dev *wdev = dev->ieee80211_ptr;
979 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
980 	struct cfg80211_event *ev;
981 	unsigned long flags;
982 
983 	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
984 	if (!ev)
985 		return;
986 
987 	ev->type = EVENT_DISCONNECTED;
988 	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
989 	ev->dc.ie_len = ie_len;
990 	memcpy((void *)ev->dc.ie, ie, ie_len);
991 	ev->dc.reason = reason;
992 	ev->dc.locally_generated = locally_generated;
993 
994 	spin_lock_irqsave(&wdev->event_lock, flags);
995 	list_add_tail(&ev->list, &wdev->event_list);
996 	spin_unlock_irqrestore(&wdev->event_lock, flags);
997 	queue_work(cfg80211_wq, &rdev->event_work);
998 }
999 EXPORT_SYMBOL(cfg80211_disconnected);
1000 
1001 /*
1002  * API calls for nl80211/wext compatibility code
1003  */
1004 int cfg80211_connect(struct cfg80211_registered_device *rdev,
1005 		     struct net_device *dev,
1006 		     struct cfg80211_connect_params *connect,
1007 		     struct cfg80211_cached_keys *connkeys,
1008 		     const u8 *prev_bssid)
1009 {
1010 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1011 	int err;
1012 
1013 	ASSERT_WDEV_LOCK(wdev);
1014 
1015 	if (WARN_ON(wdev->connect_keys)) {
1016 		kzfree(wdev->connect_keys);
1017 		wdev->connect_keys = NULL;
1018 	}
1019 
1020 	cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1021 				  rdev->wiphy.ht_capa_mod_mask);
1022 
1023 	if (connkeys && connkeys->def >= 0) {
1024 		int idx;
1025 		u32 cipher;
1026 
1027 		idx = connkeys->def;
1028 		cipher = connkeys->params[idx].cipher;
1029 		/* If given a WEP key we may need it for shared key auth */
1030 		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1031 		    cipher == WLAN_CIPHER_SUITE_WEP104) {
1032 			connect->key_idx = idx;
1033 			connect->key = connkeys->params[idx].key;
1034 			connect->key_len = connkeys->params[idx].key_len;
1035 
1036 			/*
1037 			 * If ciphers are not set (e.g. when going through
1038 			 * iwconfig), we have to set them appropriately here.
1039 			 */
1040 			if (connect->crypto.cipher_group == 0)
1041 				connect->crypto.cipher_group = cipher;
1042 
1043 			if (connect->crypto.n_ciphers_pairwise == 0) {
1044 				connect->crypto.n_ciphers_pairwise = 1;
1045 				connect->crypto.ciphers_pairwise[0] = cipher;
1046 			}
1047 		}
1048 	}
1049 
1050 	wdev->connect_keys = connkeys;
1051 	memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1052 	wdev->ssid_len = connect->ssid_len;
1053 
1054 	wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1055 					      IEEE80211_BSS_TYPE_ESS;
1056 
1057 	if (!rdev->ops->connect)
1058 		err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1059 	else
1060 		err = rdev_connect(rdev, dev, connect);
1061 
1062 	if (err) {
1063 		wdev->connect_keys = NULL;
1064 		wdev->ssid_len = 0;
1065 		return err;
1066 	}
1067 
1068 	return 0;
1069 }
1070 
1071 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1072 			struct net_device *dev, u16 reason, bool wextev)
1073 {
1074 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1075 	int err = 0;
1076 
1077 	ASSERT_WDEV_LOCK(wdev);
1078 
1079 	kzfree(wdev->connect_keys);
1080 	wdev->connect_keys = NULL;
1081 
1082 	if (wdev->conn)
1083 		err = cfg80211_sme_disconnect(wdev, reason);
1084 	else if (!rdev->ops->disconnect)
1085 		cfg80211_mlme_down(rdev, dev);
1086 	else if (wdev->current_bss)
1087 		err = rdev_disconnect(rdev, dev, reason);
1088 
1089 	return err;
1090 }
1091