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