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