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