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