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