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