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