xref: /openbmc/linux/net/bluetooth/hci_conn.c (revision 83946783)
1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
4 
5    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License version 2 as
9    published by the Free Software Foundation;
10 
11    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22    SOFTWARE IS DISCLAIMED.
23 */
24 
25 /* Bluetooth HCI connection handling. */
26 
27 #include <linux/export.h>
28 #include <linux/debugfs.h>
29 
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
32 #include <net/bluetooth/l2cap.h>
33 
34 #include "hci_request.h"
35 #include "smp.h"
36 #include "a2mp.h"
37 
38 struct sco_param {
39 	u16 pkt_type;
40 	u16 max_latency;
41 	u8  retrans_effort;
42 };
43 
44 static const struct sco_param esco_param_cvsd[] = {
45 	{ EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a,	0x01 }, /* S3 */
46 	{ EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007,	0x01 }, /* S2 */
47 	{ EDR_ESCO_MASK | ESCO_EV3,   0x0007,	0x01 }, /* S1 */
48 	{ EDR_ESCO_MASK | ESCO_HV3,   0xffff,	0x01 }, /* D1 */
49 	{ EDR_ESCO_MASK | ESCO_HV1,   0xffff,	0x01 }, /* D0 */
50 };
51 
52 static const struct sco_param sco_param_cvsd[] = {
53 	{ EDR_ESCO_MASK | ESCO_HV3,   0xffff,	0xff }, /* D1 */
54 	{ EDR_ESCO_MASK | ESCO_HV1,   0xffff,	0xff }, /* D0 */
55 };
56 
57 static const struct sco_param esco_param_msbc[] = {
58 	{ EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d,	0x02 }, /* T2 */
59 	{ EDR_ESCO_MASK | ESCO_EV3,   0x0008,	0x02 }, /* T1 */
60 };
61 
62 /* This function requires the caller holds hdev->lock */
63 static void hci_connect_le_scan_cleanup(struct hci_conn *conn)
64 {
65 	struct hci_conn_params *params;
66 	struct hci_dev *hdev = conn->hdev;
67 	struct smp_irk *irk;
68 	bdaddr_t *bdaddr;
69 	u8 bdaddr_type;
70 
71 	bdaddr = &conn->dst;
72 	bdaddr_type = conn->dst_type;
73 
74 	/* Check if we need to convert to identity address */
75 	irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
76 	if (irk) {
77 		bdaddr = &irk->bdaddr;
78 		bdaddr_type = irk->addr_type;
79 	}
80 
81 	params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr,
82 					   bdaddr_type);
83 	if (!params || !params->explicit_connect)
84 		return;
85 
86 	/* The connection attempt was doing scan for new RPA, and is
87 	 * in scan phase. If params are not associated with any other
88 	 * autoconnect action, remove them completely. If they are, just unmark
89 	 * them as waiting for connection, by clearing explicit_connect field.
90 	 */
91 	params->explicit_connect = false;
92 
93 	list_del_init(&params->action);
94 
95 	switch (params->auto_connect) {
96 	case HCI_AUTO_CONN_EXPLICIT:
97 		hci_conn_params_del(hdev, bdaddr, bdaddr_type);
98 		/* return instead of break to avoid duplicate scan update */
99 		return;
100 	case HCI_AUTO_CONN_DIRECT:
101 	case HCI_AUTO_CONN_ALWAYS:
102 		list_add(&params->action, &hdev->pend_le_conns);
103 		break;
104 	case HCI_AUTO_CONN_REPORT:
105 		list_add(&params->action, &hdev->pend_le_reports);
106 		break;
107 	default:
108 		break;
109 	}
110 
111 	hci_update_background_scan(hdev);
112 }
113 
114 static void hci_conn_cleanup(struct hci_conn *conn)
115 {
116 	struct hci_dev *hdev = conn->hdev;
117 
118 	if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags))
119 		hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type);
120 
121 	hci_chan_list_flush(conn);
122 
123 	hci_conn_hash_del(hdev, conn);
124 
125 	if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
126 		switch (conn->setting & SCO_AIRMODE_MASK) {
127 		case SCO_AIRMODE_CVSD:
128 		case SCO_AIRMODE_TRANSP:
129 			if (hdev->notify)
130 				hdev->notify(hdev, HCI_NOTIFY_DISABLE_SCO);
131 			break;
132 		}
133 	} else {
134 		if (hdev->notify)
135 			hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
136 	}
137 
138 	hci_conn_del_sysfs(conn);
139 
140 	debugfs_remove_recursive(conn->debugfs);
141 
142 	hci_dev_put(hdev);
143 
144 	hci_conn_put(conn);
145 }
146 
147 static void le_scan_cleanup(struct work_struct *work)
148 {
149 	struct hci_conn *conn = container_of(work, struct hci_conn,
150 					     le_scan_cleanup);
151 	struct hci_dev *hdev = conn->hdev;
152 	struct hci_conn *c = NULL;
153 
154 	BT_DBG("%s hcon %p", hdev->name, conn);
155 
156 	hci_dev_lock(hdev);
157 
158 	/* Check that the hci_conn is still around */
159 	rcu_read_lock();
160 	list_for_each_entry_rcu(c, &hdev->conn_hash.list, list) {
161 		if (c == conn)
162 			break;
163 	}
164 	rcu_read_unlock();
165 
166 	if (c == conn) {
167 		hci_connect_le_scan_cleanup(conn);
168 		hci_conn_cleanup(conn);
169 	}
170 
171 	hci_dev_unlock(hdev);
172 	hci_dev_put(hdev);
173 	hci_conn_put(conn);
174 }
175 
176 static void hci_connect_le_scan_remove(struct hci_conn *conn)
177 {
178 	BT_DBG("%s hcon %p", conn->hdev->name, conn);
179 
180 	/* We can't call hci_conn_del/hci_conn_cleanup here since that
181 	 * could deadlock with another hci_conn_del() call that's holding
182 	 * hci_dev_lock and doing cancel_delayed_work_sync(&conn->disc_work).
183 	 * Instead, grab temporary extra references to the hci_dev and
184 	 * hci_conn and perform the necessary cleanup in a separate work
185 	 * callback.
186 	 */
187 
188 	hci_dev_hold(conn->hdev);
189 	hci_conn_get(conn);
190 
191 	/* Even though we hold a reference to the hdev, many other
192 	 * things might get cleaned up meanwhile, including the hdev's
193 	 * own workqueue, so we can't use that for scheduling.
194 	 */
195 	schedule_work(&conn->le_scan_cleanup);
196 }
197 
198 static void hci_acl_create_connection(struct hci_conn *conn)
199 {
200 	struct hci_dev *hdev = conn->hdev;
201 	struct inquiry_entry *ie;
202 	struct hci_cp_create_conn cp;
203 
204 	BT_DBG("hcon %p", conn);
205 
206 	/* Many controllers disallow HCI Create Connection while it is doing
207 	 * HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
208 	 * Connection. This may cause the MGMT discovering state to become false
209 	 * without user space's request but it is okay since the MGMT Discovery
210 	 * APIs do not promise that discovery should be done forever. Instead,
211 	 * the user space monitors the status of MGMT discovering and it may
212 	 * request for discovery again when this flag becomes false.
213 	 */
214 	if (test_bit(HCI_INQUIRY, &hdev->flags)) {
215 		/* Put this connection to "pending" state so that it will be
216 		 * executed after the inquiry cancel command complete event.
217 		 */
218 		conn->state = BT_CONNECT2;
219 		hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL);
220 		return;
221 	}
222 
223 	conn->state = BT_CONNECT;
224 	conn->out = true;
225 	conn->role = HCI_ROLE_MASTER;
226 
227 	conn->attempt++;
228 
229 	conn->link_policy = hdev->link_policy;
230 
231 	memset(&cp, 0, sizeof(cp));
232 	bacpy(&cp.bdaddr, &conn->dst);
233 	cp.pscan_rep_mode = 0x02;
234 
235 	ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
236 	if (ie) {
237 		if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
238 			cp.pscan_rep_mode = ie->data.pscan_rep_mode;
239 			cp.pscan_mode     = ie->data.pscan_mode;
240 			cp.clock_offset   = ie->data.clock_offset |
241 					    cpu_to_le16(0x8000);
242 		}
243 
244 		memcpy(conn->dev_class, ie->data.dev_class, 3);
245 	}
246 
247 	cp.pkt_type = cpu_to_le16(conn->pkt_type);
248 	if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
249 		cp.role_switch = 0x01;
250 	else
251 		cp.role_switch = 0x00;
252 
253 	hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
254 }
255 
256 int hci_disconnect(struct hci_conn *conn, __u8 reason)
257 {
258 	BT_DBG("hcon %p", conn);
259 
260 	/* When we are central of an established connection and it enters
261 	 * the disconnect timeout, then go ahead and try to read the
262 	 * current clock offset.  Processing of the result is done
263 	 * within the event handling and hci_clock_offset_evt function.
264 	 */
265 	if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER &&
266 	    (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) {
267 		struct hci_dev *hdev = conn->hdev;
268 		struct hci_cp_read_clock_offset clkoff_cp;
269 
270 		clkoff_cp.handle = cpu_to_le16(conn->handle);
271 		hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp),
272 			     &clkoff_cp);
273 	}
274 
275 	return hci_abort_conn(conn, reason);
276 }
277 
278 static void hci_add_sco(struct hci_conn *conn, __u16 handle)
279 {
280 	struct hci_dev *hdev = conn->hdev;
281 	struct hci_cp_add_sco cp;
282 
283 	BT_DBG("hcon %p", conn);
284 
285 	conn->state = BT_CONNECT;
286 	conn->out = true;
287 
288 	conn->attempt++;
289 
290 	cp.handle   = cpu_to_le16(handle);
291 	cp.pkt_type = cpu_to_le16(conn->pkt_type);
292 
293 	hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
294 }
295 
296 static bool find_next_esco_param(struct hci_conn *conn,
297 				 const struct sco_param *esco_param, int size)
298 {
299 	for (; conn->attempt <= size; conn->attempt++) {
300 		if (lmp_esco_2m_capable(conn->link) ||
301 		    (esco_param[conn->attempt - 1].pkt_type & ESCO_2EV3))
302 			break;
303 		BT_DBG("hcon %p skipped attempt %d, eSCO 2M not supported",
304 		       conn, conn->attempt);
305 	}
306 
307 	return conn->attempt <= size;
308 }
309 
310 static bool hci_enhanced_setup_sync_conn(struct hci_conn *conn, __u16 handle)
311 {
312 	struct hci_dev *hdev = conn->hdev;
313 	struct hci_cp_enhanced_setup_sync_conn cp;
314 	const struct sco_param *param;
315 
316 	bt_dev_dbg(hdev, "hcon %p", conn);
317 
318 	/* for offload use case, codec needs to configured before opening SCO */
319 	if (conn->codec.data_path)
320 		hci_req_configure_datapath(hdev, &conn->codec);
321 
322 	conn->state = BT_CONNECT;
323 	conn->out = true;
324 
325 	conn->attempt++;
326 
327 	memset(&cp, 0x00, sizeof(cp));
328 
329 	cp.handle   = cpu_to_le16(handle);
330 
331 	cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
332 	cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
333 
334 	switch (conn->codec.id) {
335 	case BT_CODEC_MSBC:
336 		if (!find_next_esco_param(conn, esco_param_msbc,
337 					  ARRAY_SIZE(esco_param_msbc)))
338 			return false;
339 
340 		param = &esco_param_msbc[conn->attempt - 1];
341 		cp.tx_coding_format.id = 0x05;
342 		cp.rx_coding_format.id = 0x05;
343 		cp.tx_codec_frame_size = __cpu_to_le16(60);
344 		cp.rx_codec_frame_size = __cpu_to_le16(60);
345 		cp.in_bandwidth = __cpu_to_le32(32000);
346 		cp.out_bandwidth = __cpu_to_le32(32000);
347 		cp.in_coding_format.id = 0x04;
348 		cp.out_coding_format.id = 0x04;
349 		cp.in_coded_data_size = __cpu_to_le16(16);
350 		cp.out_coded_data_size = __cpu_to_le16(16);
351 		cp.in_pcm_data_format = 2;
352 		cp.out_pcm_data_format = 2;
353 		cp.in_pcm_sample_payload_msb_pos = 0;
354 		cp.out_pcm_sample_payload_msb_pos = 0;
355 		cp.in_data_path = conn->codec.data_path;
356 		cp.out_data_path = conn->codec.data_path;
357 		cp.in_transport_unit_size = 1;
358 		cp.out_transport_unit_size = 1;
359 		break;
360 
361 	case BT_CODEC_TRANSPARENT:
362 		if (!find_next_esco_param(conn, esco_param_msbc,
363 					  ARRAY_SIZE(esco_param_msbc)))
364 			return false;
365 		param = &esco_param_msbc[conn->attempt - 1];
366 		cp.tx_coding_format.id = 0x03;
367 		cp.rx_coding_format.id = 0x03;
368 		cp.tx_codec_frame_size = __cpu_to_le16(60);
369 		cp.rx_codec_frame_size = __cpu_to_le16(60);
370 		cp.in_bandwidth = __cpu_to_le32(0x1f40);
371 		cp.out_bandwidth = __cpu_to_le32(0x1f40);
372 		cp.in_coding_format.id = 0x03;
373 		cp.out_coding_format.id = 0x03;
374 		cp.in_coded_data_size = __cpu_to_le16(16);
375 		cp.out_coded_data_size = __cpu_to_le16(16);
376 		cp.in_pcm_data_format = 2;
377 		cp.out_pcm_data_format = 2;
378 		cp.in_pcm_sample_payload_msb_pos = 0;
379 		cp.out_pcm_sample_payload_msb_pos = 0;
380 		cp.in_data_path = conn->codec.data_path;
381 		cp.out_data_path = conn->codec.data_path;
382 		cp.in_transport_unit_size = 1;
383 		cp.out_transport_unit_size = 1;
384 		break;
385 
386 	case BT_CODEC_CVSD:
387 		if (lmp_esco_capable(conn->link)) {
388 			if (!find_next_esco_param(conn, esco_param_cvsd,
389 						  ARRAY_SIZE(esco_param_cvsd)))
390 				return false;
391 			param = &esco_param_cvsd[conn->attempt - 1];
392 		} else {
393 			if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
394 				return false;
395 			param = &sco_param_cvsd[conn->attempt - 1];
396 		}
397 		cp.tx_coding_format.id = 2;
398 		cp.rx_coding_format.id = 2;
399 		cp.tx_codec_frame_size = __cpu_to_le16(60);
400 		cp.rx_codec_frame_size = __cpu_to_le16(60);
401 		cp.in_bandwidth = __cpu_to_le32(16000);
402 		cp.out_bandwidth = __cpu_to_le32(16000);
403 		cp.in_coding_format.id = 4;
404 		cp.out_coding_format.id = 4;
405 		cp.in_coded_data_size = __cpu_to_le16(16);
406 		cp.out_coded_data_size = __cpu_to_le16(16);
407 		cp.in_pcm_data_format = 2;
408 		cp.out_pcm_data_format = 2;
409 		cp.in_pcm_sample_payload_msb_pos = 0;
410 		cp.out_pcm_sample_payload_msb_pos = 0;
411 		cp.in_data_path = conn->codec.data_path;
412 		cp.out_data_path = conn->codec.data_path;
413 		cp.in_transport_unit_size = 16;
414 		cp.out_transport_unit_size = 16;
415 		break;
416 	default:
417 		return false;
418 	}
419 
420 	cp.retrans_effort = param->retrans_effort;
421 	cp.pkt_type = __cpu_to_le16(param->pkt_type);
422 	cp.max_latency = __cpu_to_le16(param->max_latency);
423 
424 	if (hci_send_cmd(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
425 		return false;
426 
427 	return true;
428 }
429 
430 static bool hci_setup_sync_conn(struct hci_conn *conn, __u16 handle)
431 {
432 	struct hci_dev *hdev = conn->hdev;
433 	struct hci_cp_setup_sync_conn cp;
434 	const struct sco_param *param;
435 
436 	bt_dev_dbg(hdev, "hcon %p", conn);
437 
438 	conn->state = BT_CONNECT;
439 	conn->out = true;
440 
441 	conn->attempt++;
442 
443 	cp.handle   = cpu_to_le16(handle);
444 
445 	cp.tx_bandwidth   = cpu_to_le32(0x00001f40);
446 	cp.rx_bandwidth   = cpu_to_le32(0x00001f40);
447 	cp.voice_setting  = cpu_to_le16(conn->setting);
448 
449 	switch (conn->setting & SCO_AIRMODE_MASK) {
450 	case SCO_AIRMODE_TRANSP:
451 		if (!find_next_esco_param(conn, esco_param_msbc,
452 					  ARRAY_SIZE(esco_param_msbc)))
453 			return false;
454 		param = &esco_param_msbc[conn->attempt - 1];
455 		break;
456 	case SCO_AIRMODE_CVSD:
457 		if (lmp_esco_capable(conn->link)) {
458 			if (!find_next_esco_param(conn, esco_param_cvsd,
459 						  ARRAY_SIZE(esco_param_cvsd)))
460 				return false;
461 			param = &esco_param_cvsd[conn->attempt - 1];
462 		} else {
463 			if (conn->attempt > ARRAY_SIZE(sco_param_cvsd))
464 				return false;
465 			param = &sco_param_cvsd[conn->attempt - 1];
466 		}
467 		break;
468 	default:
469 		return false;
470 	}
471 
472 	cp.retrans_effort = param->retrans_effort;
473 	cp.pkt_type = __cpu_to_le16(param->pkt_type);
474 	cp.max_latency = __cpu_to_le16(param->max_latency);
475 
476 	if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0)
477 		return false;
478 
479 	return true;
480 }
481 
482 bool hci_setup_sync(struct hci_conn *conn, __u16 handle)
483 {
484 	if (enhanced_sco_capable(conn->hdev))
485 		return hci_enhanced_setup_sync_conn(conn, handle);
486 
487 	return hci_setup_sync_conn(conn, handle);
488 }
489 
490 u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
491 		      u16 to_multiplier)
492 {
493 	struct hci_dev *hdev = conn->hdev;
494 	struct hci_conn_params *params;
495 	struct hci_cp_le_conn_update cp;
496 
497 	hci_dev_lock(hdev);
498 
499 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
500 	if (params) {
501 		params->conn_min_interval = min;
502 		params->conn_max_interval = max;
503 		params->conn_latency = latency;
504 		params->supervision_timeout = to_multiplier;
505 	}
506 
507 	hci_dev_unlock(hdev);
508 
509 	memset(&cp, 0, sizeof(cp));
510 	cp.handle		= cpu_to_le16(conn->handle);
511 	cp.conn_interval_min	= cpu_to_le16(min);
512 	cp.conn_interval_max	= cpu_to_le16(max);
513 	cp.conn_latency		= cpu_to_le16(latency);
514 	cp.supervision_timeout	= cpu_to_le16(to_multiplier);
515 	cp.min_ce_len		= cpu_to_le16(0x0000);
516 	cp.max_ce_len		= cpu_to_le16(0x0000);
517 
518 	hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
519 
520 	if (params)
521 		return 0x01;
522 
523 	return 0x00;
524 }
525 
526 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
527 		      __u8 ltk[16], __u8 key_size)
528 {
529 	struct hci_dev *hdev = conn->hdev;
530 	struct hci_cp_le_start_enc cp;
531 
532 	BT_DBG("hcon %p", conn);
533 
534 	memset(&cp, 0, sizeof(cp));
535 
536 	cp.handle = cpu_to_le16(conn->handle);
537 	cp.rand = rand;
538 	cp.ediv = ediv;
539 	memcpy(cp.ltk, ltk, key_size);
540 
541 	hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
542 }
543 
544 /* Device _must_ be locked */
545 void hci_sco_setup(struct hci_conn *conn, __u8 status)
546 {
547 	struct hci_conn *sco = conn->link;
548 
549 	if (!sco)
550 		return;
551 
552 	BT_DBG("hcon %p", conn);
553 
554 	if (!status) {
555 		if (lmp_esco_capable(conn->hdev))
556 			hci_setup_sync(sco, conn->handle);
557 		else
558 			hci_add_sco(sco, conn->handle);
559 	} else {
560 		hci_connect_cfm(sco, status);
561 		hci_conn_del(sco);
562 	}
563 }
564 
565 static void hci_conn_timeout(struct work_struct *work)
566 {
567 	struct hci_conn *conn = container_of(work, struct hci_conn,
568 					     disc_work.work);
569 	int refcnt = atomic_read(&conn->refcnt);
570 
571 	BT_DBG("hcon %p state %s", conn, state_to_string(conn->state));
572 
573 	WARN_ON(refcnt < 0);
574 
575 	/* FIXME: It was observed that in pairing failed scenario, refcnt
576 	 * drops below 0. Probably this is because l2cap_conn_del calls
577 	 * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is
578 	 * dropped. After that loop hci_chan_del is called which also drops
579 	 * conn. For now make sure that ACL is alive if refcnt is higher then 0,
580 	 * otherwise drop it.
581 	 */
582 	if (refcnt > 0)
583 		return;
584 
585 	/* LE connections in scanning state need special handling */
586 	if (conn->state == BT_CONNECT && conn->type == LE_LINK &&
587 	    test_bit(HCI_CONN_SCANNING, &conn->flags)) {
588 		hci_connect_le_scan_remove(conn);
589 		return;
590 	}
591 
592 	hci_abort_conn(conn, hci_proto_disconn_ind(conn));
593 }
594 
595 /* Enter sniff mode */
596 static void hci_conn_idle(struct work_struct *work)
597 {
598 	struct hci_conn *conn = container_of(work, struct hci_conn,
599 					     idle_work.work);
600 	struct hci_dev *hdev = conn->hdev;
601 
602 	BT_DBG("hcon %p mode %d", conn, conn->mode);
603 
604 	if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
605 		return;
606 
607 	if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
608 		return;
609 
610 	if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
611 		struct hci_cp_sniff_subrate cp;
612 		cp.handle             = cpu_to_le16(conn->handle);
613 		cp.max_latency        = cpu_to_le16(0);
614 		cp.min_remote_timeout = cpu_to_le16(0);
615 		cp.min_local_timeout  = cpu_to_le16(0);
616 		hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
617 	}
618 
619 	if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
620 		struct hci_cp_sniff_mode cp;
621 		cp.handle       = cpu_to_le16(conn->handle);
622 		cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
623 		cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
624 		cp.attempt      = cpu_to_le16(4);
625 		cp.timeout      = cpu_to_le16(1);
626 		hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
627 	}
628 }
629 
630 static void hci_conn_auto_accept(struct work_struct *work)
631 {
632 	struct hci_conn *conn = container_of(work, struct hci_conn,
633 					     auto_accept_work.work);
634 
635 	hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
636 		     &conn->dst);
637 }
638 
639 static void le_disable_advertising(struct hci_dev *hdev)
640 {
641 	if (ext_adv_capable(hdev)) {
642 		struct hci_cp_le_set_ext_adv_enable cp;
643 
644 		cp.enable = 0x00;
645 		cp.num_of_sets = 0x00;
646 
647 		hci_send_cmd(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE, sizeof(cp),
648 			     &cp);
649 	} else {
650 		u8 enable = 0x00;
651 		hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
652 			     &enable);
653 	}
654 }
655 
656 static void le_conn_timeout(struct work_struct *work)
657 {
658 	struct hci_conn *conn = container_of(work, struct hci_conn,
659 					     le_conn_timeout.work);
660 	struct hci_dev *hdev = conn->hdev;
661 
662 	BT_DBG("");
663 
664 	/* We could end up here due to having done directed advertising,
665 	 * so clean up the state if necessary. This should however only
666 	 * happen with broken hardware or if low duty cycle was used
667 	 * (which doesn't have a timeout of its own).
668 	 */
669 	if (conn->role == HCI_ROLE_SLAVE) {
670 		/* Disable LE Advertising */
671 		le_disable_advertising(hdev);
672 		hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT);
673 		return;
674 	}
675 
676 	hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
677 }
678 
679 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
680 			      u8 role)
681 {
682 	struct hci_conn *conn;
683 
684 	BT_DBG("%s dst %pMR", hdev->name, dst);
685 
686 	conn = kzalloc(sizeof(*conn), GFP_KERNEL);
687 	if (!conn)
688 		return NULL;
689 
690 	bacpy(&conn->dst, dst);
691 	bacpy(&conn->src, &hdev->bdaddr);
692 	conn->hdev  = hdev;
693 	conn->type  = type;
694 	conn->role  = role;
695 	conn->mode  = HCI_CM_ACTIVE;
696 	conn->state = BT_OPEN;
697 	conn->auth_type = HCI_AT_GENERAL_BONDING;
698 	conn->io_capability = hdev->io_capability;
699 	conn->remote_auth = 0xff;
700 	conn->key_type = 0xff;
701 	conn->rssi = HCI_RSSI_INVALID;
702 	conn->tx_power = HCI_TX_POWER_INVALID;
703 	conn->max_tx_power = HCI_TX_POWER_INVALID;
704 
705 	set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
706 	conn->disc_timeout = HCI_DISCONN_TIMEOUT;
707 
708 	/* Set Default Authenticated payload timeout to 30s */
709 	conn->auth_payload_timeout = DEFAULT_AUTH_PAYLOAD_TIMEOUT;
710 
711 	if (conn->role == HCI_ROLE_MASTER)
712 		conn->out = true;
713 
714 	switch (type) {
715 	case ACL_LINK:
716 		conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
717 		break;
718 	case LE_LINK:
719 		/* conn->src should reflect the local identity address */
720 		hci_copy_identity_address(hdev, &conn->src, &conn->src_type);
721 		break;
722 	case SCO_LINK:
723 		if (lmp_esco_capable(hdev))
724 			conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
725 					(hdev->esco_type & EDR_ESCO_MASK);
726 		else
727 			conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
728 		break;
729 	case ESCO_LINK:
730 		conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
731 		break;
732 	}
733 
734 	skb_queue_head_init(&conn->data_q);
735 
736 	INIT_LIST_HEAD(&conn->chan_list);
737 
738 	INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
739 	INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
740 	INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
741 	INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout);
742 	INIT_WORK(&conn->le_scan_cleanup, le_scan_cleanup);
743 
744 	atomic_set(&conn->refcnt, 0);
745 
746 	hci_dev_hold(hdev);
747 
748 	hci_conn_hash_add(hdev, conn);
749 
750 	/* The SCO and eSCO connections will only be notified when their
751 	 * setup has been completed. This is different to ACL links which
752 	 * can be notified right away.
753 	 */
754 	if (conn->type != SCO_LINK && conn->type != ESCO_LINK) {
755 		if (hdev->notify)
756 			hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
757 	}
758 
759 	hci_conn_init_sysfs(conn);
760 
761 	return conn;
762 }
763 
764 int hci_conn_del(struct hci_conn *conn)
765 {
766 	struct hci_dev *hdev = conn->hdev;
767 
768 	BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
769 
770 	cancel_delayed_work_sync(&conn->disc_work);
771 	cancel_delayed_work_sync(&conn->auto_accept_work);
772 	cancel_delayed_work_sync(&conn->idle_work);
773 
774 	if (conn->type == ACL_LINK) {
775 		struct hci_conn *sco = conn->link;
776 		if (sco)
777 			sco->link = NULL;
778 
779 		/* Unacked frames */
780 		hdev->acl_cnt += conn->sent;
781 	} else if (conn->type == LE_LINK) {
782 		cancel_delayed_work(&conn->le_conn_timeout);
783 
784 		if (hdev->le_pkts)
785 			hdev->le_cnt += conn->sent;
786 		else
787 			hdev->acl_cnt += conn->sent;
788 	} else {
789 		struct hci_conn *acl = conn->link;
790 		if (acl) {
791 			acl->link = NULL;
792 			hci_conn_drop(acl);
793 		}
794 	}
795 
796 	if (conn->amp_mgr)
797 		amp_mgr_put(conn->amp_mgr);
798 
799 	skb_queue_purge(&conn->data_q);
800 
801 	/* Remove the connection from the list and cleanup its remaining
802 	 * state. This is a separate function since for some cases like
803 	 * BT_CONNECT_SCAN we *only* want the cleanup part without the
804 	 * rest of hci_conn_del.
805 	 */
806 	hci_conn_cleanup(conn);
807 
808 	return 0;
809 }
810 
811 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type)
812 {
813 	int use_src = bacmp(src, BDADDR_ANY);
814 	struct hci_dev *hdev = NULL, *d;
815 
816 	BT_DBG("%pMR -> %pMR", src, dst);
817 
818 	read_lock(&hci_dev_list_lock);
819 
820 	list_for_each_entry(d, &hci_dev_list, list) {
821 		if (!test_bit(HCI_UP, &d->flags) ||
822 		    hci_dev_test_flag(d, HCI_USER_CHANNEL) ||
823 		    d->dev_type != HCI_PRIMARY)
824 			continue;
825 
826 		/* Simple routing:
827 		 *   No source address - find interface with bdaddr != dst
828 		 *   Source address    - find interface with bdaddr == src
829 		 */
830 
831 		if (use_src) {
832 			bdaddr_t id_addr;
833 			u8 id_addr_type;
834 
835 			if (src_type == BDADDR_BREDR) {
836 				if (!lmp_bredr_capable(d))
837 					continue;
838 				bacpy(&id_addr, &d->bdaddr);
839 				id_addr_type = BDADDR_BREDR;
840 			} else {
841 				if (!lmp_le_capable(d))
842 					continue;
843 
844 				hci_copy_identity_address(d, &id_addr,
845 							  &id_addr_type);
846 
847 				/* Convert from HCI to three-value type */
848 				if (id_addr_type == ADDR_LE_DEV_PUBLIC)
849 					id_addr_type = BDADDR_LE_PUBLIC;
850 				else
851 					id_addr_type = BDADDR_LE_RANDOM;
852 			}
853 
854 			if (!bacmp(&id_addr, src) && id_addr_type == src_type) {
855 				hdev = d; break;
856 			}
857 		} else {
858 			if (bacmp(&d->bdaddr, dst)) {
859 				hdev = d; break;
860 			}
861 		}
862 	}
863 
864 	if (hdev)
865 		hdev = hci_dev_hold(hdev);
866 
867 	read_unlock(&hci_dev_list_lock);
868 	return hdev;
869 }
870 EXPORT_SYMBOL(hci_get_route);
871 
872 /* This function requires the caller holds hdev->lock */
873 void hci_le_conn_failed(struct hci_conn *conn, u8 status)
874 {
875 	struct hci_dev *hdev = conn->hdev;
876 	struct hci_conn_params *params;
877 
878 	params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
879 					   conn->dst_type);
880 	if (params && params->conn) {
881 		hci_conn_drop(params->conn);
882 		hci_conn_put(params->conn);
883 		params->conn = NULL;
884 	}
885 
886 	conn->state = BT_CLOSED;
887 
888 	/* If the status indicates successful cancellation of
889 	 * the attempt (i.e. Unknown Connection Id) there's no point of
890 	 * notifying failure since we'll go back to keep trying to
891 	 * connect. The only exception is explicit connect requests
892 	 * where a timeout + cancel does indicate an actual failure.
893 	 */
894 	if (status != HCI_ERROR_UNKNOWN_CONN_ID ||
895 	    (params && params->explicit_connect))
896 		mgmt_connect_failed(hdev, &conn->dst, conn->type,
897 				    conn->dst_type, status);
898 
899 	hci_connect_cfm(conn, status);
900 
901 	hci_conn_del(conn);
902 
903 	/* The suspend notifier is waiting for all devices to disconnect and an
904 	 * LE connect cancel will result in an hci_le_conn_failed. Once the last
905 	 * connection is deleted, we should also wake the suspend queue to
906 	 * complete suspend operations.
907 	 */
908 	if (list_empty(&hdev->conn_hash.list) &&
909 	    test_and_clear_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks)) {
910 		wake_up(&hdev->suspend_wait_q);
911 	}
912 
913 	/* Since we may have temporarily stopped the background scanning in
914 	 * favor of connection establishment, we should restart it.
915 	 */
916 	hci_update_background_scan(hdev);
917 
918 	/* Re-enable advertising in case this was a failed connection
919 	 * attempt as a peripheral.
920 	 */
921 	hci_req_reenable_advertising(hdev);
922 }
923 
924 static void create_le_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
925 {
926 	struct hci_conn *conn;
927 
928 	hci_dev_lock(hdev);
929 
930 	conn = hci_lookup_le_connect(hdev);
931 
932 	if (hdev->adv_instance_cnt)
933 		hci_req_resume_adv_instances(hdev);
934 
935 	if (!status) {
936 		hci_connect_le_scan_cleanup(conn);
937 		goto done;
938 	}
939 
940 	bt_dev_err(hdev, "request failed to create LE connection: "
941 		   "status 0x%2.2x", status);
942 
943 	if (!conn)
944 		goto done;
945 
946 	hci_le_conn_failed(conn, status);
947 
948 done:
949 	hci_dev_unlock(hdev);
950 }
951 
952 static bool conn_use_rpa(struct hci_conn *conn)
953 {
954 	struct hci_dev *hdev = conn->hdev;
955 
956 	return hci_dev_test_flag(hdev, HCI_PRIVACY);
957 }
958 
959 static void set_ext_conn_params(struct hci_conn *conn,
960 				struct hci_cp_le_ext_conn_param *p)
961 {
962 	struct hci_dev *hdev = conn->hdev;
963 
964 	memset(p, 0, sizeof(*p));
965 
966 	p->scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
967 	p->scan_window = cpu_to_le16(hdev->le_scan_window_connect);
968 	p->conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
969 	p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
970 	p->conn_latency = cpu_to_le16(conn->le_conn_latency);
971 	p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
972 	p->min_ce_len = cpu_to_le16(0x0000);
973 	p->max_ce_len = cpu_to_le16(0x0000);
974 }
975 
976 static void hci_req_add_le_create_conn(struct hci_request *req,
977 				       struct hci_conn *conn,
978 				       bdaddr_t *direct_rpa)
979 {
980 	struct hci_dev *hdev = conn->hdev;
981 	u8 own_addr_type;
982 
983 	/* If direct address was provided we use it instead of current
984 	 * address.
985 	 */
986 	if (direct_rpa) {
987 		if (bacmp(&req->hdev->random_addr, direct_rpa))
988 			hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
989 								direct_rpa);
990 
991 		/* direct address is always RPA */
992 		own_addr_type = ADDR_LE_DEV_RANDOM;
993 	} else {
994 		/* Update random address, but set require_privacy to false so
995 		 * that we never connect with an non-resolvable address.
996 		 */
997 		if (hci_update_random_address(req, false, conn_use_rpa(conn),
998 					      &own_addr_type))
999 			return;
1000 	}
1001 
1002 	if (use_ext_conn(hdev)) {
1003 		struct hci_cp_le_ext_create_conn *cp;
1004 		struct hci_cp_le_ext_conn_param *p;
1005 		u8 data[sizeof(*cp) + sizeof(*p) * 3];
1006 		u32 plen;
1007 
1008 		cp = (void *) data;
1009 		p = (void *) cp->data;
1010 
1011 		memset(cp, 0, sizeof(*cp));
1012 
1013 		bacpy(&cp->peer_addr, &conn->dst);
1014 		cp->peer_addr_type = conn->dst_type;
1015 		cp->own_addr_type = own_addr_type;
1016 
1017 		plen = sizeof(*cp);
1018 
1019 		if (scan_1m(hdev)) {
1020 			cp->phys |= LE_SCAN_PHY_1M;
1021 			set_ext_conn_params(conn, p);
1022 
1023 			p++;
1024 			plen += sizeof(*p);
1025 		}
1026 
1027 		if (scan_2m(hdev)) {
1028 			cp->phys |= LE_SCAN_PHY_2M;
1029 			set_ext_conn_params(conn, p);
1030 
1031 			p++;
1032 			plen += sizeof(*p);
1033 		}
1034 
1035 		if (scan_coded(hdev)) {
1036 			cp->phys |= LE_SCAN_PHY_CODED;
1037 			set_ext_conn_params(conn, p);
1038 
1039 			plen += sizeof(*p);
1040 		}
1041 
1042 		hci_req_add(req, HCI_OP_LE_EXT_CREATE_CONN, plen, data);
1043 
1044 	} else {
1045 		struct hci_cp_le_create_conn cp;
1046 
1047 		memset(&cp, 0, sizeof(cp));
1048 
1049 		cp.scan_interval = cpu_to_le16(hdev->le_scan_int_connect);
1050 		cp.scan_window = cpu_to_le16(hdev->le_scan_window_connect);
1051 
1052 		bacpy(&cp.peer_addr, &conn->dst);
1053 		cp.peer_addr_type = conn->dst_type;
1054 		cp.own_address_type = own_addr_type;
1055 		cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval);
1056 		cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
1057 		cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
1058 		cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
1059 		cp.min_ce_len = cpu_to_le16(0x0000);
1060 		cp.max_ce_len = cpu_to_le16(0x0000);
1061 
1062 		hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
1063 	}
1064 
1065 	conn->state = BT_CONNECT;
1066 	clear_bit(HCI_CONN_SCANNING, &conn->flags);
1067 }
1068 
1069 static void hci_req_directed_advertising(struct hci_request *req,
1070 					 struct hci_conn *conn)
1071 {
1072 	struct hci_dev *hdev = req->hdev;
1073 	u8 own_addr_type;
1074 	u8 enable;
1075 
1076 	if (ext_adv_capable(hdev)) {
1077 		struct hci_cp_le_set_ext_adv_params cp;
1078 		bdaddr_t random_addr;
1079 
1080 		/* Set require_privacy to false so that the remote device has a
1081 		 * chance of identifying us.
1082 		 */
1083 		if (hci_get_random_address(hdev, false, conn_use_rpa(conn), NULL,
1084 					   &own_addr_type, &random_addr) < 0)
1085 			return;
1086 
1087 		memset(&cp, 0, sizeof(cp));
1088 
1089 		cp.evt_properties = cpu_to_le16(LE_LEGACY_ADV_DIRECT_IND);
1090 		cp.own_addr_type = own_addr_type;
1091 		cp.channel_map = hdev->le_adv_channel_map;
1092 		cp.tx_power = HCI_TX_POWER_INVALID;
1093 		cp.primary_phy = HCI_ADV_PHY_1M;
1094 		cp.secondary_phy = HCI_ADV_PHY_1M;
1095 		cp.handle = 0; /* Use instance 0 for directed adv */
1096 		cp.own_addr_type = own_addr_type;
1097 		cp.peer_addr_type = conn->dst_type;
1098 		bacpy(&cp.peer_addr, &conn->dst);
1099 
1100 		/* As per Core Spec 5.2 Vol 2, PART E, Sec 7.8.53, for
1101 		 * advertising_event_property LE_LEGACY_ADV_DIRECT_IND
1102 		 * does not supports advertising data when the advertising set already
1103 		 * contains some, the controller shall return erroc code 'Invalid
1104 		 * HCI Command Parameters(0x12).
1105 		 * So it is required to remove adv set for handle 0x00. since we use
1106 		 * instance 0 for directed adv.
1107 		 */
1108 		__hci_req_remove_ext_adv_instance(req, cp.handle);
1109 
1110 		hci_req_add(req, HCI_OP_LE_SET_EXT_ADV_PARAMS, sizeof(cp), &cp);
1111 
1112 		if (own_addr_type == ADDR_LE_DEV_RANDOM &&
1113 		    bacmp(&random_addr, BDADDR_ANY) &&
1114 		    bacmp(&random_addr, &hdev->random_addr)) {
1115 			struct hci_cp_le_set_adv_set_rand_addr cp;
1116 
1117 			memset(&cp, 0, sizeof(cp));
1118 
1119 			cp.handle = 0;
1120 			bacpy(&cp.bdaddr, &random_addr);
1121 
1122 			hci_req_add(req,
1123 				    HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1124 				    sizeof(cp), &cp);
1125 		}
1126 
1127 		__hci_req_enable_ext_advertising(req, 0x00);
1128 	} else {
1129 		struct hci_cp_le_set_adv_param cp;
1130 
1131 		/* Clear the HCI_LE_ADV bit temporarily so that the
1132 		 * hci_update_random_address knows that it's safe to go ahead
1133 		 * and write a new random address. The flag will be set back on
1134 		 * as soon as the SET_ADV_ENABLE HCI command completes.
1135 		 */
1136 		hci_dev_clear_flag(hdev, HCI_LE_ADV);
1137 
1138 		/* Set require_privacy to false so that the remote device has a
1139 		 * chance of identifying us.
1140 		 */
1141 		if (hci_update_random_address(req, false, conn_use_rpa(conn),
1142 					      &own_addr_type) < 0)
1143 			return;
1144 
1145 		memset(&cp, 0, sizeof(cp));
1146 
1147 		/* Some controllers might reject command if intervals are not
1148 		 * within range for undirected advertising.
1149 		 * BCM20702A0 is known to be affected by this.
1150 		 */
1151 		cp.min_interval = cpu_to_le16(0x0020);
1152 		cp.max_interval = cpu_to_le16(0x0020);
1153 
1154 		cp.type = LE_ADV_DIRECT_IND;
1155 		cp.own_address_type = own_addr_type;
1156 		cp.direct_addr_type = conn->dst_type;
1157 		bacpy(&cp.direct_addr, &conn->dst);
1158 		cp.channel_map = hdev->le_adv_channel_map;
1159 
1160 		hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1161 
1162 		enable = 0x01;
1163 		hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
1164 			    &enable);
1165 	}
1166 
1167 	conn->state = BT_CONNECT;
1168 }
1169 
1170 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
1171 				u8 dst_type, bool dst_resolved, u8 sec_level,
1172 				u16 conn_timeout, u8 role, bdaddr_t *direct_rpa)
1173 {
1174 	struct hci_conn_params *params;
1175 	struct hci_conn *conn;
1176 	struct smp_irk *irk;
1177 	struct hci_request req;
1178 	int err;
1179 
1180 	/* This ensures that during disable le_scan address resolution
1181 	 * will not be disabled if it is followed by le_create_conn
1182 	 */
1183 	bool rpa_le_conn = true;
1184 
1185 	/* Let's make sure that le is enabled.*/
1186 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1187 		if (lmp_le_capable(hdev))
1188 			return ERR_PTR(-ECONNREFUSED);
1189 
1190 		return ERR_PTR(-EOPNOTSUPP);
1191 	}
1192 
1193 	/* Since the controller supports only one LE connection attempt at a
1194 	 * time, we return -EBUSY if there is any connection attempt running.
1195 	 */
1196 	if (hci_lookup_le_connect(hdev))
1197 		return ERR_PTR(-EBUSY);
1198 
1199 	/* If there's already a connection object but it's not in
1200 	 * scanning state it means it must already be established, in
1201 	 * which case we can't do anything else except report a failure
1202 	 * to connect.
1203 	 */
1204 	conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
1205 	if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) {
1206 		return ERR_PTR(-EBUSY);
1207 	}
1208 
1209 	/* Check if the destination address has been resolved by the controller
1210 	 * since if it did then the identity address shall be used.
1211 	 */
1212 	if (!dst_resolved) {
1213 		/* When given an identity address with existing identity
1214 		 * resolving key, the connection needs to be established
1215 		 * to a resolvable random address.
1216 		 *
1217 		 * Storing the resolvable random address is required here
1218 		 * to handle connection failures. The address will later
1219 		 * be resolved back into the original identity address
1220 		 * from the connect request.
1221 		 */
1222 		irk = hci_find_irk_by_addr(hdev, dst, dst_type);
1223 		if (irk && bacmp(&irk->rpa, BDADDR_ANY)) {
1224 			dst = &irk->rpa;
1225 			dst_type = ADDR_LE_DEV_RANDOM;
1226 		}
1227 	}
1228 
1229 	if (conn) {
1230 		bacpy(&conn->dst, dst);
1231 	} else {
1232 		conn = hci_conn_add(hdev, LE_LINK, dst, role);
1233 		if (!conn)
1234 			return ERR_PTR(-ENOMEM);
1235 		hci_conn_hold(conn);
1236 		conn->pending_sec_level = sec_level;
1237 	}
1238 
1239 	conn->dst_type = dst_type;
1240 	conn->sec_level = BT_SECURITY_LOW;
1241 	conn->conn_timeout = conn_timeout;
1242 
1243 	hci_req_init(&req, hdev);
1244 
1245 	/* Disable advertising if we're active. For central role
1246 	 * connections most controllers will refuse to connect if
1247 	 * advertising is enabled, and for peripheral role connections we
1248 	 * anyway have to disable it in order to start directed
1249 	 * advertising. Any registered advertisements will be
1250 	 * re-enabled after the connection attempt is finished.
1251 	 */
1252 	if (hci_dev_test_flag(hdev, HCI_LE_ADV))
1253 		__hci_req_pause_adv_instances(&req);
1254 
1255 	/* If requested to connect as peripheral use directed advertising */
1256 	if (conn->role == HCI_ROLE_SLAVE) {
1257 		/* If we're active scanning most controllers are unable
1258 		 * to initiate advertising. Simply reject the attempt.
1259 		 */
1260 		if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
1261 		    hdev->le_scan_type == LE_SCAN_ACTIVE) {
1262 			hci_req_purge(&req);
1263 			hci_conn_del(conn);
1264 			return ERR_PTR(-EBUSY);
1265 		}
1266 
1267 		hci_req_directed_advertising(&req, conn);
1268 		goto create_conn;
1269 	}
1270 
1271 	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
1272 	if (params) {
1273 		conn->le_conn_min_interval = params->conn_min_interval;
1274 		conn->le_conn_max_interval = params->conn_max_interval;
1275 		conn->le_conn_latency = params->conn_latency;
1276 		conn->le_supv_timeout = params->supervision_timeout;
1277 	} else {
1278 		conn->le_conn_min_interval = hdev->le_conn_min_interval;
1279 		conn->le_conn_max_interval = hdev->le_conn_max_interval;
1280 		conn->le_conn_latency = hdev->le_conn_latency;
1281 		conn->le_supv_timeout = hdev->le_supv_timeout;
1282 	}
1283 
1284 	/* If controller is scanning, we stop it since some controllers are
1285 	 * not able to scan and connect at the same time. Also set the
1286 	 * HCI_LE_SCAN_INTERRUPTED flag so that the command complete
1287 	 * handler for scan disabling knows to set the correct discovery
1288 	 * state.
1289 	 */
1290 	if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
1291 		hci_req_add_le_scan_disable(&req, rpa_le_conn);
1292 		hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
1293 	}
1294 
1295 	hci_req_add_le_create_conn(&req, conn, direct_rpa);
1296 
1297 create_conn:
1298 	err = hci_req_run(&req, create_le_conn_complete);
1299 	if (err) {
1300 		hci_conn_del(conn);
1301 
1302 		if (hdev->adv_instance_cnt)
1303 			hci_req_resume_adv_instances(hdev);
1304 
1305 		return ERR_PTR(err);
1306 	}
1307 
1308 	return conn;
1309 }
1310 
1311 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
1312 {
1313 	struct hci_conn *conn;
1314 
1315 	conn = hci_conn_hash_lookup_le(hdev, addr, type);
1316 	if (!conn)
1317 		return false;
1318 
1319 	if (conn->state != BT_CONNECTED)
1320 		return false;
1321 
1322 	return true;
1323 }
1324 
1325 /* This function requires the caller holds hdev->lock */
1326 static int hci_explicit_conn_params_set(struct hci_dev *hdev,
1327 					bdaddr_t *addr, u8 addr_type)
1328 {
1329 	struct hci_conn_params *params;
1330 
1331 	if (is_connected(hdev, addr, addr_type))
1332 		return -EISCONN;
1333 
1334 	params = hci_conn_params_lookup(hdev, addr, addr_type);
1335 	if (!params) {
1336 		params = hci_conn_params_add(hdev, addr, addr_type);
1337 		if (!params)
1338 			return -ENOMEM;
1339 
1340 		/* If we created new params, mark them to be deleted in
1341 		 * hci_connect_le_scan_cleanup. It's different case than
1342 		 * existing disabled params, those will stay after cleanup.
1343 		 */
1344 		params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
1345 	}
1346 
1347 	/* We're trying to connect, so make sure params are at pend_le_conns */
1348 	if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
1349 	    params->auto_connect == HCI_AUTO_CONN_REPORT ||
1350 	    params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
1351 		list_del_init(&params->action);
1352 		list_add(&params->action, &hdev->pend_le_conns);
1353 	}
1354 
1355 	params->explicit_connect = true;
1356 
1357 	BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
1358 	       params->auto_connect);
1359 
1360 	return 0;
1361 }
1362 
1363 /* This function requires the caller holds hdev->lock */
1364 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1365 				     u8 dst_type, u8 sec_level,
1366 				     u16 conn_timeout,
1367 				     enum conn_reasons conn_reason)
1368 {
1369 	struct hci_conn *conn;
1370 
1371 	/* Let's make sure that le is enabled.*/
1372 	if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
1373 		if (lmp_le_capable(hdev))
1374 			return ERR_PTR(-ECONNREFUSED);
1375 
1376 		return ERR_PTR(-EOPNOTSUPP);
1377 	}
1378 
1379 	/* Some devices send ATT messages as soon as the physical link is
1380 	 * established. To be able to handle these ATT messages, the user-
1381 	 * space first establishes the connection and then starts the pairing
1382 	 * process.
1383 	 *
1384 	 * So if a hci_conn object already exists for the following connection
1385 	 * attempt, we simply update pending_sec_level and auth_type fields
1386 	 * and return the object found.
1387 	 */
1388 	conn = hci_conn_hash_lookup_le(hdev, dst, dst_type);
1389 	if (conn) {
1390 		if (conn->pending_sec_level < sec_level)
1391 			conn->pending_sec_level = sec_level;
1392 		goto done;
1393 	}
1394 
1395 	BT_DBG("requesting refresh of dst_addr");
1396 
1397 	conn = hci_conn_add(hdev, LE_LINK, dst, HCI_ROLE_MASTER);
1398 	if (!conn)
1399 		return ERR_PTR(-ENOMEM);
1400 
1401 	if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) {
1402 		hci_conn_del(conn);
1403 		return ERR_PTR(-EBUSY);
1404 	}
1405 
1406 	conn->state = BT_CONNECT;
1407 	set_bit(HCI_CONN_SCANNING, &conn->flags);
1408 	conn->dst_type = dst_type;
1409 	conn->sec_level = BT_SECURITY_LOW;
1410 	conn->pending_sec_level = sec_level;
1411 	conn->conn_timeout = conn_timeout;
1412 	conn->conn_reason = conn_reason;
1413 
1414 	hci_update_background_scan(hdev);
1415 
1416 done:
1417 	hci_conn_hold(conn);
1418 	return conn;
1419 }
1420 
1421 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1422 				 u8 sec_level, u8 auth_type,
1423 				 enum conn_reasons conn_reason)
1424 {
1425 	struct hci_conn *acl;
1426 
1427 	if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1428 		if (lmp_bredr_capable(hdev))
1429 			return ERR_PTR(-ECONNREFUSED);
1430 
1431 		return ERR_PTR(-EOPNOTSUPP);
1432 	}
1433 
1434 	acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
1435 	if (!acl) {
1436 		acl = hci_conn_add(hdev, ACL_LINK, dst, HCI_ROLE_MASTER);
1437 		if (!acl)
1438 			return ERR_PTR(-ENOMEM);
1439 	}
1440 
1441 	hci_conn_hold(acl);
1442 
1443 	acl->conn_reason = conn_reason;
1444 	if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
1445 		acl->sec_level = BT_SECURITY_LOW;
1446 		acl->pending_sec_level = sec_level;
1447 		acl->auth_type = auth_type;
1448 		hci_acl_create_connection(acl);
1449 	}
1450 
1451 	return acl;
1452 }
1453 
1454 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1455 				 __u16 setting, struct bt_codec *codec)
1456 {
1457 	struct hci_conn *acl;
1458 	struct hci_conn *sco;
1459 
1460 	acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING,
1461 			      CONN_REASON_SCO_CONNECT);
1462 	if (IS_ERR(acl))
1463 		return acl;
1464 
1465 	sco = hci_conn_hash_lookup_ba(hdev, type, dst);
1466 	if (!sco) {
1467 		sco = hci_conn_add(hdev, type, dst, HCI_ROLE_MASTER);
1468 		if (!sco) {
1469 			hci_conn_drop(acl);
1470 			return ERR_PTR(-ENOMEM);
1471 		}
1472 	}
1473 
1474 	acl->link = sco;
1475 	sco->link = acl;
1476 
1477 	hci_conn_hold(sco);
1478 
1479 	sco->setting = setting;
1480 	sco->codec = *codec;
1481 
1482 	if (acl->state == BT_CONNECTED &&
1483 	    (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
1484 		set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
1485 		hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
1486 
1487 		if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
1488 			/* defer SCO setup until mode change completed */
1489 			set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags);
1490 			return sco;
1491 		}
1492 
1493 		hci_sco_setup(acl, 0x00);
1494 	}
1495 
1496 	return sco;
1497 }
1498 
1499 /* Check link security requirement */
1500 int hci_conn_check_link_mode(struct hci_conn *conn)
1501 {
1502 	BT_DBG("hcon %p", conn);
1503 
1504 	/* In Secure Connections Only mode, it is required that Secure
1505 	 * Connections is used and the link is encrypted with AES-CCM
1506 	 * using a P-256 authenticated combination key.
1507 	 */
1508 	if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
1509 		if (!hci_conn_sc_enabled(conn) ||
1510 		    !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
1511 		    conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
1512 			return 0;
1513 	}
1514 
1515 	 /* AES encryption is required for Level 4:
1516 	  *
1517 	  * BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 3, Part C
1518 	  * page 1319:
1519 	  *
1520 	  * 128-bit equivalent strength for link and encryption keys
1521 	  * required using FIPS approved algorithms (E0 not allowed,
1522 	  * SAFER+ not allowed, and P-192 not allowed; encryption key
1523 	  * not shortened)
1524 	  */
1525 	if (conn->sec_level == BT_SECURITY_FIPS &&
1526 	    !test_bit(HCI_CONN_AES_CCM, &conn->flags)) {
1527 		bt_dev_err(conn->hdev,
1528 			   "Invalid security: Missing AES-CCM usage");
1529 		return 0;
1530 	}
1531 
1532 	if (hci_conn_ssp_enabled(conn) &&
1533 	    !test_bit(HCI_CONN_ENCRYPT, &conn->flags))
1534 		return 0;
1535 
1536 	return 1;
1537 }
1538 
1539 /* Authenticate remote device */
1540 static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
1541 {
1542 	BT_DBG("hcon %p", conn);
1543 
1544 	if (conn->pending_sec_level > sec_level)
1545 		sec_level = conn->pending_sec_level;
1546 
1547 	if (sec_level > conn->sec_level)
1548 		conn->pending_sec_level = sec_level;
1549 	else if (test_bit(HCI_CONN_AUTH, &conn->flags))
1550 		return 1;
1551 
1552 	/* Make sure we preserve an existing MITM requirement*/
1553 	auth_type |= (conn->auth_type & 0x01);
1554 
1555 	conn->auth_type = auth_type;
1556 
1557 	if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
1558 		struct hci_cp_auth_requested cp;
1559 
1560 		cp.handle = cpu_to_le16(conn->handle);
1561 		hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
1562 			     sizeof(cp), &cp);
1563 
1564 		/* If we're already encrypted set the REAUTH_PEND flag,
1565 		 * otherwise set the ENCRYPT_PEND.
1566 		 */
1567 		if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
1568 			set_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
1569 		else
1570 			set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
1571 	}
1572 
1573 	return 0;
1574 }
1575 
1576 /* Encrypt the link */
1577 static void hci_conn_encrypt(struct hci_conn *conn)
1578 {
1579 	BT_DBG("hcon %p", conn);
1580 
1581 	if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
1582 		struct hci_cp_set_conn_encrypt cp;
1583 		cp.handle  = cpu_to_le16(conn->handle);
1584 		cp.encrypt = 0x01;
1585 		hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
1586 			     &cp);
1587 	}
1588 }
1589 
1590 /* Enable security */
1591 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
1592 		      bool initiator)
1593 {
1594 	BT_DBG("hcon %p", conn);
1595 
1596 	if (conn->type == LE_LINK)
1597 		return smp_conn_security(conn, sec_level);
1598 
1599 	/* For sdp we don't need the link key. */
1600 	if (sec_level == BT_SECURITY_SDP)
1601 		return 1;
1602 
1603 	/* For non 2.1 devices and low security level we don't need the link
1604 	   key. */
1605 	if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn))
1606 		return 1;
1607 
1608 	/* For other security levels we need the link key. */
1609 	if (!test_bit(HCI_CONN_AUTH, &conn->flags))
1610 		goto auth;
1611 
1612 	/* An authenticated FIPS approved combination key has sufficient
1613 	 * security for security level 4. */
1614 	if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256 &&
1615 	    sec_level == BT_SECURITY_FIPS)
1616 		goto encrypt;
1617 
1618 	/* An authenticated combination key has sufficient security for
1619 	   security level 3. */
1620 	if ((conn->key_type == HCI_LK_AUTH_COMBINATION_P192 ||
1621 	     conn->key_type == HCI_LK_AUTH_COMBINATION_P256) &&
1622 	    sec_level == BT_SECURITY_HIGH)
1623 		goto encrypt;
1624 
1625 	/* An unauthenticated combination key has sufficient security for
1626 	   security level 1 and 2. */
1627 	if ((conn->key_type == HCI_LK_UNAUTH_COMBINATION_P192 ||
1628 	     conn->key_type == HCI_LK_UNAUTH_COMBINATION_P256) &&
1629 	    (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW))
1630 		goto encrypt;
1631 
1632 	/* A combination key has always sufficient security for the security
1633 	   levels 1 or 2. High security level requires the combination key
1634 	   is generated using maximum PIN code length (16).
1635 	   For pre 2.1 units. */
1636 	if (conn->key_type == HCI_LK_COMBINATION &&
1637 	    (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW ||
1638 	     conn->pin_length == 16))
1639 		goto encrypt;
1640 
1641 auth:
1642 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
1643 		return 0;
1644 
1645 	if (initiator)
1646 		set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
1647 
1648 	if (!hci_conn_auth(conn, sec_level, auth_type))
1649 		return 0;
1650 
1651 encrypt:
1652 	if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) {
1653 		/* Ensure that the encryption key size has been read,
1654 		 * otherwise stall the upper layer responses.
1655 		 */
1656 		if (!conn->enc_key_size)
1657 			return 0;
1658 
1659 		/* Nothing else needed, all requirements are met */
1660 		return 1;
1661 	}
1662 
1663 	hci_conn_encrypt(conn);
1664 	return 0;
1665 }
1666 EXPORT_SYMBOL(hci_conn_security);
1667 
1668 /* Check secure link requirement */
1669 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
1670 {
1671 	BT_DBG("hcon %p", conn);
1672 
1673 	/* Accept if non-secure or higher security level is required */
1674 	if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS)
1675 		return 1;
1676 
1677 	/* Accept if secure or higher security level is already present */
1678 	if (conn->sec_level == BT_SECURITY_HIGH ||
1679 	    conn->sec_level == BT_SECURITY_FIPS)
1680 		return 1;
1681 
1682 	/* Reject not secure link */
1683 	return 0;
1684 }
1685 EXPORT_SYMBOL(hci_conn_check_secure);
1686 
1687 /* Switch role */
1688 int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
1689 {
1690 	BT_DBG("hcon %p", conn);
1691 
1692 	if (role == conn->role)
1693 		return 1;
1694 
1695 	if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) {
1696 		struct hci_cp_switch_role cp;
1697 		bacpy(&cp.bdaddr, &conn->dst);
1698 		cp.role = role;
1699 		hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
1700 	}
1701 
1702 	return 0;
1703 }
1704 EXPORT_SYMBOL(hci_conn_switch_role);
1705 
1706 /* Enter active mode */
1707 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
1708 {
1709 	struct hci_dev *hdev = conn->hdev;
1710 
1711 	BT_DBG("hcon %p mode %d", conn, conn->mode);
1712 
1713 	if (conn->mode != HCI_CM_SNIFF)
1714 		goto timer;
1715 
1716 	if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active)
1717 		goto timer;
1718 
1719 	if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) {
1720 		struct hci_cp_exit_sniff_mode cp;
1721 		cp.handle = cpu_to_le16(conn->handle);
1722 		hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
1723 	}
1724 
1725 timer:
1726 	if (hdev->idle_timeout > 0)
1727 		queue_delayed_work(hdev->workqueue, &conn->idle_work,
1728 				   msecs_to_jiffies(hdev->idle_timeout));
1729 }
1730 
1731 /* Drop all connection on the device */
1732 void hci_conn_hash_flush(struct hci_dev *hdev)
1733 {
1734 	struct hci_conn_hash *h = &hdev->conn_hash;
1735 	struct hci_conn *c, *n;
1736 
1737 	BT_DBG("hdev %s", hdev->name);
1738 
1739 	list_for_each_entry_safe(c, n, &h->list, list) {
1740 		c->state = BT_CLOSED;
1741 
1742 		hci_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM);
1743 		hci_conn_del(c);
1744 	}
1745 }
1746 
1747 /* Check pending connect attempts */
1748 void hci_conn_check_pending(struct hci_dev *hdev)
1749 {
1750 	struct hci_conn *conn;
1751 
1752 	BT_DBG("hdev %s", hdev->name);
1753 
1754 	hci_dev_lock(hdev);
1755 
1756 	conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1757 	if (conn)
1758 		hci_acl_create_connection(conn);
1759 
1760 	hci_dev_unlock(hdev);
1761 }
1762 
1763 static u32 get_link_mode(struct hci_conn *conn)
1764 {
1765 	u32 link_mode = 0;
1766 
1767 	if (conn->role == HCI_ROLE_MASTER)
1768 		link_mode |= HCI_LM_MASTER;
1769 
1770 	if (test_bit(HCI_CONN_ENCRYPT, &conn->flags))
1771 		link_mode |= HCI_LM_ENCRYPT;
1772 
1773 	if (test_bit(HCI_CONN_AUTH, &conn->flags))
1774 		link_mode |= HCI_LM_AUTH;
1775 
1776 	if (test_bit(HCI_CONN_SECURE, &conn->flags))
1777 		link_mode |= HCI_LM_SECURE;
1778 
1779 	if (test_bit(HCI_CONN_FIPS, &conn->flags))
1780 		link_mode |= HCI_LM_FIPS;
1781 
1782 	return link_mode;
1783 }
1784 
1785 int hci_get_conn_list(void __user *arg)
1786 {
1787 	struct hci_conn *c;
1788 	struct hci_conn_list_req req, *cl;
1789 	struct hci_conn_info *ci;
1790 	struct hci_dev *hdev;
1791 	int n = 0, size, err;
1792 
1793 	if (copy_from_user(&req, arg, sizeof(req)))
1794 		return -EFAULT;
1795 
1796 	if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1797 		return -EINVAL;
1798 
1799 	size = sizeof(req) + req.conn_num * sizeof(*ci);
1800 
1801 	cl = kmalloc(size, GFP_KERNEL);
1802 	if (!cl)
1803 		return -ENOMEM;
1804 
1805 	hdev = hci_dev_get(req.dev_id);
1806 	if (!hdev) {
1807 		kfree(cl);
1808 		return -ENODEV;
1809 	}
1810 
1811 	ci = cl->conn_info;
1812 
1813 	hci_dev_lock(hdev);
1814 	list_for_each_entry(c, &hdev->conn_hash.list, list) {
1815 		bacpy(&(ci + n)->bdaddr, &c->dst);
1816 		(ci + n)->handle = c->handle;
1817 		(ci + n)->type  = c->type;
1818 		(ci + n)->out   = c->out;
1819 		(ci + n)->state = c->state;
1820 		(ci + n)->link_mode = get_link_mode(c);
1821 		if (++n >= req.conn_num)
1822 			break;
1823 	}
1824 	hci_dev_unlock(hdev);
1825 
1826 	cl->dev_id = hdev->id;
1827 	cl->conn_num = n;
1828 	size = sizeof(req) + n * sizeof(*ci);
1829 
1830 	hci_dev_put(hdev);
1831 
1832 	err = copy_to_user(arg, cl, size);
1833 	kfree(cl);
1834 
1835 	return err ? -EFAULT : 0;
1836 }
1837 
1838 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1839 {
1840 	struct hci_conn_info_req req;
1841 	struct hci_conn_info ci;
1842 	struct hci_conn *conn;
1843 	char __user *ptr = arg + sizeof(req);
1844 
1845 	if (copy_from_user(&req, arg, sizeof(req)))
1846 		return -EFAULT;
1847 
1848 	hci_dev_lock(hdev);
1849 	conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1850 	if (conn) {
1851 		bacpy(&ci.bdaddr, &conn->dst);
1852 		ci.handle = conn->handle;
1853 		ci.type  = conn->type;
1854 		ci.out   = conn->out;
1855 		ci.state = conn->state;
1856 		ci.link_mode = get_link_mode(conn);
1857 	}
1858 	hci_dev_unlock(hdev);
1859 
1860 	if (!conn)
1861 		return -ENOENT;
1862 
1863 	return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1864 }
1865 
1866 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1867 {
1868 	struct hci_auth_info_req req;
1869 	struct hci_conn *conn;
1870 
1871 	if (copy_from_user(&req, arg, sizeof(req)))
1872 		return -EFAULT;
1873 
1874 	hci_dev_lock(hdev);
1875 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1876 	if (conn)
1877 		req.type = conn->auth_type;
1878 	hci_dev_unlock(hdev);
1879 
1880 	if (!conn)
1881 		return -ENOENT;
1882 
1883 	return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1884 }
1885 
1886 struct hci_chan *hci_chan_create(struct hci_conn *conn)
1887 {
1888 	struct hci_dev *hdev = conn->hdev;
1889 	struct hci_chan *chan;
1890 
1891 	BT_DBG("%s hcon %p", hdev->name, conn);
1892 
1893 	if (test_bit(HCI_CONN_DROP, &conn->flags)) {
1894 		BT_DBG("Refusing to create new hci_chan");
1895 		return NULL;
1896 	}
1897 
1898 	chan = kzalloc(sizeof(*chan), GFP_KERNEL);
1899 	if (!chan)
1900 		return NULL;
1901 
1902 	chan->conn = hci_conn_get(conn);
1903 	skb_queue_head_init(&chan->data_q);
1904 	chan->state = BT_CONNECTED;
1905 
1906 	list_add_rcu(&chan->list, &conn->chan_list);
1907 
1908 	return chan;
1909 }
1910 
1911 void hci_chan_del(struct hci_chan *chan)
1912 {
1913 	struct hci_conn *conn = chan->conn;
1914 	struct hci_dev *hdev = conn->hdev;
1915 
1916 	BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan);
1917 
1918 	list_del_rcu(&chan->list);
1919 
1920 	synchronize_rcu();
1921 
1922 	/* Prevent new hci_chan's to be created for this hci_conn */
1923 	set_bit(HCI_CONN_DROP, &conn->flags);
1924 
1925 	hci_conn_put(conn);
1926 
1927 	skb_queue_purge(&chan->data_q);
1928 	kfree(chan);
1929 }
1930 
1931 void hci_chan_list_flush(struct hci_conn *conn)
1932 {
1933 	struct hci_chan *chan, *n;
1934 
1935 	BT_DBG("hcon %p", conn);
1936 
1937 	list_for_each_entry_safe(chan, n, &conn->chan_list, list)
1938 		hci_chan_del(chan);
1939 }
1940 
1941 static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon,
1942 						 __u16 handle)
1943 {
1944 	struct hci_chan *hchan;
1945 
1946 	list_for_each_entry(hchan, &hcon->chan_list, list) {
1947 		if (hchan->handle == handle)
1948 			return hchan;
1949 	}
1950 
1951 	return NULL;
1952 }
1953 
1954 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle)
1955 {
1956 	struct hci_conn_hash *h = &hdev->conn_hash;
1957 	struct hci_conn *hcon;
1958 	struct hci_chan *hchan = NULL;
1959 
1960 	rcu_read_lock();
1961 
1962 	list_for_each_entry_rcu(hcon, &h->list, list) {
1963 		hchan = __hci_chan_lookup_handle(hcon, handle);
1964 		if (hchan)
1965 			break;
1966 	}
1967 
1968 	rcu_read_unlock();
1969 
1970 	return hchan;
1971 }
1972 
1973 u32 hci_conn_get_phy(struct hci_conn *conn)
1974 {
1975 	u32 phys = 0;
1976 
1977 	/* BLUETOOTH CORE SPECIFICATION Version 5.2 | Vol 2, Part B page 471:
1978 	 * Table 6.2: Packets defined for synchronous, asynchronous, and
1979 	 * CPB logical transport types.
1980 	 */
1981 	switch (conn->type) {
1982 	case SCO_LINK:
1983 		/* SCO logical transport (1 Mb/s):
1984 		 * HV1, HV2, HV3 and DV.
1985 		 */
1986 		phys |= BT_PHY_BR_1M_1SLOT;
1987 
1988 		break;
1989 
1990 	case ACL_LINK:
1991 		/* ACL logical transport (1 Mb/s) ptt=0:
1992 		 * DH1, DM3, DH3, DM5 and DH5.
1993 		 */
1994 		phys |= BT_PHY_BR_1M_1SLOT;
1995 
1996 		if (conn->pkt_type & (HCI_DM3 | HCI_DH3))
1997 			phys |= BT_PHY_BR_1M_3SLOT;
1998 
1999 		if (conn->pkt_type & (HCI_DM5 | HCI_DH5))
2000 			phys |= BT_PHY_BR_1M_5SLOT;
2001 
2002 		/* ACL logical transport (2 Mb/s) ptt=1:
2003 		 * 2-DH1, 2-DH3 and 2-DH5.
2004 		 */
2005 		if (!(conn->pkt_type & HCI_2DH1))
2006 			phys |= BT_PHY_EDR_2M_1SLOT;
2007 
2008 		if (!(conn->pkt_type & HCI_2DH3))
2009 			phys |= BT_PHY_EDR_2M_3SLOT;
2010 
2011 		if (!(conn->pkt_type & HCI_2DH5))
2012 			phys |= BT_PHY_EDR_2M_5SLOT;
2013 
2014 		/* ACL logical transport (3 Mb/s) ptt=1:
2015 		 * 3-DH1, 3-DH3 and 3-DH5.
2016 		 */
2017 		if (!(conn->pkt_type & HCI_3DH1))
2018 			phys |= BT_PHY_EDR_3M_1SLOT;
2019 
2020 		if (!(conn->pkt_type & HCI_3DH3))
2021 			phys |= BT_PHY_EDR_3M_3SLOT;
2022 
2023 		if (!(conn->pkt_type & HCI_3DH5))
2024 			phys |= BT_PHY_EDR_3M_5SLOT;
2025 
2026 		break;
2027 
2028 	case ESCO_LINK:
2029 		/* eSCO logical transport (1 Mb/s): EV3, EV4 and EV5 */
2030 		phys |= BT_PHY_BR_1M_1SLOT;
2031 
2032 		if (!(conn->pkt_type & (ESCO_EV4 | ESCO_EV5)))
2033 			phys |= BT_PHY_BR_1M_3SLOT;
2034 
2035 		/* eSCO logical transport (2 Mb/s): 2-EV3, 2-EV5 */
2036 		if (!(conn->pkt_type & ESCO_2EV3))
2037 			phys |= BT_PHY_EDR_2M_1SLOT;
2038 
2039 		if (!(conn->pkt_type & ESCO_2EV5))
2040 			phys |= BT_PHY_EDR_2M_3SLOT;
2041 
2042 		/* eSCO logical transport (3 Mb/s): 3-EV3, 3-EV5 */
2043 		if (!(conn->pkt_type & ESCO_3EV3))
2044 			phys |= BT_PHY_EDR_3M_1SLOT;
2045 
2046 		if (!(conn->pkt_type & ESCO_3EV5))
2047 			phys |= BT_PHY_EDR_3M_3SLOT;
2048 
2049 		break;
2050 
2051 	case LE_LINK:
2052 		if (conn->le_tx_phy & HCI_LE_SET_PHY_1M)
2053 			phys |= BT_PHY_LE_1M_TX;
2054 
2055 		if (conn->le_rx_phy & HCI_LE_SET_PHY_1M)
2056 			phys |= BT_PHY_LE_1M_RX;
2057 
2058 		if (conn->le_tx_phy & HCI_LE_SET_PHY_2M)
2059 			phys |= BT_PHY_LE_2M_TX;
2060 
2061 		if (conn->le_rx_phy & HCI_LE_SET_PHY_2M)
2062 			phys |= BT_PHY_LE_2M_RX;
2063 
2064 		if (conn->le_tx_phy & HCI_LE_SET_PHY_CODED)
2065 			phys |= BT_PHY_LE_CODED_TX;
2066 
2067 		if (conn->le_rx_phy & HCI_LE_SET_PHY_CODED)
2068 			phys |= BT_PHY_LE_CODED_RX;
2069 
2070 		break;
2071 	}
2072 
2073 	return phys;
2074 }
2075